题目链接: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. ASP.NET Core 依赖注入基本用法

    ASP.NET Core 依赖注入 ASP.NET Core从框架层对依赖注入提供支持.也就是说,如果你不了解依赖注入,将很难适应 ASP.NET Core的开发模式.本文将介绍依赖注入的基本概念,并 ...

  2. 济南day1下午

    下午 预:60+100+30 实:30+30+30 T1水题(water) T1写了二分图匹配 听说有70分,写挫了.... 正解:贪心,按长度排序, 对于第一幅牌里面的,在第二个里面,找一个长度小于 ...

  3. maven项目对于maven远程仓库没有资源的解决办法

    下载资源到本地 方法一:dependency scope 是system 类型 <dependency> <groupId>edu.ucar</groupId> & ...

  4. 【webpack2】-- 入门与解析

    每次学新东西总感觉自己是不是变笨了,看了几个博客,试着试着就跑不下去,无奈只有去看官方文档. webpack是基于node的.先安装最新的node. 1.初始化 安装node后,新建一个目录,比如ht ...

  5. NMM3DViewer 设计

    在FrameworkInterfaces工程的INMM3DServer.cs中定义了 岩石材料结构 BlockMaterial  -----> StrBLOCKProperty     publ ...

  6. CNN网络--VGGNet

    Simonyan, Karen, and Andrew Zisserman. "Very deep convolutional networks for large-scale image ...

  7. C++简单介绍

    一.怎样用C++的源文件产生一个可运行程序 一个C++程序由一个或者多个编译单元组成.每一个编译单元都是一个独立的源码文件.一般是一个带.cpp的文件,编译器每次编一个文件编译单元,生成一个以.obj ...

  8. 10934 - Dropping water balloons(DP)

    这道题的思路非常难想. 问你须要的最少实验次数,这是非常难求解的.并且我们知道的条件仅仅有三个.k.n.实验次数 . 所以我们最好还是改变思路,转而求最高所能确定的楼层数 .  那么用d[i][j]表 ...

  9. mysql复制表命令

    http://hi.baidu.com/dwspider/item/908bf5e1746275bd2e140b03     上面命令是实现复制表的一种方法,缺陷就是索引等表信息不会复制过去,只是复制 ...

  10. 【git】强制覆盖本地代码

    [git]强制覆盖本地代码(与git远程仓库保持一致) 2018年04月27日 23:53:57 不才b_d 阅读数:21145   版权声明:本文为博主不才b_d原创文章,未经允许不得转载. || ...