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. 离线安装Cloudera Manager 5和CDH5

    关于CDH和Cloudera Manager CDH (Cloudera's Distribution, including Apache Hadoop),是Cloudera 完全开源的Hadoop  ...

  2. layoutSubviews相关总结

    ios layout机制相关方法 - (CGSize)sizeThatFits:(CGSize)size - (void)sizeToFit ------- - (void)layoutSubview ...

  3. scrollTop实现图像循环滚动(实例1)

    <html><head><title>scrollTop实现图像循环滚动(实例1)</title><meta http-equiv="C ...

  4. 1Java语言概述——重拾Java

    1.1 Java 语言诞生 Java 是1995 年 由 Sun公司开发的革命性编程语言. 1.2 Java 的特点 序号 主题 描述 1 简单 比C++简单 2 面向对象 基于对象的编程更符合人的思 ...

  5. HSTS详解

    1. 缘起:启用HTTPS也不够安全 有不少网站只通过HTTPS对外提供服务,但用户在访问某个网站的时候,在浏览器里却往往直接输入网站域名(例如www.example.com),而不是输入完整的URL ...

  6. ValueError: update only works with $ operators

    问题:在执行pymongo的update语句时,提示了ValueError: update only works with $ operators 脚本:db.user.update_one({&qu ...

  7. Web标准:六、html列表

    Web标准:六.html列表 知识点: 1.ul无序和ol有序列表 2.改变项目符号样式或用图片定义项目符号 3.横向图文列表 4.浮动后父容器高度自适应 5.IE6的双倍边距bug   1)ul无序 ...

  8. Oracle的下载、安装和配置

    win10下Oracle的下载.安装和配置     --------------siwuxie095                 1.首先到Oracle官网下载Oracle,中文版官网,传送阵:点 ...

  9. 安装Ubuntu后一些准备

    一些基础 安装的时候,先不选镜像就可以避开简易安装. 更改root密码:sudo passwd root 更改源,更新,不行就打断在更新 安装vim 改为unity模式,安装VMware Tools, ...

  10. collections系列之Counter

    collections模块中有一个叫做Counter的类,该类的作用就是计数器,Counter是对dict的加工,所有Counter继承了dict的方法 1.创建一个Counter,需要import ...