Educational Codeforces Round 48 (Rated for Div. 2) B. Segment Occurrences(数组前缀和防TLE)

Posted fy1999

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 48 (Rated for Div. 2) B. Segment Occurrences(数组前缀和防TLE)相关的知识,希望对你有一定的参考价值。

题目链接:http://codeforces.com/contest/1016/problem/B

给两个字符串s,t,之后给出s的一个区间,问这个子串中存在多少个子串与t相同

如果一个一个查找就会超时,因此只需要把前缀和相减即可

#include <bits/stdc++.h>

using namespace std;


int n,m,q;
string s,t;
int l,r;
int hay[10000],shay[10000];

int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m>>q;
    cin>>s>>t;
    s=" "+s;
    t=" "+t;
    for(int i=1;i<=n-m+1;i++)
    {
        hay[i]=1;
        for(int j=0;j<m;j++)
        {
            if(s[i+j]!=t[1+j])
            {
                hay[i]=0;
                break;
            }
        }
        shay[i]=shay[i-1]+hay[i];
    }

    while(q--)
    {
        cin>>l>>r;
        r=r-m+1;
        if(r<l) cout<<"0"<<endl;
        else cout<<shay[r]-shay[l-1]<<endl;
    }
    return 0;
}

 

以上是关于Educational Codeforces Round 48 (Rated for Div. 2) B. Segment Occurrences(数组前缀和防TLE)的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27