POJ 2406 - Power String
2016-08-30 15:55
253 查看
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
Sample Output
题意:给出一个周期字符串,求出其中子字符串循环的次数。
把第一个子字符串作为模式,求出长度,再进行计算即可。
#include <cstdio>
#include <cstring>
char str[1000005];
int tar[1000005];
int main()
{
while (scanf("%s", str) != EOF)
{
if (strcmp(str, ".") == 0)
break;
int len = strlen(str);
tar[0] = 0;
int cur = -1;
for (int i = 1; i < len; ++i)
{
while (cur > 0 && str[cur+1] != str[i])
cur = -1;
if (str[cur+1] == str[i])
cur++;
tar[i] = cur;
}
if (len % (len - tar[len - 1] -1) == 0)
printf("%d\n", len / (len - tar[len - 1] - 1));
else
printf("1\n");
}
return 0;
}
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
题意:给出一个周期字符串,求出其中子字符串循环的次数。
把第一个子字符串作为模式,求出长度,再进行计算即可。
#include <cstdio>
#include <cstring>
char str[1000005];
int tar[1000005];
int main()
{
while (scanf("%s", str) != EOF)
{
if (strcmp(str, ".") == 0)
break;
int len = strlen(str);
tar[0] = 0;
int cur = -1;
for (int i = 1; i < len; ++i)
{
while (cur > 0 && str[cur+1] != str[i])
cur = -1;
if (str[cur+1] == str[i])
cur++;
tar[i] = cur;
}
if (len % (len - tar[len - 1] -1) == 0)
printf("%d\n", len / (len - tar[len - 1] - 1));
else
printf("1\n");
}
return 0;
}
相关文章推荐
- Power string(poj 2406)
- POJ 2406 Power String 后缀数组
- POJ 2406 Power String(KMP)
- POJ 2406 Power String(KMP)
- poj 2406 power string(KMP)
- 【坑爹卡时题】Poj 2406 Power String
- POJ-2406 Power string
- POJ-2406 Power string
- POJ 2406 Power String 最短循环子串 - KMP
- POJ 2406 Power String
- 【poj-2406-powerstring】(KMP)
- poj 2406 Power String (KMP)
- Poj 2406 Power Strings
- POJ -2406 power strings --KMP中next函数的应用
- POJ 2406-Power Strings (KMP)
- (字符串的模式匹配4.7.18)POJ 2406 Power Strings(求一个字符串的最小重复串)
- poj 2406 Power Strings(KMP || 暴力)
- POj 2406 Power Strings
- |poj 2406|KMP|Power Strings
- poj 2406 Power Strings KMP