Codeforces 1016B Segment Occurrences、KMP:【题解】

Segment Occurrences

time limit per test2 seconds
memory limit per test256 megabytes

Problem descrip

You are given two strings s and t, both consisting only of lowercase Latin letters.

The substring s[l…r] is the string which is obtained by taking characters sl,sl+1,…,sr without changing the order.

Each of the occurrences of string a in a string b is a position i (1≤i≤|b|−|a|+1) such that b[i…i+|a|−1]=a (|a| is the length of string a).

You are asked q queries: for the i-th query you are required to calculate the number of occurrences of string t in a substring s[li…ri].

Input

The first line contains three integer numbers n, m and q (1≤n,m≤103, 1≤q≤105) — the length of string s, the length of string t and the number of queries, respectively.

The second line is a string s (|s|=n), consisting only of lowercase Latin letters.

The third line is a string t (|t|=m), consisting only of lowercase Latin letters.

Each of the next q lines contains two integer numbers li and ri (1≤li≤ri≤n) — the arguments for the i-th query.

Output

Print q lines — the i-th line should contain the answer to the i-th query, that is the number of occurrences of string t in a substring s[li…ri].

input

10 3 4
codeforces
for
1 3
3 10
5 6
5 7

output

0
1
0
1

input

15 2 3
abacabadabacaba
ba
1 15
3 4
2 14

output

4
0
3

input

3 5 2
aaa
baaab
1 3
1 1

output

0
0

题目大意:第一行输入n, m, q 分别代表文本串的长度,模式串的长度和测试次数。输入文本串和模式串,设第三行后每行输入statr和end,让你找在两者之间出现过几次模式串。之前有一个题是寻找模式串在文本串中出现几次,简单修改一下kmp函数中的count和返回值即可,而这个题又增加了一点是:在start和end之间,模式串出现过几次。
所以,设立一个新数组net [ ],在每次输入start和end之后,将文本串的这个范围内的字符串赋值到net[ ],将net[ ] 当成新的文本串KMP就行了。
有一个小技巧是为net[ ]赋初值的问题:

char net[2000] = {'\0'};

这样,net的长度就不会出错了,就是存在字符的真正长度。

AC代码如下:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
char t[2000];
char p[2000];
int nxt[2000];
//建立公共前后缀表, /*****next[j] = k 代表p[j] 之前的模式串子串中,有长度为k 的相同前缀和后缀*****/
void GetNext(char p[], int nxt[]){	// 
	int i, j;
	int m = strlen(p);
	nxt[0] = 0;
	for(j = 1, i = 0; j < m; j++){	//前缀 i,从0开始 后缀j,从1开始 
		while(i > 0 && p[j] != p[i])
			i = nxt[i-1];
		if(p[j] == p[i]){			//前后匹配时,next就加一 
			i++;
		}
		nxt[j] = i;	//①p[j] != p[i] && i == 0,赋值0
					//②p[j] == p[i] && i == 0,i已经加一(i在原匹配串上增加) 
	}				//③p[j] == p[i] && i != 0,回溯过后,给值为第一次匹配的后一位 
} 
int kmp(char t[], char p[], int nxt[]){
	unsigned long n, m;
	int i, q, count = 0;
	n = strlen(t);
	m = strlen(p);
	GetNext(p, nxt);
	for(i = 0, q = 0; i < n; ++i){
		while(q > 0 && p[q] != t[i])
			q = nxt[q-1]; 
		if(p[q] == t[i]){
			q++;
		}
		if(q == m){
			count ++;
		}
	}
	return count;
}
int main(){
	int n, m, q, st, en; 
	while(scanf("%d %d %d", &n, &m, &q) != EOF){
		scanf("%s", t);
		scanf("%s", p);
		while(q--){
			char net[2000] = {'\0'};
			scanf("%d %d", &st, &en);
//			printf("%d %d", st, en);
			for(int i = st-1, j = 0; i < en; i++)
				net[j++] = t[i];
			int ans = kmp(net, p, nxt);
//			printf("%s", net);
			printf("%d\n", ans);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值