Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 33178   Accepted: 13792

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

用next数组求出整个数组的最大前缀。假设整个串是用循环节组成的,那么 n - next[n] 也就是最小循环节,验证最小循环节会被n整出。

#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( 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. [KMP求最小循环节][HDU1358][Period]

    题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...

  3. KMP + 求最小循环节 --- POJ 2406 Power Strings

    Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...

  4. poj 2406 Power Strings【字符串+最小循环节的个数】

                                                                                                      Po ...

  5. [KMP求最小循环节][HDU3746][Cyclic Nacklace]

    题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...

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

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

  7. HDU 3746 (KMP求最小循环节) Cyclic Nacklace

    题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...

  8. KMP + 求最小循环节 --- HUST 1010 - The Minimum Length

    The Minimum Length Problem's Link: http://acm.hust.edu.cn/problem/show/1010 Mean: 给你一个字符串,求这个字符串的最小循 ...

  9. 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace

    Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...

  10. KMP + 求最小循环节 --- HDU 1358 Period

    Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度 ...

随机推荐

  1. 桂电在线-转变成bootstrap版

    由于angularjs的不熟悉,而且SEO需要学习更多东西,于是先采用bootstrap版本,毕竟工作上也需要使用bootstrap,然后参照视频教程学习. bootstrap 基本模板 <!D ...

  2. mysql的字段类型范围必须重视起来

    在MySQL数据类型中,例如INT,FLOAT,DOUBLE,CHAR,DECIMAL等,它们都有各自的作用,下面我们就主要来介绍一下MySQL数据类型中的DECIMAL类型的作用和用法. 一般赋予浮 ...

  3. ubuntu 14.04安装quickbuild buildagent (二)

    使用方法: /home/carloz/programfiles/quickbuild6/buildagent/bin/agent.sh start /home/carloz/programfiles/ ...

  4. HttpRuntime类的缓存以及redis,memercache

    http://www.cnblogs.com/kissdodog/archive/2013/05/07/3065208.html

  5. tyvj 1342 教主泡嫦娥 环上DP

    342 教主泡嫦娥 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 2012年12月21日下午3点14分35秒,全世界各国的总统以及领导人都已经汇聚在中国的方 ...

  6. DJANGO中获取登陆用名及别名

    练练,标准认证的. VIEW中导入: from django.contrib.auth.models import User TEMPLATE中可引用: 列表 {{ user.username }}{ ...

  7. Jsp中获得集合List或Set的长度

    首先要引入<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ...

  8. SlimFTPd, LFTP和FileZilla Client/Server

    https://lftp.yar.ru/ 绝好的Socket项目

  9. [LeetCode#266] Palindrome Permutation

    Problem: Given a string, determine if a permutation of the string could form a palindrome. For examp ...

  10. 进程创建函数fork()、vfork() ,以及excel()函数

    一.进程的创建步骤以及创建函数的介绍 1.使用fork()或者vfork()函数创建新的进程 2.条用exec函数族修改创建的进程.使用fork()创建出来的进程是当前进程的完全复制,然而我们创建进程 ...