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. Spring Boot @Condition 注解,组合条件你知道吗

    上一篇文章 你应该知道的 @ConfigurationProperties 注解的使用姿势,这一篇就够了 介绍了如何通过 @ConfigurationProperties 注解灵活读取配置属性,这篇文 ...

  2. 全开源C++ DirectUI 界面库SOUI 3.0更新

    从2019.5.22开始,SOUI版本号更新到2.9.0.2,后面开始准备3.0的开发,历时近3个月,现在3.0的主要工作基本完成. 为了便于大家区别2.x,3.0启用了新的代码仓库:https:// ...

  3. luogu1330_封锁阳光大学 图的遍历

    传送门 解释:(转自洛谷题解) 首先,肯定要明确一点,那就是这个图是不一定联通的.于是,我们就可以将整张图切分成许多分开的连同子图来处理.然而最重要的事情是:如何处理一个连通图? 乍看下去,似乎无从下 ...

  4. 浅谈微服务架构与服务治理的Eureka和Dubbo

    前言 本来计划周五+周末三天自驾游,谁知人算不如天算,周六恰逢台风来袭,湖州附近的景点全部关停,不得已只能周五玩完之后,于周六踩着台风的边缘逃回上海.周末过得如此艰难,这次就聊点务虚的话题,一是浅谈微 ...

  5. 转载 | float 清除浮动的7种方法

    什么叫浮动:浮动会使当前标签脱离文档流,产生上浮的效果,同时还会影响周边元素(前后标签)及父级元素的位置和width,height属性.下面用一个小例子来看一看浮动的全过程:1.首先我们新建一个网页, ...

  6. pythonday06数据类型(四)

    今日内容 1.集合 2内存相关 1.集合set 无序 无重复 v = {1,2,3,4,5,6,99,100} # 疑问:v = {} """ None int v1 = ...

  7. H5中的history方法Api介绍

    最近公司在做一个微信公众号,看了项目源码,看到项目中用到了history的Api来进行控制浏览器的历史记录及前进/后退键: 下面来跟大家一起来捋捋history的Api方法和使用: history.p ...

  8. javaScript基础-02 javascript表达式和运算符

    一.原始表达式 原始表达式是表达式的最小单位,不再包含其他表达式,包含常量,直接量,关键字和变量. 二.对象和数组的初始化表达式 对象和数组初始化表达式实际上是一个新创建的对象和数组. 三.函数表达式 ...

  9. 分布式存储——ceph 的 python 基础接口

    python 使用 boto 库完成分布式存储读.写.判断接口 import boto import boto.s3.connection from boto.s3.key import Key im ...

  10. 用python写排序算法

    希尔排序 希尔排序通过将比较的全部元素分为几个区域来提升插入排序的性能.这样可以让一个元素可以一次性地朝最终位置前进一大步.然后算法再取越来越小的步长进行排序,算法的最后一步就是普通的插入排序,但是到 ...