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. ADV-292 计算行列式 java

    问题描述 //据说很多人的题目会有一大堆废话,本傻×就不在这里废话了. 给定一个N×N的矩阵A,求|A|. 输入格式 第一行一个正整数N. 接下来N行,每行N个整数,第i行第j个数字表示A[i][j] ...

  2. Keras入门——(5)长短期记忆网络LSTM(二)

    参考: https://blog.csdn.net/zwqjoy/article/details/80493341 https://blog.csdn.net/u012735708/article/d ...

  3. 十五、web中处理乱码问题总结

    一.jsp变成之道---中文乱码 jsp在转换为Servlet的过程经过三次编码转化: 转自  http://www.cnblogs.com/chenssy/p/4235191.html 二.java ...

  4. SpringMVC controller中业务方法的参数、返回值

    业务方法的参数 业务方法的参数类型.参数个数是任意的,根据需要使用. 常见的参数类型: HttpServletRequest.HttpServletResponse.HttpSession    获取 ...

  5. Lesson 47 The great escape

    What is one of the features of modern camping where nationality is concerned? Economy is one powerfu ...

  6. 二、Linux目录结构&常用指令

    Linux目录结构: ps -ef:任务管理器 ifconfig: 查看ip ping :  测试与目标主机的连通性,ctrl+c停止 目录指令: ll:列出当前目录下的文件信息 ls -al : 列 ...

  7. 初级入门 --- 认识 WebGL

    WebGL 是什么? WebGL 是一组基于 JavaScript 语言的图形规范,浏览器厂商按照这组规范进行实现,为 Web 开发者提供一套3D图形相关的 API. 这些 API 能够让 Web 开 ...

  8. JSONObject.fromObject() 转string时,实体里面的时间错乱的问题

    在把要入库的数据实体转成JSON字符串发给kafka的时候,出现了问题,转换完以后,就变成这样子的了,真的是第一次见到这种,真的是长见识了 然后百度了一下:https://www.cnblogs.co ...

  9. 深入解读EOS源代码之——区块链内核

    EOS进入大众视野并且受到热议已经有一段时间了,各种热捧和争议过后,是时候让我们静下来搞清楚EOS到底是一个什么样的产品.本文从技术角度深入的分析EOS底层设计,从源代码入手,一层层揭开EOS区块链底 ...

  10. QEMU 运行uboot,动态加载内核与文件系统

    背景 上一讲我们完成了 编译 QEMU 以及简单地做了仿真.这一讲在 启动uboot 的基础上进行,以加强对于 运行地址,加载地址等理解. 有关资料: uboot 与 代码重定位 有这样的约定,ubo ...