4300.Clairewd’s message

来源:互联网 发布:行业数据分析 编辑:程序博客网 时间:2024/05/20 13:04

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

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.  //由题意知,我们应求尽可能长的plaintext
 

Sample Input
2abcdefghijklmnopqrstuvwxyzabcdabqwertyuiopasdfghjklzxcvbnmqwertabcde
 

Sample Output
abcdabcdqwertabcde
tips:用KMP可以做,但是貌似EKKMP才是正解,不过。。。毕竟才接触KMP,见谅
这道题,没啥好说的,先直接输出the intercepted text,且保存其后一半为s2,然后s1用密钥翻译
利用s2去匹配s2,这里的KMP需要改一下下

#include <iostream>

#include <string.h>

using namespace std;

void Get(char *a,int *next);

int KMP(char *a, char *b,int *next);

int mynext[100005];

char s1[100005],s2[100005],table[26];

int main()

{

    int T;

    scanf("%d",&T);

    while (T--)

    {

        scanf("%s%s",table,s1);

        printf("%s",s1);  //先直接输出intercepted text

        int len = (int) strlen(s1);

        strcpy(s2,s1 + (len+1)/2); //这里注意是len+1,因为奇数下len/2会使得s2超出原字符一半,会出错。。。因为我就卡在这里。。。无语

for (int n = 0;n < len;n++)

        {

            for (int m = 0;m < 26;m++)

                if (s1[n] == table[m]) {s1[n]='a'+m; break;}  //解码

        }

        Get(s1,mynext);

        int tmp = KMP(s2,s1,mynext);

        for (int n = tmp;n < len - tmp;n++) printf("%c",s1[n]);

        printf("\n");

    }

    return 0;

}

void Get(char *a,int *next)

{

    int len = (int) strlen(a);

    int j = 0,k = -1;

    mynext[0] = -1;

    while (j < len - 1)

    {

        if (k == -1 || a[j] == a[k])

        {

            j++;

            k++;

            if (a[j] != a[k]) mynext[j] = k;

            else mynext[j] = mynext[k];

        }

        else k = mynext[k];

    }

    return ;

}

int KMP(char *a, char *b,int *next)

{

    int len_a,len_b,A,B;

    len_a = (int) strlen(a);

    len_b = (int) strlen(b);

    A=0;

    B=0;

    while (A < len_a && B < len_b)

    {

        if (B == -1 || a[A] == b[B])

        {

            A++;

            B++;

            if (A == len_a) return B;

        }

        else

            B = mynext[B];

    }

    return 0;

}

0 0
原创粉丝点击