HDU 4300 Clairewd’s message(KMP)

                                                                                       Clairewd’s message

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,求出最长后缀长度,但由于密文的长度必须大于等于明文的长度,所以找到最接近len/2的最长后缀。可以用map保存对应的翻译表的字符。最后输出即可。
代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=1000000007;
const double eps=0.00000001;
int pi[200005];
void GetPi(const string& sub)   //KMP得到len长度字符串的最大后缀长度pi[len-1](从0到len-1,所以len对应pi[len-1])
{
    memset(pi,0,sizeof(pi));
    int len=sub.length();
    int be=1,match=0;
    while(be+match<len)
    {
        if(sub[be+match]==sub[match])
        {
            match++;
            pi[be+match-1]=match;
        }
        else
        {
            if(match==0)  be++;
            else
            { be+=match-pi[match-1];
                match=pi[match-1];
            }
        }
    }
}
map<char,char> change,rev;  //翻译表和逆向翻译表
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        string table,S;
        change.clear(),rev.clear();
        cin>>table>>S;
        for(int i=0;i<26;i++) change[i+'a']=table[i],rev[table[i]]=i+'a';  //保存下来
        string another=S;
        int len=S.length();
        for(int i=0;i<len;i++) another[i]=change[S[i]];
        GetPi(S+another);  //KMP一下
        int ans=pi[2*len-1];
        while(ans>0&&ans*2>len)  ans=pi[ans-1];  //找到最接近len/2的最大后缀长度
        string res=S.substr(0,len-ans);   //只截取密文部分
        string t=res;
        for(int i=0;i<res.length();i++)  t[i]=rev[res[i]];  //逆向翻译
        cout<<res+t<<endl;   

    }
    return 0;
}
View Code

 

posted @ 2015-08-24 21:14  wust_ouyangli  阅读(248)  评论(0编辑  收藏  举报