package chelper; import io.InputReader; import io.OutputWriter; import misc.SimpleSavingChelperSolution; public class TaskD extends SimpleSavingChelperSolution { public void solve(int testNumber, InputReader in, OutputWriter out) { wrapSolve(testNumber, in, out); } @Override public void solve(int testNumber) { char[] a = in.nextString().toCharArray(); char[] b = in.nextString().toCharArray(); int n = a.length; int m = b.length; int q = in.nextInt(); int[] aCumSum1 = new int[n]; int[] bCumSum1 = new int[m]; for (int i = 0; i < n; i++) { if (i > 0) { aCumSum1[i] += aCumSum1[i - 1]; } if (a[i] == 'B' || a[i] == 'C') { aCumSum1[i]++; } } for (int i = 0; i < m; i++) { if (i > 0) { bCumSum1[i] += bCumSum1[i - 1]; } if (b[i] == 'B' || b[i] == 'C') { bCumSum1[i]++; } } int[] aCumSum2 = new int[n]; int[] bCumSum2 = new int[m]; for (int i = 0; i < n; i++) { if (i > 0) { aCumSum2[i] += aCumSum2[i - 1]; } if (a[i] == 'A') { aCumSum2[i]++; } else { aCumSum2[i] = 0; } } for (int i = 0; i < m; i++) { if (i > 0) { bCumSum2[i] += bCumSum2[i - 1]; } if (b[i] == 'A') { bCumSum2[i]++; } else { bCumSum2[i] = 0; } } for (int i = 0; i < q; i++) { int al = in.nextInt() - 1; int ar = in.nextInt() - 1; int bl = in.nextInt() - 1; int br = in.nextInt() - 1; int x_bc = aCumSum1[ar]; if (al > 0) { x_bc -= aCumSum1[al - 1]; } int x_a = aCumSum2[ar]; if (al > 0 && aCumSum1[ar] == aCumSum1[al - 1]) { x_a -= aCumSum2[al - 1]; } int y_bc = bCumSum1[br]; if (bl > 0) { y_bc -= bCumSum1[bl - 1]; } int y_a = bCumSum2[br]; if (bl > 0 && bCumSum1[br] == bCumSum1[bl - 1]) { y_a -= bCumSum2[bl - 1]; } boolean ok; ok = (x_a >= y_a) && ((x_a - y_a) % 3 == 0); ok &= (x_bc <= y_bc) && ((y_bc - x_bc) % 2 == 0); out.print(ok ? '1' : '0'); } } }