HDU 4300 Clairewd’s message(扩展KMP)题解
题意:先给你一个密码本,再给你一串字符串,字符串前面是密文,后面是明文(明文可能不完成整),也就是说这个字符串由一个完整的密文和可能不完整的该密文的明文组成,要你找出最短的密文+明文。
思路:我们把字符串当做全是密文然后解密成明文,这样前面密文部分就是完整的明文,后面明文部分就乱码了,要求最短密文+明文就是求最短的密文,就是求原串和解密后的串的最大公共前缀,所以用EXMP求解。
坑爹了,前面没加‘\0’后面加了WA了一早上...orz
#include<cstdio>
#include<map>
#include<set>
#include<queue>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
const int maxn = 100000+5;
const int MOD = 10007;
const int INF = 0x3f3f3f3f;
using namespace std;
int Next[maxn],extend[maxn];
char trans[30],s1[maxn],s2[maxn];
char kk[30];
void getNext(char *x){
int len = strlen(x);
Next[0] = len;
int j = 0;
while(j + 1 < len && x[j] == x[j + 1]) j++;
Next[1] = j;
int k = 1;
for(int i = 2;i < len;i++){
int p = Next[k] + k - 1;
int L = Next[i - k];
if(i + L < p + 1)
Next[i] = L;
else{
j = max(0,p - i + 1);
while(i + j < len && x[i + j] == x[j]) j++;
Next[i] = j;
k = i;
}
}
}
void ExKMP(char *y,char *x){
getNext(x);
int j = 0;
int lenx = strlen(x);
int leny = strlen(y);
while(j < leny && j < lenx && x[j] == y[j]) j++;
extend[0] = j;
int k = 0;
for(int i = 1;i < leny;i++){
int p = extend[k] + k - 1;
int L = Next[i - k];
if(i + L < p + 1)
extend[i] = L;
else{
j = max(0,p - i + 1);
while(i + j < leny && j < lenx && y[i + j] == x[j]) j++;
extend[i] = j;
k = i;
}
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s%s",kk,s1);
for(int i = 0;i < 26;i++)
trans[kk[i] - 'a'] = i + 'a'; //密文转化为明文
int len = strlen(s1);
for(int i = 0;i < len;i++)
s2[i] = trans[s1[i] - 'a'];
s2[len] = '\0';
//printf("%s\n%s\n",s1,s2);
ExKMP(s1,s2);
//for(int i = 0;i < len;i++) printf("%d ",extend[i]);
int i;
for(i = 0;i < len;i++){
if(i + extend[i] >= len && i >= extend[i]){
break;
}
}
len = len - extend[i];
s1[len] = s2[len] = '\0';
printf("%s%s\n",s1,s2);
}
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 )
题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]……以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文 ...
- 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 (拓展kmp)
HDU - 4300 题意:这个题目好难读懂,,先给你一个字母的转换表,然后给你一个字符串密文+明文,密文一定是全的,但明文不一定是全的,求最短的密文和解密后的明文: 题解:由于密文一定是全的,所以他 ...
- hdu4300 Clairewd’s message 扩展KMP
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...
- 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)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 Problem Description Clairewd is a member of FBI. ...
- HDU 4300 Clairewd’s message(扩展KMP)
思路:extend[i]表示原串以第i開始与模式串的前缀的最长匹配.经过O(n)的枚举,我们能够得到,若extend[i]+i=len且i>=extend[i]时,表示t即为该点之前的串,c即为 ...
随机推荐
- 【linux系列】vi模式下查找和替换
一.查找 1.查找命令 /pattern<Enter> :向下查找pattern匹配字符串 ?pattern<Enter> :向上查找匹配字符串 在使用了查找命令之后,使用如下 ...
- 配置linux DNS
DNS服务器地址配置 在Linux下面,有一个默认的DNS服务器地址配置文件的设置,存放在 /etc/resolv.conf 设置方法很简单,通过编辑 vi /etc/resolv.conf 设置首选 ...
- Unity3D笔记十二 游戏元素二之摄像机
一.摄像机 摄像头用以捕捉和显示的世界给玩家.通过自定义和操纵相机,你可以让你的游戏的呈现真正独一无二的.您可以在一个场景无限数量的摄像机.它们可以设置在任何顺序呈现在屏幕上的任何地方,或者屏幕的某些 ...
- idea的svn插件中compare with the same repository version和compare with latest repository version的区别?
Idea的svn插件中compare with the same repository version和compare with latest repository version的区别? 1.com ...
- 徐州网络赛C-Cacti Lottery【DFS】
54.19% 2000ms 262144K Morgana is playing a game called cacti lottery. In this game, morgana has a 3 ...
- POI3的资料整理
转自http://aman.cao.blog.163.com/blog/static/32951336201010823557408/ POI3的资料整理一.POI简介 Jakarta POI 是ap ...
- Python开发【项目】:大型模拟战争游戏(外星人入侵)
外星人入侵 游戏概述: 现在准备用python开始搞一个大型游戏,模拟未来战争,地球人狙击外星人大战(其实就是小蜜蜂游戏2333),玩家控制一个飞船,用子弹歼灭屏幕上空的外星飞船:项目用到了Pygam ...
- mysql 表的增删改查
一.表介绍 表相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段 对于一张表来说,字段是必须要有的. 数据表 类似于excel id,name,qq, ...
- MySQL创建索引命令
MySQL索引类型 普通索引 创建索引的方式 -- 直接新建索引 CREATE INDEX indexName ON mytable(username(length)) -- 修改表结构新建索引 AL ...
- java.lang.NoSuchMethodError: scala.Predef$.refArrayOps([Ljava/lang/Object;)Lscala/collection/mutable/ArrayOps;
用Maven创建了一个spark sql项目,在引入spark sql jar包时引入的是: <dependency> <groupId>org.apache.spark< ...