HDU4300-Clairewd’s message -字符串 exkmp类板子题

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

 

题意:题目实在是太太太太太难读懂了!!!

给你一个26个字母的密码转换表 从a到z,代表加密原理

重点是第二串,给你一串字符串。首先题目说,完整的信息应该是前半段是加密的,后半段对应的就是未加密的明文。给你的第二串密文一定是完整的,明文可能有缺失也可能没有。你不知道他加密了多少,分析可知至少前半段一定是加密的,当然也可能整个串都是加密的。现在让你求最短的完整信息。

对于第二个样例,qwertabcde恰好前半是加密的,也就是把前半反加密得到abcdeabcde,刚好是一一对应的,则原串就是完整信息。

对应第一个样例,abcdab,abcd是加密的,则完整信息是abcdabcd

具体还得意会,不可言传

下面是代码加注释

// extend[i] 代表 从 i开始的字符子串与原串的公共前缀
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000005;   //字符串长度最大值
int nextt[maxn],ex[maxn]; //ex数组即为extend数组
//预处理计算next数组
char a[1000005];
char c[1000005];
char b[28];
map<char,char>mp;
void GETNEXT(char *str)
{
    int i=0,j,po,len=strlen(str);
    nextt[0]=len;//初始化next[0]
    while(str[i]==str[i+1]&&i+1<len)//计算next[1]
    i++;
    nextt[1]=i;
    po=1;//初始化po的位置
    for(i=2;i<len;i++)
    {
        if(nextt[i-po]+i<nextt[po]+po)//第一种情况,可以直接得到next[i]的值
        nextt[i]=nextt[i-po];
        else//第二种情况,要继续匹配才能得到next[i]的值
        {
            j=nextt[po]+po-i;
            if(j<0)j=0;//如果i>po+nextt[po],则要从头开始匹配
            while(i+j<len&&str[j]==str[j+i])//计算next[i]
            j++;
            nextt[i]=j;
            po=i;//更新po的位置
        }
    }
}
//计算extend数组
void EXKMP(char *s1,char *s2)  //
{
    int i=0,j,po,len=strlen(s1),l2=strlen(s2);
    GETNEXT(s2);//计算子串的next数组
    while(s1[i]==s2[i]&&i<l2&&i<len)//计算ex[0]
    i++;
    ex[0]=i;
    po=0;//初始化po的位置
    for(i=1;i<len;i++)
    {
        if(nextt[i-po]+i<ex[po]+po)//第一种情况,直接可以得到ex[i]的值
        ex[i]=nextt[i-po];
        else//第二种情况,要继续匹配才能得到ex[i]的值
        {
            j=ex[po]+po-i;
            if(j<0)j=0;//如果i>ex[po]+po则要从头开始匹配
            while(i+j<len&&j<l2&&s1[j+i]==s2[j])//计算ex[i]
            j++;
            ex[i]=j;
            po=i;//更新po的位置
        }
    }
}
int main(){
	int n,t,m,i,j,k,ans;
	scanf("%d",&t);
	while(t--){
        int sym=0;
        scanf("%s",b);
        for(i=0;i<26;i++)
            mp[b[i]]=char('a'+i);
        scanf("%s",a);
        int len=strlen(a);
        m=len/2+len%2;
        for(i=0;i<len;i++){
            c[i]=mp[a[i]];
        }
        EXKMP(a,c);
        for(i=m;i<len;i++){
            if(ex[i]+i>=len){
                sym=1;
                break;
            }
        }
        j=len-i;//缺失的明文起点
        printf("%s",a);
        k=0;
        int num=j;
        if(len-2*num>0) //代表缺失的部分 原串里面已经有长度各为num的密文和明文(对应)
        while(1){
            printf("%c",c[j++]);
            k++;
            if(k>=len-2*num)
                break;
        }
        printf("\n");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值