//	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  September 2015 , May 2017
//

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

FILE	*f1,*f2;

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

char 	ps[256]="out_d";
char 	ips[256]="iout_d";
char 	ss[256]=".txt";

char	sv[256];
char	sv_[256][256];int sv_i,sv_j;
char 	ssl[256];

int p_2(char infn_[256], int sl, char ssl_[256])
{

int	a,b;
int	d,e;
int	i,j,k,l,m,n;
int	fb;
char 	outfn__[256];

	for(i=0;i<256;++i){outfn__[i]='\0';}
	f1=fopen(infn_,"rb");
	strcat(outfn__,infn_);
	outfn__[-1+strlen(outfn__)]='\0';outfn__[-1+strlen(outfn__)]='\0';outfn__[-1+strlen(outfn__)]='\0';outfn__[-1+strlen(outfn__)]='\0';outfn__[-1+strlen(outfn__)]='\0';
	outfn__[strlen(outfn__)]='_';outfn__[strlen(outfn__)]='l';
	strcat(outfn__,ssl);
	strcat(outfn__,ss);
	f2=fopen(outfn__,"wb");
	fb=getc(f1);
	while(!feof(f1))
	{
		for(i=0;i<256;++i){sv[i]='\0';}i=0;
		while((fb!=10)&&(fb!=13)&&!feof(f1)){sv[i]=fb;++i;fb=getc(f1);}
		while((fb==10)||(fb==13)&&!feof(f1)){fb=getc(f1);}
		for(i=0;i<256;++i){for(j=0;j<256;++j){sv_[i][j]='\0';}}sv_i=0;sv_j=0;
		for(i=0;i<strlen(sv);++i){if(sv[i]=='_'){if(sv_i){sv_i=0;++sv_j;}}else{sv_[sv_j][sv_i]=sv[i];++sv_i;}}
		for(i=0;i<sv_j;++i){if(strlen(sv_[i])==sl){fprintf(f2,"%05d%c%s%c%c",strlen(sv_[i]),(char)9,sv_[i],(char)13,(char)10);}}
	}
	fclose(f1);
	fclose(f2);
	return(0);

}

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

int	a,b,cc,cl;
int	i,j,k,l,m,cm,im,n;
int	fb;

char	cv[2][256];int cvi=0,cvi_=0;
int	c[256];

char 	outfn__[256];

	f1=fopen(infn,"rb");
	f2=fopen(outfn,"wb");
	fb=getc(f1);

	for(j=0;j<256;++j){cv[0][j]='\0';cv[1][j]='\0';}
	while((fb!=10)&&(fb!=13)){cv[0][cvi]=fb;++cvi;fb=getc(f1);}
	while((fb==10)||(fb==13)){fb=getc(f1);}

	while(!feof(f1))
	{

			k=0;cvi_=cvi;while(k<strlen(cv[1])){cv[0][cvi_]=cv[1][k];++cvi_;++k;}
			cm=-1;a=0;
			for(i=1;i<cvi;++i)
			{
				
				for(k=0;k<256;++k){c[k]=0;}
				k=0;for(j=i;j<cvi+i;++j){if(cv[0][i+j]==cv[0][k]){++c[k];}++k;}
				cc=0;cl=0;m=-1;
				for(k=0;k<cvi;++k)
				{
					if(c[k]){++cc;++cl;}
					if(k){if(!c[-1+k]){if(m==-1){m=cl;}else{if(m<cl){m=cl;}}cl=0;}}
				}
				//printf("%d ",cc);
				if(cc>=3){++a;}
				if(cm==-1){cm=m;im=i;}else{if(cm<m){cm=m;im=i;}}
			}
			//
			for(k=0;k<256;++k){c[k]=0;}
			k=0;for(j=im;j<cvi+im;++j){if(cv[0][im+j]==cv[0][k]){++c[k];}++k;}

			for(i=0;i<cvi;++i){if(c[i]){fprintf(f2 ,"%c",cv[0][i]);}else{fprintf(f2 ,"_");}}fprintf(f2 ,"\n");

			k=0;for(i=cvi;i<cvi_;++i){cv[0][k]=cv[0][i];++k;}cvi=k;
			for(j=0;j<256;++j){cv[1][j]='\0';}
			k=0;while((fb!=10)&&(fb!=13)){cv[1][k]=fb;++k;fb=getc(f1);}
			while((fb==10)||(fb==13)){fb=getc(f1);}
	}
	fclose(f1);
	fclose(f2);

	for(j=0;j<256;++j){ssl[j]='\0';}
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+6);p_2(outfn,6,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+7);p_2(outfn,7,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+8);p_2(outfn,8,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+9);p_2(outfn,9,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)10/(int)10));
	ssl[strlen(ssl)]=(char)(48+10-(int)10*((int)10/(int)10));p_2(outfn,10,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)11/(int)10));
	ssl[strlen(ssl)]=(char)(48+11-(int)10*((int)11/(int)10));p_2(outfn,11,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)12/(int)10));
	ssl[strlen(ssl)]=(char)(48+12-(int)10*((int)12/(int)10));p_2(outfn,12,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)13/(int)10));
	ssl[strlen(ssl)]=(char)(48+13-(int)10*((int)13/(int)10));p_2(outfn,13,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)14/(int)10));
	ssl[strlen(ssl)]=(char)(48+14-(int)10*((int)14/(int)10));p_2(outfn,14,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)15/(int)10));
	ssl[strlen(ssl)]=(char)(48+15-(int)10*((int)15/(int)10));p_2(outfn,15,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)16/(int)10));
	ssl[strlen(ssl)]=(char)(48+16-(int)10*((int)16/(int)10));p_2(outfn,16,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)17/(int)10));
	ssl[strlen(ssl)]=(char)(48+17-(int)10*((int)17/(int)10));p_2(outfn,17,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)18/(int)10));
	ssl[strlen(ssl)]=(char)(48+18-(int)10*((int)18/(int)10));p_2(outfn,18,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)19/(int)10));
	ssl[strlen(ssl)]=(char)(48+19-(int)10*((int)19/(int)10));p_2(outfn,19,ssl);
	//
	for(j=0;j<256;++j){ssl[j]='\0';}ssl[strlen(ssl)]=(char)(48+((int)20/(int)10));
	ssl[strlen(ssl)]=(char)(48+20-(int)10*((int)20/(int)10));p_2(outfn,20,ssl);

	return(0);

}