Power Strings(POJ-2406)(KMP简单循环节)
来源:互联网 发布:一直播如何挂淘宝链接 编辑:程序博客网 时间:2024/06/06 03:33
Power Strings
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 50983 Accepted: 21279
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.
题目链接:http://poj.org/problem?id=2406
题目大意:给出一个字符串 问它最多由多少相同的字串组成
如 abababab由4个ab组成
题目分析:要用到KMP中的next数组来计算最小循环节。
KMP最小循环节、循环周期:
定理:假设S的长度为len,则S存在最小循环节,循环节的长度L为len-next[len],子串为S[0…len-next[len]-1]。
(1)如果len可以被len - next[len]整除,则表明字符串S可以完全由循环节循环组成,循环周期T=len/L。
(2)如果不能,说明还需要再添加几个字母才能补全。需要补的个数是循环个数L-len%L=L-(len-L)%L=L-next[len]%L,L=len-next[len]。
讲解KMP循环节非常不错的博主 不懂得可以点进去。代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <vector>using namespace std;typedef long long LL;const int N=1000000+999;char s2[N];int nex[N];void makeNext(int m){ int i,j; nex[0] = 0; for (i = 1,j = 0; i < m; i++) { while(j > 0 && s2[i] != s2[j]) j = nex[j-1]; if (s2[i] == s2[j]) j++; nex[i] = j; }}int main(){ int n; while(scanf(" %s",s2)!=EOF) { if(s2[0]=='.') break; n=strlen(s2); memset(nex,0,sizeof(nex)); makeNext(n); int j=nex[n-1]; //1到n-1的最长前缀后缀相等长度 int ans=1; if(n%(n-j)==0) //判断是否能由循环节组成 ans=n/(n-j); //由几个循环节构成 printf("%d\n",ans); } return 0;}
阅读全文
0 0
- Power Strings(POJ-2406)(KMP简单循环节)
- POJ - 2406 Power Strings (KMP循环节)
- POJ - 2406 Power Strings(KMP 循环节)
- POJ 2406 Power Strings [KMP+循环节]
- POJ 2406 Power Strings(KMP循环节)
- poj 2406 Power Strings【kmp循环节】
- POJ 2406 Power Strings (KMP) kmp循环节
- poj 2406 Power Strings (KMP+最小循环节)
- POJ 2406 Power Strings KMP 求最小循环节
- POJ 2406 Power Strings(KMP:找串循环节)
- POJ 2406 Power Strings (KMP找字符串的循环节)
- POJ 2406 Power Strings(kmp循环节)
- POJ 2406 Power Strings(KMP 找最短循环节)
- POJ 2406 Power Strings(KMP求最小循环节)
- poj 2406 Power Strings(kmp循环节)
- 【POJ 2406】 Power Strings(KMP求循环节)
- POJ 2406 Power Strings [KMP+循环节规律]
- POJ 2406Power Strings kmp算法求循环节
- 反射应用进阶篇之自定义反射工具类在springmvc中的应用
- C++ #define
- python实现判断给定列表是否存在重复元素,且索引差小于k
- combobox实现下拉框及其值的获取
- 使用list方法插入datatable中的数据到数据库
- Power Strings(POJ-2406)(KMP简单循环节)
- 数据结构与算法之string篇
- sdut 数据结构实验之链表九:双向链表
- 怎么将本地文件上传到远程git仓库
- JAVA web连接MySQL报错添加SSL设置
- (TCP) 从服务器下载数据
- MFC UpdateData(FALSE)和UpdateData(TRUE)的区别
- Object Detection论文清单
- java设计模式