Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 33163   Accepted: 13784

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

Waterloo local 2002.07.0
 
#include <cstdio>
#include <cstring>
#include <algorithm>
int next[1100000] ;
char str[1100000] ;
void getnext(int l)
{
int j = 0 , k = -1 ;
next[0] = -1 ;
while(j < l)
{
if( k == -1 || str[j] == str[k] )
{
j++ ;
k++ ;
next[j] = k ;
}
else
k = next[k] ;
}
}
int main()
{
int l , m ;
while(scanf("%s", str)!=EOF)
{
if( str[0] == '.' ) break;
l = strlen(str);
getnext(l) ;
m = next[l];
if( m == -1 )
printf("1\n");
else if(m == l)
printf("%d\n", l);
else if( l % (l-m) != 0 )
printf("1\n");
else
{
m = l / ( l-m );
printf("%d\n", m);
}
memset(str,0,sizeof(str));
}
return 0;
}

poj2406--Power Strings(kmp:求循环串的次数)的更多相关文章

  1. poj2406 Power Strings (kmp 求最小循环字串)

    Power Strings   Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 ...

  2. POJ2406 Power Strings —— KMP or 后缀数组 最小循环节

    题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  3. UVA - 10298 Power Strings (KMP求字符串循环节)

    Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...

  4. POJ 2406 Power Strings KMP求周期

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

  5. POJ2406 Power Strings KMP算法

    给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的  譬 ...

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

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

  7. POJ2406 Power Strings(KMP)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56162   Accepted: 23370 Description Giv ...

  8. POJ2406 Power Strings(KMP,后缀数组)

    这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...

  9. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

随机推荐

  1. js 秒杀

    秒杀活动页面 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" ...

  2. 面试准备——Zookeeper

    转自https://www.cnblogs.com/shan1393/p/9479109.html 1. Zookeeper是什么框架 分布式的.开源的分布式应用程序协调服务,原本是Hadoop.HB ...

  3. 使用pipework将Docker容器桥接到本地网络环境中

    在使用Docker的过程中,有时候我们会有将Docker容器配置到和主机同一网段的需求.要实现这个需求,我们只要将Docker容器和主机的网卡桥接起来,再给Docker容器配上IP就可以了.pipew ...

  4. UVA - 10591 Happy Number

    Happy Number UVA - 10591 Let the sum of the square of the digits of a positive integer S0 be represe ...

  5. 命令行-s的意思

    -s,signal,意思就是信号,一般是发送信号. 如: # 关闭 nginx -s stop;

  6. 【Kubernetes】Deployment控制器模型

    在Kubernetes中,Deployment是最基本的控制器对象 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deploym ...

  7. php.ini配置参数详解

    下面对php.ini中一些安全相关参数的配置进行说明 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 2 ...

  8. 【bzoj4408】[Fjoi 2016]神秘数 主席树

    题目描述 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13},1 = 12 = 1+13 = 1+1+14 = 45 = 4+16 = 4+1+1 ...

  9. spring之scope作用域

    spring中,bean的作用域有五种类型:默认是单例模式,         singleton         prototype         request         session   ...

  10. 【最优K叉树】hdu 5884 Sort

    http://acm.hdu.edu.cn/showproblem.php?pid=5884 参考:https://www.cnblogs.com/jhz033/p/5879452.html [题意] ...