A. Palindromic Twist

题目链接

Educational Codeforces Round 49 (Rated for Div. 2)--A. Palindromic Twist

题解

简单题,题意大概是给定一个字符串,判断能否通过将每个字符或不变或加一或减一变成回文字符串。
从头至尾依次判断对应的回文位置是否为相差0或2即可。

代码

#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

string s;
int main() {
    int n, temp;
    for (cin >> n; n; n--) {
        cin >> temp;
        cin >> s;
        bool pd(0);
        for (int i = 0; i < (s.size() - 1) / 2 + 1; i++) {
            if (abs(s[i] - s[temp - i - 1]) == 1 || abs(s[i] - s[temp - i - 1]) > 2)
                pd = true;
        }
        if (pd)cout << "NO" << endl;
        else cout << "YES" << endl;
    }
}

标签: implementation, strings

添加新评论