Power Strings

Time Limit: 3000MS

Memory Limit: 65536K

Total Submissions: 39291

Accepted: 16315

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

【思路】

KMP。

应用KMP算法中的失配函数,如果是一个周期串那么错位部分(n-f[n])一定是一个最小循环节(仔细想想f函数的意义),则答案为i/(n-f[n])。否则ans=1。

  时间复杂度为O(n)。

【代码】

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
  5. using namespace std;
  6.  
  7. const int maxn = +;
  8.  
  9. void getFail(char* P,int* f) {
  10. int m=strlen(P);
  11. f[]=f[]=;
  12. for(int i=;i<m;i++) {
  13. int j=f[i];
  14. while(j && P[i]!=P[j]) j=f[j];
  15. f[i+]=P[i]==P[j]?j+:;
  16. }
  17. }
  18.  
  19. char s[maxn];
  20. int f[maxn];
  21.  
  22. int main() {
  23. while(scanf("%s",s)== && s[]!='.') {
  24. getFail(s,f);
  25. int n=strlen(s);
  26. if(f[n]> && n%(n-f[n])==) printf("%d\n",n/(n-f[n]));
  27. else printf("%d\n",);
  28. }
  29. return ;
  30. }

poj2406 Power Strings(kmp失配函数)的更多相关文章

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

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

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

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

  3. POJ2406Power Strings[KMP 失配函数]

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 45005   Accepted: 18792 D ...

  4. POJ2406 Power Strings KMP算法

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

  5. POJ2406 Power Strings(KMP)

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

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

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

  7. poj2406 Power Strings(kmp)

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

  8. poj 2406 Power Strings kmp算法

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

  9. POJ 2406 Power Strings (KMP)

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

随机推荐

  1. 用WebStorm调试本地html(含嵌入的javascript).

    题外话: 以前很少能调试,甚至以为不能调试(好笨),后来我看了一本叫做<<Learning Three.js>>的一本书后,里面推荐有几种javascript的编辑工具,都蛮好 ...

  2. openURL的使用方法

    openURL的使用方法 openURL的使用方法: view plaincopy to clipboardprint? [[UIApplication sharedApplication] open ...

  3. (whh仅供自己参考)进行ip网络请求的步骤

    这个过程大致是这个样子: 1 添加通知 2 发送网络请求 里边有一个发送通知的操作 3 执行发送通知的具体操作 代码如下: 1 在VC添加通知 [[NSNotificationCenter defau ...

  4. [转]Delphi I/O Errors

    The following are the Windows API (and former DOS) IO errors, which are also the IO errors often ret ...

  5. Java线程(学习整理)--1--守护线程

    1.什么是守护线程? 今天老师讲解我才知道有守护线程这回事!原来守护线程经常存在于我们的身边,比如:一个免费的网页游戏,里面都会或多或少有些插入性的广告!! 一般情况下,我们不会去点击这些广告的,但是 ...

  6. linux负载均衡

    1.linux lvs nat实现负载均衡 添加两块网卡并开启路由管道 > /proc/sys/net/ipv4/ip_forward //开始路由管道 安装ipvsadm yum instal ...

  7. sass编译css(转自阮一峰)

    一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 本文总结了SASS的主要用法.我的目标是,有了这篇文章,日常的一 ...

  8. html5本地数据库(一)

    本地数据库 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important ...

  9. 关于点击空白关闭弹窗的js写法推荐?

    $(document).mouseup(function(e){ var _con = $(' 目标区域 '); // 设置目标区域 ){ // Mark 1 some code... // 功能代码 ...

  10. 基于BitNami for XAMPP进行Drupal7安装的教程(Win7平台)

    BitNami是一个开源项目,该项目产生的开源软件包可用于安装Web应用程序和解决方案堆栈,以及虚拟设备.BitNami 提供wordpress.joomla.drupal.bbpress等开源程序的 ...