hdu 4300 Clairewd’s message (kmp)

来源:互联网 发布:播放器 mac 编辑:程序博客网 时间:2024/05/20 06:26

http://acm.hdu.edu.cn/showproblem.php?pid=4300

Clairewd’s message

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
 

题意就是给你26的字母的加密方式,然后又给了一个串s1是包含加密后的和没有加密的但是没有加密的可能不齐全;求完整的密文和原文;

例如第二个例子:

      abcdefghijklmnopqrstuvwxyz

加密方式:  qwertyuiopasdfghjklzxcvbnm

  s1:   qwertabcde

在s1中qwert就是密文,后面的abcde就是原文;

假如加密方式不变,s1变成qwertabc,那么答案还是qwertabcde;

密文的长度肯定大于s1总长度的一半,我们可以把s1前一半当成密文后一半不变,然后解密得到s2,那么s2的Next【len】就是给出的明文的长度,总长度-明文

的长度得到的就是密文的总长度,然后输出密文和密文对应的明文就可以了


#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 101000#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-8#define met(a, b) memset (a, b, sizeof (a))char s1[N], s2[N], s3[N];int Next[N], n;void get_next (char s[], int n);int main (){    int t;    scanf ("%d", &t);    while (t--)    {        scanf ("%s %s", s1, s2);        int len2 = strlen (s2);        int len1 = strlen (s1);        int k = 0;        for (int i=0; i<len2/2; i++)        {            for (int j=0; j<len1; j++)                if (s2[i] == s1[j])                    s3[k++] = 'a' + j;        }        s3[k++] = '*';        for (int i=len2/2; i<len2; i++)            s3[k++] = s2[i];        get_next (s3, k);        int ans = Next[k];        printf ("%s", s2);        for (int i=ans; i<len2-ans; i++)        for (int j=0; j<len1; j++)        if (s2[i] == s1[j])            printf ("%c", 'a' + j);        puts ("");    }    return 0;}void get_next (char s[], int n){    int i = 0, j = -1;    Next[i] = -1;    while (i < n)    {        if (j == -1 || s[i] == s[j])            Next[++i] = ++j;        else j = Next[j];    }}


0 0