題目來源ios
舊鍵盤上壞了幾個鍵,因而在敲一段文字的時候,對應的字符就不會出現。如今給出應該輸入的一段文字、以及實際被輸入的文字,請你列出確定壞掉的那些鍵。spa
輸入在 2 行中分別給出應該輸入的文字、以及實際被輸入的文字。每段文字是不超過 80 個字符的串,由字母 A-Z(包括大、小寫)、數字 0-九、以及下劃線 _
(表明空格)組成。題目保證 2 個字符串均非空。code
按照發現順序,在一行中輸出壞掉的鍵。其中英文字母只輸出大寫,每一個壞鍵只輸出一次。題目保證至少有 1 個壞鍵。orm
7_This_is_a_test _hs_s_a_es
7TI
題目要求英文只輸出大寫,因此先把接收的字符串轉成大寫,用 <algorithm> 的 transform() 將字符串轉大寫blog
將殘缺的字符串保存到哈希表中,而後遍歷完整的字符串,若是遍歷到一個字符不在哈希表中,就加到哈希表裏,同時輸出該字符ip
#include <iostream> #include <vector> #include <string> #include <stack> #include <queue> #include <algorithm> #include <unordered_map> #include <unordered_set> using namespace std; int main() { string wholeStr; // 完整的字符串 string fragStr; // 不完整的字符串 cin >> wholeStr >> fragStr; transform(wholeStr.begin(), wholeStr.end(), wholeStr.begin(), ::toupper); transform(fragStr.begin(), fragStr.end(), fragStr.begin(), ::toupper); unordered_map<char, int> countMap; for (int i = 0; i < fragStr.length(); ++i) { if (countMap.find(fragStr[i]) == countMap.end()) { countMap[fragStr[i]] = 1; } } for (int i = 0; i < wholeStr.size(); ++i) { if (countMap.find(wholeStr[i]) == countMap.end()) { countMap[wholeStr[i]] = 1; cout << wholeStr[i]; } } return 0; }