hdu4300——扩展kmp(hash转换)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300

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

题目翻译:

其实就是给你一个转换表,再给你一个密文+明文(不完整)的字符串,然后让你求最短的完整的密文+明文的字符串。

 

 

可惜我不是很懂,抛出大佬的解释:

1.用kmp来做:

首先肯定的是,给定的串中明文长度一定小于等于密文。也就是说明文长度小于等于总长的一半。

于是,取总长的后一半作为主串,然后把串反翻译一遍得到str2,然后用str2与str1的后一半进行匹配。首次把str1的后一半匹配完的位置即是给定的串中明文开始的位置。

因为是首次,所以保证了前面的密文长度最小,即总长度最小。

然后输出密文+明文,即可。

2.用扩展kmp来做:

//next[i]:x[i...m-1]与x[0...m-1]的最长公公前缀

//extend[i]:y[i...n-1]与x[0...m-1]的最长公共前缀

可以用extend数组,根据它的意义,str1作为y,str2作为x,当 i+extend[i]==len1时代表y中的从i到结尾均与x的开头匹配,如果此时i大于串长度的一半,则满足条件。

此时的i即为实际密文(明文)的长度,然后按要求输出答案即可。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=200005;
int nxt[maxn];
int extend[maxn];
void ex_kmp(char s[],char t[])
{
    int i,j,p,l;
    int len=strlen(t);
    int len1=strlen(s);
    memset(nxt,0,sizeof(nxt));
    memset(extend,0,sizeof(extend));
    nxt[0]=len;
    j=0;
    while(j+1<len&&t[j]==t[1+j])j++;
    nxt[1]=j;
    int a=1;
    for(int i=2;i<len;i++)
    {
        p=nxt[a]+a-1;
        l=nxt[i-a];
        if(i+l<p+1)nxt[i]=l;
        else
        {
            j=max(0,p-i+1);
            while(i+j<len&&t[i+j]==t[j])j++;
            nxt[i]=j;
            a=i;
        }
    }
    j=0;
    while(j<len1&&j<len&&s[j]==t[j])j++;
    extend[0]=j;
    a=0;
    for(i=1;i<len1;i++)
    {
        p=extend[a]+a-1;
        l=nxt[i-a];
        if(l+i<p+1)nxt[i]=l;
        else
        {
            j=max(0,p-i+1);
            while(i+j<len1&&j<len&&s[i+j]==t[j])j++;
            extend[i]=j;
            a=i;
        }
    }
}
char s[maxn],p[maxn],tmp[maxn];
map<char,char> mp; 
int main(int argc, char** argv) {
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%s%s",&p,&s);
		int len1=strlen(p);
		int len2=strlen(s);
		for(int i=0;i<len1;++i) mp[p[i]]='a'+i;
		for(int i=0;i<len2;++i) tmp[i]=mp[s[i]];
		tmp[len2]=0;
		ex_kmp(s,tmp);
		int k;
		for(k = 0;k<len2;++k){
			if(k+extend[k]>=len2&&extend[k]<=k){
				break;
			}
		} 
		for(int i=0;i<k;++i) printf("%c",s[i]);
		for(int i = 0;i<k;++i) printf("%c",mp[s[i]]);
		printf("\n");
	}
	return 0;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值