您的位置:首页 > 其它

Match:Power Strings(POJ 2406)

2016-02-04 14:47 435 查看
                


                字符串前缀的阶

  题目大意:求前缀的阶

  和POJ1961是一样的,KMP的Next数组的应用,不要用STL,不要一个一个读入字符(IO永远是最慢的)

  

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

using namespace std;

static char Text[1000002];
static int _Next[1000002];

void Input(int &);
void Get_Next(const int);

int main(void)
{
int Length, k_count, if_res;
while (1)
{
Input(Length);
if (*Text == '.')//小数点结束
break;
Get_Next(Length);
if_res = Length % (Length - _Next[Length]);
k_count = Length / (Length - _Next[Length]);
if (if_res == 0 && k_count > 1)
printf("%d\n", k_count);
else printf("1\n");
}
return EXIT_SUCCESS;
}

void Input(int &Length)
{
Length = 0;
scanf("%s", Text);
Length = strlen(Text);
}

void Get_Next(const int Length)
{
int i = 0, k = -1;
_Next[0] = -1;

while (i < Length)
{
if (k == -1 || Text[i] == Text[k])
{
i++;
k++;
_Next[i] = k;
}
else k = _Next[k];
}
}


  


  另外以这一题有散列做法,但是没有kmp那么快,但是用到了一个非常好的算法——矩阵快速幂,以后补
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: