Clairewd’s message
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8379 Accepted Submission(s): 3071
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.
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.
T<= 100 ;
n<= 100000;
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
Author
BUPT
Source
Recommend
zhuyuanchen520 | We have carefully selected several similar problems for you:
4302
4301
4303
4304
4308
这道题目
这道题目属于kmp算法的应用类型,题目大意就是有个每个字符的对应法则,
然后给定一个串,这个串如果拆成两部分且前半部分是密文后半部分是对应的明文的
前缀,那么符合要求,现在题目想求这样得到的最短的原串是什么。
解:把给定的串翻译成明文拼接到后面,(拼接手段在字符串处理中很常见)
对整体进行取next数组的操作,得到的最大前缀后缀相等部分,便是答案(注意
密文一定是超过原串长度一半的)。
#include<string.h>
#include<stdio.h>
const int maxn=2e5+7;
char table[maxn];
char text[maxn];
int Next[maxn],pass[maxn];
/*
³¬Ê±¡£¡£¡£¡£
*/
void getNext(char* t)
{
int k=-1,i=0,len=strlen(t);
Next[0]=-1;
while(i < len+1)
{
if(k==-1 || t[i]==t[k])
Next[++i] = ++k;
else
k = Next[k];
}
}
//void handle(string& s)
//{
// int len=s.size();
// if(len&1) s=s.substr(0,len/2+1)+'*';
// else s=s.substr(0,len/2)+'*';
//}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
scanf("%s%s",table,text);
int i;
int N=strlen(text);
int Mid=(N+1)/2;
for(i=0; table[i]; i++)
{
pass[ table[i]-'a' ] = i+'a';
}
for(i=0;i<Mid;i++) table[i]=pass[text[i]-'a'];
table[i]='*',table[i+1]=0;
strcat(table,text+i);
getNext(table);
Mid=N-Next[strlen(table)-1]-1;
for( i=0;i<Mid;i++)
{
table[i]=text[i];
table[i+Mid]=pass[table[i]-'a'];
}
table[i+Mid]=0;
printf("%s\n",table);
}
return 0;
}