PAT 1084 Broken Keyboard

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
作者: CHEN, Yue
单位: 浙江大学
时间限制: 200 ms
内存限制: 64 MB
代码长度限制: 16 KB
 
 
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

void transferrr(string &s){
    for(int i=0;i < s.size();i++){
        if(s[i] >= 'a'&&s[i] <= 'z'){
            s[i] = s[i]-32;
        }
    }
}




int main(){
    string s1;
    string s2;
    cin >> s1 >> s2;transferrr(s1);transferrr(s2);

    map<char,int> mp;
    int i=0,j=0;
    while(i < s1.size()&&j < s2.size()){
        if(s1[i]!=s2[j]){
            if(!mp[s1[i]]){ //未访问过
                mp[s1[i]] = 1;
                cout << s1[i];
            }
            i++;
        }
        else {
            i++;j++;
        }
    }
    if(i < s1.size()){
        for(;i < s1.size();i++){
            if(!mp[s1[i]]){ //未访问过
                mp[s1[i]] = 1;
                cout << s1[i];
            }
        }
    }


    return 0;
}

——一开始漏掉了i还未访问完的测试点:

7_This_is_a_testx
_hs_s_a_es

——最好的思路(网上的):

用map记录s2(都是完好的按键)

然后遍历s1(不在map中的都是坏的按键)

 ——不一定每个人都能想出最好的方法,考场上只要能得分就行

猜你喜欢

转载自www.cnblogs.com/cunyusup/p/10780487.html