//	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 <ctype.h>
#include <math.h>

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

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

char	out1fn[256]="out1_.txt";
char	out2fn[256]="out2_.txt";
char	out3fn[256]="out3_.txt";
char	out4fn[256]="out4_.txt";
char	out5fn[256]="out5_.txt";
char	out6fn[256]="out6_.txt";
char	out7fn[256]="out7_.txt";
char	out8fn[256]="out8_.txt";
char	out9fn[256]="out9_.txt";
char	out10fn[256]="out10_.txt";
char	out11fn[256]="out11_.txt";
char	out12fn[256]="out12_.txt";
char	out13fn[256]="out13_.txt";
char	out14fn[256]="out14_.txt";
char	out15fn[256]="out15_.txt";
char	out16fn[256]="out16_.txt";
char	out17fn[256]="out17_.txt";
char	out18fn[256]="out18_.txt";
char	out19fn[256]="out19_.txt";
char	out20fn[256]="out20_.txt";

char	sv[256][256];int svi;
int	cv[256];
int	cv_[256];

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

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];

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

	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 p_1(int d,int l)
{

int	a,b,c,cc,cm;

int	i,j,k,m,n;
int	fb;

	fi=fopen(infn,"rb");
	f1=fopen(out1fn,"wb");
	f2=fopen(out2fn,"wb");
	f3=fopen(out3fn,"wb");
	f4=fopen(out4fn,"wb");
	f5=fopen(out5fn,"wb");
	f6=fopen(out6fn,"wb");
	f7=fopen(out7fn,"wb");
	f8=fopen(out8fn,"wb");
	f9=fopen(out9fn,"wb");
	f10=fopen(out10fn,"wb");
	f11=fopen(out11fn,"wb");
	f12=fopen(out12fn,"wb");
	f13=fopen(out13fn,"wb");
	f14=fopen(out14fn,"wb");
	f15=fopen(out15fn,"wb");
	f16=fopen(out16fn,"wb");
	f17=fopen(out17fn,"wb");
	f18=fopen(out18fn,"wb");
	f19=fopen(out19fn,"wb");
	f20=fopen(out20fn,"wb");
	fb=getc(fi);
	svi=0;
	while(!feof(fi)&&svi<=d)
	{
		for(i=0;i<256;++i){sv[svi][i]='\0';}i=0;
		while(!feof(fi)&&(fb!=10)&&(fb!=13)){sv[svi][i]=fb;++i;fb=getc(fi);}
		while(!feof(fi)&&(fb==10)||(fb==13)){fb=getc(fi);}++svi;
	}
	while(!feof(fi))
	{
		c=1;while(c<svi)
		{
			cm=-1;b=0;while(b<strlen(sv[0])-l)
			{
				cc=0;for(i=0;i<256;++i){cv[i]=0;}a=0;while(a+b<strlen(sv[0])-l)
				{

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

		c=0;
		while(c<-1+svi){for(i=0;i<256;++i){sv[c][i]=sv[1+c][i];}++c;}
		for(i=0;i<256;++i){sv[-1+svi][i]='\0';}i=0;
		while(!feof(fi)&&(fb!=10)&&(fb!=13)){sv[-1+svi][i]=fb;++i;fb=getc(fi);}
		while(!feof(fi)&&(fb==10)||(fb==13)){fb=getc(fi);}		
	}
	fclose(fi);
	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);

	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];

	if(argc<2){return(0);}
	if(strlen(argv[1])==0){return(0);}
	strcpy(infn,argv[1]);
	
	p_1(20,3);

	ii=1;
	while(ii<=20)
	{	
	if(ii==1 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out1fn );}
	if(ii==2 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out2fn );}
	if(ii==3 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out3fn );}
	if(ii==4 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out4fn );}
	if(ii==5 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out5fn );}
	if(ii==6 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out6fn );}
	if(ii==7 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out7fn );}
	if(ii==8 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out8fn );}
	if(ii==9 ){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out9fn );}
	if(ii==10){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out10fn);}
	if(ii==11){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out11fn);}
	if(ii==12){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out12fn);}
	if(ii==13){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out13fn);}
	if(ii==14){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out14fn);}
	if(ii==15){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out15fn);}
	if(ii==16){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out16fn);}
	if(ii==17){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out17fn);}
	if(ii==18){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out18fn);}
	if(ii==19){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out19fn);}
	if(ii==20){for(i=0;i<256;++i){outfn__[i]='\0';}strcat(outfn__,out20fn);}
	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);

}