POJ Oulipo(KMP模板题)

题意:找出模板在文本串中出现的次数

思路:KMP模板题

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii (pair<int, int>)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

const int maxt = 1000000 + 100;
const int maxp = 10000 + 100;
//const int INF = 0x3f3f3f3f;

char P[maxp], T[maxt];
int f[maxp];

void getFail() {
	int m = strlen(P);
	f[0] = 0; f[1] = 0;
	for(int i = 1; i < m; i++) {
		int j = f[i];
		while(j && P[i]!=P[j]) j = f[j];
		f[i+1] = P[i]==P[j] ? j+1 : 0;
	}
}

int find_p() {
	int ans = 0;
	int n = strlen(T), m = strlen(P);
	getFail();
	int j = 0;
	for(int i = 0; i < n; i++) {
		while(j && P[j]!=T[i]) j = f[j];
		if(P[j] == T[i]) j++;
		if(j == m) ans++, j=f[j];
	}
	return ans;
} 

int main() {
    //freopen("input.txt", "r", stdin);
	int t; cin >> t;
	while(t--) {
		scanf("%s%s", P, T);
		int ans = find_p();
		cout << ans << endl;
	}
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 09:01:07

POJ Oulipo(KMP模板题)的相关文章

POJ Oulipo KMP 模板题

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

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 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 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 1113 凸包模板题

上模板. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <utility> #include <stack> #include <queue> #include <map> #include

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

Number Sequence - HDU 1711(KMP模板题)

题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ============================================================================================== #include<stdio.h> #include<string.h> const int MAXM = 1e4+7; const int

poj 2752(拓展KMP模板题)

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24399   Accepted: 12731 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names

HDU 6153 A Secret(扩展KMP模板题)

A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others) Total Submission(s): 2523    Accepted Submission(s): 934 Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,w