//      This source and Arbitrary Lengths of Text Sequences method apply to GNU General Public License. 
//                      Copyright (C) 2012  Jasenko Dzinleski 

//      This source applies to the GNU General Public License as
//      published by the Free Software Foundation 
//      and can not be used, copied, sold, redistributed or 
//      used in any other way but only by written permission by Jasenko Dzinleski . 
//      Copyright (C) from 2001 - 2012 and later by Jasenko Dzinleski 

//      This program is distributed in the hope that it will be useful, but
//      WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//      General Public License for more details. 

//      You should have received a copy of the GNU General Public License along
//      with this program; if not, write to the Free Software Foundation, Inc.,
//      51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

//
//                      Arbitrary Lengths of Text Sequences
//                      written by Dzinleski Jasenko March , 2013
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

FILE    *f1,*f2;

char    infn[256]="War_and_Peace_NT.txt";

int     ba16[16][16][16][16][9];int bai;
int             b8o,b8e;
bool    df=false;

bool    bt32_1[32];     int     bt32i_1=0;
bool    bt32_2[32];     int     bt32i_2=0;

long    b_[1024][3]; int b_i=0;

int     pr24_(long e,bool b_[32],int b_i,bool af)
{

        int     a,b=0,d=-1,c;
        b_i=0;
        e&=0x00ffffff;
        for(a=24-1;a>=0;--a)
        {
                c=0;c=1<<a;
                if(((c&e)>>a)==1){if(d==-1){d=a;}if(af){b_[b_i]=true;++b_i;}}else{
                        if(d!=-1){if(af){b_[b_i]=false;++b_i;}}
                }
        }
        //printf("!%d %d!",e,d);
        return(d+1);

}

int p_1()
{

int     a,b,c,d,e;
int     i,j,k,l,m,n;
int     d1,d2,d3;
int     d1_,d2_,d3_;
int     b1,b2,b3,b1_,b2_,b3_;
int     fbyte,bc=0,bitc=0;
int     mc=0,unmc=0;


        for(i=0;i<15;++i){
        for(j=0;j<15;++j){
        for(k=0;k<15;++k){
        for(l=0;l<15;++l){
                ba16[i][j][k][l][0]=-1;
        }}}}bai=0;

        f1=fopen(infn,"rb");

        while(!feof(f1))
        {
                
                b1_=getc(f1);while(b1_==13||b1_==10){b1_=getc(f1);}b_[b_i][0]=b1_;++b_i;
                b2_=getc(f1);while(b2_==13||b2_==10){b2_=getc(f1);}b_[b_i][0]=b2_;++b_i;
                b3_=getc(f1);while(b3_==13||b3_==10){b3_=getc(f1);}b_[b_i][0]=b3_;++b_i;
                bc+=3;
                
                n=30;mc=0,unmc=0;
                while(!feof(f1)&&b_i<3*n)
                {
        
                        b=((b1_<<8)&0xff00)|(b3_&(0xff));

                        b1=b1_<<1;
                        b2=b2_<<1;
                        b3=b3_<<1;

                        d1=((((b1&b8e)>>1)^(b2&b8o))|((b1&b8e)^((b2&b8o)<<1))); 
                        d2=((((b1&b8e)>>1)^(b3&b8o))|((b1&b8e)^((b3&b8o)<<1)));
                        d3=((((d1&b8e)>>1)^(d2&b8o))|((d1&b8e)^((d2&b8o)<<1)));

                        d1_=((((b2&b8e)>>1)^(b1&b8o))|((b2&b8e)^((b1&b8o)<<1)));        
                        d2_=((((b2&b8e)>>1)^(b3&b8o))|((b2&b8e)^((b3&b8o)<<1)));
                        d3_=((((d2_&b8e)>>1)^(d1_&b8o))|((d2_&b8e)^((d1_&b8o)<<1)));

                        if(ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][0]==-1)
                        {
                                ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][0]=d1;
                                ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][1]=d2;
                                ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][2]=d3;
                                ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][3]=d1_;
                                ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][4]=d2_;
                                ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][5]=d3_;
                                ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][6]=bai;
                                ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][7]=(b2>>1);
                                ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][8]=1;

                                bitc+=pr24_((long)bai,bt32_1,bt32i_1,false);++bai;

                        }else{
                
                                e=ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][6];
                                bitc+=pr24_((long)e,bt32_1,bt32i_1,false);
                        
                                if(ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][2]==d3)
                                        {++ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][8];}
                                if((ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][2]!=d3)||(ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][5]!=d3_))
                                        {++unmc;}
                                if((ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][2]==d3)&&(ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][5]==d3_))
                                        {++mc;}
                        }

                        b1_=b2_;
                        b2_=b3_;
                        b3_=getc(f1);while(b3_==13||b3_==10){b3_=getc(f1);}
                        b_[b_i][0]=b3_;++b_i;
                        ++bc;
                
                        if(unmc==0||mc==0){continue;}
                        //printf("\t(%d)\t%d\t%e\n",b_i,(3*n),(100*((double)unmc/(double)mc)));
                        if
                        (
                        ((100*((double)unmc/(double)mc))<=90.00)
                        ||
                        ((100*((double)unmc/(double)mc))>=200.00)
                        )
                        {++n;if(b_i>=1024){break;}}else{break;}
                
                }
                printf("\t(%d)\t%e\n",b_i,(100*((double)unmc/(double)mc)));
                for(i=0;i<b_i;++i){printf("%c",(char)b_[i][0]);}printf("\n\n");b_i=0;
                
        }
        fclose(f1);

    return(0);
        
}

int main(int argc,char *argv[])
{

        int     a,b,c,d;
        int     i,j,k,l,m;

        printf("\n\tArbitrary Lengths of Text Sequences\n");
        printf("\twritten by Dzinleski Jasenko March , 2013\n\n");

        if(argv[1]==NULL){return(0);}else{strcpy(infn,argv[1]);f1=fopen(infn,"rb");if(f1==NULL){return(0);}else{fclose(f1);}}

        b8o=0;
        b8o|=1<<0;
        b8o|=1<<2;
        b8o|=1<<4;
        b8o|=1<<6;

        b8e=0;
        b8e|=1<<1;
        b8e|=1<<3;
        b8e|=1<<5;
        b8e|=1<<7;

        p_1();

        return(0);

}