//	This source and text sequencing method apply to GNU General Public License. 
//			Copyright (C) 2003  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 2003 - 2014 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.

//
//		text longest reocuring string (4)
//		written by Dzinleski Jasenko  October , 2016
//


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

FILE		*f1,*f2;

char		infn[1024]="fasta__.txt";
char		outfn[1024]="out_.txt";

int 		ba2b[256][256][5];	int ba2bi=0;
int 		ba2b_[256][256][5];	int ba2bi_=0;

int			b8o,b8e;
int			b16o,b16e;

int 		fb_[3][1024];
char 		sq_[1024];
char 		msq_[1024];

int p_1()
{

int	a,b,c,d,e;
int	i,j,k;

int	b1_,b2_,b3_,b4_,b5_;
int	b1,b2,b3,b4,b5;

int	d1,d2,d3,d4,d5,d6;
int	d1_,d2_,d3_,d4_,d5_,d6_;

int	fbyte;
int	bi;

int	mc=0,unmc=0;



	for(i=0;i<256;++i){for(j=0;j<256;++j){ba2b[i][j][0]=-1;}}ba2bi=0;
	for(i=0;i<256;++i){for(j=0;j<256;++j){ba2b_[i][j][0]=-1;}}ba2bi_=0;

	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;
	
	f1=fopen(infn,"rb");
	f2=fopen(outfn,"wb");		
	while(!feof(f1))
	{

		fb_[0][0]=1;while(!feof(f1)&&fb_[0][0]<1024){fb_[1][fb_[0][0]]==0;fb_[2][fb_[0][0]]=0;fb_[0][fb_[0][0]]=getc(f1);++fb_[0][0];}

		i=1;b1_=fb_[0][i];b2_=fb_[0][++i];b3_=fb_[0][++i];b4_=fb_[0][++i];
		while(i<fb_[0][0])
		{
			
			b1=b1_<<1;
			b2=b2_<<1;
			b3=b3_<<1;
			b4=b4_<<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_=((((b3&b8e)>>1)^(b2&b8o))|((b3&b8e)^((b2&b8o)<<1)));	
			d2_=((((b3&b8e)>>1)^(b1&b8o))|((b3&b8e)^((b1&b8o)<<1)));
			d3_=((((d1_&b8e)>>1)^(d2_&b8o))|((d1_&b8e)^((d2_&b8o)<<1)));

			d4=((((b2&b8e)>>1)^(b3&b8o))|((b2&b8e)^((b3&b8o)<<1)));	
			d5=((((b2&b8e)>>1)^(b4&b8o))|((b2&b8e)^((b4&b8o)<<1)));
			d6=((((d4&b8e)>>1)^(d5&b8o))|((d4&b8e)^((d5&b8o)<<1)));

			d4_=((((b4&b8e)>>1)^(b3&b8o))|((b4&b8e)^((b3&b8o)<<1)));	
			d5_=((((b4&b8e)>>1)^(b1&b8o))|((b4&b8e)^((b1&b8o)<<1)));
			d6_=((((d4_&b8e)>>1)^(d5_&b8o))|((d4_&b8e)^((d5_&b8o)<<1)));

			if(ba2b[(b1_&0xff)][(b3_&0xff)][0]==-1)
			{

				ba2b[(b1_&0xff)][(b3_&0xff)][0]=ba2bi;++ba2bi;
				ba2b[(b1_&0xff)][(b3_&0xff)][1]=b2_;
				ba2b[(b1_&0xff)][(b3_&0xff)][2]=1;
				ba2b[(b1_&0xff)][(b3_&0xff)][3]=d3;
				ba2b[(b1_&0xff)][(b3_&0xff)][4]=d3_;
			
			}else{

				++ba2b[(b1_&0xff)][(b3_&0xff)][2];

				if(ba2b_[(b2_&0xff)][(b4_&0xff)][0]==-1)
				{

					ba2b_[(b2_&0xff)][(b4_&0xff)][0]=ba2bi_;++ba2bi_;
					ba2b_[(b2_&0xff)][(b4_&0xff)][1]=b3_;
					ba2b_[(b2_&0xff)][(b4_&0xff)][2]=1;
					ba2b_[(b2_&0xff)][(b4_&0xff)][3]=d6;
					ba2b_[(b2_&0xff)][(b4_&0xff)][4]=d6_;
			
				}else{

					++ba2b_[(b2_&0xff)][(b4_&0xff)][2];

					if((ba2b_[(b2_&0xff)][(b4_&0xff)][3]!=d6)||(ba2b_[(b2_&0xff)][(b4_&0xff)][4]!=d6_)){++unmc;fb_[1][-1-1-1+i]=1;fb_[1][-1-1+i]=1;}
					if((ba2b_[(b2_&0xff)][(b4_&0xff)][3]==d6)&&(ba2b_[(b2_&0xff)][(b4_&0xff)][4]==d6_)){++mc;fb_[2][-1-1-1+i]=1;fb_[2][-1-1+i]=1;}
				}
			}
			
			b1_=b2_;
			b2_=b3_;
			b3_=b4_;
			b4_=fb_[0][++i];
		}

		
		i=1;j=0;k=0;
		while(i<fb_[0][0])
		{
			if(fb_[0][i]==13||fb_[0][i]==10)
			{
				while((fb_[0][i]==13||fb_[0][i]==10)){++i;}
				if(strlen(sq_)){printf("\t");for(k=0;k<strlen(sq_);++k){if(sq_[k]!=13&&sq_[k]!=10){printf("%c",sq_[k]);fprintf(f2,"%c",sq_[k]);}}printf("\t%d",strlen(sq_));fprintf(f2,"\t%d",strlen(sq_));}
				printf("%c",(char)10);fprintf(f2,"%c",(char)10);
				for(k=0;k<1024;++k){sq_[k]='\0';}
			}else{
					if(fb_[2][i]==1)
					{
						if(strlen(sq_))
						{
							k=i;while(fb_[2][k]==1){++k;}
							if(strlen(sq_)<(k-i)){for(k=0;k<1024;++k){sq_[k]='\0';}k=i;j=0;while(fb_[2][k]==1){sq_[j]=fb_[0][k];++k;++j;}}
						}else{
							for(k=0;k<1024;++k){sq_[k]='\0';}k=i;j=0;while(fb_[2][k]==1){sq_[j]=fb_[0][k];++k;++j;}
						}
						printf("%c",fb_[0][i]);++i;
					}else{printf("_");++i;}
			}
		}
	}
	fclose(f1);
	fclose(f2);

	return(0);

}

int main()
{
	p_1();
	return(0);

}