HDU 4300 (KMP)

来源:互联网 发布:北京宝库在线网络 编辑:程序博客网 时间:2024/06/06 08:52

Clairewd’s message

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5014    Accepted Submission(s): 1900


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
2abcdefghijklmnopqrstuvwxyzabcdabqwertyuiopasdfghjklzxcvbnmqwertabcde
 

Sample Output
abcdabcdqwertabcde
 


题意:给出一个字符的映射表来加密字符,然后有一个串由密文+对应的明文构成,

但是后面的明文有缺失,然后求出长度最小的符合要求的串.

先把原串T都解密一遍,假设新串是S,相当于要求S的前缀和T后缀匹配的长度要尽量

大.这就是next数组的含义了,但是显然这个匹配的前缀长度需要小于等于n/2的,所

以如果这个条件不满足就沿着next一直匹配.这里可以通过求S+T而避免扩栈KMP.

边界的地方容易搞错.


#include <cstring>#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <cmath>using namespace std;#define maxn 211111char P[maxn], T[maxn];int n, m;#define next Nextint next[maxn];char change[33], ss[33];void get_next (char *p) {int t;t = next[0] = -1;int j = 0, m = strlen (p);while (j < m) {if (t < 0 || p[j] == p[t]) {//匹配j++, t++;next[j] = t;}else //失配t = next[t];}}void Change (char *s) {    for (int i = 0; i < 26; i++)        change[ss[i]-'a'] = 'a'+i;    int n = strlen (s);    for (int i = 0; i < n; i++) {        s[i] = change[s[i]-'a'];    }    return  ;}int main () {    //freopen ("in.txt", "r", stdin);    int t;    scanf ("%d", &t);while (t--) {        scanf ("%s%s", ss, T);        memset (P, 0, sizeof P);        strcpy (P, T);        n = strlen (T);        m = n*2;        Change (P);        strcat (P, T);        get_next (P);        int j = m, mid = n/2;        while (next[j] > 0 && next[j] > mid) j = next[j];        int len = n-next[j];        for (int i = 0; i < len; i++) printf ("%c", T[i]);        for (int i = len; i < len*2; i++) printf ("%c", change[T[i-len]-'a']);        cout << "\n";}return 0;}/*7qwertyuiopasdfghjklzxcvbnmqwertqwertyuiopasdfghjklzxcvbnmqwertaqwertyuiopasdfghjklzxcvbnmqabcdefghijklmnopqrstuvwxyzaaaaabcdefghijklmnopqrstuvwxyzaaaabcdefghijklmnopqrstuvwxyzaaabcdefghijklmnopqrstuvwxyza*/




0 0
原创粉丝点击