Breaking the Curse Gym - 101840B 子串查找

Egypt has finally qualified to the World Cup 2018 after 28 years. It was almost a curse that has been
broken. The main factor of this curse was the goal of Abdelghany in the World Cup 1990. Abdelghany
has been talking over and over again about this goal that Egyptians started to regret that they have even
qualified to that World Cup. Now, since Egypt has made it to the World Cup, lots have said that the
curse is over, but of course Abdelghany disagrees. The curse is about someone scoring for Egypt there
after him, not only participating. Hence, all Egyptians are praying day and night for any player to score
in the 2018 World Cup.
Of course all the hopes are for Egypt’s star Salah to score in the World Cup. Egyptians are interested in
knowing the probability that Salah will score in the World Cup and end this curse. Egyptian Scientists
decided to calculate this probability for the benefit of the country and the sanity of its people. They
encapsulated Salah’s traits, like DNA, personality and quality in a string s1 and did the same for
Abdelghany in a string s2. Now, they will study the similarity of these two strings in order to calculate
the probability of Salah scoring in the World Cup, like Abdelghany. They are making some experiments
initially in order to be sure and they want your help, by answering q queries each consisting of two integers
L and R; in each query you should find the number of intervals [i, j] (inclusive) where L ≤ i ≤ j ≤ R and
s1[i· · · j] is a substring of s2.
Input
The first line of the input contains a single integer 1 ≤ T ≤ 50 the number of test cases. Each test case
begins with two lines, the first line is s1 and the second line is s2. The two lines are followed by a line
containing a single integer q the number of queries, then followed by q lines each containing two space
separated integers L, R the values of the query; where the strings s1, s2 consist of lower case English
characters, 1 ≤ |s1|, |s2|, q ≤ 75 · 103
.
Output
For each test case output a line displaying the case number, followed by q lines each containing a single
integer which is the answer to the corresponding query.
Example
curse.in
2
bbc
abb
4
1 3
1 2
2 3
3 3
hadafkaselalaam
kas
1
1 10
standard output
Case 1:
3
3
1
0
Case 2:
8

题意:

给你两个串,q个询问,每次给你一对l,r,问你在第一个串的l到r中,有多少子串是第二个串的子串。

题解:

每次都做一遍,类似尺取的方法,判断一个串是不是另一个串的子串string.find!=string::npos

#include<bits/stdc++.h>
using namespace std;
#define ll long long
string s1,s2,x;
int main()
{
    freopen("curse.in","r",stdin);
    int t;
    scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        cin>>s1>>s2;
        int q;
        scanf("%d",&q);
        printf("Case %d:\n",++cas);
        while(q--)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            l--,r--;
            int pos=l,pre=l;
            ll ans=0;
            while(pos<=r)
            {
                x="";
                x+=s1[pos];
                pre=pos;
                while(s2.find(x)!=string::npos&&pos<=r)
                    pos++,x+=s1[pos];
                if(pos!=pre)
                    ans+=(ll)(1+(pos-pre))*(pos-pre)/2;
                else
                   pos++;
            }
            printf("%lld\n",ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/83956943