密码截获
Time Limit:1000MS Memory Limit:1024K
Description:
Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解。比如进行下列变化ABBA->12ABBA,ABA->ABAKK,123321->51233214 。因为截获的串太长了,而且存在多种可能的情况(abaaab可看作是aba,或baaab的加密形式),Cathcer的工作量实在是太大了,他只能向电脑高手求助,你能帮Catcher找出最长的有效密码串吗?
Input:
测试数据有若干行字符串,包括字母,数字,符号。(字母区分大小写)Output:
与输入相对应每一行输出一个整数,代表最长有效密码串的长度。Sample Input:
ABBA 12ABBA A ABAKK 51233214 abaaab
Sample Output:
4 4 1 3 6 5
Source:
Jin Qiwei后来才发现只要80长就够了,所以测试数据很弱,不用DP也行。
/*
#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];
int length;
int getcode()
{
int i,j,notfound1,notfound2=0;
if(length<2)
return 1;
memset(B,0,(length+1)*MAX_LEN);
for(i=0;i<2;++i)
{
for(j=0;j<length;++j)
B[i][j]=1;
}
for(i=2;i<=length;++i)
{
for(notfound1=1,j=0;j<=length-i;++j)
{
if(B[i-2][j+1] && szInput[j]==szInput[j+i-1])
{
B[i][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;
}
35 49172 rickone 0 184 902 GCC 2006-8-20 0:42:20
*/
#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[8]={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);
for(i=0;i<2;++i)
{
for(j=0;j<length;++j)
SETB(i,j);//B[i][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[i][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;
}
#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];
int length;
int getcode()
{
int i,j,notfound1,notfound2=0;
if(length<2)
return 1;
memset(B,0,(length+1)*MAX_LEN);
for(i=0;i<2;++i)
{
for(j=0;j<length;++j)
B[i][j]=1;
}
for(i=2;i<=length;++i)
{
for(notfound1=1,j=0;j<=length-i;++j)
{
if(B[i-2][j+1] && szInput[j]==szInput[j+i-1])
{
B[i][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;
}
35 49172 rickone 0 184 902 GCC 2006-8-20 0:42:20
*/
#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[8]={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);
for(i=0;i<2;++i)
{
for(j=0;j<length;++j)
SETB(i,j);//B[i][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[i][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;
}
/*35 49181 rickone 0 180 1122 GCC 2006-8-20 1:10:04*/
以为改成用bit会Memory/8,呵呵
转载请注明文章出处:http://www.cnblogs.com/gredswsh Gredswsh 专栏