(C++)反片語
阿新 • • 發佈:2018-11-09
題目:
輸入一些單詞,找出所有滿足如下條件的單詞:該單詞不能通過字母重排,得到輸入文字中的另外一個單詞。
在判斷是否滿足條件時,字母不分大小寫,但在輸入時應保留輸入中的大小寫,按字典序進行排列(所有大寫字母在小寫字母的前面)
樣例輸入:
ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIed
noel dire Disk mace Rob dries
樣例輸出:
Disk
NotE
derail
drIed
eye
ladder
soon
這一題目是偶然從柳婼小姐姐的部落格上看到的,看到之後覺得很有意思,然後試著寫了一下,雖然勉強寫了出來,但是看了柳婼小姐姐寫的答案之後自愧不如。。我用的方法太繁瑣了,這裡先將柳婼小姐姐的答案貼上,等我在改進一下我的答案再酌情貼不貼。。。原博傳送門: https://blog.csdn.net/liuchuo/article/details/52014892
#include "stdafx.h" #include <iostream> #include <map> #include <set> #include <algorithm> #include <vector> #include <string> #include <cctype> using namespace std; map<string, int> mapp; vector<string> words; //將單詞s標準化 string standard(const string &s) { string t = s; for (int i = 0; i < t.length(); i++) { t[i] = tolower(t[i]); } sort(t.begin(), t.end()); return t; } int main() { string s; while (cin >> s) { if (s[0] == '#') break; words.push_back(s); string r = standard(s); if (!mapp.count(r)) mapp[r] = 0; mapp[r]++; } vector<string> ans; for (int i = 0; i < words.size(); i++) { if (mapp[standard(words[i])] == 1) ans.push_back(words[i]); } sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) { cout << ans[i] << endl; } system("pause"); }