首页 > 代码库 > HDU4300-Clairewd’s message(KMP前缀匹配后缀)

HDU4300-Clairewd’s message(KMP前缀匹配后缀)

Clairewd’s message

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


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
2 abcdefghijklmnopqrstuvwxyz abcdab qwertyuiopasdfghjklzxcvbnm qwertabcde
 

Sample Output
abcdabcd qwertabcde
 

题意:给你一个加密协议,即26个英文字母加密对应表,然后给你一个前一段是加密串,后一段为加密串对应的原串的字符串(原串可能小于加密串)输出最短的原串和加密串

思路:用二分之一前缀去匹配二分之一后缀,因为加密串大于等于原串,利用KMP得到匹配的位置,即可输出答案。这道题弹了很久的TLE,发现不能一个字符一个字符地输出。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100000+10;
const int esize = 28;
int next[maxn],mid;
char mp[esize],str[maxn];
string str1,str2;

void getNext(){
    next[0] = next[1] = 0;
    int n = str1.size();
    for(int i = 1,j; i < n; i++){
        j = next[i];
        while(j && str1[i] != str1[j]) j = next[j];
        if(str1[i] == str1[j]) next[i+1] = j+1;
        else next[i+1] = 0;
    }
}

void KMP(int sta){
    int n = strlen(str) , j = 0;
    for(int i = sta; i < n; i++){
        while(j && str1[j] != str[i]) j = next[j];
        if(str1[j] == str[i]) j++;
        if(j==str1.size()) break;
    }
    int  k;
    if(n%2==0) k = sta-j;
    else k = sta-j-1;
    for(int i = 0; i < k; i++){
        str1 += mp[str[sta+i]-'a'];
        str2 += str[sta+i];
    }
    cout<<str2<<str1<<endl;

}
int main(){
    int ncase;
    cin >> ncase;
    char tmp[esize];
    getchar();
    while(ncase--){
        scanf("%s%s",tmp,str);
        int n = strlen(str);
        if(n==0){
            puts("");
            continue;
        }
        str1.clear();
        str2.clear();
        for(int i = 0; i < 26; i++)
            mp[tmp[i]-'a'] = char('a'+i);
        if(n%2==0) mid = n/2-1;
        else mid = n/2;
        for(int i = 0; i <= mid; i++){
            str1 += mp[str[i]-'a'];
            str2 += str[i];
        }
        getNext();
        KMP(mid+1);
    }
    return 0;
}