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

hdu 4300 Clairewd’s message(具体解释,扩展KMP)

2017-05-09 21:50 495 查看
题目链接: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


Author

BUPT

Source

field=problem&key=2012%20Multi-University%20Training%20Contest%201&source=1&searchmode=source" style="color:rgb(26,92,200); text-decoration:none">2012 Multi-University Training Contest
1

题意:

比較难理解,给定两组字符串,第一组仅仅有26个字符表相应明文中a,b,c,d....z能够转换第1个,第2个...第26个字符变成密文,

第二组字符串是给定的密文+明文,明文可能不完整(缺失或没有),叫你补充完整整个密文+明文是最短的;

PS:读了好几遍都不理解题意。用了翻译工具还是不能理解。最后还是百度了才知道题意!(毕竟英语太渣)。



思路:

把给出的不完整的明文加密文字符串所有转换一次。将转换后的做模式串,与原串进行kmp,记录返回的j值(j值代表的就是前缀和后缀的最长相等长度)。然后再与给出的明文加密文字符串比較一下。

代码例如以下:

//#pragma warning (disable:4786)
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps=1e-9;
const double pi=3.1415926535897932384626;
#define INF 1e18
//typedef long long LL;
//typedef __int64 LL;
const int MAXN = 100017;

char strm[MAXN];//password表
char strz[MAXN];//不完整的密文和密文
char str[MAXN]; //password表转换后
char s[MAXN];   //密文和明文转换后
int next[MAXN];

void getnext( char T[], int len)
{
int i = 0, j = -1;
next[0] = -1;
while(i < len)
{
if(j == -1 || T[i] == T[j])
{
i++, j++;
next[i] = j;
}
else
j = next[j];
}
}
int KMP(int len1, int len2)
{
int i, j = 0;
if(len1%2 == 1)
{
i = len1/2+1;
}
else
i = len1/2;
while(i < len1 && j < len2)
{
if(j == -1 || strz[i] == s[j])
{
i++, j++;
}
else
j = next[j];
}
return j;//j值代表的就是前缀和后缀的最长相等长度
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",strm);
for(int i = 0; i < 26; i++)
{
char ss = strm[i];
str[ss] = i;
}
scanf("%s",strz);
int lenz = strlen(strz);
for(int i = 0; i < lenz; i++)//将密文和明文依照password表转换
{
int tt = str[strz[i]];
s[i] = 'a' + tt;
}
int lens = strlen(s);
getnext(s, lens);
int j = KMP(lenz, lens);//得到密文的个数
if(j*2 == lenz)//前缀和后缀恰各占一半
{
printf("%s\n",strz);
}
else
{
int tt = lenz - j;
printf("%s",strz);
for(int i = j; i < tt; i++)//须要加入的明文
{
printf("%c",s[i]);
}
printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: