题目链接: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

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

KMP:

求字符串的最小循环节。

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

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

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e6+; char x[MAXN];
int Next[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} int main()
{
while(scanf("%s", x) && strcmp(x, "."))
{
int len = strlen(x);
get_next(x, len);
int r = len-Next[len]; //求出最小循环节
if(len%r) //如果长度/最小循环节除不尽,则对于此字符串来说,实际的最小循环节是自己
printf("1\n");
else
printf("%d\n", len/r);
}
}

后缀数组:

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

    Description 小C在MC里有n个牧场,自西向东呈一字形排列(自西向东用1-n编号),于是他就烦恼了:为了控制这n个牧场,他需要在某些牧场上面建立控制站, 每个牧场上只能建立一个控制站,每个控 ...

  2. Ubuntu 16.04下使用Wine安装PowerDesigner15

    说明: 1.关于没有.wine文件夹的解决方法:在命令行上运行winecfg: 2.使用的Wine版本是深度出品(Deepin),已经精简了很多没用的配置,使启动能非常快,占用资源小. 下载: (链接 ...

  3. ORACLE MOS 翻译

    http://blog.csdn.net/msdnchina/article/details/53174196

  4. java判断日期与星期

    原文:http://www.open-open.com/code/view/1440592372888 import java.text.SimpleDateFormat; import java.u ...

  5. 邁向IT專家成功之路的三十則鐵律 鐵律十六:IT人交友之道-單純

    元曲知名的作家 白樸,曾在沉醉東風﹒漁夫一文創作中,寫道:「雖無刎頸交,卻有忘機友」.IT人交朋友應首重在單純而非廣泛,因為實際上越複雜的朋友圈,只會為你的工作以及生活帶來許多不必要的麻煩.至於男女朋 ...

  6. 理解Neural Style

    paperA Neural Algorithm of Artistic Style 在艺术领域,尤其是绘画,艺术家们通过创造不同的内容与风格,并相互交融影响来创立独立的视觉体验.如果给定两张图像,现在 ...

  7. xshell登陆腾讯云服务器

    2016-12-11   00:17:36 前段时间在同学的介绍下关注了一下腾讯云:然后里面有学生优惠可以拿到免费的域名和云服务器.所以感兴趣就实验了一下,今天中午抢到了“1元特惠的学生包”,里面有免 ...

  8. Android Studio调试工具总结

       前言:写代码不可避免有Bug.通常情况下除了日志最直接的调试手段就是debug.当我们的程序出现bug时,调试能够高速的找到bug. 进入调试状态.我们能够清晰的了解程序的整个运行过程,能够对内 ...

  9. npm升级所有可更新包

    使用npm管理node的包,可以使用npm update <name>对单个包升级,对于npm的版本大于 2.6.1,可以使用命令: npm install -g 升级全局的本地包. 对于 ...

  10. 在.NET中怎样取得代码行数

    文章目的 介绍在.NET中取得代码行数的方法 代码 [STAThread] static void Main(string[] args) { ReportError("Yay!" ...