/*
this program from a master's blog but i don't know what dose it work,hope
that somebody will explain this working detailedly(may be related to the
palindrome ).
this's about descriptions to be solved quesion:
Catcher to be an agent of MCA country,he have been found any symmetrical code
that enemy states used to comunicate with,such as:
ABBA,ABA,A,123321 .
but they sometimes encoded into irrelevant characters to prevent others contry
's crack,such as the following Variants:
ABBA->12ABBA,ABA->ABAKK,123321->51233214
because of being intercepted characters to be too long,and with vary situation(abaab
seems to be aba ,or baaab),of catcher ' s a great deal of working ,he have
to get master helping,could you find out the lengthest vaild characters?
*/
Code:
#include <stdio.h>
#include <string.h>
#include <memory.h>
//#include <assert.h>
#define MAX_LEN 80
char szInput[MAX_LEN+1];
unsigned char B[(MAX_LEN+1)*MAX_LEN/8];
unsigned char Bit8={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
#define GETB(i,j) (B[(i)*10+((j)>>3)] & Bit8[(j) & 0x7])
#define SETB(i,j) (B[(i)*10+((j)>>3)] |= Bit8[(j) & 0x7])
int length;
int getcode()
{
int i,j,notfound1,notfound2=0;
if(length <2)
return 1;
memset(B,0,(length+1)*10);//MAX_LEN/8?
for(i=0;i <2;++i)
{
for(j=0;j <length;++j)
SETB(i,j);//B[j]=1;
}
for(i=2;i <=length;++i)
{
for(notfound1=1,j=0;j <=length-i;++j)
{
if(GETB(i-2,j+1)/*B[i-2][j+1]*/ && szInput[j]==szInput[j+i-1])
{
SETB(i,j);//B[j]=1;
//printf("%d,%d = 1\n",i,j);
notfound1=0;
}
}
if(notfound1)
{
if(notfound2)
return i-2;
notfound2=1;
}
else
{
notfound2=0;
}
}
if(notfound2)
return length-1;
return length;
}
int main(void)
{
while(scanf("%s",szInput)!=EOF)
{
length=strlen(szInput);
//assert(length <=MAX_LEN);
printf("%d\n",getcode());
}
return 0;
}
help.........................................pleas e