Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 28859   Accepted: 12045

Description

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

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

Source

 //42704K    2829MS    C++    2709B    2013-12-18 13:31:43
/* 题意:
给出一个由某个子串重复R次组成的字符串,求R的最大值 后缀数组:
KMP应该会简单些,因为此处要练习后缀数组,故用后缀数组。首先考虑用DA的话会TLE,
因为其时间复杂度为O(nlgn),数据太大(n=2000000)。
此处用dc3,dc3的算法没研究,只是套用其模板,dc3算法会比DA快些,在此处勉勉强强
的过了。先用dc3求出后缀数组,然后再求height数组,再穷举字符串长度len。求k能整除len
且suffix(1) 与 suffix(k+1) 最长前缀等于len-k。
在求最长公共前缀时,由于suffix(1)是固定的,利用height数组的特性,求出height数组
中每一个数到height[rank[0]]间的最小值即可。 */
#include<stdio.h>
#include<string.h>
#define N 2000005
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
int wa[N],wb[N],wv[N],ws[N];
int rank[N],height[N];
int sa[N],r[N];
char c[N];
int lcp[N]; //记录到height[rank[0]]的最小值
int Max(int a,int b)
{
return a>b?a:b;
}
int Min(int a,int b)
{
return a<b?a:b;
}
int cmp(int *y,int a,int b,int l)
{
return y[a]==y[b]&&y[a+l]==y[b+l];
} int c0(int *y,int a,int b)
{
return y[a]==y[b]&&y[a+]==y[b+]&&y[a+]==y[b+];
}
int c12(int k,int *y,int a,int b)
{
if(k==) return y[a]<y[b]||y[a]==y[b]&&c12(,y,a+,b+);
else return y[a]<y[b]||y[a]==y[b]&&wv[a+]<wv[b+];
}
void sort(int *r,int *a,int *b,int n,int m)
{
int i;
for(i=;i<n;i++) wv[i]=r[a[i]];
for(i=;i<m;i++) ws[i]=;
for(i=;i<n;i++) ws[wv[i]]++;
for(i=;i<m;i++) ws[i]+=ws[i-];
for(i=n-;i>=;i--) b[--ws[wv[i]]]=a[i];
return;
}
void dc3(int *r,int *sa,int n,int m)
{
int i,j,*rn=r+n,*san=sa+n,ta=,tb=(n+)/,tbc=,p;
r[n]=r[n+]=;
for(i=;i<n;i++) if(i%!=) wa[tbc++]=i;
sort(r+,wa,wb,tbc,m);
sort(r+,wb,wa,tbc,m);
sort(r,wa,wb,tbc,m);
for(p=,rn[F(wb[])]=,i=;i<tbc;i++)
rn[F(wb[i])]=c0(r,wb[i-],wb[i])?p-:p++;
if(p<tbc) dc3(rn,san,tbc,p);
else for(i=;i<tbc;i++) san[rn[i]]=i;
for(i=;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*;
if(n%==) wb[ta++]=n-;
sort(r,wb,wa,ta,m);
for(i=;i<tbc;i++) wv[wb[i]=G(san[i])]=i;
for(i=,j=,p=;i<ta && j<tbc;p++)
sa[p]=c12(wb[j]%,r,wa[i],wb[j])?wa[i++]:wb[j++];
for(;i<ta;p++) sa[p]=wa[i++];
for(;j<tbc;p++) sa[p]=wb[j++];
return;
}
void get_height(int n)
{
int i,j,k=;
for(i=;i<=n;i++) rank[sa[i]]=i;
for(i=;i<n;height[rank[i++]]=k)
for(k?k--:,j=sa[rank[i]-];r[i+k]==r[j+k];k++);
return;
}
int main(void)
{
while(scanf("%s",c)!=EOF)
{
if(strcmp(c,".")==) break;
int n=strlen(c);
for(int i=;i<n;i++)
r[i]=c[i]+;
r[n]=;
dc3(r,sa,n+,);
get_height(n);
//for(int i=0;i<n;i++) printf("%d %d %d\n",i,rank[i],height[i]);
memset(lcp,,sizeof(lcp));
lcp[rank[]]=N;
for(int i=rank[]-;i>=;i--) lcp[i]=Min(lcp[i+],height[i+]);
for(int i=rank[]+;i<=n;i++) lcp[i]=Min(lcp[i-],height[i]);
//for(int i=0;i<=n;i++) printf("%d %d %d\n",rank[i],height[i],lcp[i]);
for(int k=;k<=n;k++) //遍历所有值
if(n%k== && lcp[rank[k]]==n-k){
printf("%d\n",n/k);
break;
}
}
return ;
}
/*
abcd
aaaa
ababab
*/

poj 2406 Power Strings (后缀数组 || KMP)的更多相关文章

  1. poj 2406 Power Strings 后缀数组解法

    连续重复子串问题 poj 2406 Power Strings http://poj.org/problem?id=2406 问一个串能否写成a^n次方这种形式. 虽然这题用kmp做比较合适,但是我们 ...

  2. (简单) POJ 2406 Power Strings,扩展KMP。

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

  3. POJ 2406 Power Strings next数组循环节应用、

    题意:就给出个字符串做*的定义.a^0 = "" (the empty string) and a^(n+1) = a*(a^n).    题目要求n的最大值. 思路: 化简上面的 ...

  4. KMP POJ 2406 Power Strings

    题目传送门 /* 题意:一个串有字串重复n次产生,求最大的n KMP:nex[]的性质应用,感觉对nex加深了理解 */ /************************************** ...

  5. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  6. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  7. poj 2406:Power Strings(KMP算法,next[]数组的理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30069   Accepted: 12553 D ...

  8. 【poj 2406】Power Strings 后缀数组DC3模板 【连续重复子串】

    Power Strings 题意 给出一个字符串s,求s最多由几个相同的字符串重复而成(最小循环节的重复次数) 思路 之前学习KMP的时候做过. 我的思路是:枚举字符串的长度,对于当前长度k,判断\( ...

  9. POJ - 2406 Power Strings (后缀数组DC3版)

    题意:求最小循环节循环的次数. 题解:这个题其实可以直接用kmp去求最小循环节,然后在用总长度除以循环节.但是因为在练后缀数组,所以写的后缀数组版本.用倍增法会超时!!所以改用DC3法.对后缀数组还不 ...

随机推荐

  1. mpvue项目中安装weui

    观察一下发现,mpvue项目打包css的规律是:根组件App.vue里的style样式全部打包到 dist  /  static / css / app.wxss ..   参照微信小程序的原生引入使 ...

  2. 给网站添加icon图标

    只需制成ico结尾的图片即可

  3. IntelliJ IDEA 方法注释教程

    首先Ctrl +Alt +S ,打开Settings ,找到Live Template,然后点击右侧的绿色的“+”,选择Template Group 然后给新建的Group随便命个名 选中自己刚才创建 ...

  4. Element-ui组件--pagination分页

    一般写后台系统都会有很多的列表,有列表就相应的要用到分页,根据项目中写的几个分页写一下我对分页的理解,就当是学习笔记了. 这是Element-ui提供的完整的例子 <template>  ...

  5. 关于sql server 2008 r2 展开时报错:参数名:viewInfo ( Microsoft SqlServer Management SqlStudio Explorer )解决思路

    今天安装了sql server 2008 R2,安装成功之后我打开软件登陆都没问题,但是一展开选项就弹出错误提示框: 参数名:viewInfo 不能为空 (Microsoft SqlServer Ma ...

  6. Pandas 数值计算和统计基础

    1.(1) # 基本参数:axis.skipna import numpy as np import pandas as pd df = pd.DataFrame({'key1':[4,5,3,np. ...

  7. POJ:3421-X-factor Chains(因式分解)(全排列)

    X-factor Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7986 Accepted: 2546 Descr ...

  8. linux c scanf()小解

    今天学习了新的内容,关于c语言的scanf()函数. scanf()函数,读取指定格式的值赋值给相应变量.空格(‘ ‘),回车('\n'),TAB是分隔符,轻易不会被读取.还有,该函数的返回值是正确读 ...

  9. 清空Fragment回退栈中某个Fragment之上的所有Fragment

    根据debug信息查看Fragment回退栈的情况,具体debug代码如下: int num = getActivity().getSupportFragmentManager().getBackSt ...

  10. Android面试收集录12 View测量、布局及绘制原理

    一.View绘制的流程框架 View的绘制是从上往下一层层迭代下来的.DecorView-->ViewGroup(--->ViewGroup)-->View ,按照这个流程从上往下, ...