//	This source and Text sequences' binary patterns method apply to GNU General Public License. 
//			Copyright (C) 2015  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 2001 - 2013 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 sequences' binary patterns
//		written by Dzinleski Jasenko March , 2015
//


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

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

FILE	*f1;

int bl[10000][4*4+1+5];
int bli=0;
int bl_[4*4];
int bl_i;

int pm_x,pj_x,pm_n,pj_n;

int df=0;

int p_mx(int bn , int n , int fl)
{

int i,j,k,l,m;
int a,b,c;
int bf,pm,pj,lbn=0,ln;

j=fl-1;while(j>=0){if(((bn>>j)&0x1)==0x1){break;lbn=j;}--j;}

j=0;pm=-1;pj=0;
while(j<fl-lbn)
{
	k=0;bf=bn<<j;c=0;m=-1;
	while(k<fl)
	{
		if(((n>>k)&0x1)==0x1){a=1;}else{a=0;}
		if(((bf>>k)&0x1)==0x1){b=1;}else{b=0;}
		if(((a==1)||(b==1))&&(a==b)){++c;}
		++k;
	}	
	if(pm==-1){pm=c;pj=j;}else{if(pm<c){pm=c;pj=j;}}
	++j;
}
if(df){printf("max:\tcnt:%d\tpos:%d\n",pm,pj);}
pm_x=pm;pj_x=pj;
return(pm);
}

int p_mn(int bn , int n , int fl)
{

int i,j,k,m;
int a,b,c;
int bf,pm,pj,lbn=0,ln;

j=fl-1;while(j>=0){if(((bn>>j)&0x1)==0x1){break;lbn=j;}--j;}

j=0;pm=-1;pj=0;
while(j<fl-lbn)
{
	k=0;bf=bn<<j;c=0;m=99;
	while(k<fl)
	{
		if(((n>>k)&0x1)==0x1){a=1;}else{a=0;}
		if(((bf>>k)&0x1)==0x1){b=1;}else{b=0;}
		if(((a==1)||(b==1))&&(a==b)){}else{++c;}
		++k;
	}
	if(pm==-1){pm=c;pj=j;}else{if((pm<c)&&(c>0)){pm=c;pj=j;}}
	++j;
}
if(df){printf("min:\tcnt:%d\tpos:%d\n",pm,pj);}
pm_n=pm;pj_n=pj;
return(pm);
}

int p_1(int n,int fl)
{

int i,j,k,m;
int b_,b1,b2,b3;
int a,b,c;
int mr,md,mx,mn;

//	11110	11111
//	10100	10101
//	01010	01011

b1=(1<<4)|(1<<3)|(1<<2)|(1<<1)|(0<<0);
b2=(1<<4)|(0<<3)|(1<<2)|(0<<1)|(0<<0);
b3=(0<<4)|(1<<3)|(0<<2)|(1<<1)|(0<<0);

mx=p_mx(b1,n,fl);
mn=p_mn(b1,n,fl);

mx=p_mx(b2,n,fl);
mn=p_mn(b2,n,fl);

mx=p_mx(b3,n,fl);
mn=p_mn(b3,n,fl);

return(0);

}

