[编程题] 制造回文
2017-08-03 14:20
176 查看
牛牛有一些字母卡片,每张卡片上都有一个小写字母,所有卡片组成一个字符串s。牛牛一直认为回文这种性质十分优雅,于是牛牛希望用这些卡片拼凑出一些回文串,但是有以下要求:
1、每张卡片只能使用一次
2、要求构成的回文串的数量最少
牛牛想知道用这些字母卡片,最少能拼凑出多少个回文串。
例如: s = "abbaa",输出1,因为最少可以拼凑出"ababa"这一个回文串
s = "abc", 输出3,因为最少只能拼凑出"a","b","c"这三个回文串
[b]输入描述:[/b]
[b]输出描述:[/b]
[b]输入例子1:[/b]
[b]输出例子1:[/b]
#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;
}
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;
}
相关文章推荐
- [编程题] 制造回文
- 网易秋招编程题——回文序列
- [编程题] 最长回文子串
- [编程题] 回文序列
- 腾讯2017暑期实习生编程题-构造回文
- 腾讯编程题:构造回文
- 2017百度校园招聘编程题 删除字符构造最长回文字符串(求最长公共子序列的变种)
- Introduction to Java Programming编程题9.13<回文字符串(在命令行传字符串)>
- [编程题] 回文序列
- 构造回文--腾讯2017暑期实习生编程题
- 2016腾讯实习生笔试编程题:构造回文
- 网易编程题一,回文序列(贪心法)
- 【Python】 易笔试编程题(回文序列)
- 制造回文
- 牛客 [编程题] 回文解码
- 网易2017秋招编程题:回文序列 [python]
- [编程题] 统计回文
- [编程题] LeetCode上的Palindrome(回文)类型的题目
- 腾讯笔试编程题:构造回文(C++)
- 制造回文