B - Segment Occurrences-kmp算法模板

cf 502 div2 题目链接http://codeforces.com/problemset/problem/1016/B

题意 第一行第一个数字主串的长度,第二个是子串长度,第三个数字代表几次询问

例如 1-3 在主串中子串出现的次数

kmp模板题

具体kmp算法详见https://blog.csdn.net/u010232171/article/details/41945605

#include<iostream>
#include<cstdio>
#include<string>
#include<math.h>
using namespace std;
const int maxn=1005;
char s[maxn],t[maxn];
int slen,tlen;
int next[maxn];
void getnext()
{
    int j,k;
    j=0;k=-1;next[0]=-1;
    while(j<tlen)
    if(k==-1||t[j]==t[k])
        next[++j]=++k;
    else
        k=next[k];
}
int kmp_count(int m,int n)
{
    int ans=0;
    int i,j=0;
    if(slen==1&&tlen==1)
    {
        if(s[0]==t[0])
            return 1;
        else
            return 0;
    }
    getnext();
    for(int i=m;i<n;i++)
    {
        while(j>0&&s[i]!=t[j])
            j=next[j];
        if(s[i]==t[j])
            j++;
        if(j==tlen)
        {
            ans++;
            j=next[j];
        }
    }return ans;
}
int main()
{
    int q;
    while(scanf("%d%d%d",&slen,&tlen,&q)!=EOF)
    {
        scanf("%s",s);
        scanf("%s",t);
        while(q--)
        {
            int m,n;
            scanf("%d%d",&m,&n);
            printf("%d\n",kmp_count(m-1,n));
        }
    }
    return 0;
}

kmp模板

#include <iostream>
#include <cstring>
using namespace std;
 
const int N = 1000002;
int next[N];
char S[N], T[N];
int slen, tlen;
 
void getNext()
{
    int j, k;
    j = 0; k = -1; next[0] = -1;
    while(j < tlen)
        if(k == -1 || T[j] == T[k])
            next[++j] = ++k;
        else
            k = next[k];
 
}
/*
返回模式串T在主串S中首次出现的位置
返回的位置是从0开始的。
*/
int KMP_Index()
{
    int i = 0, j = 0;
    getNext();
 
    while(i < slen && j < tlen)
    {
        if(j == -1 || S[i] == T[j])
        {
            i++; j++;
        }
        else
            j = next[j];
    }
    if(j == tlen)
        return i - tlen;
    else
        return -1;
}
/*
返回模式串在主串S中出现的次数
*/
int KMP_Count()
{
    int ans = 0;
    int i, j = 0;
 
    if(slen == 1 && tlen == 1)
    {
        if(S[0] == T[0])
            return 1;
        else
            return 0;
    }
    getNext();
    for(i = 0; i < slen; i++)
    {
        while(j > 0 && S[i] != T[j])
            j = next[j];
        if(S[i] == T[j])
            j++;
        if(j == tlen)
        {
            ans++;
            j = next[j];
        }
    }
    return ans;
}
int main()
{
    
    int TT;
    int i, cc;
    cin>>TT;
    while(TT--)
    {
        cin>>S>>T;
        slen = strlen(S);
        tlen = strlen(T);
        cout<<"模式串T在主串S中首次出现的位置是: "<<KMP_Index()<<endl;
        cout<<"模式串T在主串S中出现的次数为: "<<KMP_Count()<<endl;
    }
    return 0;
}

题目

B. Segment Occurrences

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two strings ss and tt, both consisting only of lowercase Latin letters.

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

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

You are asked qq queries: for the ii-th query you are required to calculate the number of occurrences of string tt in a substring s[li..ri]s[li..ri].

Input

The first line contains three integer numbers nn, mm and qq (1≤n,m≤1031≤n,m≤103, 1≤q≤1051≤q≤105) — the length of string ss, the length of string tt and the number of queries, respectively.

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

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

Each of the next qq lines contains two integer numbers lili and riri (1≤li≤ri≤n1≤li≤ri≤n) — the arguments for the ii-th query.

Output

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

Examples

input

Copy

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

output

Copy

0
1
0
1

input

Copy

15 2 3
abacabadabacaba
ba
1 15
3 4
2 14

output

Copy

4
0
3

input

Copy

3 5 2
aaa
baaab
1 3
1 1

output

Copy

0
0

Note

In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.

猜你喜欢

转载自blog.csdn.net/qq_41568836/article/details/81435466