
数据结构之KMP算法---poj2406---Power Strings
发布日期:2022-02-02 02:58:09
浏览次数:2
分类:技术文章
本文共 1253 字,大约阅读时间需要 4 分钟。
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代码实现:
#include#include #define max 1000001using namespace std;int next[max];char str[max];int getnext(char *str){
int i=0,j=-1;
int len=strlen(str);
next[0]=-1;
while(i
{
if(j==-1||str[i]==str[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
i=len-j;
if(len%i==0)
return len/i;
else
return 1;}int main(){
while(cin>>str)
{
if(str[0]=='.')break;
int ans=getnext(str);
cout<<
}
return 0;}
转载地址:https://blog.csdn.net/u010368749/article/details/19341149 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.249.68.14]2022年05月24日 17时57分17秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
最新文章
iPhone开发【七】常用控件之表TableView
2019-07-26 18:46:01
iPhone开发【六】常用控件之Picker View
2019-07-26 18:46:00
iPhone开发【五】常用控件之Slider(不使用xib构建UI)
2019-07-26 18:45:59
iPhone开发【四】常用控件之ActionSheet与AlertView
2019-07-26 18:45:58
iPhone开发【三】处理基本交互
2019-07-26 18:45:57
iPhone开发【二】重写HelloWorld(不使用XIB)
2019-07-26 18:45:56
iPhone开发【一】从HelloWorld开始
2019-07-26 18:45:55
IOS 入门
2019-07-26 18:45:54
Java Web HelloWorld!
2019-07-26 18:45:54
MyBatis
2019-07-26 18:45:53
Guice学习(一)
2019-07-26 18:45:53
ListView 动态设置高度问题
2019-07-26 18:45:52
清除应用程序所有缓存
2019-07-26 18:45:52
SQL入门语句
2019-07-26 18:45:51
新浪微博认证——应用程序支持新浪微博登陆
2019-07-26 18:45:51
Static Handler
2019-07-26 18:45:50
Android Axis2 KSOAP WebService SOAP
2019-07-26 18:45:50
慢慢的,我们长成了受困于数字的大人
2019-07-26 18:45:49
【-废弃-】自己的产品,已经有个初略的想法了。
2019-07-26 18:45:49
Android support V4 package
2019-07-26 18:45:48