poj-2406-Power Strings-kmp

2014-11-24 08:50:53 · 作者: · 浏览: 0

利用kmp求next数组的思想对字符串进行模式匹配。

显而易见,最后一个字符的匹配到的位置到最后一个字符是一个循环节。

例如

匹配串: a b a c a b a c

next数组:0 1 1 2 1 2 3 4

第8个字符的next数组的值为4;

则一个循环节为8-4+1=4;

总共的循环次数为8/4=2次;

#include
  
   
#include
   
     #include
    
      #include
     
       using namespace std; char str[1100000]; int next[1100000]; void chu() { int i=0,j=-1; next[i]=j; int len=strlen(str); while(i