POJ 1961 Period
来源:互联网 发布:女排五连冠 知乎 编辑:程序博客网 时间:2024/06/05 06:00
题目大意:找到前N个前缀中有几个重复出现的子串。kmp算法。。。找前缀的重复子串。
Period
Time Limit: 3000MS Memory Limit: 30000KTotal Submissions: 11446 Accepted: 5326
Description
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
#include <stdio.h>#include <string.h>#include <string>#include <iostream>#include <cmath>#include <map>using namespace std;const int M = 1001000;string str;int f[M];int main(){ int n, case1 = 1; while(cin >>n) { if(n == 0) break; cin >>str; f[0] = 0; f[1] = 0; for(int i = 1; i < n; i++) { int j = f[i]; while(j && str[i] != str[j]) j = f[j]; if(str[i] == str[j]) f[i+1] = j+1; else f[i+1] = 0; } printf("Test case #%d\n",case1++); for(int i = 2; i <= n; i++) { if(f[i] > 0 && i%(i-f[i]) == 0) cout <<i<< ' '<<i/(i-f[i])<<endl; } cout <<endl; } return 0;}
- POJ 1961 Period
- poj 1961 Period
- POJ 1961 Period
- poj 1961 Period
- poj 1961:Period (KMP)
- POJ-1961 Period
- poj 1961 Period
- POJ 1961 - Period
- poj 1961 Period
- poj 1961Period
- poj 1961 Period (KMP)
- POJ-1961-Period
- POJ 1961 Period
- poj 1961 Period
- poj 1961 Period
- poj 1961 Period---kmp
- POJ 1961 Period
- poj-Period-1961
- JMS ActiveMQ 对应关系
- 浮点数float是如何在计算机里存储的?
- 20131010配置hadoop集群遇到namespaceID、storage ID、防火墙的问题
- Active X 控件
- dede5.7添加栏目缩略图
- POJ 1961 Period
- isKindOfClass, isMemberOfClass的区别
- PMP考试心得
- 摄像头旋转问题
- Android简单文件浏览器源代码
- 为什么通过sp & ~(THREAD_SIZE - 1) 可以获取到 task_struct
- UITableView多选删除,类似mail中的多选删除效果
- 定义JQuery插件
- MoreWindows 专注于Windows编程 白话经典算法系列之六 快速排序 快速搞定