POJ 2406 Power Strings 求最小循环节数(KMP)
2017-07-25 20:06
495 查看
Power Strings
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 49380 Accepted: 20571
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
abcd
aaaa
ababab
.
Sample Output
1
4
3
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
思路:
简单题贴个链接
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N = 1000005; int nxt ; char s ; int len; void calcnext() { int i = 0, j = -1; nxt[0] = -1; while(i < len) { if(j == -1 || s[i] == s[j]) { i++, j++; nxt[i] = j; } else j = nxt[j]; } } int main(){ while( scanf("%s", s) > 0 ){ if( s[0] == '.' ) break; len = strlen(s); calcnext(); if( len % ( len - nxt[len] ) == 0 ) printf("%d\n", len / (len - nxt[len])); else printf("1\n"); } return 0; }
相关文章推荐
- poj2406--Power Strings(KMP求最小循环节)
- [KMP求最小周期]POJ 2406 Power Strings
- 【poj 2406】 Power Strings 【KMP 求最小循环节】
- poj 2406 Power Strings(kmp的nxt数组找最小循环节)
- 【KMP求最小循环节】POJ2406 Power Strings
- POJ 2406 Power Strings KMP 求最小循环节
- 【kmp+最小循环节】poj 2406 Power Strings
- POJ 2406 Power Strings(KMP+最小循环节)
- POJ 2406 Power Strings(KMP求最小循环节)
- poj 2406 Power Strings (KMP+最小循环节)
- POJ 2406 Power Strings【KMP求最小循环节/后缀数组】
- POJ 2406 Power Strings 1961的简化版,kmp的next数组的应用
- 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中的next数组)
- POJ-2406 Power Strings(KMP求重复子串出现的最大次数)