ZOJ1905Power Strings (KMP||后缀数组+RMQ求循环节)
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实现)。
#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
int min(int x,int y) { if(x<y) return x;return y;}
using namespace std;
const int maxn=;
char ch[maxn];
struct SA
{
int Rank[maxn],sa[maxn],tsa[maxn],A[maxn],cntA[maxn],B[maxn],cntB[maxn];
int ht[maxn],Min[maxn][],N;
void get_SA()
{
N=strlen(ch+);
for(int i=;i<=;i++) cntA[i]=;
for(int i=;i<=N;i++) cntA[ch[i]]++;
for(int i=;i<=;i++) cntA[i]+=cntA[i-];
for(int i=N;i>=;i--) sa[cntA[ch[i]]--]=i;
Rank[sa[]]=;
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(ch[sa[i]]==ch[sa[i-]]?:);
for(int l=;Rank[sa[N]]<N;l<<=){
for(int i=;i<=N;i++) cntA[i]=cntB[i]=;
for(int i=;i<=N;i++) cntA[A[i]=Rank[i]]++;
for(int i=;i<=N;i++) cntB[B[i]=i+l<=N?Rank[i+l]:]++;
for(int i=;i<=N;i++) cntA[i]+=cntA[i-],cntB[i]+=cntB[i-];
for(int i=N;i>=;i--) tsa[cntB[B[i]]--]=i;
for(int i=N;i>=;i--) sa[cntA[A[tsa[i]]]--]=tsa[i];
Rank[sa[]]=;
for(int i=;i<=N;i++) Rank[sa[i]]=Rank[sa[i-]]+(A[sa[i]]==A[sa[i-]]&&B[sa[i]]==B[sa[i-]]?:);
}
}
void get_hgt()
{
for(int i=,j=;i<=N;i++){
if(j) j--;
while(ch[i+j]==ch[sa[Rank[i]-]+j]) j++;
ht[Rank[i]]=j;
}
}
void get_rmq()
{
for(int i=;i<=N;i++) Min[i][]=ht[i];
for(int i=;(<<i)<=N;i++)
for(int j=;j+(<<i)-<=N;j++){
Min[j][i]=min(Min[j][i-],Min[j+(<<(i-))][i-]);
}
}
int query_rmq(int L,int R)
{
if(L>R) swap(L,R);L++;
int k=log2(R-L+);
return min(Min[L][k],Min[R-(<<k)+][k]);
}
void solve()
{
int ans=;
for(int i=;i<=N;i++){
if(N%i!=) continue;
if(i+query_rmq(Rank[],Rank[+i])==N) {
ans=N/i; break;
}
} printf("%d\n",ans);
}
}Sa;
int main()
{
while(~scanf("%s",ch+)){
if(ch[]=='.') return ;
Sa.get_SA();
Sa.get_hgt();
Sa.get_rmq();
Sa.solve();
} return ;
}
ZOJ1905Power Strings (KMP||后缀数组+RMQ求循环节)的更多相关文章
- POJ2406 Power Strings(KMP,后缀数组)
这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...
- URAL 题目1297. Palindrome(后缀数组+RMQ求最长回文子串)
1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The "U.S. Robots" HQ has just ...
- hdu 2459 (后缀数组+RMQ)
题意:让你求一个串中连续重复次数最多的串(不重叠),如果重复的次数一样多的话就输出字典序小的那一串. 分析:有一道比这个简单一些的题spoj 687, 假设一个长度为l的子串重复出现两次,那么它必然会 ...
- 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过
题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...
- HDU 3746 Cyclic Nacklace (KMP求循环节问题)
<题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题] #include &l ...
- HDU 4691 后缀数组+RMQ
思路: 求一发后缀数组,求个LCP 就好了 注意数字有可能不只一位 (样例2) //By SiriusRen #include <bits/stdc++.h> using namespac ...
- POJ 3693 后缀数组+RMQ
思路: 论文题 后缀数组&RMQ 有一些题解写得很繁 //By SiriusRen #include <cmath> #include <cstdio> #includ ...
- 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 ...
- POJ2406Power Strings (最小循环节)(KMP||后缀数组)
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...
随机推荐
- redis错误error记录
早上登服务器,看到程序的redis的报错, 具体如下: (error) MISCONF Redis is configured to save RDB snapshots, but is curren ...
- UVALive 6530 Football (水
题目链接:点击打开链 #include <cstdio> #include <vector> #include <algorithm> using namespac ...
- Android-解决Fail to post notification on channel "null"的方法
原文:https://blog.csdn.net/weixin_40604111/article/details/78674563 在sdk版本为25或25之前想在notification中添加一个点 ...
- Android 超高仿微信图片选择器 图片该这么载入
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39943731,本文出自:[张鸿洋的博客] 1.概述 关于手机图片载入器,在当今像 ...
- VS2012,VS2013启用SQLite的Data Provider界面显示
VS2012,VS2013启用SQLite的Data Provider界面显示 VS 2012默认是不带的SQLite的Data Provider,所以无法直接在VS 2012里管理SQLite的数据 ...
- grok表达式
grok表达式 grok其实就是封装了各种常用的正则表达式,屏蔽了直接写正则的复杂性:通过它可以提取日志内容,按照自己指定的格式输出到kibana. http://udn.yyuap.com/doc/ ...
- native2ascii转码工具的使用
native2ascii转码工具是JDK自带的一种,方便我们将非unicode的编码文件转为unicode格式的文件,位置一般是位于JAVA_HOME/bin目录下. Why? 在做Java开发的时候 ...
- 【WinForm】创建自定义控件(转)
转自:http://www.cnblogs.com/bomo/archive/2012/12/09/2810559.html 虽然VS为我们提供了很多控件可以使用,但有时候这些控件仍然不能满足我们的要 ...
- c++编码习惯
1 大驼峰命名法 类名和函数名由单词构成,每个单词的首字母大写. 2 函数命名 大驼峰命名法. 3 类命名 大驼峰命名,但是为了和函数名区分开,在前面加上一个大写的C.
- "静态方法里仅仅能调用静态变量和静态方法"具体解释
静态方法里能够调用静态方法和静态变量,同一时候也能调用非静态方法和非静态变量. public class Test { public Test() {}; public Test(int i) {th ...