题目描述
回文字符串是指一个字符串从左往右读和从右往左读是相同的,例如:abcba,ccc等。现在小图灵得到了 T 个仅包含小写字母的字符串,他想知道对于每个字符串来说,能否重新组合得到 K 个回文字符串。请你帮助他解决问题。
输入
第1行包括一个整数T,代表字符串的个数。
第2~T+1行包含一个仅含有小写字母的字符串,每个字符串的长度都不超过n。
输出
输出共T行,对于每个字符串来说,结果输出在一行上。若重组得到回文字符串有K个,当K=1时,输出only one;当K=2时输出 only two;当K >2时,输出 more;若不能得到回文字符串则输出 no。
样例输入 Copy
5 a abcda bbccd zzb mmnnpp
样例输出 Copy
only one no only two only one more
提示
本题共有10个测试点。
对于测试点1,n = 2.
对于测试点2,n = 3.
对于测试点3、4,n < 5。
对于测试点5、6,字符串中只会出现a和b。
对于测试点7、8,输出只有 only one 和 only two 的情况。
对于全部测试点,T≤10,1≤n≤20。
#include <bits/stdc++.h> using namespace std; const int N = 1010; int main() { int n; cin >> n; getchar(); while (n--) { int a[N], b[N]; map<char, int>cc; string s; cin >> s; for (int i = 0; i < s.size(); i++) cc[s[i]]++; int idx1 = 0, idx2 = 0; for (auto& it : cc) { if (it.second % 2 != 0) a[++idx1] = it.second; else b[++idx2] = it.second; } if (idx1 >= 2) printf("no\n"); else if (idx1 == 0) { if (idx2 == 1) printf("only one\n"); else if (idx2 == 2 && b[1] == b[2] && b[1] == 2) printf("only two\n"); else printf("more\n"); } else { if (a[idx1] == 1) { if (idx2 == 0 || idx2 == 1) printf("only one\n"); else if (idx2 == 2 && b[1] == 2 && b[2] == 2) printf("only two\n"); else printf("more\n"); } else if (a[idx1] == 3) { if (idx2 == 0) printf("only one\n"); else if (idx2 == 1 && b[idx2] == 2) printf("only two\n"); else printf("more\n"); } else { if (idx2 == 0) printf("only one\n"); else printf("more\n"); } } } return 0; }
文章讨论了如何确定由小写字母组成的字符串是否可以通过重新组合形成1个、2个或多个回文字符串,以及输出相应的结果(onlyone,onlytwo,more或no)。

369

被折叠的 条评论
为什么被折叠?



