Clairewd’s message

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

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

题意:有一份文件,原文由密文和明文组成,前面一部分是密文,后面一部分是明文,密文可以通过转换规则(26个字母,每个字母变成它所在的下标的字母qwerty->abcdef)表示成明文,但那个人接到这个文件后不知道从哪里开始是明文,所以你要帮忙还原一下,

如果后面明文比密文少,你就将明文补全

所以这题的关键就是找到密文和明文交界的位置,

因为密文是完整的给出的,明文可能不完整,所以明文开始的位置一定是 >= len/2+len%2 的(数据是从0开始存,所以可以取等),因为如果长度为奇数个要从后一位开始。

对原文用哈希处理两次,第一次都按密文转换规则处理(p1),第二次按明文处理(p2),之后从中间位置开始,不断比较两者哈希值,当相等是就找到交界位置,若找不到,就说明只有密文

#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#define ull unsigned long long
using namespace std;
string str,s;
ull num[];
ull p1[],p2[],pow1[];//存储哈希值
ull n;
void init()//计算131的i次方
{
pow1[]=;
for(int i=;i<=;i++)
pow1[i]=pow1[i-]*;
}
void hs()
{
p1[]=num[str[]-'a'], p2[]=str[]-'a';
for(int i=;i<str.length();i++)
{
p1[i]=p1[i-]*+num[str[i]-'a'];//计算原文str的哈希值
p2[i]=p2[i-]*+(str[i]-'a');//计算明文的哈希值
}
}
ull get(ull pp[],ull l,ull r)//pow()函数会溢出
{
return pp[r]-pp[l-]*pow1[r-l+];
}
int main()
{
cin>>n;
init();
while(n--)
{
cin>>s;
cin>>str;
for(int i=;i<;i++)//先把转换规则表示成[0,25]个数字,输出结果的时候在转换回去
{
num[s[i]-'a']=i;
}
hs();
ull x1,x2,len,flag=;
len=str.length();
x1=len/+len%;//原文中第一个明文的下标
for(int i=x1;i<len;i++)//通过比较哈希值,找到原文的第一个明文下标
{
ull y1,y2;
x2=len--i;
y1=get(p1,,x2);
y2=get(p2,i,len-);
if(y1==y2)//密文与明文的哈希值相等
{
flag=;
x1=i;
break;
}
}
if(flag==)//原文都是密文
x1=len;
for(int i=;i<x1;i++)//输出密文
cout<<str[i];
for(int i=;i<x1;i++)//把密文翻译成明文输出
printf("%c",num[str[i]-'a']+'a');
cout<<endl;
}
}

hdu 4300 Clairewd’s message 字符串哈希的更多相关文章

  1. hdu 4300 Clairewd’s message KMP应用

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

  2. HDU - 4300 Clairewd’s message (拓展kmp)

    HDU - 4300 题意:这个题目好难读懂,,先给你一个字母的转换表,然后给你一个字符串密文+明文,密文一定是全的,但明文不一定是全的,求最短的密文和解密后的明文: 题解:由于密文一定是全的,所以他 ...

  3. hdu 4300 Clairewd’s message(具体解释,扩展KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 Problem Description Clairewd is a member of FBI. ...

  4. hdu 4300 Clairewd’s message(扩展kmp)

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

  5. HDU 4300 Clairewd’s message(KMP+思维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题目大意:题目大意就是给以一段字符xxxxzzz前面x部分是密文z部分是明文,但是我们不知道是从 ...

  6. 【HDU 4300 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)题解

    题意:先给你一个密码本,再给你一串字符串,字符串前面是密文,后面是明文(明文可能不完成整),也就是说这个字符串由一个完整的密文和可能不完整的该密文的明文组成,要你找出最短的密文+明文. 思路:我们把字 ...

  8. hdu 4300 Clairewd’s message(kmp/扩展kmp)

    题意:真难懂.. 给出26个英文字母的加密表,明文中的'a'会转为加密表中的第一个字母,'b'转为第二个,...依次类推. 然后第二行是一个字符串(str1),形式是密文+明文,其中密文一定完整,而明 ...

  9. HDU 4300 Clairewd's message ( 拓展KMP )

    题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]……以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文 ...

随机推荐

  1. Hadoop端口与界面

    NameNode:7180 Cloudera Manager集群管理界面: NameNode:50070 NameNode Web UI/数据管理界面:   NameNode:8020/9000 Ha ...

  2. [经验] Cocos Creator使用笔记 --- 俄罗斯方块 (1)

    一:  实现 物体匀速掉落 这是我在做俄罗斯方块的时候遇到的一个问题,  因为原来的方块的掉落是每秒掉落一个像素点, 但是这样看起来的话会是一卡一卡的, 为了让方块在掉落的过程中看起来更加的流畅, 于 ...

  3. Ajax--概述

    1.Ajax(Asynchronous JavaScript and XML),允许浏览器与服务器通信而无需刷新当前页面的技术都被叫做Ajax; 2.XMLHttpRequest:该对象是对JavaS ...

  4. JavaScript(3)---事件冒泡、事件捕获

    JavaScript(3)---事件冒泡与事件捕获 一.理解冒泡与捕获 假设有这么一段代码 <body> <div><p>标签</p> </div ...

  5. leetcode445 Add Two Numbers II

    """ You are given two non-empty linked lists representing two non-negative integers. ...

  6. leetcode200 Number of Islands

    """ Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...

  7. 多元线性回归算法python实现(非常经典)

    对于多元线性回归算法,它对于数据集具有较好的可解释性,我们可以对比不过特征参数的输出系数的大小来判断它对数据的影响权重,进而对其中隐含的参数进行扩展和收集,提高整体训练数据的准确性.整体实现代码如下所 ...

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:按钮标签

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Window Server 2019 配置篇(8)- 利用MDT定制自动加入域的脚本

    我们要更改我们的镜像文件,使得自动安装完成后,计算机已经自动在域中 删除在WDS-server上做的自动部署,我们在上面安装一下MDT和ADK文件,这两个文件如果需要的话可以评论,我会在随后上传 之后 ...

  10. docker安装mysql中注意事项

    前言 怎么安装docker和拉mysql镜像不是本文的重点,在这里我主要讲我安装mysql容器的三个注意事项:启动容器, 修改密码,远程登录 run容器 docker run -di --name f ...