zoukankan      html  css  js  c++  java
  • C

    解题思路:

          求不重叠的匹配次数。

    参考代码:

     1 #include <iostream>
     2 #include <vector>
     3 #include <map>
     4 #include <string>
     5 #include <queue>
     6 #include <stack>
     7 #include <set>
     8 #include <algorithm>
     9 
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <cmath>
    13 #include <cstdlib>
    14 using namespace std;
    15 
    16 const int INF=0x3f3f3f3f;
    17 const int SIZE=10000;
    18 typedef long long LL;
    19 
    20 string a,b;
    21 int nextt[1000005];
    22 void nexxt()
    23 {
    24     memset(nextt,0,sizeof(nextt));
    25     int j=0,k=-1;
    26     nextt[0]=-1;
    27     int len=b.size();
    28     while(j<len)
    29     {
    30         if(k==-1||b[j]==b[k])
    31         {
    32             ++k;
    33             ++j;
    34             if(b[j]!=b[k])
    35                 nextt[j]=k;
    36             else
    37                 nextt[j]=nextt[k];
    38         }
    39         else
    40             k=nextt[k];
    41     }
    42 }
    43 
    44 int kmp()
    45 {
    46     int i=0,j=0,ans=0;
    47     int la=a.size(),lb=b.size();
    48     while(i<la)
    49     {
    50         if(j==-1||a[i]==b[j])
    51         {
    52             i++;
    53             j++;
    54            // printf("i:%d j:%d
    ",i,j);
    55         }
    56         else
    57             j=nextt[j];
    58         if(j==lb)
    59         {
    60             j=0;
    61             ans++;
    62         }
    63 
    64     }
    65     return ans;
    66 
    67 }
    68 
    69 int main()
    70 {
    71     while(cin>>a)
    72     {
    73          if(a=="#") break;
    74          cin>>b;
    75          nexxt();
    76          printf("%d
    ",kmp());
    77     }
    78     return 0;
    79 }
    View Code
    まだまだだね
  • 相关阅读:
    treap模板
    Codeforces Round #446 (Div. 2)
    BZOJ 1001: [BeiJing2006]狼抓兔子 (最小割)
    NOIP2017总结
    Python 操作 Mysql 模块
    poj 3660 Cow Contest (传递闭包)
    poj 1964 Cow Cycling(dp)
    poj 3671 Dining Cows (Dp)
    cogs 线型网络(状压dp)
    codevs 2800 送外卖(状压dp)
  • 原文地址:https://www.cnblogs.com/xxQ-1999/p/7522488.html
Copyright © 2011-2022 走看看