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

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

 
  KMP算法,next[]数组的理解
  这道题考察的是对next[]数组的理解。如果 i 能整除 (i-next[i]) 的话,证明在这个字符之前,是一个由同一前缀重复连接构成的字符串。例如:aabaabaabc,c位置的下标 i 就可以整除他的下标和next数组值的差(i-next[i]),则它之前的字符串aabaabaab就是一个重复字符串,构成它的前缀的长度就是 (i - next[i])。
  注意:输入以一个'.'结束;注意这一组测试数据“aabaabaa”,WA的可以测试一下,正确结果应该为1。
  如果觉得文字太枯燥可以猛戳这里:hdu 1358:Period(KMP算法,next[]数组的使用)
  代码
 #include <stdio.h>

 char s[];
int next[];
void GetNext(char a[],int next[],int n) //获得a数列的next数组
{
int i=,k=-;
next[] = -;
while(i<n){
if(k==-){
next[i+] = ;
i++;k++;
}
else if(a[i]==a[k]){
next[i+] = k+;
i++;k++;
}
else
k = next[k];
}
} int main()
{
while(scanf("%s",s)!=EOF){
if(s[]=='.') break;
int i;
for(i=;s[i];i++);
int len = i;
GetNext(s,next,len);
if(len%(len-next[len])==)
printf("%d\n",len/(len-next[len]));
else
printf("1\n");
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 2406:Power Strings(KMP算法,next[]数组的理解)的更多相关文章

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

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

  2. poj 2406 Power Strings kmp算法

    点击打开链接 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27368   Accepted:  ...

  3. POJ 2406 Power Strings KMP算法之next数组的应用

    题意:给一个字符串,求该串最多由多少个相同的子串相接而成. 思路:只要做过poj 1961之后,这道题就很简单了.poj 1961 详细题解传送门. 假设字符串的长度为len,如果 len % (le ...

  4. POJ 2406 Power Strings (KMP)

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

  5. poj 2406 Power Strings(KMP变形)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28102   Accepted: 11755 D ...

  6. POJ 2406 Power Strings KMP运用题解

    本题是计算一个字符串能完整分成多少一模一样的子字符串. 原来是使用KMP的next数组计算出来的,一直都认为是能够利用next数组的.可是自己想了非常久没能这么简洁地总结出来,也仅仅能查查他人代码才恍 ...

  7. POJ 2406 - Power Strings - [KMP求最小循环节]

    题目链接:http://poj.org/problem?id=2406 Time Limit: 3000MS Memory Limit: 65536K Description Given two st ...

  8. POJ 2406 Power Strings KMP求周期

    传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...

  9. poj 2406 Power Strings KMP匹配

    对于数组s[0~n-1],计算next[0~n](多计算一位). 考虑next[n],如果t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1. 这样考虑: 比方字符串"a ...

随机推荐

  1. java中request,application,session三个域及参数简单示例

    直接上代码: java代码: public class HelloAction implements Action { @Override public String execute() throws ...

  2. linux 定时 svn 代码更新,配置文件不修改

    普通参数: 普通参数为正常的传参数:  例子:  f1("111") 指定参数: 指定参数为指定哪个参数给函数方法里面某个形式参数专用,优点:不受传参数的位置约束.   例子:  ...

  3. java gc的工作原理、如何优化GC的性能、如何和GC进行有效的交互

    java gc的工作原理.如何优化GC的性能.如何和GC进行有效的交互 一个优秀的Java 程序员必须了解GC 的工作原理.如何优化GC的性能.如何和GC进行有效的交互,因为有一些应用程序对性能要求较 ...

  4. IHttpModule在webconfig中的注册

    在asp.net中,提供了两种方式用来解决获取由asp.net服务器创建和维护的HttpApplication对象,方便注册HttpApplication对象的事件处理.这两种方式为:IHtpModu ...

  5. 基于HttpListener的web服务器

    写在前面 前面两篇文章分别介绍了基于原始socket的web服务器和基于tcpListener的web服务器,本篇文章将继续介绍另外一种基于HttpListener的. HttpListener Ht ...

  6. sed 指令

    sed -e 's/:/ /g' 将待处理文本行中:替换为空格, s/A/B/g 是sed中的替换命令, 将A替换为B, 其中,A可以是正则表达式. g表示全部替换. sed 指令 瀏覽數 : 6,5 ...

  7. Windows下安装配置SubVersion的简明步骤

    [使用的安装程序和文档说明] svn-1.4.0-setup.exe:Subversion服务端1.4.0安装程序: SubService.rar:  SubServe服务Windows辅助工具; T ...

  8. CSS2-3常见的demo列子总结

    CSS2-3常见的demo列子总结 阅读目录 1. css超过一行或者多行后显示省略号. 2. css图片未知高度垂直居中完美解决方案. 3. 学习使用 :before和 :after伪元素 回到顶部 ...

  9. javascript 快速排序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. (9)UI(容器)

    1.基础容器   基础容器可以设置子容器布局.是否裁切子元素.填充颜色.背景图片资源等属性.   使用场景.   在官方示例中,大量使用了基础容器作布局管理,如下面的主界面中,用户名称,钻石和金币就使 ...