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. Lintcode 730 所有子集的和

    已知: 给一整数 n, 我们需要求前n个自然数形成的集合的所有可能子集中所有元素的和. 示例: 给出 n = , 返回 可能的子集为 {{}, {}, {, }}. 子集的元素和为 + + + = 给 ...

  2. phpBB3.2开发环境配置

    从Github导出项目 如果只是查看代码, 可以直接clone官方的git https://github.com/phpbb/phpbb.git . 如果需要开发, 就fork一下再从自己的Git里c ...

  3. [原创]茗洋AaronYang的 jquery.myselect.js 我的一次前端突破[上]

    [评论,楼层数为30的倍数的,我送你我自己的博客园的皮肤,该博客参与活动]   介绍 myselect 自己原创的js插件   这是一个可以可以支持ie6+的所有浏览器的,适应很多场景和需求的一个下拉 ...

  4. 怎么去掉Xcodeproject中的某种类型的警告 Implicit conversion loses integer precision: &#39;NSInteger&#39; (aka &#39;long&#39;) to &#39;int32

    问题描写叙述  在我们的项目中,通常使用了大量的第三方代码,这些代码可能非常复杂,我们不敢修改他们,但是作者已经停止更新了,当sdk升级或者是编译器升级后,这些遗留的代码可能会出现许很多多的警告,那么 ...

  5. 无意识(无知)-->有意识-->进入潜意识-->无意识(本能状态)

    无意识(无知)-->有意识-->进入潜意识-->无意识(本能状态) 1. 从“无意识-->有意识”的两个重要内容是“反省”+“要努力学习”,估计有80%的人无法跨过这一步 2. ...

  6. [Big Data - Codis] Codis集群的搭建与使用

    一.简介 Codis是一个分布式的Redis解决方案,对于上层的应用来说,连接Codis Proxy和连接原生的Redis Server没有明显的区别(不支持的命令列表),上层应用可以像使用单机的Re ...

  7. Thrift 源码学习一——源码结构

    Thrift 客户端与服务端的交互图 源码结构 传输层 TTransport: TTransport:客户端传输层抽象基础类,read.write.flush.close 等方法 TSocket 与 ...

  8. vs问题集

    ******将获取的所有的','换成'<br/>'********** data.Data.StandardSeating.replace(/,/g,"<br/>&q ...

  9. Solr中的q与fq参数的区别

    转自:搜索系统5:Solr中的q与fq参数的区别在那儿 1.对结果排序有影响 今天遇到一个问题,把相同的参数比如name:张三,放到q与fq,两者返回的结果完全不一样. 经过debug发现,原因是这两 ...

  10. centos7 防火墙

    1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...