Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 43514   Accepted: 18153

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.
 

KMP算法,next表示模式串如果第i位(设str[0]为第0位)与文本串第j位不匹配则要回到第next[i]位继续与文本串第j位匹配。则模式串第1位到next[n]与模式串第n-next[n]位到n位是匹配的。如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n]。对于一个串,如果abcdabc, 那么next[len]=3,那么len-next【len】就大于len/2,那么len%(len-next[len])!=0;而对于一个周期串ababab next[len]=4,此时len-next[len]应该等于最小串的长度,最小周期就可以用len%(len-next[len])是否为0来判断。

 
 
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int maxn = ;
char str[maxn];
int Next[maxn];
int len;
void get_next()
{
int j = -;
int i = ;
Next[] = ;
while(i < len)
{
if(str[i] == str[j] || j == -)
{
i++;
j++;
if(str[i] != str[j])
Next[i] = j;
else
Next[i] = Next[j];
}
else
j = Next[j];
}
} int main()
{
while(~scanf("%s",str))
{
if(str[] == '.')
break;
len =strlen(str);
get_next();
int ans = ;
if(len%(len-Next[len]) == )
ans = len/(len-Next[len]);
//for(int g = 0; g <= len; g++)
//{
// cout << Next[g] << ' ';
//}
//cout << endl;
cout << ans << endl;
}
return ;
}

POJ2406----Power Strings解题报告的更多相关文章

  1. poj2406 Power Strings(kmp)

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

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

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

  3. poj2406 Power Strings(kmp失配函数)

    Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...

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

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

  5. 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...

  6. 【LeetCode】555. Split Concatenated Strings 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...

  7. 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

  8. 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...

  9. 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 利用SOLR搭建企业搜索平台 之——MultiCore

    Solr Multicore 是 solr 1.3 的新特性.其目是一个solr实例,可以有多个搜索应用. 下面着手来将solr给出的一个example跑出来.这篇文章是基于<利用SOLR搭建企 ...

  2. c#快捷键大全

    转发:http://zhidao.baidu.com/question/444655283 直接贴出来吧(关于VS的): 快捷键 功能 CTRL + SHIFT + B生成解决方案 CTRL + F7 ...

  3. ubuntu下实现openerp 7使用nginx反正代理及绑定域名

    这里要记录一个nginx upstream实现反向代理的配置过程. 连接vps的ssh. 先安装nginx sudo apt-get install nginx 修改/etc/nginx/nginx. ...

  4. BZOJ 3631 松鼠的新家

    链剖. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  5. HDU 3555 Bomb (数位DP-记忆化搜索模板)

    题意 求区间[1,n]内含有相邻49的数. 思路 比较简单的按位DP思路.这是第一次学习记忆化搜索式的数位DP,确实比递推形式的更好理解呐,而且也更通用~可以一般化: [数位DP模板总结] int d ...

  6. 【英语】Bingo口语笔记(38) - See系列

    see somebody off 送别 see somebody out 看到谁从哪里出来

  7. Eclipse实用快捷键

    经典常用快捷键1. [ALT+/]此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ALT+/]快捷键带来的好处吧. 2. ...

  8. JFinal 部署在 Tomcat 下推荐方法(转)

    首先明确一下 JFinal 项目是标准的 java web 项目,其部署方式与普通 java web 项目没有任何差别.Java Web 项目在 Tomcat 下部署有一些不必要的坑需要避免,所以撰写 ...

  9. 戴维·卡梅伦(David William Donald Cameron)经典语录

    戴维·威廉·唐纳德·卡梅伦(英语:David William Donald Cameron,1966年10月9日-),汉化译名为甘民乐.现任英国首相.第一财政大臣.公务员事务部部长和保守党党魁,也是英 ...

  10. php 使用phpmailer 发送邮件(附带中文乱码的解决方法)

    下载phpmailer ,在程序里包含class.phpmailer.php 类  ,这里有中文乱码的解决方法 实例代码如下 <html> <head> <title&g ...