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用戶 ...
随机推荐
- 用Ajax去读取服务器端的XML格式的数据
<html> <head></head> <script type="text/javascript"> /*---定义一个全局变量 ...
- Linux TCP/IP parameters reference
This is a reference of IP networking parameters that are configurable as described in our linux twea ...
- Btrace是一个实时监控工具
http://blog.csdn.net/gzh0222/article/details/9731031 Btrace是一个实时监控工具,可以无需修改应用代码(事实上它修改了字节码),来达到不可告人的 ...
- android 5.0 创建多用户 双开多开应用(2)
上一讲 讲了如何创建一个user android 5.0 创建多用户 双开多开应用(1) 为什么要创建User 例如window 系统创建了一个user 会在当前用户下进行操作,而android 多 ...
- [原创]ie6,7中td和img之间有间隙
情形描述 开发工具:VS2010: 浏览器版本:IE6以上,火狐,谷歌: 页面布局设计:Table+Img布局: 项目预览问题:火狐,谷歌,IE8以上未出现问题,IE6,IE7图片之间有间隙. 分析原 ...
- fmt:formatDate标签的输出格式
http://blog.csdn.net/lidawei201/article/details/7197834
- iOS 使用GBK编码的hmacMD5算法
该方法是写在工具类中的,而不是写在NSString的类别中 方法的声明: /** * 使用hmac-md5加密 * * @param clearText 原文 * @param secret ...
- C# 控制台程序 托盘图标 事件响应
static void Main(string[] args) { NotifyIconHelper ni = new NotifyIconHelper(); NotifyIconHelper.Sho ...
- JavaScript的push(),pop(),concat()方法
push 方法 将新元素添加到一个数组中,并返回数组的新长度值. arrayObj.push([item1 [item2 [. . . [itemN ]]]]) 参数 arrayObj 必选项.一个 ...
- Java stackoverflow error
本文想记录一下尝试产生stackoverflow的程序 1 -Xss=1k, 设置stack大小1024个字节,产生515个long,想把stack撑爆. 2 嵌套调用 3 创建大量线程 1 -Xss ...