hdu 4300
Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2557 Accepted Submission(s): 1005
Unfortunately, GFW(someone's name, not what you just think about) has detected their action. He also got their conversion table by some unknown methods before. Clairewd was so clever and vigilant that when she realized that somebody was monitoring their action, she just stopped transmitting messages.
But GFW knows that Clairewd would always firstly send the ciphertext and then plaintext(Note that they won't overlap each other). But he doesn't know how to separate the text because he has no idea about the whole message. However, he thinks that recovering the shortest possible text is not a hard task for you.
Now GFW will give you the intercepted text and the conversion table. You should help him work out this problem.
Each test case contains two lines. The first line of each test case is the conversion table S. S[i] is the ith latin letter's cryptographic letter. The second line is the intercepted text which has n letters that you should recover. It is possible that the text is complete.
T<= 100 ;
n<= 100000;
#include<stdio.h>
#include<string.h>
int next[100001];
char s[27],t[100001],t2[100001];
void getnext(int len,char *t)
{
int i,j;
next[0]=-1;
i=0,j=-1;
while(i<len)
{
if(j==-1||t[i]==t[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int kmp(char *s,char *t)
{
int i,j,lens,lent;
lens=strlen(s);
lent=strlen(t);
getnext(lent,t);
i=j=0;
while(i<lens&&j<lent)
{
if(j==-1 || s[i]==t[j])
{
i++;
j++;
}
else
j=next[j];
}
if(i==lens)
return j;
return 0;
}
int main()
{
int T,i,len,j,dex;
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
scanf("%s",t);
len=strlen(t);
for(i=(len+1)/2,j=0;i<len;i++,j++)
t2[j]=t[i];
t2[j]='\0';
printf("%s",t);
for(i=0;i<len;i++)
for(j=0;j<26;j++)
if(t[i]==s[j])
{
t[i]='a'+j;
break;
}
dex=kmp(t2,t);
for(i=dex;i<len-dex;i++)
printf("%c",t[i]);
printf("\n");
}
return 0;
}