hdu 2087剪花布条

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087

思路:正常KMP求解aaaaaa  aa得到的结果是6,这题是3。仅仅改一点代码就行

当匹配完之后将j=m_next[j]改为j=0即可,见代码

  1 #include<cstdio>  
  2 #include<iostream>  
  3 #include<algorithm>
  4 #include<math.h> 
  5 #include<string.h>  
  6 #include<vector> 
  7 #include<queue>
  8 #include<iterator>
  9 #include<vector>
 10 #include<set>
 11 #define dinf 0x3f3f3f3f
 12 typedef long long ll;
 13 //const int Max=(1<<16)+10;
 14 using namespace std;
 15 #define SIZE 100000005
 16 
 17 const int N = 100000005;
 18 int m_next[N];
 19 char S[N],T[N];
 20 int slen, tlen;
 21 
 22 void getNext()
 23 {
 24     int j, k;
 25     j = 0; k = -1; m_next[0] = -1;
 26     while(j < tlen)
 27         if(k == -1 || T[j] == T[k])
 28             m_next[++j] = ++k;
 29         else
 30             k = m_next[k];
 31 
 32 }
 33 /*
 34 返回模式串T在主串S中首次出现的位置
 35 返回的位置是从0开始的。
 36 */
 37 int KMP_Index()
 38 {
 39     int i = 0, j = 0;
 40     getNext();
 41 
 42     while(i < slen && j < tlen)
 43     {
 44         if(j == -1 || S[i] == T[j])
 45         {
 46             i++; j++;
 47         }
 48         else
 49             j = m_next[j];
 50     }
 51     if(j == tlen)
 52         return i - tlen+1;
 53     else
 54         return -1;
 55 }
 56 /*
 57 返回模式串在主串S中出现的次数
 58 */
 59 int KMP_Count()
 60 {
 61     int ans = 0;
 62     int i, j = 0;
 63 
 64     if(slen == 1 && tlen == 1)
 65     {
 66         if(S[0] == T[0])
 67             return 1;
 68         else
 69             return 0;
 70     }
 71     getNext();
 72     for(i = 0; i < slen; i++)
 73     {
 74         while(j > 0 && S[i] != T[j])
 75             j = m_next[j];
 76         if(S[i] == T[j])
 77             j++;
 78         if(j == tlen)
 79         {
 80             ans++;
 81             j = 0;
 82         }
 83     }
 84     return ans;
 85 }
 86 int main()
 87 {
 88     int TT;
 89     int i, cc;
 90     string str;
 91     //cin>>TT;
 92     //while(TT--)
 93     //{
 94         //getchar();
 95     
 96         while(~scanf("%s",&S))
 97         {
 98             if(S[0]!='#')
 99             {
100                 scanf("%s",&T);
101                 slen = strlen(S);
102                 tlen = strlen(T);
103                 printf("%d
",KMP_Count());
104             }
105             else
106                 break;
107       
108         }
109         
110     //}
111     return 0;
112 }
原文地址:https://www.cnblogs.com/pter/p/5691846.html