您的位置:首页 > 其它

Match:Period(POJ 1961)

2016-02-03 23:03 375 查看
             


             Period

  题目大意:给定一个字符串,要你找到前缀重复了多少次

  思路,就是kmp的next数组的简单应用,不要修正next的距离就好了,直接就可以跳转了

  PS:喝了点酒用递归实现除法和取余了。。。结果tle不知道怎么回事。。。

  

#include <iostream>
#include <functional>
#include <algorithm>
#include <string>

using namespace std;

typedef int Positon;
void Get_Next(const int);

static int _next[1000002];
static char str[1000002];

int main(void)
{
int str_length, k_count, if_res;
int case_count = 1;

while (~scanf("%d", &str_length))
{
if (str_length == 0)break;
getchar();

scanf("%s", str);

Get_Next(str_length);
printf("Test case #%d\n", case_count++);
for (int i = 2; i <= str_length; i++)
{
k_count = i / (i - _next[i]);
if_res = i % (i - _next[i]);
if (if_res == 0 && k_count > 1)
printf("%d %d\n", i, k_count);
}
cout << endl;
}
return EXIT_SUCCESS;
}

void Get_Next(const int str_length)
{
Positon i = 0, k = -1;
_next[0] = -1;

while (i < str_length)
{
if (k == -1 || str[i] == str[k])
{
i++;
k++;
_next[i] = k;
}
else k = _next[k];
}
}


  


  这题用STL的string会卡很长的时间,很奇怪,加了std::ios::sync_with_stdio(false);都没用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: