Clairewd’s message

来源:互联网 发布:cell数组怎么比较 编辑:程序博客网 时间:2024/06/05 08:42

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
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4302 4301 4303 4304 4308 


扩展kmp:刘雅琼


题意:第一行先给出转换表(S),再给一个字符串s(包含全部的密文和部分的明文),现在让你求出字符串s密文的明文,并先输出密文,再输出密文对应的明文;

将s全部当作是密文,转化成明文t,则s中的密文部分就转化成了明文,那么s中得的明文部分(s的后缀)就是t的前缀;扩展kmp

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<map>using namespace std;const int MAXN=100000+10;const int MAXM=100+10;int nextra[MAXN],extend[MAXN];char s[MAXN],t[MAXN],s1[MAXN];int flag[300];int n,m,maxn;char k[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};void get_next(){    nextra[0]=m;    int st,p;    for(int i=1,j=-1; i<m; i++,j--)    {        if(j==-1||i+nextra[i-st]>=p)        {            if(j==-1)            {                p=i;                j=0;            }            while(p<m&&t[p]==t[j])                p++,j++;            nextra[i]=j;            st=i;        }        else            nextra[i]=nextra[i-st];    }}void get_extend()//用s的后缀匹配t的前缀{    memset(nextra,0,sizeof(nextra));    memset(extend,0,sizeof(extend));    get_next();    int st,p;    for(int i=0,j=-1; i<n; i++,j--)    {        if(j==-1||i+nextra[i-st]>=p)        {            if(j==-1)            {                p=i;                j=0;            }            while(p<n&&j<m&&s[p]==t[j])                p++,j++;            extend[i]=j;            st=i;        }        else            extend[i]=nextra[i-st];        maxn=max(maxn,extend[i]);    }}int main(){     int T;     scanf("%d",&T);     while(T--)     {         maxn=0;         memset(flag,0,sizeof(flag));         scanf("%s%s",s1,s);         for(int i=0;i<26;i++)            flag[s1[i]]=i;         n=strlen(s);         m=n;         for(int i=0;i<n;i++)            t[i]=k[flag[s[i]]];        get_extend();        int kk=n;        for(int i=(n+1)/2;i<n;i++)        {            if((extend[i]+i)==n)            {                kk=i;                break;            }        }        for(int i=0;i<kk;i++)            printf("%c",s[i]);        for(int i=0;i<kk;i++)            printf("%c",t[i]);        printf("\n");     }     return 0;}


因为密文是完整的,所以一定大于等于原串(s)的一半,将原串的前半部分转化成明文,构成新的串(t),那么新串的后缀(原s串中的明文)一定是新串(t)的前缀;那么就是Kmp求next数组的问题了;

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<map>using namespace std;const int MAXN=200000+10;const int MAXM=1000000+10;int n,m;char t[MAXN],s1[30],s2[MAXN];char s[30]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};int nextra[MAXN];int flag[MAXN];void get_next(){    int i,k;    i=0,k=-1,nextra[0]=-1;    while(i<m)    {        if(k==-1||t[i]==t[k])        {            i++;            k++;            nextra[i]=k;        }        else            k=nextra[k];    }}int main(){    int T;    cin>>T;    while(T--)    {        scanf("%s",s1);        scanf("%s",s2);        m=strlen(s2);        for(int i=0;i<26;i++)            flag[s1[i]]=i;        int j=m/2;        if(m%2) j+=1;        for(int i=0;i<j;i++)        {            t[i]=s[flag[s2[i]]];        }        t[j]=' ';        for(int i=j;i<m;i++)            t[i+1]=s2[i];        m+=1;        get_next();        int k=m-1-nextra[m];        for(int i=0;i<k;i++)        {            printf("%c",s2[i]);        }        for(int i=0;i<k;i++)        {            printf("%c",s[flag[s2[i]]]);        }        printf("\n");    }    return 0;}