CF1072A Palindromic Twist 思维
2 seconds
256 megabytes
standard input
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.
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.
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.
5
6
abccba
2
cf
4
adfa
8
abaazaba
2
ml
YES
NO
YES
NO
NO
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 思维的更多相关文章
- 【CF1027A】Palindromic Twist(模拟)
题意:输入T组字符串,每个字符串都必须改变一次,每个字母改变的规则是变成相邻的字母,字母a只能变b,z只能变y,判断改变后的字符依旧是否能够变成回文串 n<=1e2 思路: #include&l ...
- 1027A. Palindromic Twist#变形回文串
题目内容:http://codeforces.com/contest/1027/problem/A 题目解析:输入T组字符串,每个字符串都必须改变一次,每个字母改变的规则是变成相邻的字母,字母a只能变 ...
- 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, ...
- R49 A-D D图有向有环图
A. Palindromic Twist 给一个字符串(小写字母) 每个字符+1,-1:变成其他字符 a只能变b z只能变y 看能否变成回文字符串 #include<bits/stdc+ ...
- Educational Codeforces Round 49 (Rated for Div. 2)
题目链接 还缺F和G,至少上橙之后把F补了吧. A - Palindromic Twist 题意:每个字母恰好操作一次,变成其之前或者其之后的一个字母,注意'a'和'z'不互通,求是否可以变成回文串. ...
- Educational Codeforces Round49
A Palindromic Twist(字符串) 问每个字母必须向左或向右变成另一个字母,问能不能构成回文 #include <iostream> #include <string. ...
- Codeforces Edu Round 49 A-E
A. Palindromic Twist 由于必须改变.所以要使\(a[i] = a[n - i + 1]\). 要么同向走,但必须满足之前的\(a[i] = a[n - i + 1]\). 要么相遇 ...
- 883H - Palindromic Cut(思维+STL)
题目链接:http://codeforces.com/problemset/problem/883/H 题目大意:给一段长度为n的字符串s,想让你把s切成几段长度相同的回文串,可以改变s中字符的排列, ...
- 5. Longest Palindromic Substring 返回最长的回文子串
[抄题]: Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...
随机推荐
- DataOps系列丨数据的“资产负债表”与“现状”
作者:DataPipeline CEO 陈诚 <跨越鸿沟>的作者Geoffrey Moore曾说“没有数据,运营企业就像一个又聋又瞎的人在高速上开车一样”.数据的价值从未像现在这样被企业重 ...
- Linux基础进程管理
一.进程 了解如进程的: • PID,PPID • 当前的进程状态 • 内存的分配情况 • CPU和已花费的实际时间 • 用户UID,他决定进程的特权 (一).静态查看进程 # ps axu | le ...
- 【JDK】JDK源码分析-ReentrantLock
概述 在 JDK 1.5 以前,锁的实现只能用 synchronized 关键字:1.5 开始提供了 ReentrantLock,它是 API 层面的锁.先看下 ReentrantLock 的类签名以 ...
- 对比度拉伸(一些基本的灰度变换函数)基本原理及Python实现
1. 基本原理 对比度拉伸是扩展图像灰度级动态范围的处理.通过在灰度级中确定两个点来控制变换函数的形状.下面是对比度拉伸函数中阈值处理的代码示例,阈值为平均值. 2. 测试结果 图源自skimage ...
- 用命令将本地jar包导入到本地maven仓库
[**前情提要**]在日常开发过程中,我们总是不可避免的需要依赖某些不在中央仓库,同时也不在本地仓库中的jar包,这是我们就需要使用命令行将需要导入本地仓库中的jar包导入本地仓库,使得项目依赖本地仓 ...
- java8(1)--- lambda
项目马上切java8了,之前对于java8的东西都是东打一棒西打一锤的了解了些.这次搜集整理了下,从lambda到stream相关的API等. 1.Lambda和匿名内部类 Lambda 是一个匿名的 ...
- Spring IoC源码解析之getBean
一.实例化所有的非懒加载的单实例Bean 从org.springframework.context.support.AbstractApplicationContext#refresh方法开发,进入到 ...
- 深度学习模型训练技巧 Tips for Deep Learning
一.深度学习建模与调试流程 先看训练集上的结果怎么样(有些机器学习模型没必要这么做,比如决策树.KNN.Adaboost 啥的,理论上在训练集上一定能做到完全正确,没啥好检查的) Deep Learn ...
- Flink 源码解析 —— JobManager 处理 SubmitJob 的过程
JobManager 处理 SubmitJob https://t.zsxq.com/3JQJMzZ 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1 ...
- Powered by .NET Core 进展:第5次发布尝试(Windows部署)
(图注:Windows 自带的性能监控,红色表示 CPU 占用,绿色表示 QPS) 今天中午 12:30 左右,我们进行了 .NET Core 博客站点的第5次发布(页脚会显示"Powere ...