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. GridView获取单个单元格的值

    0.GridView中的所有数据都存储在Rows集合中,可以通过Rows的Cell属性获取单个单元格的值:如果某个单元格包含其他控件,则通过使用单元格的 Controls 集合,从单元格检索控件:如果 ...

  2. 一次 php nusoap 调试过程

    今天跟同事调用一个数据api ,用soap方式调用.本以为很简单的事情,却弄到了晚上. 因为有过调试经验,直接按照以往的过程直接部署,结果是错误. 1. 以为是调用方式错了,问了一下对接的同事,没问题 ...

  3. 帝国cms7.0修改默认搜索模版中的分页[!--show.page--]

    修改默认搜索模版的分页是在e/class/connect.php下 搜索下function page1就是我们要修改的分页了 下面贴上我修改后的分页 //前台分页 function page1($nu ...

  4. tabBar选中底部弹出窗口

    //UITabBarControllerDelegate方法 - (BOOL)tabBarController:(UITabBarController *)tabBarController shoul ...

  5. core文件找不到了

    开始以为是core文件太大,设置ulimit -c unlimited  以后,再次访问,显示 ./a.out Segmentation fault (core dumped) 但是却找不到这个文件的 ...

  6. hadoo namenode format 异常 java.net.UnknownHostException: localhost.localdomain: localhost.localdomain

    /etc/sysconfig/network换成你在hosts里设置的值 /etc/rc.d/init.d/network restart 重启网络 hostname后就会发现hostname变了,也 ...

  7. [BZOJ 2440] [中山市选2011] 完全平方数 【二分 + 莫比乌斯函数】

    题目链接:BZOJ - 2440 题目分析 首先,通过打表之类的方法可以知道,答案不会超过 2 * k . 那么我们使用二分,对于一个二分的值 x ,求出 [1, x] 之间的可以送出的数有多少个. ...

  8. PHP 如何安全的使用 MySQL ?

    大多数 PHP 程序员对 MySQL 肯定不陌生,至于各种 MySQL 函数的用法在开发手册和 w3school 这类网站上也有很多介绍.但是,你所用的写法真的安全吗?面对越来越猖獗的黑客攻击,SQL ...

  9. 安装drupal练习网站遇到的问题

    1 Skip #conjunction key in __clone() method of core/includes/database/query.inc 解决方案:https://www.dru ...

  10. poj3580

    区间操作的究极题,我们一个个来分析其实只有insert,delete,revolve三种没讲过insert 先把x旋到根,一开始我比较SB的,准备把新节点插入到右子树的最左节点,这显然很烦 好的方法是 ...