題目鏈接:http://zju.acmclub.com/index.php?app=problem_title&id=1&problem_id=21373
題目描述
土豪大學的高富帥實驗室裏有一個迴文帝,名叫小明。小明每次看到一個字符串的時候總喜歡看看它是不是迴文串,如果這個字符串不是迴文串的話,他就會將這個字符串裏的字符重新排列,看看能不能構成迴文串。
現在小明想讓你寫一個程序來幫助他實現上述過程。
輸入格式
輸入包含多組測試數據。每組輸入包含一個字符串,長度不超過5,不包含空格等空白符。
提示:本題數據量不大,不必想複雜了。
輸出
對於每組輸入,如果輸入的字符串本身就是迴文串,或者將這個字符串裏的字符重新排列後可以構成迴文串,則輸出Yes,否則輸出No。
樣例輸入
qq
abc
aab
樣例輸出
Yes
No
Yes
Hint:如果判斷字符串不是迴文後,分別求出字符串中不同字符出現的次數。如果出現次數爲奇數次的個數大於1,則字符重新排列後也不可能是迴文。
代碼如下:
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
bool isodd(int n)
{
if (n % 2 != 0)
return true;
return false;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("D:\\in.txt", "r", stdin);
freopen("D:\\out.txt", "w", stdout);
#endif
string str;
while (cin >> str)
{
string s = str;
reverse(str.begin(),str.end());
if (s == str)
{
cout << "Yes" << endl;
}
else
{
vector<char> coll;
vector<int> cnt(5, 0);
coll.push_back(s[0]);
cnt[0] = 1;
for (int i = 1; i < s.size(); i++)
{
int j(0);
for (j = 0; j < coll.size(); j++)
{
if (s[i] == coll[j])
{
cnt[j]++;
break;
}
}
if (j == coll.size())
{
coll.push_back(s[i]);
cnt[coll.size() - 1]++;
}
}
int t = count_if(cnt.begin(), cnt.end(), isodd);
if (t <=1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
return 0;
}