//	This source and similar text sequences method apply to GNU General Public License. 
//			Copyright (C) 2001-2016 Jasenko Dzinleski 

//		This program is free software; you can redistribute it
//	and/or modify it under the terms of the GNU General Public License as
//	published by the Free Software Foundation; either version 2 of the
//	License, or (at your option) any later version. 

//	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.

//  	Similar Text Sequences 
//	
//	written by Dzinleski Jasenko  April 2016



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

#define		seql_ 22

FILE	*f1;

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

char	sv[5000][256];int svi=0;
char	iv[5000][30][256];
char	la[256];int lai;
char	lc[3];

int p_1(int sl)
{
int	i,j,k,l,m,n;

	for(i=0;i<svi;++i){for(j=0;j<30;++j){for(k=0;k<256;++k){iv[i][j][k]='_';}}}
	for(i=0;i<svi;++i){for(j=0;j<sl;++j){iv[i][-65+sv[i][j]][j]='1';}}
	for(i=0;i<svi;++i){for(j=0;j<30;++j){for(k=0;k<256;++k){
		if(iv[i][j][k]!='_')
		{
			
			m=0;for(l=0;l<sl;++l){if(iv[i][j][l]=='1'){++m;}}printf("%02d\t",m);

			for(l=0;l<sl;++l){if(iv[i][j][l]=='_')
			{
			printf("%c",iv[i][j][l]);
			while(iv[i][j][l]=='_'&&l<sl){++l;}if(iv[i][j][l]!='_'){printf("%c",iv[i][j][l]);}}else{printf("%c",iv[i][j][l]);}
			}

			printf("\t%c\t",65+j);

			for(l=0;l<sl;++l){printf("%c",iv[i][j][l]);}printf("\t%d",i);printf("\n");

			break;
		}
	}}printf("\n");}

	return(0);
}

int p_2(int seql)
{
	int	i,j,k,l,m,n;
	long 	mult[10];
	char	la_[256];int la_i=0;

	for(i=0;i<256;++i){la_[i]='\0';}
	for(i=0;i<10;++i){mult[i]=-1;}

	j=0;i=0;
	while(seql-pow(10,i)>=0)
	{
		//printf("%d ",(int)pow(10,i));
		mult[j]=(long)pow(10,i);
		++j;++i;
	}
	//printf("\n");

	m=-1;
	while(j>0)
	{
		--j;
		n=(int)((double)seql/(double)mult[j]);
		if(m!=-1)
		{
			//printf("%d\n",n-m*10);
			la_[la_i]=48+(n-m*10);++la_i;m=n;
		}else{
			if(n==10)
			{
				//printf("1");
				la_[la_i]='1';++la_i;
			}else{
				//printf("%d\n",n);
				la_[la_i]=48+n;++la_i;
			}
			m=n;
		}
		n=seql-mult[j]*n;
	}
	//for(i=0;i<strlen(la_);++i){printf("%c",la_[i]);}printf("\n");
	lc[0]='0';lc[1]='0';lc[2]='0';
	j=2;for(i=-1+strlen(la_);i>=0;--i){lc[j]=la_[i];--j;}
printf("%s\n",lc);


	return(0);

}

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

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

	p_2(seql_);

	for(i=0;i<5000;++i){for(j=0;j<256;++j){sv[i][j]='\0';}}svi=0;

	f1=fopen(infn,"rb");
	fb=getc(f1);lai=0;while((fb!=9)&&(fb!=10)&&(fb!=13)&&!feof(f1)){la[lai]=fb;++lai;fb=getc(f1);}fb=getc(f1);
	while(!feof(f1)&&svi<5000)
	{
		
		if(la[0]==lc[0]&&la[1]==lc[1]&&la[2]==lc[2])
		{
			j=0;while((fb!=10)&&(fb!=13)&&!feof(f1)){sv[svi][j]=fb;++j;fb=getc(f1);}
			while((fb==10)||(fb==13)&&!feof(f1)){fb=getc(f1);}
			//printf("%s\n",sv[svi]);
			++svi;
			if(svi>=5000){p_1(seql_);for(i=0;i<5000;++i){for(j=0;j<256;++j){sv[i][j]='\0';}}svi=0;}
		}
		lai=0;while((fb!=9)&&(fb!=10)&&(fb!=13)&&!feof(f1)){la[lai]=fb;++lai;fb=getc(f1);}fb=getc(f1);
	}
	fclose(f1);
	p_1(seql_);
	//printf("%d\n",svi);

	return(0);

}