kmp hdu 4300

Clairewd’s message

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

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

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
 
这道题的意思是,先输入一个转义表,再输入一个字符串,前面是加密了的,后面是是加密的内容,加密的内容可能有缺陷,就是求完整的字符串,
就是
#include<stdio.h>//网上大佬的代码 
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;

const int maxn = 1e5+5;
int Next[maxn];
char str[maxn];
char str2[maxn];
char mo[maxn];
int dp[maxn];
int mm[30];
int nn[maxn];
int n1,n2;

void GetNext()
{
    int i=0,j=-1;
    while(i<n2)
    {
        if(j==-1||mo[i]==mo[j]) {++i,++j,Next[i]=j;}
        else j=Next[j];
    }
    return ;
}

int ans[maxn];

int main()
{
    int cnt;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",str,mo);
        //strcpy(str2,mo);
        n2=strlen(mo);
        n1=strlen(str);
        
        for(int i=0;i<n1;i++)//对转换表进行处理; 
        {
            mm[i]=str[i]-'a';//将字符串转化为数字数组; 
            
            nn[str[i]-'a']=i;//记录每个字符的位置; 
            
        }
        
        for( int i=0; i<(n2+1)/2; i++ )//遍历前半段,单数遍历到前半段加一; 
        {
            mo[i]=nn[mo[i]-'a']+'a';//将前半段的字符串进行转义;十分巧妙; 
        }
        
        Next[0]=-1;
        
        GetNext();
        
        int ans=Next[n2];// 
        
        while( ans>min( (n2+1)/2, n2-(n2+1)/2) )//找最小相同前后缀; 
        {
            ans=Next[ans];//确保ans的值在前半段且在后半段内; 
        }
        
        for(int i=0;i<n2-ans;i++)//ans为文本串的长度;n2-ans为密码的长度;
            printf("%c",str2[i]);
        for(int i=0;i<n2-ans;i++)//输出密码对应的文本串;
            printf("%c",nn[str2[i]-'a']+'a');
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/qqshiacm/p/11593525.html