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

 

Clairewd’s message

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


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
 
 
给你26的字母的加密方式,然后又给了一个串s是包含加密后的和没有加密的但是没有加密的可能不齐全;求完整的密文和原文;
给两个串第一个串是翻译表(密文可以通过翻译表翻译成明文),第二个串是由密文+明文组成,前面是密文(完整的),后面是明文(未必完整),问能不能把第二个串补全,输出最短的一种可能。
 
qwertabcde-> qwert是密文,abcde是明文,密文可以通过上面的qwertyuiopasdfghjklzxcvbnm(26个字母,每个字母变成它所在的下标的字母q->a) 翻译成后面的明文,这么看应该明白怎么回事了吧。然后知到要求出来短的补全,要使补全最短的话,那么就要求出来后缀前缀的最大相等值是多少,然后把未匹配的补全就行了比如第一组数据 abcdab 为了防止匹配超过一半(因为密文是不缺少的,所以密文最少也得占一半),在中间的位置加一个‘*’分割,变成了 abc*dab 很明显最后的最大匹配度就是 2 ,也就是后面的两个是明文,前面的都是密文

 

 

代码1:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

#define N 200005

int Next[N];
char s1[N], s2[N];
char a[N], b[N];

void FindNext(char S[])
{
    int Slen = strlen(S);
    int i=0, j=-1;
    Next[0] = -1;

    while(i<Slen)
    {
        if(j==-1 || S[i]==S[j])
            Next[++i] = ++j;
        else
            j = Next[j];
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s%s", s1, s2);

        int  len=strlen(s2), i, L;
        L = (len+1)/2;

        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));

        for(i=0; i<26; i++)
            a[s1[i]-'a'] = 'a'+i;
        for(i=0; i<L; i++)
            b[i] = a[s2[i]-'a'];
        b[i] = '*';
        b[i+1] = '\0';
        strcat(b, s2+L);

        FindNext(b);

        L = len - Next[len+1];

        for(i=0; i<L; i++)
          printf("%c", s2[i]);
        for(i=0; i<L; i++)
          printf("%c", a[s2[i]-'a']);
        printf("\n");
    }
    return 0;
}
View Code

 代码2:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

const int maxn = 10000007;

int Next[maxn];
char s[maxn], a[maxn], pass[maxn];

void FindNext(char S[])
{
    int i=0, j=-1;
    Next[0] = -1;

    int Slen = strlen(S);

    while(i<Slen)
    {
        if(j==-1 || S[i]==S[j])
            Next[++i] = ++j;
        else j = Next[j];
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int i, len, L;
        scanf("%s%s", s, a);
        len = strlen(a);
        L = (len+1)/2;

        for(i=0; s[i]; i++)
            pass[s[i]-'a'] = i + 'a';

        for(i=0; i<L; i++)
            s[i] = pass[a[i]-'a'];
        s[i] = '*';
        s[i+1] = 0;

        strcat(s, a+i);

        FindNext(s);

        L = len - Next[len+1];

        for(i=0; i<L; i++)
        {
            s[i] = a[i];
            s[i+L] = pass[ a[i]-'a' ];
        }
        s[i+L] = 0;

        printf("%s\n", s);
    }

    return 0;
}
View Code

 

posted on 2015-09-28 16:33  栀蓝  阅读(140)  评论(0编辑  收藏  举报

levels of contents