zoukankan      html  css  js  c++  java
  • hdu 1358 KMP next数组的运用

    题意:给一个字符串,从第二个字符开始,判断前面的是不是循环串,是的话就输出当前位置和循环次数。

    考的是对于next数组的理解和灵活运用,字符编号从0开始,那么if(i%(i-next[i])==0),则i前面的串为一个循环串,其中循环子串出现i/(i-next[i])次。

     1 #include<iostream>
     2 #include<string>
     3 #include<string.h>
     4 #define MAX_N 1000005
     5 
     6 using namespace std;
     7 
     8 int len;
     9 string s;
    10 int nexxt[MAX_N];
    11 
    12 void getNext()
    13 {
    14     nexxt[0]=-1;
    15     int i = 0,j=-1;
    16     while(i<=len)
    17     {
    18         if(j==-1 || s[i]==s[j])
    19         {
    20             i++,j++;
    21             nexxt[i]=j;
    22         }
    23         else
    24             j=nexxt[j];
    25     }
    26 }
    27 int main()
    28 {
    29     int con = 1;
    30     while(cin>>len,len)
    31     {
    32         cin>>s;
    33         printf("Test case #%d
    ",con++);
    34         getNext();
    35         for(int i = 1; i <= len; i++)
    36         {
    37             if(i%(i-nexxt[i])==0 && i/(i-nexxt[i])>1)
    38                 cout<<i<<' '<<i/(i-nexxt[i])<<endl;
    39         }
    40         cout<<endl;
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    queue
    hiho1095(二分)
    uvaliva3942(trie树)
    hiho1014(trie树)
    uvalive4329(树状数组)
    Dropping tests POJ
    linux mysql命令
    linux文件系统和mount(硬盘,win分区,光驱,U盘)
    linux共享windows资料
    linux常用命令
  • 原文地址:https://www.cnblogs.com/Xycdada/p/7327926.html
Copyright © 2011-2022 走看看