PAT 甲級1050 String Subtraction 散列(字符串簡單處理)

題意

給出兩個字符串S1,S2,輸出字符串S = S1-S2.

注意

1.S1,S2中可能包含空格,因此都要用gets(str)或者getline(cin,str).
2.若先使用scanf再使用geline,需要在getline前用getchar()讀取掉回車/n

方法一:map容器

代碼

#include <iostream>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

typedef long long ll;
const int maxn = 100000 + 5;
map<char, int> mark;
string ori, deleteChar;


int main(){
    getline(cin, ori);
    getline(cin, deleteChar);

    for(int i = 0; i < deleteChar.size(); i++){
        char c = deleteChar[i];
        mark[c] = 1;
    }
    for(int i = 0; i < ori.size(); i++){
        char c = ori[i];
        if(mark[c]){
            continue;
        }else{
            printf("%c", c);
        }
    }

    return 0;
}

方法2:散列bool數組 vis[128]

acsii碼一共128個:0~127

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