您的位置:首页 > 编程语言

[编程题] 制造回文

2017-08-03 14:20 176 查看
牛牛有一些字母卡片,每张卡片上都有一个小写字母,所有卡片组成一个字符串s。牛牛一直认为回文这种性质十分优雅,于是牛牛希望用这些卡片拼凑出一些回文串,但是有以下要求:

1、每张卡片只能使用一次

2、要求构成的回文串的数量最少

牛牛想知道用这些字母卡片,最少能拼凑出多少个回文串。

例如: s = "abbaa",输出1,因为最少可以拼凑出"ababa"这一个回文串

s = "abc", 输出3,因为最少只能拼凑出"a","b","c"这三个回文串

[b]输入描述:[/b]
输入包括一行,一个字符串s,字符串s长度length(1 ≤ length ≤ 1000).
s中每个字符都是小写字母


[b]输出描述:[/b]
输出一个整数,即最少的回文串个数。


[b]输入例子1:[/b]
abc


[b]输出例子1:[/b]
3


#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
string str;
cin >> str;
map<char, int> Num;
for (int i = 0; i < str.length(); i++)
{
char temp = str[i];
if (Num.count(temp) == 0)
Num[temp] = 0; //未出现的字符,size_t先赋值0
Num[temp]++;//加个数
}
int count = 0;
for (map<char, int>::iterator it = Num.begin(); it != Num.end(); it++)
{
if ((it->second) % 2 == 1)
count++;
}
count = count == 0 ? 1 : count;
cout << count;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C基础 面试 牛客