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. Class.forName和ClassLoader.loadClass的区别

    Class的装载分了三个阶段,loading,linking和initializing,分别定义在The Java Language Specification的12.2,12.3和12.4. Cla ...

  2. Hadoop2.2.0分布式安装配置详解[3/3]

    测试启动 按照下面的每一步执行,执行完一定要看输出的信息,注意warn或error或fatal的情况.因为这都是可能是问题出现的地方.出现一个问题,不解决,可能就会影响接下来的测试.这才是真正的工作量 ...

  3. JS前台效果

    最新的在上面 2014年3月3日14:46:46 百分比思路 function fixWidth(percent) { return document.body.clientWidth * perce ...

  4. 1.关于Swift

    关于SwiftSwift是一种新的编程语言,用于iOS和OS X的应用程序,建立在最佳的C和Objective-C之上,没有C兼容性的限制.Swift采用的安全模式设计,并增加了现代的功能,使编程更简 ...

  5. [svc]sort-uniq

    uniq - report or omit repeated lines sort -r -t uniq -r -c uniq的作用: 去除相邻重复行 [root@n1 data]# cat ip.t ...

  6. 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX

    从网上抓了一些字节流,想打印出来结果发生了一下错误: UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position ...

  7. (转)解决类似 /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found 的问题

    转自:https://itbilu.com/linux/management/NymXRUieg.html 源码编译升级安装了gcc后,编译程序或运行其它程序时,有时会出现类似/usr/lib64/l ...

  8. Selenium Web 自动化 - 项目实战(二)

    Selenium Web 自动化 - 项目实战(二) 2016-08-08 什么是数据驱动?简答的理解就是测试数据决定了测试结果,这就是所谓数据驱动.数据驱动包含了数据,他就是测试数据,在自动化领域里 ...

  9. 跨控制器跳转view——RedirectToRoute和RedirectToAction

    已知控制器AccountController.cs和HomeController.cs,如果从页面Account/Login直接跳转到Home/Index,可以利用RedirectToRoute和Re ...

  10. apache kylin2.10在原生hadoop集群上安装

    Install Kylin Download latest Kylin binaries at http://kylin.apache.org/download Export KYLIN_HOME p ...