Educational Codeforces Round 48 (Rated for Div. 2) B. Segment Occurrences

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 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 nm and q (1n,m≤1031q≤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].

Examples

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

Note

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

题意:给定一个字符串 s, 字符串 t.  有q个询问,每次询问输入两个整数 l 和 r, 每次输入 字符串 s 中从 l 位置到 r 位置中 字符串 t 出现的次数(包括 l , r )。 注意 给的 l 和 r 是 第 l 个 到 第 r 个 不是下标。

思路:记录 字符串 s 中 字符串 t 出现的所有位置即可。  查询的时候判断一下 就可以了。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,q;
    while(~scanf("%d%d%d",&n,&m,&q)){
        string s,t;
        string f;
        cin >> s >> t;
        f = s;
        int loc[1050] = {0},plo = 0;
        int len = 0;
        string::size_type index;
        while(1){
            index = f.find(t);
            if(index != string::npos){
                loc[plo ++] = index + len + 1;  ///显然, 删除后 字符串会变短,所以要得到在原字符串 s 中的位置 需要加上以 删除的长度
                f.erase(0,index + 1);          ///每次都要 从下标0 开始删除长度 (index + 1)的字符
                len += index + 1;              ///如果只删除中间,可能会出现 删除后又组成 t
            }else break;
        }
        int l,r; int ans = 0;
        int vis[1050] = {0};
        for(int i = 0;i < plo;i ++)
            vis[loc[i]] = 1;
        for(int i = 1;i <= q;i ++){
            scanf("%d%d",&l,&r);
            ans = 0;
            for(int j = l;j <= r;j ++)      ///不仅要判断位置,还要判断长度
                if(vis[j] && j + m - 1 <= r) ans ++;
            printf("%d\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/81407348