Period next数组应用
来源:互联网 发布:外汇决策数据分析 编辑:程序博客网 时间:2024/06/05 12:07
For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.
Input
The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the
number zero on it.
number zero on it.
Output
For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.
Sample Input
3aaa12aabaabaabaab0
Sample Output
Test case #12 23 3Test case #22 26 29 312 4
这道题算是上一题的加强版,这里是对每一个i求它之前的power strings,所以就是比之前多一个循环
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;const int N = 1e6 + 3;char s[N];int n, next[N];void GetNext(){ int j = -1; next[0] = -1; for(int i = 1; i < n; i++) { while(j != -1 && s[j + 1] != s[i]) j = next[j]; if(s[j + 1] == s[i]) j++; next[i] = j; }}int main(){ int k = 1; while(~scanf("%d", &n) && n) { scanf("%s", s); GetNext(); printf("Test case #%d\n" , k++); for(int i = 2; i <= n; i++) { if(i % (i - next[i - 1] - 1) == 0 && i / (i - next[i - 1] - 1) > 1) printf("%d %d\n", i, i / (i - next[i - 1] - 1)); } printf("\n"); } return 0;}
阅读全文
0 0
- Period next数组应用
- Poj-1961 Period(next数组应用)
- 【next数组应用】HDU 1358 Period
- HDOJ1358 Period 【KMP next数组应用】
- HDOJ1358 Period (KMP next数组应用)
- Period +uvalive+next数组
- POJ 1961 Period KMP(next数组应用)
- HDOJ 1358.Period(KMP中next数组应用)
- POJ 1961 Period KMP next数组的应用
- [kmp算法next数组的应用][poj1961]Period
- HDU - 1358 Period(KMP next数组)
- Period(kmp简单的应用 next从零开始 )
- KMP及其next数组性质学习小记 Poj1961 Period
- 1358 Period hdu kmp的next数组运用
- POJ 1961 Period(KMP next数组巧用)
- HDOJ 1358 Period(KMP next数组运用)
- FZU - 1901 Period II(KMP next数组的理解)
- HDU 1358 Period(next数组+周期+循环节)
- Git 简易指南
- mysql(1):软件安装+基础指令使用
- Python数据分析学习笔记(一)
- nodejs借用管道实现流数据边取边处理
- java学习篇:连接mySQL(二)--自定义连接池
- Period next数组应用
- Eclipse历史版本下载
- Android 设计模式之Builder模式
- Linux之数据类型
- hibernate求和
- 原生js事件的绑定和解除
- 动态网站的搭建-学习笔记-阿里云服务器测试
- JavaScript 创建对象的模式
- java重要知识点