其他分享
首页 > 其他分享> > [codeforces923D]Picking Strings

[codeforces923D]Picking Strings

作者:互联网

time limit per test : 2 seconds
memory limit per test : 256 megabytes

Alice has a string consisting of characters A'A'′A′, B'B'′B′ and C'C'′C′. Bob can use the following transitions on any substring of our string in any order any number of times:

A -> BC
B -> AC
C -> AB
AAA -> empty string 

Note that a substring is one or more consecutive characters. For given queries, determine whether it is possible to obtain the target string from source.

Input

The first line contains a string S(1S105)S (1 ≤ |S| ≤ 10^5)S(1 ≤ ∣S∣ ≤ 105). The second line contains a string T(1T105)T (1 ≤ |T| ≤ 10^5)T(1 ≤ ∣T∣ ≤ 105), each of these strings consists only of uppercase English letters A'A'′A′, B'B'′B′ and C'C'′C′.

The third line contains the number of queries Q(1Q105)Q (1 ≤ Q ≤ 10^5)Q(1 ≤ Q ≤ 105).

The following QQQ lines describe queries. The iii-th of these lines contains four space separated integers ai,bi,ci,dia_i, b_i, c_i, d_iai​,bi​,ci​,di​. These represent the iii-th query: is it possible to create T[ci..di]T[ci..di]T[ci..di] from S[ai..bi]S[ai..bi]S[ai..bi] by applying the above transitions finite amount of times?

Here, U[x..y]U[x..y]U[x..y] is a substring of UUU that begins at index xxx (indexed from 111) and ends at index yyy. In particular, U[1..U]U[1..|U|]U[1..∣U∣] is the whole string UUU.

It is guaranteed that 1abS1 ≤ a ≤ b ≤ |S|1 ≤ a ≤ b ≤ ∣S∣ and 1cdT1 ≤ c ≤ d ≤ |T|1 ≤ c ≤ d ≤ ∣T∣.

Output

Print a string of QQQ characters, where the iii-th character is 1'1'′1′ if the answer to the iii-th query is positive, and 0'0'′0′ otherwise.

Example

Input

AABCCBAAB
ABCB
5
1 3 1 2
2 2 2 4
7 9 1 1
3 4 2 3
4 5 1 3

Output

10011

Note

In the first query we can achieve the result, for instance, by using transitions .

The third query asks for changing AABAABAAB to AAA — but in this case we are not able to get rid of the character B'B'′B′.

题意:
给定字符变换规则:

A -> BC
B -> AC
C -> AB
AAA -> 空

给定两个长度为10万以内的字符串SSS和TTT,只有ABC三种字符。
一共有q(q&lt;=105)q(q&lt;=10^5)q(q<=105)组询问,每次询问a,b,c,da,b,c,da,b,c,d
问字符串子串S[a,b]S[a,b]S[a,b]是否能通过若干次字符变换变成T[c,d]T[c,d]T[c,d]

题解:
首先我们可以知道

 B  <-> C

那么我们可以先将AAA看作111,BBB/CCC看作000,然后将S串和T串都转换成01串。
我们发现,无论每个000前面有几个111,这些111都是可以被消除的;而且每个111可以变成2kk&gt;=12*k(k&gt;=1)2∗k(k>=1)个000。
我们现在只需要考虑两个子串内的000的个数和后缀的111的个数即可,对于000的个数差值为奇数的情况或者S串中0的个数大于T串中0的个数的情况,这些是不能转换的,对于结尾的连续的1的长度大于第二个串的情况,如果差值是3的倍数,那么刚好可以转换,否则的话需要判断0的差值是否大于等于2且为偶数。注意特判一下S[a,b]S[a,b]S[a,b]是全111串的情况。

#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
using namespace std;
char s[2][100004],ans[100004];
int sum[2][100004],l[2];
int calc(int pt,int l,int r){
    return sum[pt][r]-sum[pt][l-1];
}

int getcon(int pt,int a,int b){
    int l=a,r=b,mid,ans=b+1;
    while(l<=r){
        mid=(l+r)>>1;
        if(calc(pt,mid,b)==b-mid+1){
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    return b-ans+1;
}

bool check(int a,int b,int c,int d){
     int g01=getcon(0,a,b),g11=getcon(1,c,d);
     int g00=b-a+1-calc(0,a,b),g10=d-c+1-calc(1,c,d);
     if(g01<g11||g00>g10)return 0;
     int delta=g01-g11;
     int gp=(delta%3>0);
     int sub=g10-g00;
     if(sub<(gp<<1))return 0;
     if(sub&1)return 0;
     if(g01==b-a+1&&sub>0&&g01==g11)return 0;
     return 1;
}
int LiangJiaJun(){
    scanf("%s",s[0]+1);
    scanf("%s",s[1]+1);
    l[0]=strlen(s[0]+1);
    l[1]=strlen(s[1]+1);
    sum[0][0]=0;
    sum[1][0]=0;
    for(int i=0;i<=1;i++){
        for(int j=1;j<=l[i];j++){
            sum[i][j]=sum[i][j-1]+(s[i][j]=='A');
        }
    }
    int q,len=0;scanf("%d",&q);
    for(int i=1;i<=q;i++){
        int a,b,c,d;
        scanf("%d%d%d%d",&a,&b,&c,&d);
        ans[i]=check(a,b,c,d)?'1':'0';
    }
    ans[q+1]='\n';
    puts(ans+1);
    return 0;
}

标签:string,..,int,&#,111,x27,codeforces923D,Picking,Strings
来源: https://blog.csdn.net/dxyinme/article/details/89735537