HDOJ 4300 Clairewd’s message

来源:互联网 发布:淘宝仿网红衣服的店铺 编辑:程序博客网 时间:2024/05/16 01:37

题意非常难懂:

大概是给你一个密码表,一个劫获的串。那个劫获的串的前一部分是密文后一部分是明文,但是不清楚分界线在哪里。让你把这个串补完,并使长度尽量小。。。


思路:把劫获的串整个翻译成密码串作为文本串T,原来串P作为模板串做EXKMP,从中间位置开始枚举,如果在位置i上的ex[ i ]等于i到结尾的距离,分界线就在这了。。。

Clairewd’s message

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


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
 

Author
BUPT
 

Source
2012 Multi-University Training Contest 1
 



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=110000;char T[maxn],P[maxn],table[maxn],Table[maxn];int next[maxn],ex[maxn];void pre_exkmp(char P[]){    int m=strlen(P);    next[0]=m;    int j=0,k=1;    while(j+1<m&&P[j]==P[j+1]) j++;    next[1]=j;    for(int i=2;i<m;i++)    {        int p=next[k]+k-1;        int L=next[i-k];        if(i+L<p+1) next[i]=L;        else        {            j=max(0,p-i+1);            while(i+j<m&&P[i+j]==P[j]) j++;            next[i]=j; k=i;        }    }}void exkmp(char P[],char T[]){    int m=strlen(P),n=strlen(T);    pre_exkmp(P);    int j=0,k=0;    while(j<n&&j<m&&P[j]==T[j]) j++;    ex[0]=j;    for(int i=1;i<n;i++)    {        int p=ex[k]+k-1;        int L=next[i-k];        if(i+L<p+1) ex[i]=L;        else        {            j=max(0,p-i+1);            while(i+j<n&&j<m&&T[i+j]==P[j]) j++;            ex[i]=j; k=i;        }    }}int main(){    int T_T;    scanf("%d",&T_T);while(T_T--){    scanf("%s%s",Table,P);    for(int i=0;i<26;i++)    {        table[Table[i]-'a']='a'+i;    }    memset(T,0,sizeof(T));    int n=strlen(P);    for(int i=0;i<n;i++) T[i]=Table[P[i]-'a'];    exkmp(P,T);    int pos=n;    for(int i=n-n/2;i<n;i++)    {        if(ex[i]>=n-i)        {            pos=i; break;        }    }    for(int i=0;i<pos;i++) putchar(P[i]);    for(int i=0;i<pos;i++) putchar(table[P[i]-'a']);    putchar(10);}    return 0;}



1 0