hdu 4300 Clairewd’s message

Clairewd’s message

http://acm.hdu.edu.cn/showproblem.php?pid=4300

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
 
Author
BUPT
 
Source
 
Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4302 4301 4303 4304 4308 
 
题意:
给出一个conversion table 
给出一个字符串a,由b和c拼接而成,b为密文,c为原文
密文是原文按conversion table 处理的结果
密文是完整的,原文后面的部分可能缺失
将原文补充完整,多种答案时取长度最小的一种
 
kmp算法
因为密文>=原文
截取给出字符串的后一半,记为s
给出的字符串按conversion table 处理,记为t,这样原来的密文就成了原文
用t去匹配s,当s匹配完时,t匹配到的位置,就是原文的长度
再加上剩下的原文即可
#include<cstdio>
#include<cstring>
using namespace std;
char table[26],s[100001],t[100005];
int lent,lens,f[100001],dy[26];
void getnxt()
{
    int j;
    for(int i=1;i<lent;i++)
    {
        j=f[i];
        while(j && t[i]!=t[j]) j=f[j];
        f[i+1]= t[i]==t[j] ? j+1 : 0;
    }
}  
int find()
{
    int j=0; lens=strlen(s);
    for(int i=0;i<lens;i++)
    {
        while(j && s[i]!=t[j]) j=f[j];
        if(s[i]==t[j]) j++;
    }
    return j;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s%s",table,t);
        for(int i=0;i<26;i++) dy[table[i]-'a']=i;
        lent=strlen(t);
        strcpy(s,t+(lent+1)/2);
        printf("%s",t);
        for(int i=0;i<lent;i++) t[i]='a'+dy[t[i]-'a'];
        getnxt();
        int g=find();
        for(int i=g;i<lent-g;i++) printf("%c",t[i]);
        puts("");
    }
}

 

posted @ 2017-06-18 18:23  TRTTG  阅读(416)  评论(0编辑  收藏  举报