//	This source and similar text sequences method apply to GNU General Public License. 
//			Copyright (C) 2001-2014 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  March 2014

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

FILE		*f1,*f2;

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

char		cv[256];int cvi=0;
char		nl[256];int nli=0;

int 		ba16_1[2][2][2][2][4];int ba16_1i=0;int ba16_2i=0;int ba16_3i=0;int ba16_4i=0;

int		nc_[100000][5];int nc_i=0;

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

int	a,b,c;
int	i,j,k,l,m,n,p;
int	fb;
int	nc=1;
int	v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16;
int	i1,j1,k1,l1;
int	i2,j2,k2,l2;
int	i3,j3,k3,l3;
int	i4,j4,k4,l4;


	for(i=0;i<2;++i){for(j=0;j<2;++j){for(k=0;k<2;++k){for(l=0;l<2;++l)
	{
		ba16_1[i][j][k][l][0]=-1;ba16_1[i][j][k][l][1]=-1;ba16_1[i][j][k][l][2]=-1;ba16_1[i][j][k][l][3]=-1;
	}}}}

	f1=fopen(infn,"rb");
	fb=getc(f1);
	while(!feof(f1))
	{
		cvi=0;while((fb!=10)&&(fb!=13)){cv[cvi]=fb;++cvi;fb=getc(f1);}
		if(nli==0){while((fb==10)||(fb==13)){nl[nli]=fb;++nli;fb=getc(f1);}}else{
			j=0;while((fb==10)||(fb==13)){if(nl[j]==fb){++j;if(j==nli){j=0;++nc;}}fb=getc(f1);}
		}

		//printf("%s\n",cv);

		v1=10*(-48+(int)cv[0] )+(-48+(int)cv[1] );if(v1>=1){v1=1;}
		v2=10*(-48+(int)cv[2] )+(-48+(int)cv[3] );if(v2>=1){v2=1;}
		v3=10*(-48+(int)cv[4] )+(-48+(int)cv[5] );if(v3>=1){v3=1;}
		v4=10*(-48+(int)cv[6] )+(-48+(int)cv[7] );if(v4>=1){v4=1;}
		v5=10*(-48+(int)cv[8] )+(-48+(int)cv[9] );if(v5>=1){v5=1;}
		v6=10*(-48+(int)cv[10])+(-48+(int)cv[11]);if(v6>=1){v6=1;}
		v7=10*(-48+(int)cv[12])+(-48+(int)cv[13]);if(v7>=1){v7=1;}
		v8=10*(-48+(int)cv[14])+(-48+(int)cv[15]);if(v8>=1){v8=1;}
		v9=10*(-48+(int)cv[16])+(-48+(int)cv[17]);if(v9>=1){v9=1;}
		v10=10*(-48+(int)cv[18])+(-48+(int)cv[19]);if(v10>=1){v10=1;}
		v11=10*(-48+(int)cv[20])+(-48+(int)cv[21]);if(v11>=1){v11=1;}
		v12=10*(-48+(int)cv[22])+(-48+(int)cv[23]);if(v12>=1){v12=1;}
		v13=10*(-48+(int)cv[24])+(-48+(int)cv[25]);if(v13>=1){v13=1;}
		v14=10*(-48+(int)cv[26])+(-48+(int)cv[27]);if(v14>=1){v14=1;}
		v15=10*(-48+(int)cv[28])+(-48+(int)cv[29]);if(v15>=1){v15=1;}
		v16=10*(-48+(int)cv[30])+(-48+(int)cv[31]);if(v16>=1){v16=1;}

		//printf("%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d\n",v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16);
		
		if(ba16_1[v1][v2][v3][v4][0]==-1){ba16_1[v1][v2][v3][v4][0]=ba16_1i;++ba16_1i;}
		if(ba16_1[v5][v6][v7][v8][1]==-1){ba16_1[v5][v6][v7][v8][1]=ba16_2i;++ba16_2i;}
		if(ba16_1[v9][v10][v11][v12][2]==-1){ba16_1[v9][v10][v11][v12][2]=ba16_3i;++ba16_3i;}
		if(ba16_1[v13][v14][v15][v16][3]==-1){ba16_1[v13][v14][v15][v16][3]=ba16_4i;++ba16_4i;}

		if(nc_i==0)
		{
			nc_[nc_i][0]=ba16_1[v1][v2][v3][v4][0];
			nc_[nc_i][1]=ba16_1[v5][v6][v7][v8][1];
			nc_[nc_i][2]=ba16_1[v9][v10][v11][v12][2];
			nc_[nc_i][3]=ba16_1[v13][v14][v15][v16][3];
			nc_[nc_i][4]=1;
			++nc_i;
		}else{
			for(i=0;i<nc_i;++i)
			{
				if
				(
				(nc_[i][0]==ba16_1[v1][v2][v3][v4][0])	&&
				(nc_[i][1]==ba16_1[v5][v6][v7][v8][1])	&&
				(nc_[i][2]==ba16_1[v9][v10][v11][v12][2]) &&
				(nc_[i][3]==ba16_1[v13][v14][v15][v16][3]) 
				){++nc_[i][4];break;}
			}
			if(i==nc_i)
			{
				nc_[nc_i][0]=ba16_1[v1][v2][v3][v4][0];
				nc_[nc_i][1]=ba16_1[v5][v6][v7][v8][1];
				nc_[nc_i][2]=ba16_1[v9][v10][v11][v12][2];
				nc_[nc_i][3]=ba16_1[v13][v14][v15][v16][3];
				nc_[nc_i][4]=1;
				++nc_i;
				if(nc_i>=100000)
				{
					printf(".\n");
					for(p=0;p<nc_i;++p)
					{
						for(i=0;i<2;++i){for(j=0;j<2;++j){for(k=0;k<2;++k){for(l=0;l<2;++l)
						{
							if(ba16_1[i][j][k][l][0]!=-1){if(ba16_1[i][j][k][l][0]==nc_[p][0]){i1=i;j1=j;k1=k;l1=l;break;}}
						}}}}
						for(i=0;i<2;++i){for(j=0;j<2;++j){for(k=0;k<2;++k){for(l=0;l<2;++l)
						{
							if(ba16_1[i][j][k][l][1]!=-1){if(ba16_1[i][j][k][l][1]==nc_[p][1]){i2=i;j2=j;k2=k;l2=l;break;}}
						}}}}
						for(i=0;i<2;++i){for(j=0;j<2;++j){for(k=0;k<2;++k){for(l=0;l<2;++l)
						{
							if(ba16_1[i][j][k][l][2]!=-1){if(ba16_1[i][j][k][l][2]==nc_[p][2]){i3=i;j3=j;k3=k;l3=l;break;}}
						}}}}
						for(i=0;i<2;++i){for(j=0;j<2;++j){for(k=0;k<2;++k){for(l=0;l<2;++l)
						{
							if(ba16_1[i][j][k][l][3]!=-1){if(ba16_1[i][j][k][l][3]==nc_[p][3]){i4=i;j4=j;k4=k;l4=l;break;}}
						}}}}
	
						if(i1==0){printf("0");}else{printf("1");}
						if(j1==0){printf("0");}else{printf("1");}
						if(k1==0){printf("0");}else{printf("1");}
						if(l1==0){printf("0");}else{printf("1");}
						if(i2==0){printf("0");}else{printf("1");}
						if(j2==0){printf("0");}else{printf("1");}
						if(k2==0){printf("0");}else{printf("1");}
						if(l2==0){printf("0");}else{printf("1");}
						if(i3==0){printf("0");}else{printf("1");}
						if(j3==0){printf("0");}else{printf("1");}
						if(k3==0){printf("0");}else{printf("1");}
						if(l3==0){printf("0");}else{printf("1");}
						if(i4==0){printf("0");}else{printf("1");}
						if(j4==0){printf("0");}else{printf("1");}
						if(k4==0){printf("0");}else{printf("1");}
						if(l4==0){printf("0");}else{printf("1");}
						printf("\t%05d\t%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d\n",nc_[p][4],i1,j1,k1,l1,i2,j2,k2,l2,i3,j3,k3,l3,i4,j4,k4,l4);

						ba16_1i=0;ba16_2i=0;ba16_3i=0;ba16_4i=0;
						for(i=0;i<2;++i){for(j=0;j<2;++j){for(k=0;k<2;++k){for(l=0;l<2;++l)
						{
							ba16_1[i][j][k][l][0]=-1;ba16_1[i][j][k][l][1]=-1;ba16_1[i][j][k][l][2]=-1;ba16_1[i][j][k][l][3]=-1;
						}}}}
						nc_i=0;
					}
				}
			}
		}

	}

	for(p=0;p<nc_i;++p)
	{
		for(i=0;i<2;++i){for(j=0;j<2;++j){for(k=0;k<2;++k){for(l=0;l<2;++l)
		{
			if(ba16_1[i][j][k][l][0]!=-1){if(ba16_1[i][j][k][l][0]==nc_[p][0]){i1=i;j1=j;k1=k;l1=l;break;}}
		}}}}
		for(i=0;i<2;++i){for(j=0;j<2;++j){for(k=0;k<2;++k){for(l=0;l<2;++l)
		{
			if(ba16_1[i][j][k][l][1]!=-1){if(ba16_1[i][j][k][l][1]==nc_[p][1]){i2=i;j2=j;k2=k;l2=l;break;}}
		}}}}
		for(i=0;i<2;++i){for(j=0;j<2;++j){for(k=0;k<2;++k){for(l=0;l<2;++l)
		{
			if(ba16_1[i][j][k][l][2]!=-1){if(ba16_1[i][j][k][l][2]==nc_[p][2]){i3=i;j3=j;k3=k;l3=l;break;}}
		}}}}
		for(i=0;i<2;++i){for(j=0;j<2;++j){for(k=0;k<2;++k){for(l=0;l<2;++l)
		{
			if(ba16_1[i][j][k][l][3]!=-1){if(ba16_1[i][j][k][l][3]==nc_[p][3]){i4=i;j4=j;k4=k;l4=l;break;}}
		}}}}

		if(i1==0){printf("0");}else{printf("1");}
		if(j1==0){printf("0");}else{printf("1");}
		if(k1==0){printf("0");}else{printf("1");}
		if(l1==0){printf("0");}else{printf("1");}
		if(i2==0){printf("0");}else{printf("1");}
		if(j2==0){printf("0");}else{printf("1");}
		if(k2==0){printf("0");}else{printf("1");}
		if(l2==0){printf("0");}else{printf("1");}
		if(i3==0){printf("0");}else{printf("1");}
		if(j3==0){printf("0");}else{printf("1");}
		if(k3==0){printf("0");}else{printf("1");}
		if(l3==0){printf("0");}else{printf("1");}
		if(i4==0){printf("0");}else{printf("1");}
		if(j4==0){printf("0");}else{printf("1");}
		if(k4==0){printf("0");}else{printf("1");}
		if(l4==0){printf("0");}else{printf("1");}
		printf("\t%05d\t%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d%02d\n",nc_[p][4],i1,j1,k1,l1,i2,j2,k2,l2,i3,j3,k3,l3,i4,j4,k4,l4);
	}
	return(0);

}