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

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

Power Strings
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 31111   Accepted: 12982

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
题意:输出循环节最大的个数
 
思路:next函数推断一下就可以:
若n%(n-next[n])==0 ans = n/(n-next[n])
否则 ans = 1
 
#include 
#include
#include
#include
#include
#include
#include
using namespace std;const int maxn = 1000000+10;int next[maxn];char str[maxn];int n;void getNext(){ next[0] = next[1] = 0; for(int i = 1,j; i < n; i++){ j = next[i]; while(j && str[i] != str[j]) j = next[j]; if(str[i]==str[j]) next[i+1] = j+1; else next[i+1] = 0; }}int main(){ while(~scanf("%s",str) && strcmp(str,".")!=0){ n = strlen(str); getNext(); int ans; if(n%(n-next[n])!=0) ans = 1; else ans = n/(n-next[n]); printf("%d\n",ans); } return 0;}

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

你可能感兴趣的文章
2.10环境变量PATH;2.11cp命令;2.12mv命令;2.13文档查看cat_more...
查看>>
mysql使用索引优化查询效率
查看>>
Salt Syndic配置
查看>>
优秀的开源系统恢复软件
查看>>
IE浏览器低版本判断及升级提示
查看>>
乳腺增生的早期症状?乳腺增生做什么检查最好
查看>>
java B2B2C springmvc mybatis仿淘宝电子商城系统-Hystrix服务容错
查看>>
android学习笔记2 单位
查看>>
[SQL Server][FILESTREAM] -- FileTable Feature in SQL Server 2012
查看>>
svn命令在linux下的使用
查看>>
dig 命令大全 linux
查看>>
我的友情链接
查看>>
在js中如何获取地址栏上的参数呢
查看>>
ELK日志分析单机系统详解
查看>>
MonkenRunner通过HierarchyViewer定位控件的方法和建议(Appium/UIAutomator/Robotium姊妹篇)...
查看>>
[开源] 轻量级移动设备即时通讯技术MobileIMSDK:Android客户端开发指南
查看>>
ceph上weight和reweight的区别
查看>>
那些微服务和技术堆栈教我们的事
查看>>
hadoop HDFS
查看>>
正则表达式:去除文本每行的首尾空格
查看>>