//	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 (2)
//		written by Dzinleski Jasenko  September , 2015
//


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

#define		BuffL			128
#define		BtM				8*256
#define		TBEnd			24

FILE		*f1,*f2,*f3;

char		infn[256]="fasta__.txt";
char		outfn[256]="out_.txt";
char		moutfn[256]="mout_.txt";

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

int			b8o,b8e;
int			b16o,b16e;

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

int			bc_=0,bc=0,bitc=0;
int			bytc=0,bytc1=0;

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

int p_1()
{

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

int	b1_,b2_,b3_;
int	b1,b2,b3;

int	d1,d2,d3;
int	d1_,d2_,d3_;

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;

	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");		
	f3=fopen(moutfn,"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];
		while(i<fb_[0][0])
		{
			
			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_=((((d1_&b8e)>>1)^(d2_&b8o))|((d1_&b8e)^((d2_&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[(b1_&0xff)][(b3_&0xff)][3]!=d3)||(ba2b[(b1_&0xff)][(b3_&0xff)][4]!=d3_)){++unmc;fb_[1][-1+i]=1;}
				if((ba2b[(b1_&0xff)][(b3_&0xff)][3]==d3)&&(ba2b[(b1_&0xff)][(b3_&0xff)][4]==d3_)){++mc;fb_[2][-1+i]=1;}

			}

			b1_=b2_;
			b2_=b3_;
			b3_=fb_[0][++i];
		}
		
		i=1;j=0;k=0;m=0;
		while(i<fb_[0][0])
		{
			if(fb_[0][i]==13||fb_[0][i]==10){
				++k;
				if(k==1){
					if(m==0){printf("%c\t%c\t%d",' ',' ',l);fprintf(f3,"%c\t%c\t%d",' ',' ',l);}else{
						printf("\t%d\t%s\t%d",m,msq_,l);
						fprintf(f3,"%d\t%s\t%d",m,msq_,l);
					}
					printf("%c",fb_[0][i]);
					fprintf(f3,"%c",fb_[0][i]);
					k=0;m=0;
					++l;
				}
			}else{
				if(fb_[2][i]==1){printf("%c",fb_[0][i]);sq_[j]=fb_[0][i];++j;}else{
					if(j>=4){
						fprintf(f2,"%s",sq_);
						fprintf(f2,"\t%d%c%c",j,(char)13,(char)10);
						if(m==0){m=j;for(j=0;j<1024;++j){msq_[j]=sq_[j];}}else{if(m<j){m=j;for(j=0;j<1024;++j){msq_[j]=sq_[j];}}}
						for(j=0;j<1024;++j){sq_[j]='\0';}
					}
					j=0;
					printf("_");
				}
			}
			++i;
		}
	}
	fclose(f1);
	fclose(f2);
	fclose(f3);

	return(0);

}

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

}