原文题目
谷歌翻译
样例输入
5
4
bcd cd a d abc ab
3
i io i oi
2
g g
3
t al lt a
4
bba a ab a abb ba
样例输出
NO
YES
YES
NO
YES
题解 :对于每个样例输入为2*n-2,即n!-2,即一定会有只没有第一个字符,和只没有最后一个字符的字符串,so,找到这两个字符串,(完整字符串abcdcba ,A字符串 abcdcb 与B字符串 bcdcda )由于完整字符串是回文串,则 a[0]=a[n-1],so两个字符串 在其中一个反转后应该是相等的;
献上代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t; cin >> t;
for (int test_number = 0; test_number < t; test_number++) {
int n; cin >> n;
vector <string> long_subs;
for (int i = 0; i < 2 * n - 2; i++) {
string s;
cin >> s;
if ((int)s.size() == n - 1) {
long_subs.push_back(s);
}
}
reverse(long_subs[1].begin(), long_subs[1].end());
if (long_subs[0] == long_subs[1]) {
cout << "YES\n";
}
else {
cout << "NO\n";
}
}
return 0;
}
语法知识补充
一
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
用于加快 cin与cout的速度 但是 使用之后 不能在使用printf 与scanf ;