string中 find 查询

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 (1i|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 (1n,m1031≤n,m≤103, 1q1051≤q≤105) — the length of string ss, the length of string ttand 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 (1lirin1≤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].

 

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

int main()
{
    int a,b,c,l,r,i;
    string d,e;
    scanf("%d%d%d",&a,&b,&c);
    cin>>d,cin>>e;
    while(1)
    {
        int p=d.find(e);
        if(p!=-1)
            d[p]='1';
        else
            break;
    }
    while(c--)
    {
        int ans=0;
        cin>>l>>r;
        l--,r--;
        for(i=l;i<=r+1-b;i++)
        {
            if(d[i]=='1')
                ans++;
        }
        printf("%d\n",ans);



    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/bhd123/p/9636693.html