博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2406 Power Strings(KMP)
阅读量:5914 次
发布时间:2019-06-19

本文共 1542 字,大约阅读时间需要 5 分钟。

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

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

abcdaaaaababab.

Sample Output

143

Hint

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

Source

 
 
kmp的经典应用
设$len$表示字符串的长度,$next[i]$表示$i$号字符串的最长公共前后缀的长度
如果$len \ mod  \  next[len] == 0$,那么循环节的长度为$n / next[len]$
 
#include
#include
using namespace std;const int MAXN = 1e6 + 10;char s[MAXN];int fail[MAXN];int main() {#ifdef WIN32 freopen("a.in", "r", stdin); //freopen("a.out", "w", stdout);#endif while(scanf("%s", s + 1) && s[1] != '.') { int N = strlen(s + 1), now = 0; for(int i = 2; i <= N; i++) { while(now && s[i] != s[now + 1]) now = fail[now]; if(s[i] == s[now + 1]) now++; fail[i] = now; } if(N % (N - fail[N]) == 0) printf("%d\n", N / (N - fail[N])); else printf("1\n"); } return 0;}

 

 
 

转载地址:http://elgpx.baihongyu.com/

你可能感兴趣的文章
动态Axios配置
查看>>
postgresql分区表
查看>>
NDK入门
查看>>
JavaScript对象的原型(prototype)、类(class)和可扩展性(extendible)
查看>>
SegmentFault × 简寻:为你推荐最合适的技术职位
查看>>
PHP-FPM,Nginx,FastCGI 之间的关系
查看>>
深入认识Redux(一)
查看>>
读Zepto源码之Selector模块
查看>>
JavaScript基础知识整理(1)
查看>>
简述JavaScript的垃圾回收机制
查看>>
Java反射机制详解
查看>>
阿里云服务器配置开发环境第六章:Centos7.3安装vsftpd
查看>>
从一次有趣的实验学习性能优化
查看>>
JavaScript专题之如何求数组的最大值和最小值
查看>>
如何简单快速地在电脑上关联多个git或者gitlab账号
查看>>
ASP.NET MVC - 附加类型“Model Name”的实体失败,因为相同类型的另一个实体具有相同的主键值。...
查看>>
我心目中的网络接口设计到底是怎样的过程?
查看>>
webpack-dev-server
查看>>
2017-07-04 前端日报
查看>>
python发送邮件
查看>>