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. hdu 2828 Buy Tickets

    Buy Tickets Time Limit : 8000/4000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

  2. springBoot支持PageHelp插件使用学习笔记

    首先在springboot项目的maven中加入依赖(版本可能需要自己选择合适的) <dependency> <groupId>com.github.pagehelper< ...

  3. java后台poi根据模板导出excel

    public class ExcelUtils { private static final String INSPECTIONRECORD_SURFACE_TEMPLET_PATH = " ...

  4. 使用MyEclipse/Eclipse修改项目名称报Can't convert argument: null!

    报错: java.lang.IllegalArgumentException: Can't convert argument: null! 方法/步骤    报错原因:使用MyEclipse修改项目名 ...

  5. 【yii】【php】自定义故障代码

    实际状态码: 200 操作成功 406 账号密码错误 208 请勿重复操作 401 需登陆验证 405 不容许此方法 409 验证错误

  6. php-语言参考-基本语法3.1

    一,PHP代码的开始和结束标记 1,<?php 和 ?> //重点 2,<script language="php"> 和 </script> ...

  7. SQL tp3.2 批量更新 saveAll

    /** * 批量更新数据 * @param [array] $datas [更新数据] * @param [string] $table_name [表名] */ public function sa ...

  8. DrawGrid 做图片显示 代码简单 参考性强 (Delphi7)

      运行效果图 源码 http://files.cnblogs.com/lwm8246/DrawGrid_demo.rar   procedure TfrmMain.GridDrawCell(Send ...

  9. HDU 2222 AC自动机(模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  10. Pandas 文本数据

    Pandas针对字符串配备的一套方法,使其易于对数组的每个元素(字符串)进行操作. 1.通过str访问,且自动排除丢失/ NA值 # 通过str访问,且自动排除丢失/ NA值 s = pd.Serie ...