【C++分类讨论】回文字符串

文章讨论了如何确定由小写字母组成的字符串是否可以通过重新组合形成1个、2个或多个回文字符串,以及输出相应的结果(onlyone,onlytwo,more或no)。
题目描述

回文字符串是指一个字符串从左往右读和从右往左读是相同的,例如: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值