HDU 4300 Clairewd’s message

HDU 4300 Clairewd’s message

题目:

Clairewd’s message

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


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
 

解析:

  • 题目意思是:给你一张含有26个字母的密码表,对应相应位置的字母a~z。再给你一组不完整的密文+明文,要求输出完整的密文+明文。
  • 很明显的扩展KMP习题,扩展KMP教程点此
  • 将密文+明文翻译过来,以原来的密文+明文为主串,翻译后的字符串为模式串,进行扩展KMP匹配。
  • 最后按要求输出。

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char s1[100001],s0[100001],str[100001];
int next[100001],extend[100001];
void exkmp(char *s,char *t)
{
    int a,l,p,j,i;
    int slen=strlen(s);
    int tlen=strlen(t);
    next[0]=tlen;
    a=0;
    while(a<tlen-1 && t[a]==t[a+1]) a++;
    next[1]=a;
    a=1;
    for(i=2;i<tlen;i++)
    {
        p=a+next[a]-1;
        l=next[i-a];
        if(i+l-1>=p)
        {
            j=p-i+1>0?p-i+1:0;
            while(i+j<tlen && t[j]==t[i+j]) j++;
            next[i]=j;
            a=i;
        }
        else next[i]=l;
    }
    a=0;
    while(a<tlen && a<slen && s[a]==t[a])a++;
    extend[0]=a;
    a=0;
    for(i=1;i<slen;i++)
    {
        p=a+extend[a]-1;
        l=next[i-a];
        if(l+i-1>=p)
        {
            j=p-i+1>0?p-i+1:0;
            while(j<tlen && i+j<slen && t[j]==s[i+j])j++;
            extend[i]=j;
            a=i;
        }
        else extend[i]=l;
    }
}
int main()
{
    int n,i,j;
    char v,smap[26];
    scanf("%d\n",&n);
    while(n--)
    {
        for(i=0;i<26;i++)
        {
            scanf("%c",&v);
            smap[v-'a']=i+'a';
        }
        getchar();
        gets(s1);
        int slen=strlen(s1);
        for(i=0;i<slen;i++)
            s0[i]=smap[s1[i]-'a'];
        s0[i]='\0';
        exkmp(s1,s0);
        for(i=1;i<slen;i++)
             if(i+extend[i]>=slen && i>=extend[i])
                break;
        for(j=0;j<i;j++)
        {
            str[j]=s1[j];
            str[i+j]=s0[j];
        }
        str[i+j]='\0';
        puts(str);
    }
    return 0;
}


你可能感兴趣的:(HDU 4300 Clairewd’s message)