小红有一个字符串,她可以进行以下操作:
- 拆分。把 'w' 拆成2个 'v',’m‘ 拆成 2个 'n'。
- 轴对称。把 'b' 轴对称成 'd' ,'p' 轴对称成 'q',反之亦然。
- 翻转。把 'b' 翻转成'q',把 'd' 翻转成 'p',把' n' 翻转成 'u',反之亦然。
第一行输入一个整数表示询问次数。
接下来行,每行输入一个字符串表示询问。
所有字符串长度之和不超过。
输出行,每行输出 "YES" 或 "NO" 表示是否可以变成回文串。
5 wovv bod pdd moom lalalai
YES YES YES YES NO
第一个字符串可以变成:vvovv(将第一个w拆成两个v)第二个字符串可以变成:bob、dod、pop或qoq第三个字符串可以变成:bdb第四个字符串本来就是回文的,不用进行操作第五个字符串不可以变成回文串
def is_same(a:str,b:str)->bool:
if a == b:
return True
else:
if a in ['b','d','q','p'] and b in ['b','d','q','p']:
return True
elif a in ['n','u'] and b in ['n','u']:
return True
else:
return False
def main():
t = int(input())
for _ in range(t):
dest_s = ''
s = input()
for c in s:
if 'w' == c :
dest_s += 'vv'
elif 'm' == c:
dest_s += 'nn'
else:
dest_s += (c)
l,r = 0,len(dest_s)-1
flag = True
while l < r:
if not is_same(dest_s[l],dest_s[r]):
flag = False
break
l+=1
r-=1
if flag:
print('YES')
else:
print('NO')
if __name__ == '__main__':
main() import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
while (T-- > 0) {
String str = in.next();
String str1 = str.replace("m", "nn");
String str2 = str1.replace("w", "vv");
int len = str2.length();
boolean flag = true;
for (int i = 0; i < len / 2; i++) {
char c1 = str2.charAt(i);
char c2 = str2.charAt(str2.length() - i - 1);
if ((c1 == 'b' || c1 == 'd' || c1 == 'p' || c1 == 'q') && (c2 == 'b' || c2 == 'd' || c2 == 'p' || c2 == 'q')) continue;
if ((c1 == 'n' || c1 == 'u') && (c2 == 'n' || c2 == 'u')) continue;
if (c1 != c2) {
flag = false;
break;
}
}
if (flag) System.out.println("YES");
else System.out.println("NO");
}
}
} 注意一个字母可以多次变化
#include <algorithm>
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
bool iss(string s) {
int i = 0, j = s.size() - 1;
while (i < j) {
if ((s[i] == s[j]) ||
(s[i] == 'q' && (s[j]== 'p' || s[j] == 'b' ||s[j]== 'd' )) ||
(s[i] == 'p' && (s[j]== 'q' || s[j] == 'b' ||s[j]== 'd' )) ||
(s[i] == 'd' && (s[j]== 'p' || s[j] == 'b' ||s[j]== 'q' )) ||
(s[i] == 'b' && (s[j]== 'p' || s[j] == 'q' ||s[j]== 'd' )) ||
(s[i] == 'n' && s[j] == 'u') ||
(s[i] == 'u' && s[j] == 'n')) {
i++;
j--;
} else if (((s[i] == 'n'||s[i]=='u') && i + 1 < j &&
(s[i + 1] == 'n'||s[i + 1] == 'u') && s[j] == 'm') ||
(s[i] == 'v' && i + 1 < j && s[i + 1] == 'v' && s[j] == 'w')) {
i += 2;
j--;
}
else if (((s[j] == 'n'||s[j]=='u') && j - 1 > i &&
(s[j - 1] == 'n'||s[j-1]=='u') && s[i] == 'm') ||
(s[j] == 'v' && j - 1 > i && s[j - 1] == 'v' && s[i] == 'w') ) {
i ++;
j -= 2;
}
else {
return false;
}
}
return true;
}
int main() {
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
if (iss(s)) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
// 64 位输出请用 printf("%lld")
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static final Set<Character> set1 = new HashSet<>();
private static final Set<Character> set2 = new HashSet<>();
static {
set1.add('d');
set1.add('p');
set1.add('q');
set1.add('b');
set2.add('u');
set2.add('n');
}
private static String s;
private static int i ,j;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
s = in.next();
i = 0;
j = s.length() - 1;
System.out.println(f() ? "YES" : "NO");
}
}
private static boolean f() {
while (i <= j && ((s.charAt(i) == s.charAt(j)) || judge())) {
i++;
j--;
}
return i >= j;
}
private static boolean judge() {
if (set1.contains(s.charAt(i)) && set1.contains(s.charAt(j))) return true;
else if (set2.contains(s.charAt(i)) && set2.contains(s.charAt(j))) return true;
else if (set1.contains(s.charAt(i)) || set1.contains(s.charAt(j))) return false;
else {
StringBuffer sb = new StringBuffer();
if (s.charAt(i) == 'w' && s.charAt(j) == 'v') {
sb.append(s, 0, i);
sb.append("vv");
++j;
s = sb.toString() + s.substring(i + 1);
return true;
} else if (s.charAt(i) == 'v' && s.charAt(j) == 'w') {
sb.append(s.substring(0, j));
sb.append("vv");
s = sb.toString() + s.substring(j + 1);
++j;
return true;
} else if (s.charAt(i) == 'm' && (s.charAt(j) == 'n' || s.charAt(j) == 'u')) {
sb.append(s.substring(0, i));
sb.append("nn");
++j;
s = sb.toString() + s.substring(i + 1);
return true;
} else if (s.charAt(j) == 'm' && (s.charAt(i) == 'n' || s.charAt(i) == 'u')) {
sb.append(s.substring(0, j));
sb.append("nn");
s = sb.toString() + s.substring(j + 1);
++j;
return true;
} else return false;
}
}
}
//b d q p
//w -> v v
//m -> u u n n u n #include <algorithm>
#include <iostream>
using namespace std;
int main() {
string s;
int t;
cin >> t;
while(t--){
cin >> s;
int l = s.length();
string res = "";
for(char c : s){
if(c == 'm')
res += "##";
else if(c == 'w')
res += "vv";
else if(c == 'b' || c == 'p' || c == 'd' || c == 'q')
res += '*';
else if(c == 'n' || c == 'u')
res += '#';
else
res += c;
}
string ser = res;
reverse(res.begin(), res.end());
//cout << res << " " << ser;
if(ser == res) cout << "YES";
else cout << "NO";
cout << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld") #include <iostream>
#include<string>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin>>t;
string s,_s;
while(t--){
cin>>s;
_s="";
int k=1;
for(char ch:s){
if(ch=='d'||ch=='p'||ch=='q')
_s+='b';
else if(ch=='w')
_s+="vv";
else if(ch=='m')
_s+="uu";
else if(ch=='n')
_s+='u';
else
_s+=ch;
}
int left=0,right=_s.size()-1;
while(left<right){
if(_s[left]!=_s[right]){
k=0;
break;
}
left++;
right--;
}
if(k==0)
cout<<"NO\n";
else
cout<<"YES\n";
}
} def can_be_palindrome(s):
if s == s[::-1]:
return True
# 替换拆分字符
s = s.replace('w', 'vv').replace('m', 'nn')
n = len(s)
# 等价字符对(包括对称 & 翻转) 需要扩展等价关系
equiv = {
('b', 'd'), ('b', 'p'), ('b', 'q'),
('d', 'b'), ('d', 'p'), ('d', 'q'),
('p', 'b'), ('p', 'd'), ('p', 'q'),
('q', 'b'), ('q', 'd'), ('q', 'p'),
('n', 'u'), ('u', 'n')
}
for i in range(n // 2):
a = s[i]
b = s[n - 1 - i]
if a == b:
continue
elif (a, b) in equiv:
continue
else:
return False
return True
n = int(input().strip())
for i in range(n):
s = input().strip()
if can_be_palindrome(s):
print('YES')
else:
print('NO')