PAT_甲級_1084 Broken Keyboard (20point(s)) (C++)【簽到題】

目錄

1,題目描述

 題目大意

2,思路

3,AC代碼

4,解題過程


1,題目描述

Sample Input:

7_This_is_a_test
_hs_s_a_es

 

Sample Output:

7TI

 題目大意

鍵盤上有部分按鍵損壞,現在需要根據輸入(原本想輸入的字符串s1,最終輸入的字符串s2),推測哪些鍵盤損壞。

 

2,思路

把字符串轉換爲大寫;

以s1爲參照,一個個字符進行對比,遇到沒有的,查看是否已出現過,沒有則加進去,否則不添加(注意set是默認對數據進行排序的);

 

3,AC代碼

#include<bits/stdc++.h>
using namespace std;

int main(){
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
    vector<char> ans;
    string s1, s2;
    cin>>s1>>s2;
    transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
    transform(s2.begin(), s2.end(), s2.begin(), ::toupper);
    int index = 0;
    for(int i = 0; i < s1.size(); i++){
        if(s1[i] != s2[index]){
            if(find(ans.begin(), ans.end(), s1[i]) == ans.end())
                ans.push_back(s1[i]);
        }else index++;
    }
    for(auto it : ans)
        printf("%c", it);

    return 0;
}

4,解題過程

本來想用set的(名正言順),然而set是默認對數據進行排序,於是還是採用原來的套路,vector+find解決。。。 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章