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
- 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.
The first line contains a string S (1 ≤ |S| ≤ 105). The second line contains a string T (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).
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|.
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.
AABCCBAAB ABCB 5 1 3 1 2 2 2 2 4 7 9 1 1 3 4 2 3 4 5 1 3
10011
题意:给你两个字符串S和T,查询Q次,每次查询x1, y1, x2, y2问对于S的子串[x1, y1]是否能按照一定的转换规则变成T的子串[x2, y2],如果可以输出数字1,否则输出数字0。转换规则:①A→BC;②B→AC;③C→AB;④AAA→"空字符串",当然你可以随意的转换
这题就是考虑的尽可能全面,不然就会错:
1:对于子串类型[A…AB],也就是一堆连续的A后面跟着B或C,很显然可以通过不停地②③④操作消掉任意数量的A
2:对于单个字符B,你可以经过4步操作②③②④变成单个字符C,对于单个字符C同理
3:对于单个字符B,你可以经过2步操作②以使得多出两个字符BC,对于单个字符C同理
考虑上面的三个主要转换方法,得出这题做法如下:(假设当前问串S能不能变成串T)
①根据1,你可以知道对于串S和串T,除了最右端连续的A,中间的A全部都可以无视,例如ABCAAACAAA可以当做BCCAAA
②对S和T上述处理之后,一定是若干个BC后面接着若干个A,设s1, a1, s2, a2分别代表S和T串前面的BC个数和后面的A个数
③根据2,3,当且经当s1<s2且它们的差为偶数时才可能有解,否则一定无解
④当x1<x2时肯定无解,因为只有A的情况下不存在操作能多出A而不多出B和C
⑤后面就比较难想了,如果s1!=s2,那么s1为0的时候,x1和x2不能相等,否则无解,因为你想凭空变出B和C必须用到A
⑥到这里当s1<s2时,你可以将后面的A变成BC然后再用BC造出若干个A,所以一定有解,否则一定要满足x1和x2余3相等
#include<stdio.h>
#include<algorithm>
using namespace std;
char st1[100005], st2[100005];
int F1[100005], F2[100005], B1[100005], B2[100005];
int main(void)
{
int T, i, x1, y1, x2, y2, s1, s2, ok;
scanf("%s%s", st1+1, st2+1);
for(i=1;st1[i]!=0;i++)
{
if(st1[i]=='A')
F1[i] = F1[i-1]+1;
B1[i] = B1[i-1]+(st1[i]!='A');
}
for(i=1;st2[i]!=0;i++)
{
if(st2[i]=='A')
F2[i] = F2[i-1]+1;
B2[i] = B2[i-1]+(st2[i]!='A');
}
scanf("%d", &T);
while(T--)
{
ok = 1;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
s1 = B1[y1]-B1[x1-1];
s2 = B2[y2]-B2[x2-1];
x1 = min(y1-x1+1, F1[y1]);
x2 = min(y2-x2+1, F2[y2]);
if(s1>s2 || (s1+s2)%2==1 || x1<x2)
ok = 0;
if(s1==s2 && x1%3!=x2%3 || s1==0 && x1==x2 && s1!=s2)
ok = 0;
printf("%d", ok);
}
puts("");
return 0 ;
}