题目链接:https://vjudge.net/problem/POJ-2406

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 52631   Accepted: 21921

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

  1. abcd
  2. aaaa
  3. ababab
  4. .

Sample Output

  1. 1
  2. 4
  3. 3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

Source

KMP:

求字符串的最小循环节。

1) 如果字符串长度能被“初步的最小循环节”整除,那么这个就是他的最小循环节。

2) 如果字符串长度不能被“初步的最小循环节”整除,那么这个只是它通过补全后的最小循环节。而它实际的最小循环节为自己。

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10. #include <sstream>
  11. #include <algorithm>
  12. using namespace std;
  13. typedef long long LL;
  14. const double eps = 1e-;
  15. const int INF = 2e9;
  16. const LL LNF = 9e18;
  17. const int MOD = 1e9+;
  18. const int MAXN = 2e6+;
  19.  
  20. char x[MAXN];
  21. int Next[MAXN];
  22.  
  23. void get_next(char x[], int m)
  24. {
  25. int i, j;
  26. j = Next[] = -;
  27. i = ;
  28. while(i<m)
  29. {
  30. while(j!=- && x[i]!=x[j]) j = Next[j];
  31. Next[++i] = ++j;
  32. }
  33. }
  34.  
  35. int main()
  36. {
  37. while(scanf("%s", x) && strcmp(x, "."))
  38. {
  39. int len = strlen(x);
  40. get_next(x, len);
  41. int r = len-Next[len]; //求出最小循环节
  42. if(len%r) //如果长度/最小循环节除不尽,则对于此字符串来说,实际的最小循环节是自己
  43. printf("1\n");
  44. else
  45. printf("%d\n", len/r);
  46. }
  47. }

后缀数组:

POJ2406 Power Strings —— KMP or 后缀数组 最小循环节的更多相关文章

  1. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

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

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

  3. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

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

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

  5. Power Strings POJ - 2406 后缀数组

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...

  6. POJ2406 Power Strings(KMP)

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

  7. POJ2406 Power Strings KMP算法

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

  8. Codeforces Round #146 (Div. 1) C - Cyclical Quest 后缀自动机+最小循环节

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  9. 【kmp算法】poj2406 Power Strings

    如果next[n]<n/2,一定无解. 否则,必须要满足n mod (n-next[n]) = 0 才行,此时,由于next数组的性质,0~n-next[n]-1的部分一定是最小循环节. [ab ...

随机推荐

  1. Codeforces 553D Nudist Beach(二分答案 + BFS)

    题目链接 Nudist Beach 来源  Codeforces Round #309 (Div. 1) Problem D 题目大意: 给定一篇森林(共$n$个点),你可以在$n$个点中选择若干个构 ...

  2. Python通用编程

    本文是Python通用编程系列教程,已全部更新完成,实现的目标是从零基础开始到精通Python编程语言.本教程不是对Python的内容进行泛泛而谈,而是精细化,深入化的讲解,共5个阶段,25章内容.所 ...

  3. ssm实现查看流程图

    jsp <!--显示数据列表--> <tbody id="TableData" class="dataContainer" datakey=& ...

  4. Silverlight 离线安装包

    直接下载地址 https://www.microsoft.com/getsilverlight/locale/en-us/html/Microsoft%20Silverlight%20Release% ...

  5. Xcode: This device is no longer connected error

    Quit the xcode and connect again will all right.

  6. 【redis】5.spring boot项目中,直接在spring data jpa的Repository层使用redis +redis注解@Cacheable直接在Repository层使用,报错问题处理Null key returned for cache operation

    spring boot整合redis:http://www.cnblogs.com/sxdcgaq8080/p/8028970.html 首先,明确一下问题的场景 之前在spring boot整合re ...

  7. 【Tensorflow】tf.argmax函数

    tf.argmax(input, axis=None, name=None, dimension=None) 此函数是对矩阵按行或列计算最大值   参数 input:输入Tensor axis:0表示 ...

  8. Proftpd快速搭建FTP服务器

    前言 在Linux系统中,FTP服务器软件有很多,都已经成熟,像vsftpd, wu-ftp, Pure-FTPd等.但这些软件安装配置起来都比较麻烦,搭建个人的FTP服务器,还是Proftpd比较简 ...

  9. C#中异常处理和Java的区别

    捕获异常,同样是try...catch...,这个完全一样: 抛出异常,同样是throw,这个完全一样: 函数抛出怎样的异常,Java中可以用throws定义,而C#中不用定义,相当于throws E ...

  10. Eclipse出现&quot;Running Android Lint has encountered a problem&quot;解决方式

    近期打开Eclipse的时候,总是发生这种一个错误:"Running Android Lint has encountered a problem".截图例如以下: . 可是Ecl ...