Contest 20140928 密碼破譯 字符串hash
題意:詢問字符串指定區間循環節個數。
解法:有循環節長度a的字符串s[x,y]的性質:s[x,y-a]==s[x+a,y]由此寫一個雙hash就行了。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 1100000
#define MAXV MAXN*2
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define PROB "password"
#define _a 29
#define _b 131
#define mod 1000000007
typedef unsigned long long qword;
inline int nextInt()
{
char ch;
int x=;
bool flag=false;
do
ch=getchar(),flag=(ch=='-')?true:flag;
while(ch<''||ch>'');
do x=x*+ch-'';
while (ch=getchar(),ch<='' && ch>='');
return x*(flag?-:);
} int n,m;
char str[MAXN];
pair<qword,qword> hs[MAXN];
bool pflag[MAXN];
int prime[MAXN],topp=-;
qword pow_a[MAXN];
qword pow_b[MAXN];
void init()
{
int i,j;
for (i=;i*i<MAXN;i++)
{
if (!pflag[i])
prime[++topp]=i;
for (j=;j<=topp && i*prime[j]<MAXN;j++)
{
pflag[i*prime[j]]=true;
if (i%prime[j]==)break;
}
}
pow_a[]=;
pow_b[]=;
for (i=;i<MAXN;i++)
pow_a[i]=pow_a[i-]*_a%mod,
pow_b[i]=pow_b[i-]*_b;
}
pair<qword,qword> hash(int x,int y)
{
pair<qword,qword> ret;
ret.first=((hs[x].first+mod-hs[y+].first*pow_a[(y+)-x]%mod)%mod+mod)%mod;/*Attention*/
ret.second=hs[x].second-hs[y+].second*pow_b[(y+)-x];
return ret;
}
int check(int x,int y)
{
int ret=;
int len=(y-x+);
int lo=len;
int a,i;
for (i=;i<=topp && prime[i]*prime[i]<=len;i++)
{
if (len%prime[i]==)
{
a=;
while (len%prime[i]==)
{
len/=prime[i];
a*=prime[i];
if (hash(x,y-lo/a)==hash(x+lo/a,y))
{
ret*=prime[i];
}
}
}
}
if (len>)
{
a=len;
if (hash(x,y-lo/a)==hash(x+lo/a,y))
{
ret*=a;
}
}
return ret;
} int main()
{
freopen(PROB".in","r",stdin);
//freopen(PROB".out","w",stdout);
int i,j,k;
int x,y,z;
int ans;
init();
scanf("%d\n",&n);
fgets(str,sizeof(str),stdin);
hs[n]=make_pair(,);
for (i=n-;i>=;i--)
{
hs[i].first=hs[i+].first*_a%mod+str[i]-'a'+;
hs[i].second=hs[i+].second*_b+str[i]-'a'+;
}
// pr3=hash(5,5);
scanf("%d",&m);
for (i=;i<m;i++)
{
scanf("%d%d",&x,&y);
x--;y--;
printf("%d\n",(y-x+)/check(x,y));
}
return ;
}
Contest 20140928 密碼破譯 字符串hash的更多相关文章
- 2019 Multi-University Training Contest 2 I.I Love Palindrome String(回文自动机+字符串hash)
Problem Description You are given a string S=s1s2..s|S| containing only lowercase English letters. F ...
- Mysql工作記錄之修改默認存儲引擎及重設root用戶密碼
1>修改默認存儲引擎方法 修改配置文件,然後重啟mysql服務: [root@CHW mysql]# cat /etc/my.cnf [my ...
- 字符串hash&&对字符串hash的理解
对字符串hash的一些总结: 1,首先,我们在转化的时候,取底的时候一般是取131这些数,因为要避免不同的字符串对应相同的hash值这种情况的出现.如果卡精度的时候,我们可以采取双模数的方式尽量减少 ...
- 【codeforces 514C】Watto and Mechanism(字符串hash)
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...
- [知识点]字符串Hash
1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...
- 【BZOJ-3555】企鹅QQ 字符串Hash
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1545 Solved: 593[Submit][Statu ...
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
- LA4671 K-neighbor substrings(FFT + 字符串Hash)
题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...
- 493萬Gmail用戶的賬號密碼遭洩露,Google否認自己存在安全漏洞
最近,大公司在互聯網信息安全問題上狀況頻出.上週,蘋果因iCloud被黑客攻擊而導致大量明星私照外洩,著實是熱鬧了一陣.而Google也來湊熱鬧了.據俄羅斯媒體CNews消息,近493萬Gmail用戶 ...
随机推荐
- cocos2d-x 2.0.3 设置高清模式注意事项(已移除-hd方式)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=304 在cocos2d-x 2. ...
- 如何查看电脑的链接WIFI密码
这个查看起来确实有难度,一般软件也难以破解.不如你试试在浏览器输入192.168.1.1,账户密码都是admin然后进入路由设置里.无线安全设置里有.
- oracle合并查询
1). Union 该操作符用于取得两个结果集的并集.当使用该操作符时,会自动去掉结果集中重复行. 2).union all 该操作符与union相似,但是它不会取消重复行,而且不会排序. 3). I ...
- android 6.0获取 WRITE_SETTINGS 权限
android 6.0上只写在AndroidManifest中是不行的,还必须手动打开才行 private void setBrightnessMode(Context context, int mo ...
- SCIP读书笔记(1)
这书也算是必修吧,尤其是我这种非科班人员,还是应该抽时间尽量学习一下.大致翻过一遍,习题非常多,尽力吧. ##构造过程抽象 * 为了表述认知,每种语言都提供了三种机制:基本元素:组合方式:抽象方法. ...
- PHP+jQuery+Ajax实现用户登录与退…
用户登录与退出功能应用在很多地方,而在有些项目中,我们需要使用Ajax方式进行登录,登录成功后只刷新页面局部,从而提升了用户体验度.本文将使用PHP和jQuery来实现登录和退出功能. 查看演示DEM ...
- 使用LuaInterface遇到的编码问题
今天使用LuaInterface加载脚本时忽然报“未知字符”错误信息!于是检查文件编码 将其修改为“US ASCII” 就好了.
- google map getLocation参考
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Could not parse mapping document from resource cn/spt/model/Student.hbm.xml
初始hibernate, 写第一个程序 helloworld的错误: Exception in thread "main" org.hibernate.InvalidMapping ...
- mvc5 + ef6 + autofac搭建项目(四).1视屏上传生成截图
即上一篇中上传涉及到的 一个视频生成截图的问题,这个很简单,这是上一篇中的代码片段 #region 视频上传,生成默认展示图片(自动剪切) try { string fileSavePath = Da ...