[POJ 3461] Oulipo【KMP】

  • 2018-02-20
  • 0
  • 0

Problem:

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, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
  • One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0

Source

BAPC 2006 Qualification

Solution:

本题是 KMP 字符串匹配的模板题。

字符串哈希解法请见: [POJ 3461] Oulipo【自然溢出hash】

【KMP 简介】

对于模式串 W 和文本 T,O(|W|+|T|) 找出 T 的子串为 W 的所有起始下标。

考虑暴力算法有很多重复比较,我们可以预处理出对于每一个 Wi 所对应的最大的 k (k < i),使得字符串 W1..k 与 Wi-k+1..i 匹配。我们将 k 序列称作 nxt[] 数组

那么当 Wj+1 与 Ti 失配时,由于此时 W1..j 与 Ti-j+1..i 匹配,而 W1..nxt[j] 与 Wj-nxt[j]+1..j 自匹配,所以 W1..nxt[j] 与 Ti-nxt[j]+1..i 必定匹配,即我们可以通过 j = nxt[j] 来避免重复计算。

【KMP 复杂度证明】

KMP 算法由以下两个操作组成:

  1. 成功匹配时: j++, 单次 O(1);
  2. 失配时: j = jmp[j], 单次 O(1);

根据势能分析法,由于 j ≥ 0 且 jmp[j] < j,所以 (2) 式执行次数 ≤ (1) 式执行次数。 而 (1) 式执行次数显然 ≤ |W|+|T|,所以总时间复杂度为 O(|W|+|T|)

Code: O(n(|W|+|T|)) [1684K, 110MS]

// KMP 字符串匹配 
// POJ 3461 Oulipo
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXWLEN = 10002;
const int MAXTLEN = 1000002;

int n, Wlen, Tlen, nxt[MAXWLEN];
char W[MAXWLEN], T[MAXTLEN];

int main(){
	scanf("%d", &n);
	while(n--){
		scanf("%s%s", W + 1, T + 1), Wlen = strlen(W + 1), Tlen = strlen(T + 1);
		nxt[1] = 0;
		for(register int i = 2, j = 0; i <= Wlen; i++){
			while(j && W[j + 1] != W[i]) j = nxt[j];  // 跳到失配前的状态 
			if(W[j + 1] == W[i]) j++;
			nxt[i] = j;  // 预处理最长相同前缀 
		}
		int ans = 0;
		for(register int i = 1, j = 0; i <= Tlen; i++){
			while(j && W[j + 1] != T[i]) j = nxt[j];  // 跳到失配前的状态 
			if(W[j + 1] == T[i]) j++;
			if(j == Wlen) ans++, j = nxt[j];
		}
		printf("%d\n", ans);
	}
	return 0;
} 

评论

还没有任何评论,你来说两句吧



常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
If the code cannot be displayed normally, please refresh the page.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai