Clairewd’s message

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

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
 
Author
BUPT
 
Source
 
Recommend
zhuyuanchen520
 

题意:

给定26个字母对应的加密规则和一串字符串。字符串的前半部分是密文,后半部分是对应的原文,但是原文可能只有前面一部分。

现在要你找到最短的密文,输出密文+原文

思路:

其实就是让字符串的后缀和前缀尽可能多的匹配(也就是$next[len]$),这样后面原文补全的部分就比较少。

对应的密文的长度就是$len-next[len]$。因为题意说密文和原文不能重叠,所以需要特判$next[len]$超过一半的情况。

加密规则用map存一下就好了。

刚开始数组开小了10倍T了半天。后来忘记考虑重叠的情况了,这里改掉就AC了。

 #include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<climits>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = 1e5 + ;
char str[maxn];
map<char, char>mp;
int nxt[maxn]; void getnxt(char *s)
{
int len = strlen(s);
nxt[] = -;
int k = -;
int j = ;
while(j < len){
if(k == - || s[j] == mp[s[k]]){
++k;++j;
if(s[j] != mp[s[k]]){
nxt[j] = k;
}
else{
nxt[j] = nxt[k];
}
}
else{
k = nxt[k];
}
}
} bool kmp(char *s, char *t)
{
getnxt(s);
int slen = strlen(s), tlen = strlen(t);
int i = , j = ;
while(i < slen && j < tlen){
if(j == - || s[i] == t[j]){
j++;
i++;
}
else{
j = nxt[j];
}
}
if(j == tlen){
return true;
}
else{
return false;
}
} int main()
{
int t;
scanf("%d", &t);
while(t--){
mp.clear();
getchar();
for(int i = ; i < ; i++){
char c;
scanf("%c", &c);
mp[c] = 'a' + i;
} scanf("%s", str);
getnxt(str);
int len = strlen(str);
int ans = len - nxt[len];
if(nxt[len] > (len + ) / ){
ans = (len + ) / ;
} for(int i = ; i < ans; i++){
printf("%c", str[i]);
}
for(int i = ; i < ans; i++){
printf("%c", mp[str[i]]);
}
printf("\n");
}
return ;
}

hdu4300 Clairewd’s message【next数组应用】的更多相关文章

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

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

  2. HDU-4300 Clairewd’s message

    http://acm.hdu.edu.cn/showproblem.php?pid=4300 很难懂题意.... Clairewd’s message Time Limit: 2000/1000 MS ...

  3. hdu4300 Clairewd’s message

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

  4. HDU4300 Clairewd’s message(拓展kmp)

    Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...

  5. hdu4300 Clairewd’s message 扩展KMP

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

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

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

  7. hdu 4300 Clairewd’s message KMP应用

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

  8. (KMP 扩展)Clairewd’s message -- hdu -- 4300

    http://acm.hdu.edu.cn/showproblem.php?pid=4300 Clairewd’s message Time Limit: 2000/1000 MS (Java/Oth ...

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

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

随机推荐

  1. 【PMP】项目目标的SMART原则

    详细解读 Specific 具体的 用具体的语言清楚的说明要达成的标准. Measureable 可测量的 目标应该是明确的,而不是模糊的.应该有一组明确的数据,作为衡量是否达成目标的依据. Achi ...

  2. 下载网易云音乐的MV

    网易云音乐有很多经典视频, 但是苦于没有下载按钮...今天就记录下如何保存MV到本地, 又get一项新技能!!! 一. 安装360极速浏览器(非安利) 二. 打开网易云音乐客户端, 点击"等 ...

  3. 使用Kotlin优雅的开发Android应用

    来源:https://juejin.im/post/5915c0a744d904006c4e3bcd demo下载地址:https://github.com/xiehui999/KotlinForAn ...

  4. Innotop的安装和使用

    功能特点1.显示当前innodb的全部事务列表:2.显示当前正运行着的查询:3.显示当前锁和锁等等的列表:4.服务器状态和变量的摘要信息 显示了数值的相对变化幅度:5.有多种模式可用来显示Innodb ...

  5. 改变R和Matlab的默认工作目录

    在快捷方式上右键->属性->起始位置处填上你需要的默认工作目录即可

  6. linux每日命令(32):gzip命令

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用.gzip不仅可以用 ...

  7. left join、right join、inner join的区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  8. TCP拥塞控制-慢启动、拥塞避免、快重传、快启动

    一般原理:发生拥塞控制的原因:资源(带宽.交换节点的缓存.处理机)的需求>可用资源. 作用:拥塞控制就是为了防止过多的数据注入到网络中,这样可以使网络中的路由器或者链路不至于过载.拥塞控制要做的 ...

  9. talk 1

    话转偏锋 让别人可以接话, 同时可以设计转换到的话题, 把"谈话带到正确的轨道", 就像下象棋一样, 要看三步 A: 很喜欢看篮球比赛, 对B说 我每次都堵湖人队会赢 B: 篮球最 ...

  10. Java知多少(26)源文件的声明规则

    当在一个源文件中定义多个类,并且还有import语句和package语句时,要特别注意这些规则: 一个源文件中只能有一个public类. 一个源文件可以有多个非public类. 源文件的名称应该和pu ...