Palindromic Twist
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string ss consisting of nn lowercase Latin letters. nn is even.

For each position ii (1≤i≤n1≤i≤n) in string ss 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' →→ 'd', 'o' →→ 'p', 'd' →→ 'e', 'e' →→ 'd', 'f' →→ 'e', 'o' →→ 'p', 'r' →→ 'q', 'c' →→ 'b', 'e' →→ 'f', 's' →→ 't').

String ss 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 ss a palindrome by applying the aforementioned changes to every position. Print "YES" if string ss 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 TT (1≤T≤501≤T≤50) — the number of strings in a testcase.

Then 2T2T lines follow — lines (2i−1)(2i−1) and 2i2i of them describe the ii-th string. The first line of the pair contains a single integer nn (2≤n≤1002≤n≤100, nn is even) — the length of the corresponding string. The second line of the pair contains a string ss, consisting of nnlowercase Latin letters.

Output

Print TT lines. The ii-th line should contain the answer to the ii-th string of the input. Print "YES" if it's possible to make the ii-th string a palindrome by applying the aforementioned changes to every position. Print "NO" otherwise.

Example
input

Copy
5
6
abccba
2
cf
4
adfa
8
abaazaba
2
ml
output

Copy
YES
NO
YES
NO
NO
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".

题意:每个字母可以向后向前一位或者保持不变,要变的时候必须同时变两个,问经过变化后是否可以得到回文字符串

分析:判断前后字符变化或者不变化后是否相等

AC代码:

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <windows.h>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6+10;
const ll mod = 998244353;
const double pi = acos(-1.0);
const double eps = 1e-8;
string s;
ll n;
bool ok(ll x) {
//debug(s[x]), debug(s[n-x-1]);
if( s[x] == s[n-x-1] || s[x]+1 == s[n-x-1]-1 || s[x]+1 == s[n-x-1]+1 || s[x]-1 == s[n-x-1]-1 || s[x]-1 == s[n-x-1]+1 ) {
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(0);
ll T;
cin >> T;
while( T -- ) {
cin >> n;
cin >> s;
bool flag = true;
ll t;
if( n%2 != 0 ) {
t = (n+1)/2-1;
} else {
t = (n+1)/2;
}
for( ll i = 0; i < t; i ++ ) {
if( !ok(i) ) {
flag = false;
break;
}
}
if( flag ) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
return 0;
}

  

CF1072A Palindromic Twist 思维的更多相关文章

  1. 【CF1027A】Palindromic Twist(模拟)

    题意:输入T组字符串,每个字符串都必须改变一次,每个字母改变的规则是变成相邻的字母,字母a只能变b,z只能变y,判断改变后的字符依旧是否能够变成回文串 n<=1e2 思路: #include&l ...

  2. 1027A. Palindromic Twist#变形回文串

    题目内容:http://codeforces.com/contest/1027/problem/A 题目解析:输入T组字符串,每个字符串都必须改变一次,每个字母改变的规则是变成相邻的字母,字母a只能变 ...

  3. Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths (思维)

    题意:有一个\(n\)x\(m\)的矩阵,从\((1,1)\)出发走到\((n,m)\),问最少修改多少个数,使得所有路径上的数对应相等(e.g:\((1,2)\)和\((n-1,m)\)或\((2, ...

  4. R49 A-D D图有向有环图

    A. Palindromic Twist 给一个字符串(小写字母)   每个字符+1,-1:变成其他字符  a只能变b  z只能变y 看能否变成回文字符串 #include<bits/stdc+ ...

  5. Educational Codeforces Round 49 (Rated for Div. 2)

    题目链接 还缺F和G,至少上橙之后把F补了吧. A - Palindromic Twist 题意:每个字母恰好操作一次,变成其之前或者其之后的一个字母,注意'a'和'z'不互通,求是否可以变成回文串. ...

  6. Educational Codeforces Round49

    A Palindromic Twist(字符串) 问每个字母必须向左或向右变成另一个字母,问能不能构成回文 #include <iostream> #include <string. ...

  7. Codeforces Edu Round 49 A-E

    A. Palindromic Twist 由于必须改变.所以要使\(a[i] = a[n - i + 1]\). 要么同向走,但必须满足之前的\(a[i] = a[n - i + 1]\). 要么相遇 ...

  8. 883H - Palindromic Cut(思维+STL)

    题目链接:http://codeforces.com/problemset/problem/883/H 题目大意:给一段长度为n的字符串s,想让你把s切成几段长度相同的回文串,可以改变s中字符的排列, ...

  9. 5. Longest Palindromic Substring 返回最长的回文子串

    [抄题]: Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...

随机推荐

  1. 常用GDB命令行调试命令

    po po是print-object的简写,可用来打印所有NSObject对象.使用举例如下: (gdb) po self <LauncherViewController: 0x552c570& ...

  2. 显示Mac隐藏文件的命令:

    设置查看隐藏文件的方法如下:打开终端,输入命名 显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏 ...

  3. Java连载10-数据类型取值范围&转义字符

    一.数据类型取值范围 二.八种数据类型在成员变量中的默认值 (1)成员变量,没有赋值,编译不会报错,系统会自动给赋值 byte\int\short\long默认值为0:float\double默认值为 ...

  4. ASP.NET Core - 实现自定义WebApi模型验证

    Framework时代 在Framework时代,我们一般进行参数验证的时候,以下代码是非常常见的 [HttpPost] public async Task<JsonResult> Sav ...

  5. Linux常用的命令及使用方法

    1.请用命令查出ifconfig命令程序的绝对路径 [root@localhost ~]# which ifconfig(ifconfig是linux中用于显示或配置网络设备(网络接口卡)的命令) / ...

  6. dubbo文档笔记

    配置覆盖关系 以 timeout 为例,显示了配置的查找顺序,其它 retries, loadbalance, actives 等类似: 方法级优先,接口级次之,全局配置再次之. 如果级别一样,则消费 ...

  7. 重启iis的命令是什么?三种简单的重启方式

    第一种.界面操作 打开“控制面板”->“管理工具”->“服务”.找到“IIS Admin Service” 右键点击“重新启动” 弹出 “停止其它服务” 窗口,点击“是”. 第二种.Net ...

  8. 图像反转(一些基本的灰度变换函数)基本原理及Python实现

    1. 基本原理 获取像素值在[0, L]范围内的图像的反转图像,即为负片.适用于增强图像中白色或者灰色的区域,尤其当黑色在图片中占主地位时候 $$T(r) = L-r$$ 2. 运行结果 图源自ski ...

  9. 消息中间件-activemq安全机制

    activemq作为消息中间件这样一个独立的个体存在,连通用户和服务器.如果没有一套完备的安全机制去设置用户权限设置消息分发机制可想后果是非常严重.ActiveMQ如果不加入安全机制的话,任何人只要知 ...

  10. java并发编程(二十四)----(JUC集合)ArrayBlockingQueue和LinkedBlockingQueue介绍

    这一节我们来了解阻塞队列(BlockingQueue),BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,当容量满时往BlockingQ ...