zoukankan      html  css  js  c++  java
  • KMP模板

    比赛时发现憋不出模板啦

    这真是伤心 只写2次的东西真心记不住

    贴个以前的模板 多敲几遍

    POJ 3461 

      1 #include<cstdio>

     2 #include<iostream>
     3 #include<cstring>
     4 using namespace std;
     5 int T,la,lb;
     6 char a[1000000+1],b[10000+1];
     7 int to[10000+1];
     8 int main()
     9 {
    10     scanf("%d",&T);
    11     while(T--)
    12     {
    13         scanf("%s %s",b+1,a+1);
    14         la=strlen(a+1),lb=strlen(b+1);
    15  
    16         //to
    17         int j=0; to[1]=0;
    18         for(int i=2;i<=lb;i++)
    19         {
    20             while(j>0&&b[j+1]!=b[i]) j=to[j];
    21             if(b[j+1]==b[i]) j++;
    22             to[i]=j;
    23         }
    24         
    25         // 匹配
    26         int ans=0; j=0;
    27         for(int i=1;i<=la;i++)
    28         {
    29              while(j>0&&a[i]!=b[j+1]) j=to[j];
    30              if(a[i]==b[j+1]) j++;
    31              if(j==lb) ans++,j=to[j];
    32         }
    33         
    34         printf("%d ",ans);
    35     }
    36  
    37     return 0;
    38 }
    39  
  • 相关阅读:
    YL杯超级篮球赛 (Standard IO)
    Window (Standard IO)
    toj1026 Network 双连通分量
    poj3177 Redundant Paths 双连通分量
    poj1144 Network 双连通分量
    bzoj1269
    bzoj1800
    CF911D
    CF910C
    CF910B
  • 原文地址:https://www.cnblogs.com/ofsxb/p/5178940.html
Copyright © 2011-2022 走看看