Clairewd’s message

时间:2023-03-09 07:17:08
Clairewd’s message
Problem Description
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important messages and she was preparing for sending it to ykwd. They had agreed that each letter of these messages would be transfered to another one according to a conversion table.

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.
Input
The first line contains only one integer T, which is the number of test cases.
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.
Hint

Range of test data:
T<= 100 ;
n<= 100000;

Output
For each test case, output one line contains the shorest possible complete text.
Sample Input
2
abcdefghijklmnopqrstuvwxyz
abcdab
qwertyuiopasdfghjklzxcvbnm
qwertabcde
Sample Output
abcdabcd
qwertabcde
 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int MS=;
char str1[MS],str2[MS],str3[MS];
int next[MS];
int table[];
void get_next(char *s,int *next)
{
int i=,j=;
next[]=;
int len=strlen(s);
while(i<len)
{
if(j==||s[i-]==s[j-])
{
i++;
j++;
next[i]=j; //求最大循环次数 或者前后公共缀的长度 就用这个
/*
if(s[i-1]==s[j-1])
next[i]=next[j]; //优化了功能却减弱了
else
next[i]=j;
*/
}
else
j=next[j];
}
} int KMP(char *s,char *t,int pos)
{
int i=pos,j=;
int len1=strlen(s);
int len2=strlen(t);
get_next(t,next);
while(i<=len1&&j<=len2)
{
if(j==||s[i-]==t[j-])
{
i++;
j++;
}
else
j=next[j];
}
/*
if(j>len2)
return i-len2-1;
return -1;
*/
return j-;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str1);
scanf("%s",str2);
int len1=strlen(str1);
int len2=strlen(str2);
for(int i=;i<len1;i++)
{
table[str1[i]-'a']=i;
}
int j=;
for(int i=;i<len2;i++)
{
str3[j++]=table[str2[i]-'a']+'a';
}
str3[j]='\0';
j=KMP(str2,str3,(len2+)/+);
if(j*==len2)
printf("%s\n",str2);
else
{
printf("%s",str2);
int tmp=len2-j;
for(int i=j;i<tmp;i++)
printf("%c",str3[i]);
printf("\n");
}
}
return ;
}