K. Cyclic Shift

Codeforces
IDCF10197K
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
You are given two strings a and b of the same length and consisting of lowercase English letters. You can pick at most one subsequence of string b and do a cyclic shift on that subsequence exactly once. For example, if you have a string "_abcdefg_" and you picked the letters at indices 2, 5, and 6 as a subsequence to do a cyclic shift on them, the letter at index 2 will go to index 5, the letter at index 5 will go to index 6, the letter at index 6 will go to index 2, and the string will become "_afcdbeg_". Your task is to check if it is possible to make string b equivalent to string a using at most one cyclic shift. Can you? The first line contains an integer T (1 ≤ T ≤ 200) specifying the number of test cases. The first line of each test case contains an integer n (1 ≤ n ≤ 105) specifying the length of strings a and b. Then two lines follow, giving strings a and b, respectively. Both strings consist only of lowercase English letters. For each test case, print a single line containing "_YES_" (without quotes) if it is possible to make string b equivalent to string a using at most one cyclic shift. Otherwise, print "_NO_" (without quotes). A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. For example, the sequence is a subsequence of obtained after removal of elements , , and . ## Input The first line contains an integer T (1 ≤ T ≤ 200) specifying the number of test cases.The first line of each test case contains an integer n (1 ≤ n ≤ 105) specifying the length of strings a and b. Then two lines follow, giving strings a and b, respectively. Both strings consist only of lowercase English letters. ## Output For each test case, print a single line containing "_YES_" (without quotes) if it is possible to make string b equivalent to string a using at most one cyclic shift. Otherwise, print "_NO_" (without quotes). [samples] ## Note A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. For example, the sequence is a subsequence of obtained after removal of elements , , and .
**Definitions** Let $ T \in \mathbb{Z} $ be the number of test cases. For each test case $ k \in \{1, \dots, T\} $: - Let $ n_k \in \mathbb{Z} $ denote the length of strings $ a_k $ and $ b_k $. - Let $ a_k = (a_{k,1}, a_{k,2}, \dots, a_{k,n_k}) $ and $ b_k = (b_{k,1}, b_{k,2}, \dots, b_{k,n_k}) $ be strings of length $ n_k $ over the alphabet of lowercase English letters. **Constraints** 1. $ 1 \le T \le 200 $ 2. For each $ k \in \{1, \dots, T\} $: - $ 1 \le n_k \le 10^5 $ - $ a_{k,i}, b_{k,i} \in \{ \text{a}, \text{b}, \dots, \text{z} \} $ for all $ i \in \{1, \dots, n_k\} $ **Objective** Determine whether there exists a subsequence $ I = (i_1, i_2, \dots, i_m) $ of indices of $ b_k $, with $ m \ge 1 $ and $ 1 \le i_1 < i_2 < \dots < i_m \le n_k $, such that performing a **cyclic shift** on the characters of $ b_k $ at positions in $ I $ yields $ a_k $. A cyclic shift on $ I $ means: - For each $ j \in \{1, \dots, m-1\} $, the character at position $ i_j $ moves to position $ i_{j+1} $, - The character at position $ i_m $ moves to position $ i_1 $, - All characters not in $ I $ remain unchanged. Output "YES" if such a subsequence $ I $ exists; otherwise, output "NO".
API Response (JSON)
{
  "problem": {
    "name": "K. Cyclic Shift",
    "description": {
      "content": "You are given two strings a and b of the same length and consisting of lowercase English letters. You can pick at most one subsequence of string b and do a cyclic shift on that subsequence exactly onc",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10197K"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given two strings a and b of the same length and consisting of lowercase English letters. You can pick at most one subsequence of string b and do a cyclic shift on that subsequence exactly onc...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ T \\in \\mathbb{Z} $ be the number of test cases.  \nFor each test case $ k \\in \\{1, \\dots, T\\} $:  \n- Let $ n_k \\in \\mathbb{Z} $ denote the length of strings $ a_k $ and $ b_k $....",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments