您的位置:首页 > 大数据 > 人工智能

HDU4300 Clairewd’s message 扩展kmp

2016-05-13 00:01 411 查看
题目链接:HDU4300


Clairewd’s message

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5011 Accepted Submission(s): 1898



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


题意:先是一个表,代表从a到z对应的密文字母,之后一个字符串,有完整的密文和不知道完不完整的明文。我们的任务就是把明文补全然后一起输出。

题目分析:这次又做到这一题。以前是用的KMP将之翻译后匹配相同的字段。这次换一种方法用扩展KMP。首先还是要翻译回来,得到明文和将明文翻译后得到的一堆没意义的字符。这里的明文一定能和原串的不全的明文匹配,所以这一次需要用到前缀匹配,就是扩展KMP。具体参照杨雅琼的扩展KMPppt。这次的相当于一个模板放在这,以后又更好的写法再回来改。

PS:扩展的英文是extend,我全写成了extand,,,,这么多年学的英语都还给老师了

//
//  main.cpp
//  HDU4300(1
//
//  Created by teddywang on 16/5/12.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int T,n,len;
char s[100010],t[100010],a[28];
int nexts[100010],extand[100010];

void getnexts(char *t)
{
int a=0;
nexts[0]=len;
while(a<len-1&&t[a]==t[a+1]) a++;
nexts[1]=a;
a=1;
for(int k=2;k<len;k++)
{
int p=a+nexts[a]-1,L=nexts[k-a];
if(k-1+L>=p)
{
int j=max((p-k+1),0);
while(k+j<len&&t[k+j]==t[j]) ++j;
nexts[k]=j;
a=k;
}
else nexts[k]=L;
}
}

void ekmp(char *s,char *t)
{
int lens=len,lent=strlen(t),a=0;
int minlen=min(lens,lent);
while(a<minlen&&s[a]==t[a]) ++a;
extand[0]=a;
a=0;
for(int k=1;k<lens;k++)
{
int p=a+extand[a]-1,L=nexts[k-a];
if(k-1+L>=p)
{
int j=max(p-k+1,0);
while(k+j<lens&&j<lent&&s[k+j]==t[j]) ++j;
extand[k]=j;
a=k;
}
else extand[k]=L;
}
}

int main()
{
cin>>T;
while(T--)
{
char hash[150];
scanf("%s",a);
for(int i=0;i<26;i++)
hash[ a[i] ]='a'+i;
scanf("%s",s);
len=strlen(s);
for(int i=0;i<len;i++)
t[i]=hash[s[i]];
getnexts(t);
ekmp(s,t);
int pos=len;
for(int i=0;i<len;i++)
{
if(i+extand[i]>=len&&i>=extand[i])
{
pos=i;
break;
}
}
/*for(int i=0;i<len;i++)
cout<<nexts[i];
cout<<endl;
for(int i=0;i<len;i++)
cout<<extand[i];
cout<<endl;
*/
for(int i=0;i<pos;i++)
printf("%c",s[i]);
for(int i=0;i<pos;i++)
printf("%c",t[i]);
printf("\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: