題意:詢問字符串指定區間循環節個數。

  解法:有循環節長度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的更多相关文章

  1. 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 ...

  2. Mysql工作記錄之修改默認存儲引擎及重設root用戶密碼

    1>修改默認存儲引擎方法 修改配置文件,然後重啟mysql服務: [root@CHW mysql]# cat /etc/my.cnf                            [my ...

  3. 字符串hash&&对字符串hash的理解

     对字符串hash的一些总结: 1,首先,我们在转化的时候,取底的时候一般是取131这些数,因为要避免不同的字符串对应相同的hash值这种情况的出现.如果卡精度的时候,我们可以采取双模数的方式尽量减少 ...

  4. 【codeforces 514C】Watto and Mechanism(字符串hash)

    [题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...

  5. [知识点]字符串Hash

    1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...

  6. 【BZOJ-3555】企鹅QQ 字符串Hash

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1545  Solved: 593[Submit][Statu ...

  7. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

  8. LA4671 K-neighbor substrings(FFT + 字符串Hash)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...

  9. 493萬Gmail用戶的賬號密碼遭洩露,Google否認自己存在安全漏洞

    最近,大公司在互聯網信息安全問題上狀況頻出.上週,蘋果因iCloud被黑客攻擊而導致大量明星私照外洩,著實是熱鬧了一陣.而Google也來湊熱鬧了.據俄羅斯媒體CNews消息,近493萬Gmail用戶 ...

随机推荐

  1. JMeter对Oracle数据库进行压力测试

    步骤 (1)复制ORACLE的JDBC驱动JAR包文件(ojdbc14.jar)到JMeter的lib目录下. (2)运行jmeter.bat (3)建立线程组:右键测试计划->添加->T ...

  2. Chapter 6 - How to Play Music and Sound Effect

    In this chapter, we would add background music to the game and play sound effect when the hero fires ...

  3. linux shell pushd popd dirs命令

    1.dirs 1)功能显示当前目录栈中的所有记录(不带参数的dirs命令显示当前目录栈中的记录) 2)语法(1)格式:dirs  [-clpv]  [+n]  [-n](2)选项-c    删除目录栈 ...

  4. javascript如何列出全局对象的非原生属性。

    Why 研究一个网站前端技术的时候,了解它的全局的对象是一个好的入口, 一般来说,常见的库就会用外观模式,最后暴露一个对象给用户调用, 比如jQuery,requirejs,angular,react ...

  5. ASP.NET获取汉字首字母

    /// <summary> /// 获取汉字首字母(可包含多个汉字) /// </summary> /// <param name="strText" ...

  6. 菱形实现气泡Bubble,菱形画箭头,菱形画三角形

    菱形实现气泡Bubble,菱形画箭头,菱形画三角形 >>>>>>>>>>>>>>>>>>&g ...

  7. 26、Jquery 基础

    什么是Jquery? Jquery是一套Javascript脚本库. 使用时需要先下载下来,并引用到项目中. 下载地址:http://jquery.com/download/ 目前jquery分为 1 ...

  8. JAVA 循环在一个数字前面填充0.小例子

    输入结果 00000000000567 String bala="567"; 固定长度是14位,怎么循环在bala前面填充00000000000 System.out.printl ...

  9. Eclipse项目 迁移到 Intellj IDEA

    自从用了Intellj IDEA,很多项目都想迁移到Intellj上面去开发  鉴于我们的大部分项目都是基于Maven构建的,所以就可以利用maven的命令来做这个事情.     1.选择一个ecli ...

  10. javascript:console.log()是什么js库里的?

    这个不是什么库的,这个是浏览器的函数,如果你使用firefox并且装有firebug插件,当使用console.log(……)时,会把括号内的字符串输出到控制台,当然,在IE中这个是没有的,要报错.相 ...