Clairewd’s message(HDU-4300)
题面
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.
输入
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.
Range of test data: T<= 100 ; n<= 100000;
输出
For each test case, output one line contains the shorest possible complete text.
样例输入
12
2abcdefghijklmnopqrstuvwxyz
3abcdab
4qwertyuiopasdfghjklzxcvbnm
5qwertabcde
样例输出
1abcdabcd
2qwertabcde
提示
无
思路
给你一个a-z的对照表,以及一个 密文+不完整明文 的字符串。求最短的 密文+完整明文。先解密前半部分,再求一个最大公共前缀后缀。注意考虑前后缀重叠的情况,即next[len]>len/2。
代码
1char s[mxn], t[mxn], p[mxn];
2int nxt[mxn];
3
4void getnxt(char* t, int m)
5{
6 int i = 0, j = -1; nxt[0] = -1;
7 while (i < m)
8 {
9 if (j == -1 || t[i] == t[j]) {
10 i++, j++;
11 // if (t[i] == t[j])
12 // nxt[i] = nxt[j]; // next数组优化
13 // else
14 nxt[i] = j;
15 } else
16 j = nxt[j];
17 }
18}
19
20int main()
21{
22 int T; scanf("%d", &T);
23 while(T--)
24 {
25 for(int i=0; i<26; i++){
26 char c; scanf(" %c", &c);
27 p[c-'a'] = 'a'+i;
28 }
29
30 scanf("%s", s);
31 int n = strlen(s);
32
33 int m = (n+1)/2;
34 for(int i=0; i<m; i++){
35 t[i] = p[s[i]-'a'];
36 t[i+m] = s[i+m];
37 }
38 getnxt(t, n);
39
40 m = nxt[n];
41 if(m > n/2)
42 m = n/2;
43 m = n-m;
44
45 for(int i=0; i<m; i++){
46 printf("%c", s[i]);
47 }
48 for(int i=0; i<m; i++){
49 printf("%c", p[s[i]-'a']);
50 }
51 printf("\n");
52 }
53 return 0;
54}