kmp模版题 hdu2087

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4
 5 using namespace std;
 6
 7 int m,n;
 8 char s[1010];
 9 char t[1010];
10 int next1[1010];
11
12 void getnext()
13 {
14     next1[0]=-1;
15     int i=0;
16     int j=-1;
17     while(i<m)
18     {
19         while(j!=-1&&t[i]!=t[j])
20         {
21             j=next1[j];
22         }
23         next1[++i]=++j;
24     }
25 }
26
27 int kmp()
28 {
29     int ans=0;
30     int i=0;
31     int j=0;
32     getnext();
33     while(i<n)
34     {
35         while(j!=-1&&s[i]!=t[j])
36         {
37             j=next1[j];
38         }
39         i++;
40         j++;
41         if(j>=m)
42         {
43             ans++;
44             j=0;
45         }
46     }
47     return ans;
48 }
49
50 int main()
51 {
52     while(scanf("%s",&s)!=EOF)
53     {
54         if(s[0]==‘#‘)
55             break;
56         scanf("%s",&t);
57         n=strlen(s);
58         m=strlen(t);
59         cout<<kmp()<<endl;
60     }
61     return 0;
62 }

时间: 2024-08-07 04:33:07

kmp模版题 hdu2087的相关文章

kmp模版题 hud1711

1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 6 int n,m; 7 int t[100010]; 8 int s[1000010]; 9 int next1[100010]; 10 11 void getnext() 12 { 13 next1[0]=-1; 14 int i=0,j=-1; 15 while(i<m) 16 { 17 while(j!=-1&&t[

kmp模版题 hdu1686

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 char s[1000010]; 8 char t[10010]; 9 int next1[10010]; 10 int m,n; 11 12 void getnext() 13 { 14 next1[0]=-1; 15 int i=0; 16 int j=-1; 17 while(i&l

HDU1711 Number Sequence(KMP模版题)

匹配子串 #include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <cstdio> int a[1000005],b[10005]; int Next[10005]; int n,m; void setNext() { int i=0,j=

HDU 2896 病毒侵袭(AC自动机模版题)

AC自动模版题,中文题目就不叙述题意了啊. AC自动主要是构造出字典树之后找到fail指针的跳转,类似于KMP里面的next数组的跳转啊,注意这里是多模式跳转.意思就是这个串跳到下一个串的什么位置啊. 先帖一下,做多了再一起总结吧. 病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11347    Accepted Submi

UVa 12534 Binary Matrix 2 zkw费用流模版题

题目链接:点击打开链接 思路: 我们首先假设这个图都是全0的 用n个点代表行,m个点代表列 用源点向行连一个值x 表示每行1的个数,向列连一个y表示每列y个1 则若行i和列j之间流过一个流量就表示 (i,j) 点填了1 那么若原来图中(i,j)点为0 则花费就是1 若原图中(i,j)点是1,则花费是-1 如此枚举x跑个费用流就好了 ==居然把我多年的白书费用流坑掉了... zkw走起啊 #include <stdio.h> #include <string.h> #include

lct 模版题 bzoj 2002 2049

很早就有人给我推荐的模版题,然后我最近才刷的(' '    ) 昨天的tree 不知道比他们高到哪里去了,我和他谈笑风生啊! bzoj 2002 弹飞绵羊 重点:这道题的cut和link 由于这道题链的特殊性所以不能用提根的方法搞,可以注意到每一次cut位置一定是前面的一个元素,所以access 上去之后直接把左边的儿子丢掉就行了(我原来的cut 是在不知道两个点的儿子关系时就强行提根(' '    )) 然后link的时候直接把cut的那一棵splay接过去就行了 1 #include <io

HDU3966 Aragorn&#39;s Story(树链剖分 点权 模版题)

#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <map> #include <set> #include <vector> #include <cstdio> using namespace std; const int N=50010; s

POJ Oulipo(KMP模板题)

题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack&

poj 2299 Ultra-QuickSort 逆序对模版题

用树状数组求逆序对 唯一的坑点就是sum要用long long存 直接贴代码了 以后忘了还能直接看 2333…… PS:和hdu3743代码是一样的,因为两个都是逆序对模版题…… 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 int s[500005]; 6 int N; 7 struct num{ 8 int xuhao,num; 9 }n