Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters.

<b< dd="">

Output

For each s you should print the largest n such that s = a^n for some string a. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

求最大循环长度。

KMP可以求,之前做过,见。

http://www.cnblogs.com/hua-dong/p/8016873.html

http://www.cnblogs.com/hua-dong/p/8016916.html

这里实现了后缀数组(不过好像被卡了,只能同KMP实现)。

  1. #include<cmath>
  2. #include<cstdio>
  3. #include<string>
  4. #include<cstring>
  5. #include<cstdlib>
  6. #include<iostream>
  7. #include<algorithm>
  8. int min(int x,int y) { if(x<y) return x;return y;}
  9. using namespace std;
  10. const int maxn=;
  11. char ch[maxn];
  12. struct SA
  13. {
  14. int Rank[maxn],sa[maxn],tsa[maxn],A[maxn],cntA[maxn],B[maxn],cntB[maxn];
  15. int ht[maxn],Min[maxn][],N;
  16. void get_SA()
  17. {
  18. N=strlen(ch+);
  19. for(int i=;i<=;i++) cntA[i]=;
  20. for(int i=;i<=N;i++) cntA[ch[i]]++;
  21. for(int i=;i<=;i++) cntA[i]+=cntA[i-];
  22. for(int i=N;i>=;i--) sa[cntA[ch[i]]--]=i;
  23. Rank[sa[]]=;
  24. for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(ch[sa[i]]==ch[sa[i-]]?:);
  25. for(int l=;Rank[sa[N]]<N;l<<=){
  26. for(int i=;i<=N;i++) cntA[i]=cntB[i]=;
  27. for(int i=;i<=N;i++) cntA[A[i]=Rank[i]]++;
  28. for(int i=;i<=N;i++) cntB[B[i]=i+l<=N?Rank[i+l]:]++;
  29. for(int i=;i<=N;i++) cntA[i]+=cntA[i-],cntB[i]+=cntB[i-];
  30. for(int i=N;i>=;i--) tsa[cntB[B[i]]--]=i;
  31. for(int i=N;i>=;i--) sa[cntA[A[tsa[i]]]--]=tsa[i];
  32. Rank[sa[]]=;
  33. for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(A[sa[i]]==A[sa[i-]]&&B[sa[i]]==B[sa[i-]]?:);
  34. }
  35. }
  36. void get_hgt()
  37. {
  38. for(int i=,j=;i<=N;i++){
  39. if(j) j--;
  40. while(ch[i+j]==ch[sa[Rank[i]-]+j]) j++;
  41. ht[Rank[i]]=j;
  42. }
  43. }
  44. void get_rmq()
  45. {
  46. for(int i=;i<=N;i++) Min[i][]=ht[i];
  47. for(int i=;(<<i)<=N;i++)
  48. for(int j=;j+(<<i)-<=N;j++){
  49. Min[j][i]=min(Min[j][i-],Min[j+(<<(i-))][i-]);
  50. }
  51. }
  52. int query_rmq(int L,int R)
  53. {
  54. if(L>R) swap(L,R);L++;
  55. int k=log2(R-L+);
  56. return min(Min[L][k],Min[R-(<<k)+][k]);
  57. }
  58. void solve()
  59. {
  60. int ans=;
  61. for(int i=;i<=N;i++){
  62. if(N%i!=) continue;
  63. if(i+query_rmq(Rank[],Rank[+i])==N) {
  64. ans=N/i; break;
  65. }
  66. } printf("%d\n",ans);
  67. }
  68. }Sa;
  69. int main()
  70. {
  71. while(~scanf("%s",ch+)){
  72. if(ch[]=='.') return ;
  73. Sa.get_SA();
  74. Sa.get_hgt();
  75. Sa.get_rmq();
  76. Sa.solve();
  77. } return ;
  78. }

ZOJ1905Power Strings (KMP||后缀数组+RMQ求循环节)的更多相关文章

  1. POJ2406 Power Strings(KMP,后缀数组)

    这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...

  2. URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just ...

  3. hdu 2459 (后缀数组+RMQ)

    题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...

  4. 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过

    题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...

  5. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  6. HDU 4691 后缀数组+RMQ

    思路: 求一发后缀数组,求个LCP 就好了 注意数字有可能不只一位 (样例2) //By SiriusRen #include <bits/stdc++.h> using namespac ...

  7. POJ 3693 后缀数组+RMQ

    思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...

  8. spoj687 REPEATS - Repeats (后缀数组+rmq)

    A string s is called an (k,l)-repeat if s is obtained by concatenating k>=1 times some seed strin ...

  9. POJ2406Power Strings (最小循环节)(KMP||后缀数组)

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...

随机推荐

  1. Runtime.getRuntime().exec()----记录日志案例

    Runtime.getRuntime().exec()方法主要用于运行外部的程序或命令. Runtime.getRuntime().exec共同拥有六个重载方法: 1.public Process e ...

  2. vim 查找与替换

    一.vim 查找 1. 正向查找 / 与 反向查找 ? 2. 退出查找 <Esc> 3. 跳转到下一处匹配 n ,跳转到上一处匹配 N 4. /<CR> 正向跳转到相同模式的下 ...

  3. java nio读取和写入文件

    读取 package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS ...

  4. 有一个数组a[N]顺序存放0~N-1,要求每隔两个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。以8个数(N=7)为例:{0,1,2,3,4,5,6,7},0->1->2(删除)->3->4->5(删除)->6->7->0(删除),如此循环直到最后一个数被删除。

    // ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // ConsoleApplication1 ...

  5. 【selenium+Python WebDriver API】之复选框顺序正选和顺序反选

    from selenium import webdriver from selenium.webdriver.common.by import By import os,time driver = w ...

  6. iOS --生产JSON格式,创建JSON文件,创建文件夹,指定储存

    //生成json文件 - (void)onjson { //    如果数组或者字典中存储了  NSString, NSNumber, NSArray, NSDictionary, or NSNull ...

  7. AOS应用基础平台-模块开发流程

    AOS平台简单介绍 AOS应用基础平台基于JavaEE技术体系,以"标准功能可复用.通用模块可配置.行业需求高速开发.异构系统无缝集成"为目标.为软件开发团队提供高效可控.随需应变 ...

  8. jQuery 插件开发(1)

    JavaScript 是一门混乱的语言,好的特性和坏的特性混杂在一起.而不同浏览器对标准的解析不一致,使得这门语言更加混乱,在这种情况下遵循最佳实践有诸多好处,至少不会掉入坑里.所以就有了<Ja ...

  9. Python爬虫-- Scrapy框架

    Scrapy框架 Scrapy使用了Twisted作为框架,Twisted有些特殊的地方是它是事件驱动的,并且比较适合异步的代码.对于会阻塞线程的操作包含访问文件.数据库或者Web.产生新的进程并需要 ...

  10. Tomcat学习笔记【4】--- Server.xml配置文件详解

    本文主要讲如何配置Tomcat服务器. 首先展示一个BS结构图: 1 server 一个server就表示一个Tomcat实例. 1)port 指定一个端口,这个端口负责监听关闭tomcat的请求: ...