zoukankan      html  css  js  c++  java
  • poj3461 字符串匹配 熟悉kmp算法第一题

     题意:  计算一个字符串在另一个字符串中出现的次数.
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int maxl=1000000+10;
     6 const int maxw=10000+10;
     7 char t[maxl],p[maxw];
     8 int T,ans,f[maxw];
     9 void getfail(char *p)
    10 {
    11     int m=strlen(p);
    12     f[0]=0;f[1]=0;
    13     for(int i=1;i<m;i++)
    14     {
    15         int j=f[i];
    16         while(j&&p[j]!=p[i])  j=f[j];
    17         f[i+1]= p[j]==p[i] ?  j+1:0;
    18     }
    19 }
    20 void kmp(char *t,char *p)
    21 {
    22     int n=strlen(t),m=strlen(p);
    23     getfail(p);
    24     int j=0;
    25     for(int i=0;i<n;i++)
    26     {
    27         while(j&&p[j]!=t[i])  j=f[j];//printf("--
    ");}
    28         if(p[j]==t[i])  j++;
    29         if(j==m)  ans++;
    30     }
    31 }
    32 int main()
    33 {
    34   //  freopen("in.txt","r",stdin);
    35     scanf("%d",&T);
    36     while(T--)
    37     {
    38         ans=0;
    39         scanf("%s%s",p,t);
    40         kmp(t,p);
    41         printf("%d
    ",ans);
    42     }
    43     return 0;
    44 }
    View Code
  • 相关阅读:
    poi管道流的导入导出
    Mysql导入数据库的方法
    MySQL数据库指定字符集
    eclipse 的操作
    Mysql的操作
    第十周作业
    第九周作业
    第八周作业
    第七周作业
    第六周作业
  • 原文地址:https://www.cnblogs.com/paulzjt/p/5790791.html
Copyright © 2011-2022 走看看