1084 Broken Keyboard

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

题意:

给出一个原始的字符串和一个经过键盘输入,并在电脑屏幕上显示的字符串,判断电脑上的哪一个按键坏了,不能正确输出字符。

思路:

因为题目要求按照输入的序列来判断那个按键坏了,本来以为unordered_map(),从begin到end会是输入的顺序,但是提交的时候发现并不是这样。

map: map内部实现了一个红黑树,该结构具有自动排序的功能,因此map内部的所有元素都是有序的,红黑树的每一个节点都代表着map的一个元素,因此,对于map进行的查找,删除,添加等一系列的操作都相当于是对红黑树进行这样的操作,故红黑树的效率决定了map的效率。
unordered_map: unordered_map内部实现了一个哈希表,因此其元素的排列顺序是杂乱的,无序的

Code:

#include<iostream>
#include<string>
#include<map>
#include<set>

using namespace std;

int main() {
    string origianl, test;
    cin >> origianl >> test;
    map<char, int> m;
    set<int> visited;
    char c;
    for (int i = 0; i < origianl.length(); ++i) {
        c = origianl[i];
        m[toupper(c)]++;
    }
    for (int i = 0; i < test.length(); ++i) {
        c = test[i];
        m[toupper(c)]--;
    }
    for(int i = 0; i < origianl.length(); ++i) {
        c = toupper(origianl[i]);
        if (m[c] > 0 && visited.find(c) == visited.end()) {
            cout << c;
            visited.insert(c);
        }
    }

    return 0;
}

  

永远渴望,大智若愚(stay hungry, stay foolish)
原文地址:https://www.cnblogs.com/h-hkai/p/12642942.html