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→BCA→BC
B→ACB→AC
C→ABC→AB
AAA→empty stringAAA→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 (1 ≤ |S| ≤ 105)(1 ≤ |S| ≤ 105). The second line contains a string T (1 ≤ |T| ≤ 105)(1 ≤ |T| ≤ 105), 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 ≤ 105).
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>tBsB>tB
2. sB≢tB(mod2)sB≢tB(mod2)
3. sA<tAsA<tA
4. sA=tA且sB=0且tB>0sA=tA且sB=0且tB>0
5. sB=tB且tA<sAsB=tB且tA<sA
6. otherwise且tA≢sA(mod3)otherwise且tA≢sA(mod3)
#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;
}
本文探讨了一个字符串转换问题,即如何利用特定的字符转换规则从源字符串S生成目标字符串T。通过对字符'A'、'B'、'C'的操作限制,文章提出了一种O(1)时间复杂度的解决方案,并详细分析了六种不可能实现字符串转换的情况。

141

被折叠的 条评论
为什么被折叠?



