hdu 4300 Clairewd’s message(具体解释,扩展KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300
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.
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.
Range of test data:
T<= 100 ;
n<= 100000;
2
abcdefghijklmnopqrstuvwxyz
abcdab
qwertyuiopasdfghjklzxcvbnm
qwertabcde
abcdabcd
qwertabcde
题意:
比較难理解,给定两组字符串,第一组仅仅有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;
}
hdu 4300 Clairewd’s message(具体解释,扩展KMP)的更多相关文章
- hdu 4300 Clairewd’s message(扩展kmp)
Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...
- hdu 4300 Clairewd’s message(kmp/扩展kmp)
题意:真难懂.. 给出26个英文字母的加密表,明文中的'a'会转为加密表中的第一个字母,'b'转为第二个,...依次类推. 然后第二行是一个字符串(str1),形式是密文+明文,其中密文一定完整,而明 ...
- hdu 4300 Clairewd’s message KMP应用
Clairewd’s message 题意:先一个转换表S,表示第i个拉丁字母转换为s[i],即a -> s[1];(a为明文,s[i]为密文).之后给你一串长度为n<= 100000的前 ...
- hdu 4300 Clairewd’s message 字符串哈希
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU - 4300 Clairewd’s message (拓展kmp)
HDU - 4300 题意:这个题目好难读懂,,先给你一个字母的转换表,然后给你一个字符串密文+明文,密文一定是全的,但明文不一定是全的,求最短的密文和解密后的明文: 题解:由于密文一定是全的,所以他 ...
- HDU 4300 Clairewd’s message(扩展KMP)
思路:extend[i]表示原串以第i開始与模式串的前缀的最长匹配.经过O(n)的枚举,我们能够得到,若extend[i]+i=len且i>=extend[i]时,表示t即为该点之前的串,c即为 ...
- HDU 4300 Clairewd’s message(扩展KMP)题解
题意:先给你一个密码本,再给你一串字符串,字符串前面是密文,后面是明文(明文可能不完成整),也就是说这个字符串由一个完整的密文和可能不完整的该密文的明文组成,要你找出最短的密文+明文. 思路:我们把字 ...
- HDU 4300 Clairewd’s message(KMP+思维)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题目大意:题目大意就是给以一段字符xxxxzzz前面x部分是密文z部分是明文,但是我们不知道是从 ...
- 【HDU 4300 Clairewd’s message】
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...
随机推荐
- BZOJ 5441: [Ceoi2018]Cloud computing
背包 #include<cstdio> #include<algorithm> using namespace std; int n,m,Len; long long F[2] ...
- python中json操作了解
什么是接口? 交换数据 http://openweathermap.org/current json简介 JSON 是存储和交换文本信息的语法.类似 XML JSON 语法是 JavaScript 语 ...
- 微信小程序开发 -- 点击右上角实现转发功能
// 在page的js文件中加入以下代码/** * 用户点击右上角分享 */ onShareAppMessage: function () { }
- 【java基础 17】集合中各实现类的性能分析
大致的再回顾一下java集合框架的基本情况 一.各Set实现类的性能分析 1.1,HashSet用于添加.查询 HashSet和TreeSet是Set的两个典型实现,HashSet的性能总是比Tree ...
- php preg_replace去除html xml 注释
php preg_replace去除html xml 注释 //不确定是否最优 $content = preg_replace('/<!--((?!-->).)*-->/s', '' ...
- 洛谷 [P2886] 牛继电器Cow Relays
最短路 + 矩阵快速幂 我们可以改进矩阵快速幂,使得它适合本题 用图的邻接矩阵和快速幂实现 注意 dis[i][i] 不能置为 0 #include <iostream> #include ...
- 【kindeditor】kindeditor的使用
官网:http://kindeditor.net/demo.php 效果:
- Scrapy学习-18-去重原理
Scrapy去重原理 scrapy本身自带一个去重中间件 scrapy源码中可以找到一个dupefilters.py去重器 源码去重算法 # 将返回值放到集合set中,实现去重 def reque ...
- LightOJ 1140: How Many Zeroes? (数位DP)
当前数位DP还不理解的点: 1:出口用i==0的方式 2:如何省略状态d(就是枚举下一个数的那个状态.当然枚举还是要的,怎么把空间省了) 总结: 1:此类DP,考虑转移的时候,应当同时考虑查询时候的情 ...
- java中线程切换的开销
思路: 开三个线程A,B,C 线程A不断的调用LockSupport.park()阻塞自己,一旦发现自己被唤醒,调用Thread.interrupted()清除interrupt标记位,同时增加自增计 ...