【codeforces923D】Picking Strings

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
Description
Alice has a string consisting of characters ‘A’, ‘B’ and ‘C’. Bob can use the following transitions on any substring of our string in any order any number of times:
ABC A → B C
BAC B → A C
CAB C → A B
AAAempty string A A A → e m p t y   s t r i n g
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 (1|S|105) ( 1   ≤   | S |   ≤   10 5 ) . The second line contains a string T (1|T|105) ( 1   ≤   | T |   ≤   10 5 ) , each of these strings consists only of uppercase English letters ‘A’, ‘B’ and ‘C’.

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

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

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

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

Output
Print a string of Q characters, where the i-th character is ‘1’ if the answer to the i-th query is positive, and ‘0’ otherwise.

Example
input1

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

output1

10011
Note

In the first query we can achieve the result, for instance, by using transitions AAB to AAAC to AAAAB to AB.

The third query asks for changing AAB to A— but in this case we are not able to get rid of the character ‘B’.


题意
多个询问,问能否将串S通过如下变换变成T:
A to BC
B to AC
C to AB
AAA to (empty)


Analysis

留意到A可以变成BC
BC可以互换
可知BC总数不会变少
末尾的A不会变多
通过变换BC的增量为偶数
这题变成了O(1)判断的题

但是,有6种情况要讨论,不一定能够全部想到。。。于是我贡献了4次WA on test 11滚蛋。。。

对于ST的两个子串,设sA为S中全A的后缀长度,tA为T的,同样。设sB为S串中BC的总数,tB亦然
考虑不可能的情况
1. sB>tB s B > t B
2. sB≢tB(mod2) s B ≢ t B ( mod 2 )
3. sA<tA s A < t A
4. sA=tAsB=0tB>0 s A = t A 且 s B = 0 且 t B > 0
5. sB=tBtA<sA s B = t B 且 t A < s A
6. otherwisetA≢sA(mod3) o t h e r w i s e 且 t A ≢ s A ( mod 3 )

#include<cstring>
#include<cstdio>
#define N 100100

using namespace std;

int a[N],b[N],n,m,q,lasa[N],lasb[N];
char A[N],B[N];

int main(){
    scanf("%s\n",A+1);
    scanf("%s\n",B+1);
    n=strlen(A+1);m=strlen(B+1);
    for(int i=1;i<=n;i++){
        a[i]=a[i-1]+(A[i]>'A');
        if(A[i]=='A')lasa[i]=lasa[i-1]+1;else lasa[i]=0;
    }
    for(int i=1;i<=m;i++){
        b[i]=b[i-1]+(B[i]>'A');
        if(B[i]=='A')lasb[i]=lasb[i-1]+1;else lasb[i]=0;
    }
    scanf("%d",&q);
    for(int i=1;i<=q;i++){
        int l,r,h,t;scanf("%d %d %d %d",&l,&r,&h,&t);
        int s=b[t]-b[h-1]-a[r]+a[l-1],_s=0;
        if(lasb[t]>t-h+1)_s-=t-h+1;else _s-=lasb[t];
        if(lasa[r]>r-l+1)_s+=r-l+1;else _s+=lasa[r];
        if(s>=0 && s%2==0 && (_s==0 || (_s>0 && (s>0 || _s%3==0)))){
            if(b[t]-b[h-1]>0 && a[r]-a[l-1]==0 && _s==0)printf("0");else printf("1");
        }else printf("0");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值