1、传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2725

  题目大意:找一个串在另一个串中出现的次数

  题解:kmp(纯裸题)

  

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define maxn 1000100
 5 int n,fix,ans,i,lens,lent;
 6 char s[maxn],t[maxn];
 7 int next[maxn];
 8 void getnext()
 9 {
10     fix=0;
11     for (i=2; i<=lent; i++)
12     {
13         while(fix && t[fix+1]!=t[i]) fix=next[fix]; 
14         if (t[fix+1]==t[i]) fix++;
15         next[i]=fix;
16     }
17 }
18 int KMP()
19 {
20     int count;
21     fix=0; count=0;
22     for (int i=1; i<=lens; i++)
23     {
24         while (fix && t[fix+1]!=s[i]) fix=next[fix];
25         if (t[fix+1]==s[i]) fix++;
26         if (fix==lent)
27         {
28             count++;
29             fix=next[fix];
30         }    
31     }
32     return count;
33 }
34 int main()
35 {
36     int z;
37     scanf("%d",&z);
38     for (int zz=1; zz<=z; zz++)
39     {
40         scanf("%s",t+1);
41         scanf("%s",s+1);
42         lens=strlen(s+1); 
43         lent=strlen(t+1);
44         memset(next,0,sizeof(next));
45         getnext();
46         ans=KMP();
47         printf("%d\n",ans);
48     }
49     return 0;
50 }
View Code

相关文章:

  • 2022-03-04
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2021-10-11
  • 2021-05-27
  • 2021-09-07
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-14
  • 2022-02-09
  • 2021-08-31
  • 2022-01-12
  • 2021-11-28
相关资源
相似解决方案