hdu4300

来源:互联网 发布:c语言进程的创建 编辑:程序博客网 时间:2024/05/01 07:37

Clairewd’s message

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


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
题意:先输入一个整数,代表有两组测试数据,每组测试数据的第一个字符串为一个翻译表s,第二个字符串t为密文串t1加一个明文串t2,但是明文串不一定是完整的(可能没有),举一个例子:以qwertyuiopasdfghjklzxcvbnm为例,假设qwert是密文,经过翻译表的比对之后可以翻译为明文abcde,(如q在原英文字母表a的位置,则翻译为明文a,其他同理);我们的目标是求这个完整的密文后面的明文的串
思路:len=strlen(t);我们可以假设t+(len+1)/2字符串为明文串的一部分,将其复制给字符串数组t2,输出t串,再将字符串t经过与翻译表比对,将t全部翻译为明文。然后将t2作为主串,翻译过的t作为字串进行kmp匹配,返回匹配度,然后输出明文需要补充完整的那部分,
代码如下:
#include<stdio.h>#include<string.h>int next[100001];char s[27],t[100001],t2[100001];void getnext(int len,char *t){    int i,j;    next[0]=-1;    i=0,j=-1;    while(i<len)    {        if(j==-1||t[i]==t[j])        {            i++;            j++;            next[i]=j;        }        else            j=next[j];    }}int kmp(char *s,char *t){    int i,j,lens,lent;    lens=strlen(s);    lent=strlen(t);    getnext(lent,t);    i=j=0;    while(i<lens&&j<lent)    {         if(j==-1 || s[i]==t[j])         {              i++;              j++;         }         else              j=next[j];    }    if(i==lens)    return j;    return 0;}int main(){    int T,i,len,j,dex;    scanf("%d",&T);    while(T--)    {        scanf("%s",s);        scanf("%s",t);        len=strlen(t);        for(i=(len+1)/2,j=0;i<len;i++,j++)        t2[j]=t[i];        t2[j]='\0';        printf("%s",t);        for(i=0;i<len;i++)            for(j=0;j<26;j++)                if(t[i]==s[j])                {                    t[i]='a'+j;                    break;                }        dex=kmp(t2,t);        for(i=dex;i<len-dex;i++)            printf("%c",t[i]);        printf("\n");    }    return 0;}
原创粉丝点击