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:
A→BC
A
→
B
C
B→AC
B
→
A
C
C→AB
C
→
A
B
AAA→empty 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 (1 ≤ Q ≤ 105) ( 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=tA且sB=0且tB>0
s
A
=
t
A
且
s
B
=
0
且
t
B
>
0
5.
sB=tB且tA<sA
s
B
=
t
B
且
t
A
<
s
A
6.
otherwise且tA≢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;
}