poj1961 KMP

来源:互联网 发布:毒品网络 百度云 编辑:程序博客网 时间:2024/06/06 09:07

题目 http://poj.org/problem?id=1961

题目大意 输出每个位置的最大幂,其实和poj2406是一样的。

考察点 KMP

思路 :由于s = s1 ^ n;因此len[ len[s] -next[len[s]] ] = len[s1];  len[s] /len[s1]便是答案。

由于next中记录的是最大的真前缀。故,next中包含字串s1的最大重叠数,也就是说,因为next[i]记录的是到i的最大真前缀,因此, len[i] –next[i]就是最小的独立体,独立题越小,幂就会越大的。

提交情况 wrong answer9原因 test写成了 text

                           Time limit error 1 次, 没有用模, 而是扫描

                           Accepted 1

用了next数组,个人觉得和pre数组没有区别。Next[i] 表示的是前I –1个中最大的真前缀,pre[i] 是前i个的最大真前缀,所以认为没区别,,,,,,,,,,

 

ACcode

#include <stdio.h>

#include <string.h>

#define MAXN 1210001

 

charP[MAXN];

intnext[MAXN];

 

voidGet_next(char P[],int m){

   int i, j;

   next[0] = -1;

   for(i = 0, j = -1;  i < m;i ++, j ++, next[i] = j)

      while(j > -1&& P[i] != P[j])

          j = next[j];

}

 

int main(){

   int m, i, CASE = 0;

   while(~scanf("%d", &m), m){

      scanf("%s", P);

      Get_next(P, m);

      printf("Test case #%d\n",++CASE);

      for(i = 2; i <= m; i++)

          if((i % (i - next[i]) == 0)&& (i / (i - next[i])> 1)) printf("%d%d\n", i, i / (i - next[i]));

      printf("\n");

   }

   return 0;

}

 

  

原创粉丝点击