HDU 1358 Period
来源:互联网 发布:jquery easing.js下载 编辑:程序博客网 时间:2024/06/05 18:19
题目链接:[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher E - Period
题意
给一字符串,求其所有完整循环的前缀与循环节的长度。
例:aaa
长度2前缀,循环节为a,个数为2
长度3前缀,循环节为a,个数为3
思路
kmp求出字符串前后缀重复数,遍历所有前缀子串进行下面操作:
字符串前后缀重复数next[L],则循环节的长度为L-L%next[L],如果L%循环节长度为0,则说明是完整循环,输出解。
代码
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <vector>using namespace std;char a[1000009];int p[1000009];void init(int m){ memset(p, 0, sizeof(p)); for(int i=1, k=0; i<m; i++) { while(k>0 && a[i]!=a[k]) k = p[k-1]; if(a[i] == a[k]) k++; p[i] = k; }}void solve(int n){ init(n); for(int i=1; i<n; i++) { int l = i+1-p[i]; if(l != i+1 && (i+1)%l == 0) printf("%d %d\n", i+1, (i+1)/l); }}int main(){ int n, T = 1; while(~scanf("%d", &n) && n) { scanf("%s", a); printf("Test case #%d\n", T++); solve(n); printf("\n"); } return 0;}
0 0
- HDU 1358 Period (KMP)
- HDU 1358 Period
- hdu 1358 Period
- hdu 1358 Period
- hdu 1358 Period
- HDU 1358 Period KMP
- hdu 1358 Period
- hdu 1358 Period KMP
- hdu 1358 Period(KMP)
- HDU 1358 Period KMP
- hdu 1358 Period
- hdu 1358 Period
- 【HDU】1358 Period KMP
- HDU - 1358 Period
- HDU 1358 Period (KMP)
- HDU 1358 Period
- hdu 1358 Period
- HDU 1358 Period
- jQuery22(替换元素,包裹元素)
- 设计模式六大原则
- Insertion Sort and Merge Sort
- jQuery23(获取radiobuttomvalue及选中)
- OpenvSwitch Port Mirror in OpenStack Neutron
- HDU 1358 Period
- NHibernate的几种查询方式
- 那年声明理解不了定义与初始化(一)
- 01背包问题一维空间优化的理解
- 开头很艰难,但也要写下去
- apache commons io 2.2(二)Filter部分
- 算法导论例程——计数排序
- #ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #endif
- C++大牛的博客