hihoKMP & POj3461

记录下模板。

刚学了一下,看了别人的一份模板,自己打了一下,以后就用这个吧

 1 #include <cstdio>
 2 #include <cstring>
 3 char s[1000005],ss[10005];
 4 int next[10005],ans;
 5 void getNext(int len){
 6     int i = 0,j = -1;
 7     next[0] = j;
 8     while(i<len){
 9         while(j!=-1&&ss[j]!=ss[i])j = next[j];
10         i++;j++;
11         if(j>=len)next[i] = next[j-1];
12         else next[i] = j;
13     }
14 }
15 void KMP(int len,int m){
16     int i = 0,j = 0;
17     while(i<len){
18         while(j!=-1&&ss[j]!=s[i])j = next[j];
19         i++;j++;
20         if(j==m){
21             ans++;j = next[j];
22         }
23     }
24 }
25 int main()
26 {
27     int T;scanf("%d",&T);
28     while(T--){
29         scanf("%s%s",ss,s);
30         ans = 0;
31         getNext(strlen(ss));
32         KMP(strlen(s),strlen(ss));
33         printf("%d\n",ans);
34     }
35     return 0;
36 }
时间: 2024-10-12 22:15:41

hihoKMP & POj3461的相关文章

POJ3461 Oulipo 【KMP】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22295   Accepted: 8905 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote

POJ3461 Oulipo[KMP]【学习笔记】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36916   Accepted: 14904 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quot

POJ3461 Oulipo

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36441   Accepted: 14728 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quot

poj3461 Oulipo (KMP模板题~) 前面哪些也是模板题 O.O

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; char a1[1000010],a2[1000010]; int next[1000010]; int len1,len2,cot; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len1) { if(j==-1||a1[i]==a1[j]

POJ-3461 Oulipo(KMP)

题目链接:http://poj.org/problem?id=3461 题目大意: 给你两个字符串p和s,求出p在s中出现的次数. 思路:p在s中KMP匹配,匹配成功,再从next[last]的位置匹配即可,因为允许出现的两次有重叠的部分. //1208 KB 94 ms #include<cstdio> #include<iostream> #include<cstring> using namespace std; int n; char s[1000100],p[

poj3461 Oulipo (kmp)

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35732   Accepted: 14439 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quot

C++之路进阶——poj3461(Oulipo)

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35694   Accepted: 14424 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quot

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) 1

POJ3461 Qulipo(kmp)

传送门 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24124   Accepted: 9679 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A q