【题目】
1002: Clairewd’s message
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 832 Solved: 318
[Submit][Status][Web Board]
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.
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 <stdio.h>
#include <string.h>
int main()
{
int t; scanf("%d",&t);
char a[30],aa[200],b[100005];
while(t--)
{
scanf("%s%s",a,b);
int l1=strlen(a),l2=strlen(b);
for(int i=0;i<l1;i++) aa[a[i]]=i;
int n=(l2+1)/2,i=0;
while(n+i<l2)
{
if(aa[b[i]]+'a'==b[i+n]) i++;
else n++,i=0;
}
for(int i=0;i<n;i++) printf("%c",b[i]);
for(int i=0;i<n;i++) printf("%c",aa[b[i]]+'a');
puts("");
}
return 0;
}