【题解】HDU-4300 Clairewd’s message

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.

样例输入

1
2
3
4
5
2
abcdefghijklmnopqrstuvwxyz
abcdab
qwertyuiopasdfghjklzxcvbnm
qwertabcde

样例输出

1
2
abcdabcd
qwertabcde

提示

思路

给你一个a-z的对照表,以及一个 密文+不完整明文 的字符串。求最短的 密文+完整明文。先解密前半部分,再求一个最大公共前缀后缀。注意考虑前后缀重叠的情况,即next[len]>len/2。

代码

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
char s[mxn], t[mxn], p[mxn];
int nxt[mxn];

void getnxt(char* t, int m)
{
int i = 0, j = -1; nxt[0] = -1;
while (i < m)
{
if (j == -1 || t[i] == t[j]) {
i++, j++;
// if (t[i] == t[j])
// nxt[i] = nxt[j]; // next数组优化
// else
nxt[i] = j;
} else
j = nxt[j];
}
}

int main()
{
int T; scanf("%d", &T);
while(T--)
{
for(int i=0; i<26; i++){
char c; scanf(" %c", &c);
p[c-'a'] = 'a'+i;
}

scanf("%s", s);
int n = strlen(s);

int m = (n+1)/2;
for(int i=0; i<m; i++){
t[i] = p[s[i]-'a'];
t[i+m] = s[i+m];
}
getnxt(t, n);

m = nxt[n];
if(m > n/2)
m = n/2;
m = n-m;

for(int i=0; i<m; i++){
printf("%c", s[i]);
}
for(int i=0; i<m; i++){
printf("%c", p[s[i]-'a']);
}
printf("\n");
}
return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/