VK Cup 2018 Round 1: D. Picking Strings

time limit per test  2 seconds 
memory limit per test  256 megabytes
input   standard input
output   standard output

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.

Input

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 aibicidi. 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
input
Copy
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


题意:给你两个字符串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 ;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值