Clairewd’s message(哈希模板+)
个人心得:一开始就是知道用哈希,但是无从下手,很明显是对哈希不太了解和思维不太好。
先来看一下这一题涉及到的哈希吧和这题的思路吧,思路就是对所给的密文用原文和翻译后进行hash处理,那么必然存在后面那一段用所给的密文处理等于前面
长度相同的用解密后处理的hash值一样。
比如abcdab,那么必然ab翻译后值等于末尾ab的值,注意长度应从len/2+len%2开始,因为如果长度为奇数个要从后一位开始。
typedef unsigned long long ull;
const int N = 100000 + 5;
const ull base = 163;
char s[N];
ull hash[N];
void init(){//处理hash值
p[0] = 1;
hash[0] = 0;
int n = strlen(s + 1);
for(int i = 1; i <=100000; i ++)p[i] =p[i-1] * base;
for(int i = 1; i <= n; i ++)hash[i] = hash[i - 1] * base + (s[i] - 'a');
}
ull get(int l, int r, ull g[]){//取出g里l - r里面的字符串的hash值
return g[r] - g[l - 1] * p[r - l + 1];
}
题目:
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.
InputThe 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.
Range of test data:
T<= 100 ;
n<= 100000;
OutputFor each test case, output one line contains the shorest possible complete text.Sample Input
2
abcdefghijklmnopqrstuvwxyz
abcdab
qwertyuiopasdfghjklzxcvbnm
qwertabcde
Sample Output
abcdabcd
qwertabcde
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<string>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 1000005
#define maxnum 100005
#define num 30
typedef unsigned long long ull;
int t,n;
int len;
ull m=161;
ull h1[maxnum],h2[maxnum],p[maxnum];
char s[30],ch[maxnum];
int c[30];
void getp(){
p[0]=1;
for(int i=1;i<=100005;i++)
p[i]=p[i-1]*m;
}
ull geth(ull h[],int i,int j){
return h[j]-h[i-1]*p[j-i+1];
}
void solved(){
h1[0]=h2[0]=0;
for(int i=0;i<26;i++)
c[s[i]-'a']=i;
for(int i=1;i<=len;i++){
h1[i]=h1[i-1]*m+c[ch[i]-'a'];
h2[i]=h2[i-1]*m+ch[i]-'a';
}
int a=len/2+len%2;
int flag;
for(int i=a;i<=len;i++){
int le=len-i;
ull key1=geth(h1,1,le);
ull key2=geth(h2,i+1,len);
//cout<<key1<<" "<<key2<<endl;
if(key1==key2)
{
flag=i;
break;
}
}
// cout<<flag;
for(int i=1;i<=flag;i++)
printf("%c",ch[i]);
for(int i=1;i<=flag;i++)
{
char cc=c[ch[i]-'a']+'a';
printf("%c",cc);
}
puts("");
}
int main()
{
scanf("%d",&t);
getp();
while(t--){
scanf("%s%s",s,ch+1);
len=strlen(ch+1);
solved();
}
return 0;
}
Clairewd’s message(哈希模板+)的更多相关文章
- hdu 4300 Clairewd’s message 字符串哈希
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu------(4300)Clairewd’s message(kmp)
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- 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
http://acm.hdu.edu.cn/showproblem.php?pid=4300 很难懂题意.... Clairewd’s message Time Limit: 2000/1000 MS ...
- hdu4300 Clairewd’s message【next数组应用】
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- (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 ...
- hdu4300 Clairewd’s message
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题目: Clairewd’s message Time Limit: 2000/1000 MS (J ...
- Clairewd’s message
Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...
- hdu 4300 Clairewd’s message(扩展kmp)
Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, she interce ...
随机推荐
- jmeter-对响应数据进行unicode转码
1,请求接口成功后,返回数据为unicode编码,查看不方便
- angularjs控制器之间的数据共享与通信
1.可以写一个service服务,从而达到数据和代码的共享; var app=angular.module('app',[]); app.service('ObjectService', [Objec ...
- 转载:WEB前端的优化
一.HTML优化 避免使用Iframe Iframe也叫内联frame,可以把一个HTML文档嵌入到另一个文档中.使用iframe的好处是被嵌入的文档可以完全独立于其父文档,凭借此特点我们通常可以使浏 ...
- SQL 2008R2还原对于服务器失败 备份集中的数据库与现有数据库 3154错误
以前用sql server 2005的时候就遇到过类似的问题,数据库在别的服务器上备份后,在本机无法还原,这次终于找到了解决方案,网上的没有找到类似的,希望能帮到大家! 原因分析:在SQL Serve ...
- 常用的SpringMVC注解
1.@RequestMapping() 访问链接编写: 例如: (1).请求方法: 访问链接: (2).请求参数和请求头: 访问链接: 2.@PathVariable 例如: 访问链接: 结果显示: ...
- 简单的HelloWorld
简单的HelloWorld 步骤: -加入jar包 -在web.xml中配置DispatcherServlet -加入Spring MVC的配置文件 新建文件springmvc.xml: -编写处理请 ...
- $.ajaxComplete()
https://www.cnblogs.com/RachelChen/p/5433881.html 全局ajax事件 必须当页面上存在任何ajax请求的时候都将触发这些特定的全局ajax处理函数. ...
- 关于ie下消除offset 问题解决
这个问题应该是上周就发现了,所有页面都没有问题,偏偏就一个页面出现不兼容问题,而且他们用的是同一个文件.这真是一个神奇的事情.今天一来上班,我就着手解决这个问题,分析查看原来是这个元素多了一个offs ...
- 同一主机,不同域名绑定不同网站(IIS主机头实现方法)
说明: 网站A域名:www.weba.com 网站A路径:D:\website\weba 网站B域名:www.webb.com 网站B路径:D:\website\webb 公用主机IP:111.222 ...
- 通过ssh证书远程登录
在渗透中,经常会发现某管理员主机上保存了大量机器的公私钥用于ssh证书登录.这个时候可以通过这个证书进行远程登录. 先回顾下证书登录通常的配置方法 一.生成不带passphrase的公私钥证书实现免密 ...