现在的位置: 首页 > 综合 > 正文

POJ-2406 Power Strings

2013年01月20日 ⁄ 综合 ⁄ 共 539字 ⁄ 字号 评论关闭

题目链接:http://poj.org/problem?id=2406

解题思路:

水题。。。就是求模式串中的子串循环次数。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
#define N 1000010

char s[N];
int nextval[N];
int len;

void getnext(const char *s)
{
	int i = 0, j = -1;
	nextval[0] = -1;
	while(i != len)
	{
		if(j == -1 || s[i] == s[j])
			nextval[++i] = ++j;
		else
			j = nextval[j];
	}
}

int main()
{
	int length, add;
	while(scanf("%s", s) && s[0] != '.')
	{
		len = strlen(s);
		getnext(s);
		length = len - nextval[len]; //循环节的长度
		if(len != length && len % length == 0) //循环多次
			printf("%d\n", len / length);
		else
			printf("1\n");
	}
	return 0;
}

抱歉!评论已关闭.