1084. Broken Keyboard (20)
題目例如以下:
On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.
Now given a string that you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.
Input Specification:
Each input file contains one test case. For each case, the 1st line contains the original string, and the 2nd line contains the typed-out string. Each string contains no more than 80 characters which are either English letters [A-Z] (case insensitive), digital numbers [0-9], or "_" (representing the space). It is guaranteed that both strings are non-empty.
Output Specification:
For each test case, print in one line the keys that are worn out, in the order of being detected. The English letters must be capitalized. Each worn out key must be printed once only. It is guaranteed that there is at least one worn out key.
Sample Input: 7_This_is_a_test _hs_s_a_es Sample Output: 7TI題目的實質就是從A字符串中找到全部B中沒有的。然后輸出。注意要把字母轉為大寫,而且反復的僅僅輸出一次。輸出順序為與從前到后探測到的順序同樣。
由于不區分大寫和小寫,所以我們使用一個map存儲全部實際輸出的字符,然后遍歷期望的字符串,假設遍歷到的字符是字母,先轉為大寫,然后去map查詢,假設查不到則說明是要輸出的。而且存儲map,這樣就保證了不反復輸出,也滿足了輸出順序和大寫。
代碼例如以下:
#include <iostream> #include <string> #include <map> #include <stdio.h>using namespace std;int main() {string wanner,input;cin >> wanner >> input;map<char,bool> inputMap;for(int i = 0; i < input.length(); i++){input[i] = isalpha(input[i]) ? toupper(input[i]) : input[i];inputMap[input[i]] = true;}for(int i = 0; i < wanner.length(); i++){char c = wanner[i];c = isalpha(c) ? toupper(c) : c;if(inputMap.find(c) == inputMap.end()){inputMap[c] = true;printf("%c",c);}}return 0; }
總結
以上是生活随笔為你收集整理的1084. Broken Keyboard (20)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 性能颜值齐了:努比亚Z50 Ultra获
- 下一篇: Yum源的优先级