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.
 
题目大意:给一个字符串,问这个字符串是否能由另一个字符串重复R次得到,求R的最大值。
思路:对于一个字符串,如abcd abcd abcd,由长度为4的字符串abcd重复3次得到,那么必然有原字符串的前八位等于后八位。
也就是说,对于某个字符串S,长度为N,由长度为n的字符串s重复R次得到,当R≥2时必然有S[1..N-n]=S[n+1..N]。
那么对于KMP算法来说,就有fail[N]=N-n。此时n肯定已经是最小的了。
然后只需要判断N是否n的倍数,是则输出N/n即可。否则输出1。
 
代码(157MS):
 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = ; void getFail(char *P, int m, int f[]) {
f[] = f[] = ;
for(int i = ; i < m; ++i) {
int j = f[i];
while(j && P[i] != P[j]) j = f[j];
f[i + ] = (P[i] == P[j] ? j + : );
}
} char s[MAXN];
int fail[MAXN], n; int main() {
while(scanf("%s", s) != EOF) {
if(*s == '.') break;
n = strlen(s);
getFail(s, n, fail);
if(n % (n - fail[n]) == ) printf("%d\n", n / (n - fail[n]));
else puts("");
}
}

POJ 2406 Power Strings(KMP)的更多相关文章

  1. poj 2406:Power Strings(KMP算法,next[]数组的理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30069   Accepted: 12553 D ...

  2. poj 2406 Power Strings(KMP入门,next函数理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 37685   Accepted: 15590 D ...

  3. poj 2406 Power Strings(kmp循环节)

    题目链接:http://poj.org/problem?id=2406 题目大意:如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]. 例如:      a    b  ...

  4. poj 2406 Power Strings(kmp求一个串的重复子串)

    题意:重复子串次数 思路:kmp #include<iostream> #include<stdio.h> #include<string.h> using nam ...

  5. poj 2406 Power Strings(kmp next的应用)

    题目链接:http://poj.org/problem?id=2406 题意:就是求一个字符串最多有几个相同的小字符串组成. 题解:直接求一下next然后找到长度,长度就是len-1-next[len ...

  6. poj 2406 Power Strings(kmp应用)

    题目链接:http://poj.org/problem?id=2406 题意:给出一个字符串s,求重复子串出现的最大次数. 分析:kmp的next[]数组的应用. 要求重复子串出现的最大次数,其实就是 ...

  7. poj2406 Power Strings(kmp)

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

  8. 【POJ2406】 Power Strings (KMP)

    Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...

  9. Power Strings(KMP)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 45008   Accepted: 18794 D ...

随机推荐

  1. IntelliJ IDEA安装后需要必须做的一件事

    把Alt+斜杆 删除 Ctrl+空格修改成 Alt+斜杆 Ctrl+空格用过输入法的人都应该知道为什么要做上面一件事

  2. H5——简易马祖

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. Java(Java SE7) 体系结构图

    原文:https://docs.oracle.com/javase/7/docs/

  4. MySql基本使用方法

    一,基本概念 1, 常用的两种引擎:         (1) InnoDB        a,支持ACID,简单地说就是支持事务完整性.一致性:         b,支持行锁,以及类似ORACLE的一 ...

  5. Linux 小知识翻译 - 「代理服务器」

    这回聊聊「代理服务器」. 在公司里,不通过代理服务器无法连接互联网的,由于代理服务器的原因,有些服务的使用是受到限制的. 有人可能会觉得为什么会存在这种东西?(这里指代理服务器) Proxy本来的意思 ...

  6. Fluentd初探 简介与安装

    Fluentd是一个开源的数据收集器,专为处理数据流设计,有点像 syslogd ,但是使用JSON作为数据格式.它采用了插件式的架构,具有高可扩展性高可用性,同时还实现了高可靠的信息转发. 据分(Y ...

  7. C# - 汉字与unicode之间的转换

    /// <summary> /// 字符串转Unicode码 /// </summary> /// <returns>The to unicode.</ret ...

  8. Django 中的 日志处理

    日志处理: 上线后必须使用 便于以后的 维护 管理 根据日志 处理 BUG 在 项目中 定义一个 存放日志的 文件夹 log 存放所有 等级 的 日志 配置: 将下面的日志的 配置 写入 django ...

  9. UVA1620-Lazy Susan(思维+逆序对)

    Problem UVA1620-Lazy Susan Accept: 81  Submit: 375Time Limit: 3000 mSec Problem Description There ar ...

  10. day03-课堂笔记-大纲

    字典: # 字典循环: dic.keys() | dic.values() | dic.items()for k, v in dic.items():    print(k, v)        # ...