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. 标题右边10px位置紧跟发布时间

    一个ul列表,拥有若干li,内容是新闻标题,标题右边10px位置紧跟发布时间,当标题过长需要控制标题width,需要兼容ie6,不能用max-width h4{font-size:14px;heigh ...

  2. zabbix 汉化

    zabbix2.x的版本自带汉化,3.x的版本也可以通过修改配置文件强制使用自带的汉化,但是不管哪种,翻译的精准度令人费解:偶然发现一个专门翻译zabbix的网站https://www.zabbix. ...

  3. linux 优化&安全运维&黑客攻防

    优化: 可删除用户:adm,lp,sync,shutdown,halt,news,uucp,operator,games,gopher.   :userdel games 可删除组:adm,lp,ne ...

  4. B/S C/S架构的界面测试

    网站是B/S架构的典型,从做网站的有限经验来整理一下B/S测试的基本要点,并把它与C/S进行区分. 与C/S相比,以下4个测试是除了常用测试外还要注意的: (1)链接测试 (2)表单测试 (3)脚本测 ...

  5. SOA面向服务架构简述

    在上篇中我们简单谈了下架构设计中服务层的简单理解,在这里我们将继续服务层的架构,在本节我们将重点在于分布式服务.在分布式系统中表现层和业务逻辑层 并不处于同一物理部署,所以我们必须存在分布式服务,以契 ...

  6. 用纯原生js实现jquery的ready函数(两种实现)

    第一种实现方式: var dom = new function() { var dom = []; dom.isReady = false; dom.isFunction = function(obj ...

  7. cocos基础教程(6)坐标与锚点讲解

    坐标系详解 Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系.原点为屏幕左下角,x向右,y向上. 世界坐标系(World Coordinate) VS 本地坐标系(Node L ...

  8. [转]linux awk命令详解

    原文链接 : http://blog.chinaunix.net/uid-23302288-id-3785105.html   awk是行处理器: 相比较屏幕处理的优点,在处理庞大文件时不会出现内存溢 ...

  9. js实现鼠标右键自定义菜单(弹出层),并与树形菜单(TreeView)、iframe合用(兼容IE、Firefox、Chrome)

    <table class="oa-el-panel-tree"> <tr> <td style="vertical-align: top; ...

  10. 用Lucene检索数据库

    http://blog.sina.com.cn/s/blog_82ac67c101012r9z.html package com.javabean; import java.io.File;impor ...