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

Clairewd’s message

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

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的字母的加密方式,然后又给了一个串s是包含加密后的和没有加密的但是没有加密的可能不齐全;求完整的密文和原文;
给两个串第一个串是翻译表(密文可以通过翻译表翻译成明文),第二个串是由密文+明文组成,前面是密文(完整的),后面是明文(未必完整),问能不能把第二个串补全,输出最短的一种可能。
 
qwertabcde-> qwert是密文,abcde是明文,密文可以通过上面的qwertyuiopasdfghjklzxcvbnm(26个字母,每个字母变成它所在的下标的字母q->a) 翻译成后面的明文,这么看应该明白怎么回事了吧。然后知到要求出来短的补全,要使补全最短的话,那么就要求出来后缀前缀的最大相等值是多少,然后把未匹配的补全就行了比如第一组数据 abcdab 为了防止匹配超过一半(因为密文是不缺少的,所以密文最少也得占一半),在中间的位置加一个‘*’分割,变成了 abc*dab 很明显最后的最大匹配度就是 2 ,也就是后面的两个是明文,前面的都是密文

代码1:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std; #define N 200005 int Next[N];
char s1[N], s2[N];
char a[N], b[N]; void FindNext(char S[])
{
int Slen = strlen(S);
int i=, j=-;
Next[] = -; while(i<Slen)
{
if(j==- || S[i]==S[j])
Next[++i] = ++j;
else
j = Next[j];
}
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%s%s", s1, s2); int len=strlen(s2), i, L;
L = (len+)/; memset(a, , sizeof(a));
memset(b, , sizeof(b)); for(i=; i<; i++)
a[s1[i]-'a'] = 'a'+i;
for(i=; i<L; i++)
b[i] = a[s2[i]-'a'];
b[i] = '*';
b[i+] = '\0';
strcat(b, s2+L); FindNext(b); L = len - Next[len+]; for(i=; i<L; i++)
printf("%c", s2[i]);
for(i=; i<L; i++)
printf("%c", a[s2[i]-'a']);
printf("\n");
}
return ;
}

代码2:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm> using namespace std; const int maxn = ; int Next[maxn];
char s[maxn], a[maxn], pass[maxn]; void FindNext(char S[])
{
int i=, j=-;
Next[] = -; int Slen = strlen(S); while(i<Slen)
{
if(j==- || S[i]==S[j])
Next[++i] = ++j;
else j = Next[j];
}
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int i, len, L;
scanf("%s%s", s, a);
len = strlen(a);
L = (len+)/; for(i=; s[i]; i++)
pass[s[i]-'a'] = i + 'a'; for(i=; i<L; i++)
s[i] = pass[a[i]-'a'];
s[i] = '*';
s[i+] = ; strcat(s, a+i); FindNext(s); L = len - Next[len+]; for(i=; i<L; i++)
{
s[i] = a[i];
s[i+L] = pass[ a[i]-'a' ];
}
s[i+L] = ; printf("%s\n", s);
} return ;
}

(KMP 扩展)Clairewd’s message -- hdu -- 4300的更多相关文章

  1. J - Clairewd’s message HDU - 4300(扩展kmp)

    题目链接:https://cn.vjudge.net/contest/276379#problem/J 感觉讲的很好的一篇博客:https://subetter.com/articles/extend ...

  2. Clairewd’s message - HDU 4300(next[]运用)

    题目大意:给两个串第一个串是翻译表(密文可以通过翻译表翻译成明文),第二个串是由密文+明文组成,前面是密文(完整的),后面是明文(未必完整),问能不能把第二个串补全,输出最短的一种可能.   分析:题 ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU4300 Clairewd’s message

    Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...

  4. hdu 4300 Clairewd’s message KMP应用

    Clairewd’s message 题意:先一个转换表S,表示第i个拉丁字母转换为s[i],即a -> s[1];(a为明文,s[i]为密文).之后给你一串长度为n<= 100000的前 ...

  5. HDU - 4300 Clairewd’s message (拓展kmp)

    HDU - 4300 题意:这个题目好难读懂,,先给你一个字母的转换表,然后给你一个字符串密文+明文,密文一定是全的,但明文不一定是全的,求最短的密文和解密后的明文: 题解:由于密文一定是全的,所以他 ...

  6. hdu 4300 kmp算法扩展

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. hdu 4300 Clairewd’s message 字符串哈希

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. hdu------(4300)Clairewd’s message(kmp)

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. hdu4300 Clairewd’s message

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题目: Clairewd’s message Time Limit: 2000/1000 MS (J ...

随机推荐

  1. dstat 性能监测工具

    dstat 是一个可以取代vmstat,iostat,netstat和ifstat这些命令的多功能产品.dstat克服了这些命令的局限并增加了一些另外的功能,增加了监控项,也变得更灵活了.dstat可 ...

  2. How to Pronounce ‘to the’ in a Sentence

    How to Pronounce ‘to the’ in a Sentence Share Tweet Share Tagged With: The Word THE, TO Reduction St ...

  3. servlet类第二篇

    1servlet的生命周期是什么? 服务器启动时(web.xml中配置load-on-startup=1,默认为0)或者第一次请求该servlet时,就会初始化一个Servlet对象,也就是会执行初始 ...

  4. oracle老是出现监听错误

    解决方法之一: 点击开始-->选择程序--->选中你安装的oracle版本-->选中配置与移植工具-->选中Net Configuration Assistant-->在 ...

  5. 吴裕雄 数据挖掘与分析案例实战(8)——Logistic回归分类模型

    import numpy as npimport pandas as pdimport matplotlib.pyplot as plt # 自定义绘制ks曲线的函数def plot_ks(y_tes ...

  6. Flexvolume

    https://kubernetes.io/docs/concepts/storage/volumes/ https://github.com/kubernetes/community/blob/ma ...

  7. springmvc处理url请求步骤

    处理步骤: 1.调用DispatcherServlet的doService方法 2.进入DispatchServlet的doDispatch方法 3.进入DispatchServlet的getHand ...

  8. 将2020年交期的PR回写出来了

    OUT_pr表中的交期为2020年和2019年,不应该 回写的PR却回写出来了 优化如下:

  9. 禁止直接访问ashx页面

      if (context.Request.ServerVariables["HTTP_REFERER"] == null)             {               ...

  10. 两台Linux之间传文件

    安装sudo apt-get install openssh-client openssh-server 使用scp命令: scp john@~/hallo.h /usr/include 将左边移动到 ...