hdu 4300 Clairewd’s message 字符串哈希
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
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.
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.
Range of test data:
T<= 100 ;
n<= 100000;
题意:有一份文件,原文由密文和明文组成,前面一部分是密文,后面一部分是明文,密文可以通过转换规则(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 字符串哈希的更多相关文章
- hdu 4300 Clairewd’s message KMP应用
Clairewd’s message 题意:先一个转换表S,表示第i个拉丁字母转换为s[i],即a -> s[1];(a为明文,s[i]为密文).之后给你一串长度为n<= 100000的前 ...
- HDU - 4300 Clairewd’s message (拓展kmp)
HDU - 4300 题意:这个题目好难读懂,,先给你一个字母的转换表,然后给你一个字符串密文+明文,密文一定是全的,但明文不一定是全的,求最短的密文和解密后的明文: 题解:由于密文一定是全的,所以他 ...
- hdu 4300 Clairewd’s message(具体解释,扩展KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 Problem Description Clairewd is a member of FBI. ...
- hdu 4300 Clairewd’s message(扩展kmp)
Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...
- HDU 4300 Clairewd’s message(KMP+思维)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题目大意:题目大意就是给以一段字符xxxxzzz前面x部分是密文z部分是明文,但是我们不知道是从 ...
- 【HDU 4300 Clairewd’s message】
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...
- HDU 4300 Clairewd’s message(扩展KMP)题解
题意:先给你一个密码本,再给你一串字符串,字符串前面是密文,后面是明文(明文可能不完成整),也就是说这个字符串由一个完整的密文和可能不完整的该密文的明文组成,要你找出最短的密文+明文. 思路:我们把字 ...
- hdu 4300 Clairewd’s message(kmp/扩展kmp)
题意:真难懂.. 给出26个英文字母的加密表,明文中的'a'会转为加密表中的第一个字母,'b'转为第二个,...依次类推. 然后第二行是一个字符串(str1),形式是密文+明文,其中密文一定完整,而明 ...
- HDU 4300 Clairewd's message ( 拓展KMP )
题意 : 给你一个包含26个小写字母的明文密文转换信息字符串str,第一个表示'a'对应的密文是str[0].'b'对应str[1]……以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文 ...
随机推荐
- Acwing897 最长公共子序列
题目大意:求两个字符串的最长公共子序列的长度. 分析:这是一个典型的dp入门题,LCS. 代码: #include<bits/stdc++.h> using namespace std; ...
- 每天一点点之vue框架 watch监听变量(深度监听)
<div> <p>FullName: {{fullName}}</p> <p>FirstName: <input type="text& ...
- HTML有2种路径的写法:绝对路径和相对路径
HTML有2种路径的写法:绝对路径和相对路径 2016年11月30日 17:51:20 Bolon0708 阅读数 21775 版权声明:本文为博主原创文章,未经博主允许不得转载. https:/ ...
- 转:NGINX 基于nginx_upstream_check_module-master 健康检测及平滑升级
https://www.cnblogs.com/linxizhifeng/p/7075325.html linux的nginx下安装tengine的nginx_upstream_check_modul ...
- idea中如何将一个普通项目转换为maven项目
在pom.xml 文件上右键 Add as Maven Project即可.
- bootstrap fileinput控件上传文件大小限制
部分js: language: "zh",//设置语言 showCaption: true,//是否显示标题 showUpload: true, //是否显示上传按钮 showPr ...
- python中判断素数的函数
来看这一种判断素数(质数)的函数: form math import sart def is_prime(n): if n==1: return False for i in range(2, int ...
- Codeforces 591 B:Rebranding
B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- IDEA配置Scala开发环境
1.下载scala sdk 到页面 https://www.scala-lang.org/download/2.10.5.html 下载sdk,大数据平台目前的scala版本是2.10.5,下载后解压 ...
- 【pwnable.kr】 [simple login]
Download : http://pwnable.kr/bin/login Running at : nc pwnable.kr 9003 先看看ida里面的逻辑. 比较重要的信息时input变量再 ...