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

  解法:有循環節長度a的字符串s[x,y]的性質:s[x,y-a]==s[x+a,y]由此寫一個雙hash就行了。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<ctime>
  6. #include<cmath>
  7. #include<algorithm>
  8. #include<set>
  9. #include<map>
  10. #include<vector>
  11. #include<string>
  12. #include<queue>
  13. using namespace std;
  14. #ifdef WIN32
  15. #define LL "%I64d"
  16. #else
  17. #define LL "%lld"
  18. #endif
  19. #define MAXN 1100000
  20. #define MAXV MAXN*2
  21. #define MAXE MAXV*2
  22. #define INF 0x3f3f3f3f
  23. #define INFL 0x3f3f3f3f3f3f3f3fLL
  24. #define PROB "password"
  25. #define _a 29
  26. #define _b 131
  27. #define mod 1000000007
  28. typedef unsigned long long qword;
  29. inline int nextInt()
  30. {
  31. char ch;
  32. int x=;
  33. bool flag=false;
  34. do
  35. ch=getchar(),flag=(ch=='-')?true:flag;
  36. while(ch<''||ch>'');
  37. do x=x*+ch-'';
  38. while (ch=getchar(),ch<='' && ch>='');
  39. return x*(flag?-:);
  40. }
  41.  
  42. int n,m;
  43. char str[MAXN];
  44. pair<qword,qword> hs[MAXN];
  45. bool pflag[MAXN];
  46. int prime[MAXN],topp=-;
  47. qword pow_a[MAXN];
  48. qword pow_b[MAXN];
  49. void init()
  50. {
  51. int i,j;
  52. for (i=;i*i<MAXN;i++)
  53. {
  54. if (!pflag[i])
  55. prime[++topp]=i;
  56. for (j=;j<=topp && i*prime[j]<MAXN;j++)
  57. {
  58. pflag[i*prime[j]]=true;
  59. if (i%prime[j]==)break;
  60. }
  61. }
  62. pow_a[]=;
  63. pow_b[]=;
  64. for (i=;i<MAXN;i++)
  65. pow_a[i]=pow_a[i-]*_a%mod,
  66. pow_b[i]=pow_b[i-]*_b;
  67. }
  68. pair<qword,qword> hash(int x,int y)
  69. {
  70. pair<qword,qword> ret;
  71. ret.first=((hs[x].first+mod-hs[y+].first*pow_a[(y+)-x]%mod)%mod+mod)%mod;/*Attention*/
  72. ret.second=hs[x].second-hs[y+].second*pow_b[(y+)-x];
  73. return ret;
  74. }
  75. int check(int x,int y)
  76. {
  77. int ret=;
  78. int len=(y-x+);
  79. int lo=len;
  80. int a,i;
  81. for (i=;i<=topp && prime[i]*prime[i]<=len;i++)
  82. {
  83. if (len%prime[i]==)
  84. {
  85. a=;
  86. while (len%prime[i]==)
  87. {
  88. len/=prime[i];
  89. a*=prime[i];
  90. if (hash(x,y-lo/a)==hash(x+lo/a,y))
  91. {
  92. ret*=prime[i];
  93. }
  94. }
  95. }
  96. }
  97. if (len>)
  98. {
  99. a=len;
  100. if (hash(x,y-lo/a)==hash(x+lo/a,y))
  101. {
  102. ret*=a;
  103. }
  104. }
  105. return ret;
  106. }
  107.  
  108. int main()
  109. {
  110. freopen(PROB".in","r",stdin);
  111. //freopen(PROB".out","w",stdout);
  112. int i,j,k;
  113. int x,y,z;
  114. int ans;
  115. init();
  116. scanf("%d\n",&n);
  117. fgets(str,sizeof(str),stdin);
  118. hs[n]=make_pair(,);
  119. for (i=n-;i>=;i--)
  120. {
  121. hs[i].first=hs[i+].first*_a%mod+str[i]-'a'+;
  122. hs[i].second=hs[i+].second*_b+str[i]-'a'+;
  123. }
  124. // pr3=hash(5,5);
  125. scanf("%d",&m);
  126. for (i=;i<m;i++)
  127. {
  128. scanf("%d%d",&x,&y);
  129. x--;y--;
  130. printf("%d\n",(y-x+)/check(x,y));
  131. }
  132. return ;
  133. }

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. 用Ajax去读取服务器端的XML格式的数据

    <html> <head></head> <script type="text/javascript"> /*---定义一个全局变量 ...

  2. Linux TCP/IP parameters reference

    This is a reference of IP networking parameters that are configurable as described in our linux twea ...

  3. Btrace是一个实时监控工具

    http://blog.csdn.net/gzh0222/article/details/9731031 Btrace是一个实时监控工具,可以无需修改应用代码(事实上它修改了字节码),来达到不可告人的 ...

  4. android 5.0 创建多用户 双开多开应用(2)

    上一讲 讲了如何创建一个user android 5.0 创建多用户 双开多开应用(1) 为什么要创建User  例如window 系统创建了一个user 会在当前用户下进行操作,而android 多 ...

  5. [原创]ie6,7中td和img之间有间隙

    情形描述 开发工具:VS2010: 浏览器版本:IE6以上,火狐,谷歌: 页面布局设计:Table+Img布局: 项目预览问题:火狐,谷歌,IE8以上未出现问题,IE6,IE7图片之间有间隙. 分析原 ...

  6. fmt:formatDate标签的输出格式

    http://blog.csdn.net/lidawei201/article/details/7197834

  7. iOS 使用GBK编码的hmacMD5算法

    该方法是写在工具类中的,而不是写在NSString的类别中 方法的声明: /** *  使用hmac-md5加密 * *  @param clearText 原文 *  @param secret  ...

  8. C# 控制台程序 托盘图标 事件响应

    static void Main(string[] args) { NotifyIconHelper ni = new NotifyIconHelper(); NotifyIconHelper.Sho ...

  9. JavaScript的push(),pop(),concat()方法

    push 方法 将新元素添加到一个数组中,并返回数组的新长度值. arrayObj.push([item1 [item2 [. . . [itemN ]]]]) 参数 arrayObj 必选项.一个  ...

  10. Java stackoverflow error

    本文想记录一下尝试产生stackoverflow的程序 1 -Xss=1k, 设置stack大小1024个字节,产生515个long,想把stack撑爆. 2 嵌套调用 3 创建大量线程 1 -Xss ...