int p_2()
{

int i,j,k,m;
int b1,b2,b3;
int a,b,c;

int n,b_;


int fb[8];
int fbi;


f1=fopen(fn1,"rb");

while(!feof(f1))
{

//----
	fbi=0;
	fb[0]=fb[1];
	fb[1]=fb[2];
	fb[2]=fb[3];
	fb[3]=fb[4];
	fb[4]=getc(f1);while(fb[4]==10||fb[4]==13){fb[4]=getc(f1);}

	n=0;
	n|=(((int)fb[0])<<8);
	n|=(((int)fb[1])<<0);
	if(df){j=0;while(j<16){if(((n>>j)&0x1)==0x1){printf("1");}else{printf("0");}++j;}printf("\n");}
	p_1(n,16);
	bl_i=0;
	bl_[bl_i]=pm_x;++bl_i;
	bl_[bl_i]=pj_x;++bl_i;
	bl_[bl_i]=pm_n;++bl_i;
	bl_[bl_i]=pj_n;++bl_i;

//----
	fb[0]=fb[1];
	fb[1]=fb[2];
	fb[2]=fb[3];
	fb[3]=fb[4];
	fb[4]=getc(f1);while(fb[4]==10||fb[4]==13){fb[4]=getc(f1);}

	n=0;
	n|=(((int)fb[0])<<8);
	n|=(((int)fb[1])<<0);
	if(df){j=0;while(j<16){if(((n>>j)&0x1)==0x1){printf("1");}else{printf("0");}++j;}printf("\n");}
	p_1(n,16);
	bl_[bl_i]=pm_x;++bl_i;
	bl_[bl_i]=pj_x;++bl_i;
	bl_[bl_i]=pm_n;++bl_i;
	bl_[bl_i]=pj_n;++bl_i;

//----
	fb[0]=fb[1];
	fb[1]=fb[2];
	fb[2]=fb[3];
	fb[3]=fb[4];
	fb[4]=getc(f1);while(fb[4]==10||fb[4]==13){fb[4]=getc(f1);}

	n=0;
	n|=(((int)fb[0])<<8);
	n|=(((int)fb[1])<<0);
	if(df){j=0;while(j<16){if(((n>>j)&0x1)==0x1){printf("1");}else{printf("0");}++j;}printf("\n");}
	p_1(n,16);
	bl_[bl_i]=pm_x;++bl_i;
	bl_[bl_i]=pj_x;++bl_i;
	bl_[bl_i]=pm_n;++bl_i;
	bl_[bl_i]=pj_n;++bl_i;

//----
	fb[0]=fb[1];
	fb[1]=fb[2];
	fb[2]=fb[3];
	fb[3]=fb[4];
	fb[4]=getc(f1);while(fb[4]==10||fb[4]==13){fb[4]=getc(f1);}

	n=0;
	n|=(((int)fb[0])<<8);
	n|=(((int)fb[1])<<0);
	if(df){j=0;while(j<16){if(((n>>j)&0x1)==0x1){printf("1");}else{printf("0");}++j;}printf("\n");}
	p_1(n,16);
	bl_[bl_i]=pm_x;++bl_i;
	bl_[bl_i]=pj_x;++bl_i;
	bl_[bl_i]=pm_n;++bl_i;
	bl_[bl_i]=pj_n;++bl_i;

	if(bli==0)
	{
	bl[bli][0]=bl_[0];
	bl[bli][1]=bl_[1];
	bl[bli][2]=bl_[2];
	bl[bli][3]=bl_[3];
	bl[bli][4]=bl_[4];
	bl[bli][5]=bl_[5];
	bl[bli][6]=bl_[6];
	bl[bli][7]=bl_[7];
	bl[bli][8]=bl_[8];
	bl[bli][9]=bl_[9];
	bl[bli][10]=bl_[10];
	bl[bli][11]=bl_[11];
	bl[bli][12]=bl_[12];
	bl[bli][13]=bl_[13];
	bl[bli][14]=bl_[14];
	bl[bli][15]=bl_[15];
	bl[bli][16]=1;
	bl[bli][17]=fb[0];
	bl[bli][18]=fb[1];
	bl[bli][19]=fb[2];
	bl[bli][20]=fb[3];
	bl[bli][21]=fb[4];
	++bli;

	}else{

	for(i=0;i<bli;++i)
	{
		if
		(
		bl[i][0]==bl_[0]&&
		bl[i][1]==bl_[1]&&
		bl[i][2]==bl_[2]&&
		bl[i][3]==bl_[3]&&
		bl[i][4]==bl_[4]&&
		bl[i][5]==bl_[5]&&
		bl[i][6]==bl_[6]&&
		bl[i][7]==bl_[7]&&
		bl[i][8]==bl_[8]&&
		bl[i][9]==bl_[9]&&
		bl[i][10]==bl_[10]&&
		bl[i][11]==bl_[11]&&
		bl[i][12]==bl_[12]&&
		bl[i][13]==bl_[13]&&
		bl[i][14]==bl_[14]&&
		bl[i][15]==bl_[15]
		){++bl[i][16];break;}
	}

	if(i==bli)
	{
		bl[bli][0]=bl_[0];
		bl[bli][1]=bl_[1];
		bl[bli][2]=bl_[2];
		bl[bli][3]=bl_[3];
		bl[bli][4]=bl_[4];
		bl[bli][5]=bl_[5];
		bl[bli][6]=bl_[6];
		bl[bli][7]=bl_[7];
		bl[bli][8]=bl_[8];
		bl[bli][9]=bl_[9];
		bl[bli][10]=bl_[10];
		bl[bli][11]=bl_[11];
		bl[bli][12]=bl_[12];
		bl[bli][13]=bl_[13];
		bl[bli][14]=bl_[14];
		bl[bli][15]=bl_[15];
		bl[bli][16]=1;
		bl[bli][17]=fb[0];
		bl[bli][18]=fb[1];
		bl[bli][19]=fb[2];
		bl[bli][20]=fb[3];
		bl[bli][21]=fb[4];
		++bli;
	}
	}

}
fclose(f1);

for(i=0;i<bli;++i)
{
	printf("%02d",bl[i][0]);
	printf("%02d",bl[i][1]);
	printf("%02d",bl[i][2]);
	printf("%02d",bl[i][3]);
	printf("\t");
	printf("%02d",bl[i][4]);
	printf("%02d",bl[i][5]);
	printf("%02d",bl[i][6]);
	printf("%02d",bl[i][7]);
	printf("\t");
	printf("%02d",bl[i][8]);
	printf("%02d",bl[i][9]);
	printf("%02d",bl[i][10]);
	printf("%02d",bl[i][11]);
	printf("\t");
	printf("%02d",bl[i][12]);
	printf("%02d",bl[i][13]);
	printf("%02d",bl[i][14]);
	printf("%02d",bl[i][15]);
	printf("\t");
	printf("%d",bl[i][16]);
	printf("\t");
	printf("%c%c%c%c%c\n",bl[i][17],bl[i][18],bl[i][19],bl[i][20],bl[i][21]);
}
	
return(0);
}

int main()
{

	p_2();

	return(0);
}