A. Palindromic Twist

Codeforces
IDCF1027A
Time2000ms
Memory256MB
Difficulty
implementationstrings
English · Original
Chinese · Translation
Formal · Original
You are given a string $s$ consisting of $n$ lowercase Latin letters. $n$ is even. For each position $i$ ($1 \le i \le n$) in string $s$ you are required to change the letter on this position either to the previous letter in alphabetic order or to the next one (letters '_a_' and '_z_' have only one of these options). Letter in every position must be changed **exactly once**. For example, letter '_p_' should be changed either to '_o_' or to '_q_', letter '_a_' should be changed to '_b_' and letter '_z_' should be changed to '_y_'. That way string "_codeforces_", for example, can be changed to "_dpedepqbft_" ('_c_' $\rightarrow$ '_d_', '_o_' $\rightarrow$ '_p_', '_d_' $\rightarrow$ '_e_', '_e_' $\rightarrow$ '_d_', '_f_' $\rightarrow$ '_e_', '_o_' $\rightarrow$ '_p_', '_r_' $\rightarrow$ '_q_', '_c_' $\rightarrow$ '_b_', '_e_' $\rightarrow$ '_f_', '_s_' $\rightarrow$ '_t_'). String $s$ is called a palindrome if it reads the same from left to right and from right to left. For example, strings "_abba_" and "_zz_" are palindromes and strings "_abca_" and "_zy_" are not. Your goal is to check if it's possible to make string $s$ a palindrome by applying the aforementioned changes to every position. Print "_YES_" if string $s$ can be transformed to a palindrome and "_NO_" otherwise. Each testcase contains several strings, for each of them you are required to solve the problem separately. ## Input The first line contains a single integer $T$ ($1 \le T \le 50$) — the number of strings in a testcase. Then $2T$ lines follow — lines $(2i - 1)$ and $2i$ of them describe the $i$\-th string. The first line of the pair contains a single integer $n$ ($2 \le n \le 100$, $n$ is even) — the length of the corresponding string. The second line of the pair contains a string $s$, consisting of $n$ lowercase Latin letters. ## Output Print $T$ lines. The $i$\-th line should contain the answer to the $i$\-th string of the input. Print "_YES_" if it's possible to make the $i$\-th string a palindrome by applying the aforementioned changes to every position. Print "_NO_" otherwise. [samples] ## Note The first string of the example can be changed to "_bcbbcb_", two leftmost letters and two rightmost letters got changed to the next letters, two middle letters got changed to the previous letters. The second string can be changed to "_be_", "_bg_", "_de_", "_dg_", but none of these resulting strings are palindromes. The third string can be changed to "_beeb_" which is a palindrome. The fifth string can be changed to "_lk_", "_lm_", "_nk_", "_nm_", but none of these resulting strings are palindromes. Also note that no letter can remain the same, so you can't obtain strings "_ll_" or "_mm_".
你被给定一个由 $n$ 个小写拉丁字母组成的字符串 $s$。$n$ 是偶数。 对于字符串 $s$ 中的每个位置 $i$($1 lt.eq i lt.eq n$),你必须将该位置的字母改为字母表中的前一个字母或后一个字母(字母 '_a_' 和 '_z_' 只有一种选择)。每个位置的字母必须被更改 *恰好一次*。 例如,字母 '_p_' 应改为 '_o_' 或 '_q_',字母 '_a_' 应改为 '_b_',字母 '_z_' 应改为 '_y_'。 因此,字符串 "_codeforces_" 可以被改为 "_dpedepqbft_"('_c_' $arrow.r$ '_d_','_o_' $arrow.r$ '_p_','_d_' $arrow.r$ '_e_','_e_' $arrow.r$ '_d_','_f_' $arrow.r$ '_e_','_o_' $arrow.r$ '_p_','_r_' $arrow.r$ '_q_','_c_' $arrow.r$ '_b_','_e_' $arrow.r$ '_f_','_s_' $arrow.r$ '_t_')。 如果一个字符串从左到右和从右到左读都相同,则称其为回文串。例如,字符串 "_abba_" 和 "_zz_" 是回文串,而字符串 "_abca_" 和 "_zy_" 不是。 你的目标是检查是否可以通过对每个位置应用上述更改,使字符串 $s$ 变成回文串。如果可以将字符串 $s$ 转换为回文串,则输出 "_YES_",否则输出 "_NO_"。 每个测试用例包含多个字符串,你需要对每个字符串分别求解。 第一行包含一个整数 $T$($1 lt.eq T lt.eq 50$)——测试用例中字符串的数量。 接下来有 $2 T$ 行——第 $(2 i -1)$ 行和第 $2 i$ 行描述第 $i$ 个字符串。每对中的第一行包含一个整数 $n$($2 lt.eq n lt.eq 100$,$n$ 为偶数)——对应字符串的长度。每对中的第二行包含一个由 $n$ 个小写拉丁字母组成的字符串 $s$。 输出 $T$ 行。第 $i$ 行应为输入中第 $i$ 个字符串的答案。如果可以通过对每个位置应用上述更改使第 $i$ 个字符串变成回文串,则输出 "_YES_",否则输出 "_NO_"。 示例中的第一个字符串可以被改为 "_bcbbcb_",最左边的两个字母和最右边的两个字母改为下一个字母,中间的两个字母改为前一个字母。 第二个字符串可以被改为 "_be_"、"_bg_"、"_de_"、"_dg_",但这些结果字符串都不是回文串。 第三个字符串可以被改为 "_beeb_",这是一个回文串。 第五个字符串可以被改为 "_lk_"、"_lm_"、"_nk_"、"_nm_",但这些结果字符串都不是回文串。另外请注意,没有任何字母可以保持不变,因此你无法得到字符串 "_ll_" 或 "_mm_"。 ## Input 第一行包含一个整数 $T$($1 lt.eq T lt.eq 50$)——测试用例中字符串的数量。接下来有 $2 T$ 行——第 $(2 i -1)$ 行和第 $2 i$ 行描述第 $i$ 个字符串。每对中的第一行包含一个整数 $n$($2 lt.eq n lt.eq 100$,$n$ 为偶数)——对应字符串的长度。每对中的第二行包含一个由 $n$ 个小写拉丁字母组成的字符串 $s$。 ## Output 输出 $T$ 行。第 $i$ 行应为输入中第 $i$ 个字符串的答案。如果可以通过对每个位置应用上述更改使第 $i$ 个字符串变成回文串,则输出 "_YES_",否则输出 "_NO_"。 [samples] ## Note 示例中的第一个字符串可以被改为 "_bcbbcb_",最左边的两个字母和最右边的两个字母改为下一个字母,中间的两个字母改为前一个字母。第二个字符串可以被改为 "_be_"、"_bg_"、"_de_"、"_dg_",但这些结果字符串都不是回文串。第三个字符串可以被改为 "_beeb_",这是一个回文串。第五个字符串可以被改为 "_lk_"、"_lm_"、"_nk_"、"_nm_",但这些结果字符串都不是回文串。另外请注意,没有任何字母可以保持不变,因此你无法得到字符串 "_ll_" 或 "_mm_"。
**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} $ be the length of string $ s_k $, with $ n_k $ even and $ 2 \leq n_k \leq 100 $. - Let $ s_k = (s_{k,1}, s_{k,2}, \dots, s_{k,n_k}) $ be a string of $ n_k $ lowercase Latin letters. For each character $ s_{k,i} $, define its possible transformations: - If $ s_{k,i} = \texttt{'a'} $, then $ \text{next}(s_{k,i}) = \{\texttt{'b'}\} $. - If $ s_{k,i} = \texttt{'z'} $, then $ \text{next}(s_{k,i}) = \{\texttt{'y'}\} $. - Otherwise, $ \text{next}(s_{k,i}) = \{ \text{prev}(s_{k,i}), \text{next}(s_{k,i}) \} $, where: - $ \text{prev}(c) $ is the preceding letter in the alphabet, - $ \text{next}(c) $ is the succeeding letter in the alphabet. Let $ S_k = (s_{k,1}', s_{k,2}', \dots, s_{k,n_k}') $ be a transformed string such that $ s_{k,i}' \in \text{next}(s_{k,i}) $ for all $ i \in \{1, \dots, n_k\} $. **Constraints** 1. $ 1 \leq T \leq 50 $ 2. For each $ k \in \{1, \dots, T\} $: - $ n_k $ is even, $ 2 \leq n_k \leq 100 $ - $ s_{k,i} \in \{\texttt{a}, \dots, \texttt{z}\} $ for all $ i \in \{1, \dots, n_k\} $ - Each character is changed *exactly once* to one of its valid neighbors (no stay). **Objective** Determine whether there exists a transformed string $ S_k $ such that $ S_k $ is a palindrome, i.e., $$ s_{k,i}' = s_{k,n_k+1-i}' \quad \text{for all } i \in \{1, \dots, n_k/2\}. $$ Output "YES" if such $ S_k $ exists; otherwise, output "NO".
Samples
Input #1
5
6
abccba
2
cf
4
adfa
8
abaazaba
2
ml
Output #1
YES
NO
YES
NO
NO
API Response (JSON)
{
  "problem": {
    "name": "A. Palindromic Twist",
    "description": {
      "content": "You are given a string $s$ consisting of $n$ lowercase Latin letters. $n$ is even. For each position $i$ ($1 \\le i \\le n$) in string $s$ you are required to change the letter on this position either ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1027A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a string $s$ consisting of $n$ lowercase Latin letters. $n$ is even.\n\nFor each position $i$ ($1 \\le i \\le n$) in string $s$ you are required to change the letter on this position either ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "你被给定一个由 $n$ 个小写拉丁字母组成的字符串 $s$。$n$ 是偶数。\n\n对于字符串 $s$ 中的每个位置 $i$($1 lt.eq i lt.eq n$),你必须将该位置的字母改为字母表中的前一个字母或后一个字母(字母 '_a_' 和 '_z_' 只有一种选择)。每个位置的字母必须被更改 *恰好一次*。\n\n例如,字母 '_p_' 应改为 '_o_' 或 '_q_',字母 '_a_' 应改为...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "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} $ be the length of string $ s_k $, with $ n_k $ eve...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments