D. Picking Strings

Codeforces
IDCF923D
Time2000ms
Memory256MB
Difficulty
constructive algorithmsimplementationstrings
English · Original
Chinese · Translation
Formal · Original
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 _a__i_, _b__i_, _c__i_, _d__i_. These represent the _i_\-th query: is it possible to create _T_\[_c__i_.._d__i_\] from _S_\[_a__i_.._b__i_\] 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. [samples] ## Note In the first query we can achieve the result, for instance, by using transitions . The third query asks for changing _AAB_ to _A_ — but in this case we are not able to get rid of the character '_B_'.
Alice 有一个由字符 '_A_'、'_B_' 和 '_C_' 组成的字符串。Bob 可以在字符串的任意子串上,以任意顺序任意次数应用以下变换: 注意,子串是指一个或多个连续的字符。对于给定的查询,请判断是否能从源字符串得到目标字符串。 第一行包含一个字符串 #cf_span[S] (#cf_span[1 ≤ |S| ≤ 105])。第二行包含一个字符串 #cf_span[T] (#cf_span[1 ≤ |T| ≤ 105]),这两个字符串仅由大写英文字母 '_A_'、'_B_' 和 '_C_' 组成。 第三行包含查询数量 #cf_span[Q] (#cf_span[1 ≤ Q ≤ 105])。 接下来的 #cf_span[Q] 行描述查询。第 #cf_span[i] 行包含四个用空格分隔的整数 #cf_span[ai]、#cf_span[bi]、#cf_span[ci]、#cf_span[di],表示第 #cf_span[i] 个查询:是否可以通过有限次应用上述变换,将 #cf_span[S[ai..bi]] 变为 #cf_span[T[ci..di]]? 这里,#cf_span[U[x..y]] 表示字符串 #cf_span[U] 中从索引 #cf_span[x](从 1 开始计数)开始、到索引 #cf_span[y] 结束的子串。特别地,#cf_span[U[1..|U|]] 表示整个字符串 #cf_span[U]。 保证 #cf_span[1 ≤ a ≤ b ≤ |S|] 且 #cf_span[1 ≤ c ≤ d ≤ |T|]。 请输出一个长度为 #cf_span[Q] 的字符串,其中第 #cf_span[i] 个字符为 '_1_' 表示第 #cf_span[i] 个查询的答案为肯定,为 '_0_' 表示答案为否定。 在第一个查询中,我们可以通过使用变换来实现目标结果。 第三个查询要求将 _AAB_ 变为 _A_ —— 但在这种情况下,我们无法消除字符 '_B_'。 ## Input 第一行包含一个字符串 #cf_span[S] (#cf_span[1 ≤ |S| ≤ 105])。第二行包含一个字符串 #cf_span[T] (#cf_span[1 ≤ |T| ≤ 105]),这两个字符串仅由大写英文字母 '_A_'、'_B_' 和 '_C_' 组成。第三行包含查询数量 #cf_span[Q] (#cf_span[1 ≤ Q ≤ 105])。接下来的 #cf_span[Q] 行描述查询。第 #cf_span[i] 行包含四个用空格分隔的整数 #cf_span[ai]、#cf_span[bi]、#cf_span[ci]、#cf_span[di],表示第 #cf_span[i] 个查询:是否可以通过有限次应用上述变换,将 #cf_span[S[ai..bi]] 变为 #cf_span[T[ci..di]]?这里,#cf_span[U[x..y]] 表示字符串 #cf_span[U] 中从索引 #cf_span[x](从 1 开始计数)开始、到索引 #cf_span[y] 结束的子串。特别地,#cf_span[U[1..|U|]] 表示整个字符串 #cf_span[U]。保证 #cf_span[1 ≤ a ≤ b ≤ |S|] 且 #cf_span[1 ≤ c ≤ d ≤ |T|]。 ## Output 请输出一个长度为 #cf_span[Q] 的字符串,其中第 #cf_span[i] 个字符为 '_1_' 表示第 #cf_span[i] 个查询的答案为肯定,为 '_0_' 表示答案为否定。 [samples] ## Note 在第一个查询中,我们可以通过使用变换来实现目标结果。第三个查询要求将 _AAB_ 变为 _A_ —— 但在这种情况下,我们无法消除字符 '_B_'。
**Definitions** Let $ S, T \in \{A, B, C\}^* $ be the source and target strings. Let $ Q \in \mathbb{Z}^+ $ be the number of queries. For each query $ i \in \{1, \dots, Q\} $, let $ [a_i, b_i] \subseteq [1, |S|] $ and $ [c_i, d_i] \subseteq [1, |T|] $ be intervals defining substrings $ S[a_i..b_i] $ and $ T[c_i..d_i] $. **Transitions** The allowed operations are: - $ A \leftrightarrow BC $ - $ B \leftrightarrow CA $ - $ C \leftrightarrow AB $ These are reversible, and may be applied to any substring any number of times. **Constraints** 1. $ 1 \le |S|, |T| \le 10^5 $ 2. $ 1 \le Q \le 10^5 $ 3. For each query: $ 1 \le a_i \le b_i \le |S| $, $ 1 \le c_i \le d_i \le |T| $ **Objective** For each query $ i $, determine whether $ S[a_i..b_i] $ can be transformed into $ T[c_i..d_i] $ using the given transitions. Output a string of length $ Q $, where the $ i $-th character is `'1'` if possible, `'0'` otherwise. **Key Invariant** Define the weight function $ w: \{A, B, C\} \to \mathbb{Z}/3\mathbb{Z} $ by $ w(A) = 1 $, $ w(B) = 2 $, $ w(C) = 0 $. Extend to strings: $ w(U) = \sum_{x \in U} w(x) \mod 3 $. The transitions preserve $ w $ modulo 3. Also, the length modulo 2 is preserved: since each transition replaces 1 char with 2 or vice versa, $ |U| \mod 2 $ is invariant under the reverse operations. Thus, $ S[a_i..b_i] \to T[c_i..d_i] $ is possible **iff**: - $ w(S[a_i..b_i]) \equiv w(T[c_i..d_i]) \pmod{3} $, and - $ |S[a_i..b_i]| \equiv |T[c_i..d_i}| \pmod{2} $
Samples
Input #1
AABCCBAAB
ABCB
5
1 3 1 2
2 2 2 4
7 9 1 1
3 4 2 3
4 5 1 3
Output #1
10011
API Response (JSON)
{
  "problem": {
    "name": "D. Picking Strings",
    "description": {
      "content": "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_ * ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF923D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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:\n\n*   _A_ _BC_\n*   _B_ _AC_\n* ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Alice 有一个由字符 '_A_'、'_B_' 和 '_C_' 组成的字符串。Bob 可以在字符串的任意子串上,以任意顺序任意次数应用以下变换:\n\n注意,子串是指一个或多个连续的字符。对于给定的查询,请判断是否能从源字符串得到目标字符串。\n\n第一行包含一个字符串 #cf_span[S] (#cf_span[1 ≤ |S| ≤ 105])。第二行包含一个字符串 #cf_span[T] (#cf_s...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ S, T \\in \\{A, B, C\\}^* $ be the source and target strings.  \nLet $ Q \\in \\mathbb{Z}^+ $ be the number of queries.  \nFor each query $ i \\in \\{1, \\dots, Q\\} $, let $ [a_i, b_i] \\...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments