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]……以此类推.接下来一行给你一个另一个字符串,这个字符串由密文+明文 ...
随机推荐
- Rabbitmq概念用法
MQ全称为Message Queue, 是一种分布式应用程序的的通信方法,它是消费-生产者模型的一个典型的代表,producer往消息队列中不断写入消息,而另一端consumer则可以读取或者订阅队列 ...
- css实现单行居中,两行居左
居中需要用到 text-align:center,居左是默认值也就是text-align:left.要让两者结合起来需要多一个标签. <h2><p>单行居中,多行居左</ ...
- 嵊州普及Day5T2
题意:将(w,h)的纸条折成(W,H),最少需几步. 思路:横竖互不干扰,然后最多可折int型一半,拿个函数判断两次比较即可,然后折不了的条件是需要的矩形大于给的矩形. 见代码: #include&l ...
- js获取cookie提取用户名asp.net+html
JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的. 而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一 ...
- NOIP2017tg【逛公园】 题解
先说点别的 emmm--,这是本蒟蒻的第一篇题解,大佬们勿喷QwQ(要不是看到写题解可以加贡献,我才--) 可以看到标签,是2017年提高的题目,好像是Day1T3,感觉提高考这样的题目挺好的,至少考 ...
- 七 联系人与客户多对一配置&联系人列表&分页查询联系人
联系人管理: 联系人实体类: package com.mycrm.domain; /** * 联系人的实体 * @author jt *CREATE TABLE `cst_linkman` ( `lk ...
- MQTT 协议学习:000-有关概念入门
背景 从本章开始,在没有特殊说明的情况下,文章中的MQTT版本均为 3.1.1. MQTT 协议是物联网中常见的协议之一,"轻量级物联网消息推送协议",MQTT同HTTP属于第七层 ...
- Mapreduce实例——WordCount
实验步骤 切换目录到/apps/hadoop/sbin下,启动hadoop. cd /apps/hadoop/sbin ./start-all.sh 2.在linux上,创建一个目录/data/map ...
- Centos7 rsync+inotify两台服务器同步文件(单向)
注:本篇介绍的是单向同步,即A文件同步到B,但B的文件不同步到A,双向同步的在下一篇文章中. rsync与inotify不再赘述,直接进入实战. 0.背景 两台服务器IP地址分别为: 源服务器:192 ...
- volume 方式使用 Secret【转】
Pod 可以通过 Volume 或者环境变量的方式使用 Secret,今天先学习 Volume 方式. Pod 的配置文件如下所示: ① 定义 volume foo,来源为 secret mysecr ...