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. lftp命令详解

    lftp.sh自动上传脚本: #!/bin/bash echo -e "\nScript start at \033[43;35m `date "+%H:%M:%S"` ...

  2. 一步步实现 easyui datagrid表格宽度自适应,效果非常好

    一步步实现 easyui datagrid表格宽度自适应,效果非常好: 一.设置公共方法,使得datagrid的属性  fitColumns:true $(function(){ //初始加载,表格宽 ...

  3. Hibernate hql 多表查询

    String hql="select c from Col c ,UserRole role where c.id=role.columnId and c.id=? and role.use ...

  4. Eclipse 安装JavaEE插件

    Oxygen版Eclipse 导入项目会自动安装你项目需要的一些插件,但是有时候会安装失败,需要手动安装: 这里以Dynamic Web Project项目为例 打开Eclipse,依次选择Help ...

  5. sessionStorage和localStorage

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(session)中的数据,这些数据只 ...

  6. JSP技术复习

    JSP是一种运行在服务器端的脚本语言,是用来开发动态网页的技术,它是Java Web程序的开发重要技术 JSP页面主要由HTML和JSP代码构成,JSP代码是通过"<%"和& ...

  7. 第七章 二叉搜索树 (d2)AVL树:插入

  8. keras—多层感知器MLP—MNIST手写数字识别

    一.手写数字识别 现在就来说说如何使用神经网络实现手写数字识别. 在这里我使用mind manager工具绘制了要实现手写数字识别需要的模块以及模块的功能:  其中隐含层节点数量(即神经细胞数量)计算 ...

  9. 【英宝通Unity4.0公开课学习 】(六)76讲到90讲

    还是关于Mecanim动画的内容. 这些讲的每讲长度明显比前面的长,而且很多都涉及到脚本编写. 不过我还是2倍速给略览过去了,主要目的就是学个框架嘛 :) 1. Blend Tree 可嵌套. 可理解 ...

  10. Drying

    Drying http://poj.org/problem?id=3104 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2 ...