【KMP模板】POJ3461-Oulipo

【题意】

找出第一个字符串在第二个字符串中出现次数。

【注意点】

一定要先将strlen存下来,而不能每次用每次求,否则会TLE!

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 const int MAXN=1000000+50;
 7 const int MAXM=10000+50;
 8 char t[MAXN],p[MAXN];
 9 int next[MAXM];
10 int lent,lenp;
11
12 void getnext()
13 {
14     int i=0,j=-1;
15     next[i]=j;
16     while (i<lenp)
17     {
18         if (j==-1 || p[i]==p[j]) next[++i]=++j;
19             else j=next[j];
20     }
21 }
22
23 int getans()
24 {
25     int i=0,j=0,ans=0;
26     while (i<lent)
27     {
28         if (t[i]==p[j] || j==-1)
29         {
30             i++;
31             j++;
32         }
33         else j=next[j];
34         if (j==lenp)//如果匹配成功,则视作这次匹配失败,返回到上一次。
35         {
36             ans++;
37             j=next[j-1];
38             i--;
39         }
40     }
41     return ans;
42 }
43
44 int main()
45 {
46     int kase;
47     scanf("%d",&kase);
48     for (int i=0;i<kase;i++)
49     {
50         scanf("%s%s",p,t);
51         lent=strlen(t);
52         lenp=strlen(p);//注意一定要先把strlen存下来,否则会TLE!
53         getnext();
54         cout<<getans()<<endl;
55     }
56     return 0;
57 }
时间: 2024-10-24 23:32:02

【KMP模板】POJ3461-Oulipo的相关文章

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

[POJ 3461] Oulipo &amp; KMP模板

Oulipo Time Limit: 1000ms, Memory Limit: 65536K 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 from the book: Tout avait Pair normal, mai

poj 3461 Oulipo(KMP模板题)

题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23559   Accepted: 9437 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a

POJ Oulipo(KMP模板题)

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

HDU 1711 Number Sequence(KMP模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int maxn = 1000000+5; 6 7 int n,m; 8 9 int next[maxn]; 10 int a[maxn], b[maxn]; 11 12 void get_next() 13 { 1

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int

剪花布条---hdu2087(kmp模板)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 kmp模板题: #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define N 1100 char s1[N], s2[N]; int p[N], L1, L2; void Getp() { int i=0, j=-1; p[0] = -1; while(i

hdu 1686 KMP模板

1 // hdu 1686 KMP模板 2 3 // 没啥好说的,KMP裸题,这里是MP模板 4 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 10 using namespace std; 11 12 const int MAX_N = 1000008; 13 const int MAX_M = 10008; 14 char T

kmp算法(Oulipo)

http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html http://www.matrix67.com/blog/archives/115 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22468   Accepted: 8962 Description The French author Georges Perec (1936–

数据结构实验之串三:KMP应用(KMP模板)

数据结构实验之串三:KMP应用(KMP模板) AC_Code: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <stack> 7 using namespace std; 8 typedef long long ll; 9 int Nex[1000000]; 10