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

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

FILE	*f1,*f2,*f3,*f4,*f5,*f6,*f7,*f8,*f9,*f10,*f11,*f12,*f13,*f14,*f15,*f16,*f17,*f18,*f19,*f20,*f21;

char	infn[256]="fasta__.txt";
char	outfn1[256]="out1_.txt";
char	outfn2[256]="out2_.txt";
char	outfn3[256]="out3_.txt";
char	outfn4[256]="out4_.txt";
char	outfn5[256]="out5_.txt";
char	outfn6[256]="out6_.txt";
char	outfn7[256]="out7_.txt";
char	outfn8[256]="out8_.txt";
char	outfn9[256]="out9_.txt";
char	outfn10[256]="out10_.txt";
char	outfn11[256]="out11_.txt";
char	outfn12[256]="out12_.txt";
char	outfn13[256]="out13_.txt";
char	outfn14[256]="out14_.txt";
char	outfn15[256]="out15_.txt";
char	outfn16[256]="out16_.txt";
char	outfn17[256]="out17_.txt";
char	outfn18[256]="out18_.txt";
char	outfn19[256]="out19_.txt";
char	outfn20[256]="out20_.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	ii=0,i,j,k,l,m,cm,im,n;
int	fb;

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

char 	outfn__[256];

	f1=fopen(infn,"rb");
	f2=fopen(outfn1 ,"wb");
	f3=fopen(outfn2 ,"wb");
	f4=fopen(outfn3 ,"wb");
	f5=fopen(outfn4 ,"wb");
	f6=fopen(outfn5 ,"wb");
	f7=fopen(outfn6 ,"wb");
	f8=fopen(outfn7 ,"wb");
	f9=fopen(outfn8 ,"wb");
	f10=fopen(outfn9,"wb" );
	f11=fopen(outfn10,"wb");
	f12=fopen(outfn11,"wb");
	f13=fopen(outfn12,"wb");
	f14=fopen(outfn13,"wb");
	f15=fopen(outfn14,"wb");
	f16=fopen(outfn15,"wb");
	f17=fopen(outfn16,"wb");
	f18=fopen(outfn17,"wb");
	f19=fopen(outfn18,"wb");
	f20=fopen(outfn19,"wb");

	for(i=0;i<20;++i){for(j=0;j<256;++j){cv[i][j]='\0';}}

	fb=getc(f1);
	while(!feof(f1)&&(fb!=10)&&(fb!=13)){cv[0][cvi]=fb;++cvi;fb=getc(f1);}
	while(!feof(f1)&&(fb==10)||(fb==13)){fb=getc(f1);}
	ii=1;while(ii<10){k=0;while(!feof(f1)&&(fb!=10)&&(fb!=13)){cv[ii][k]=fb;++k;fb=getc(f1);}while(!feof(f1)&&(fb==10)||(fb==13)){fb=getc(f1);}++ii;}
	//cvi_=cvi;

	while(!feof(f1))
	{
		ii=1;
		while(ii<20)
		{	
			k=0;cvi_=cvi;while(k<strlen(cv[ii])){cv[0][cvi_]=cv[ii][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;}

			if(ii==1 ){for(i=0;i<cvi;++i){if(c[i]){fprintf(f2 ,"%c",cv[0][i]);}else{fprintf(f2 ,"_");}}fprintf(f2 ,"\n");}
			if(ii==2 ){for(i=0;i<cvi;++i){if(c[i]){fprintf(f3 ,"%c",cv[0][i]);}else{fprintf(f3 ,"_");}}fprintf(f3 ,"\n");}
			if(ii==3 ){for(i=0;i<cvi;++i){if(c[i]){fprintf(f4 ,"%c",cv[0][i]);}else{fprintf(f4 ,"_");}}fprintf(f4 ,"\n");}
			if(ii==4 ){for(i=0;i<cvi;++i){if(c[i]){fprintf(f5 ,"%c",cv[0][i]);}else{fprintf(f5 ,"_");}}fprintf(f5 ,"\n");}
			if(ii==5 ){for(i=0;i<cvi;++i){if(c[i]){fprintf(f6 ,"%c",cv[0][i]);}else{fprintf(f6 ,"_");}}fprintf(f6 ,"\n");}
			if(ii==6 ){for(i=0;i<cvi;++i){if(c[i]){fprintf(f7 ,"%c",cv[0][i]);}else{fprintf(f7 ,"_");}}fprintf(f7 ,"\n");}
			if(ii==7 ){for(i=0;i<cvi;++i){if(c[i]){fprintf(f8 ,"%c",cv[0][i]);}else{fprintf(f8 ,"_");}}fprintf(f8 ,"\n");}
			if(ii==8 ){for(i=0;i<cvi;++i){if(c[i]){fprintf(f9 ,"%c",cv[0][i]);}else{fprintf(f9 ,"_");}}fprintf(f9 ,"\n");}
			if(ii==9 ){for(i=0;i<cvi;++i){if(c[i]){fprintf(f10,"%c",cv[0][i]);}else{fprintf(f10,"_");}}fprintf(f10,"\n");}
			if(ii==10){for(i=0;i<cvi;++i){if(c[i]){fprintf(f11,"%c",cv[0][i]);}else{fprintf(f11,"_");}}fprintf(f11,"\n");}
			if(ii==11){for(i=0;i<cvi;++i){if(c[i]){fprintf(f12,"%c",cv[0][i]);}else{fprintf(f12,"_");}}fprintf(f12,"\n");}
			if(ii==12){for(i=0;i<cvi;++i){if(c[i]){fprintf(f13,"%c",cv[0][i]);}else{fprintf(f13,"_");}}fprintf(f13,"\n");}
			if(ii==13){for(i=0;i<cvi;++i){if(c[i]){fprintf(f14,"%c",cv[0][i]);}else{fprintf(f14,"_");}}fprintf(f14,"\n");}
			if(ii==14){for(i=0;i<cvi;++i){if(c[i]){fprintf(f15,"%c",cv[0][i]);}else{fprintf(f15,"_");}}fprintf(f15,"\n");}
			if(ii==15){for(i=0;i<cvi;++i){if(c[i]){fprintf(f16,"%c",cv[0][i]);}else{fprintf(f16,"_");}}fprintf(f16,"\n");}
			if(ii==16){for(i=0;i<cvi;++i){if(c[i]){fprintf(f17,"%c",cv[0][i]);}else{fprintf(f17,"_");}}fprintf(f17,"\n");}
			if(ii==17){for(i=0;i<cvi;++i){if(c[i]){fprintf(f18,"%c",cv[0][i]);}else{fprintf(f18,"_");}}fprintf(f18,"\n");}
			if(ii==18){for(i=0;i<cvi;++i){if(c[i]){fprintf(f19,"%c",cv[0][i]);}else{fprintf(f19,"_");}}fprintf(f19,"\n");}
			if(ii==19){for(i=0;i<cvi;++i){if(c[i]){fprintf(f20,"%c",cv[0][i]);}else{fprintf(f20,"_");}}fprintf(f20,"\n");}

			++ii;

		}
		cvi=0;k=0;while(k<strlen(cv[1])){cv[0][cvi]=cv[1][k];++cvi;++k;}

		for(i=1;i<20-1;++i){for(j=0;j<256;++j){cv[i][j]=cv[1+i][j];}}

		k=0;while(!feof(f1)&&(fb!=10)&&(fb!=13)){cv[20-1][k]=fb;++k;fb=getc(f1);}while(!feof(f1)&&(fb==10)||(fb==13)){fb=getc(f1);}	

	}
	fclose(f1);
	fclose(f2);
	fclose(f3);
	fclose(f4);
	fclose(f5);
	fclose(f6);
	fclose(f7);
	fclose(f8);
	fclose(f9);
	fclose(f10);
	fclose(f11);
	fclose(f12);
	fclose(f13);
	fclose(f14);
	fclose(f15);
	fclose(f16);
	fclose(f17);
	fclose(f18);
	fclose(f19);
	fclose(f20);

	ii=1;
	while(ii<20)
	{	
	if(ii==1 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn1 );}
	if(ii==2 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn2 );}
	if(ii==3 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn3 );}
	if(ii==4 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn4 );}
	if(ii==5 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn5 );}
	if(ii==6 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn6 );}
	if(ii==7 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn7 );}
	if(ii==8 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn8 );}
	if(ii==9 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn9 );}
	if(ii==10){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn10);}
	if(ii==11){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn11);}
	if(ii==12){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn12);}
	if(ii==13){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn13);}
	if(ii==14){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn14);}
	if(ii==15){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn15);}
	if(ii==16){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn16);}
	if(ii==17){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn17);}
	if(ii==18){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn18);}
	if(ii==19){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,outfn19);}

	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);
	++ii;
	}

	return(0);

}