【一隻蒟蒻的刷題歷程】 【PAT】 A1050 字符串減法

題意:

給定兩個字符串S1和S2,將S = S1−S2定義爲從S1提取S2中所有字符後的剩餘字符串 �。 您的任務只是爲任何給定的字符串計算S 1-S 2。 但是,快速進行操作可能不是那麼簡單。

輸入規格:

每個輸入文件包含一個測試用例。 每個案例由兩行組成,分別給出S 1和S 2。 兩個琴絃的琴絃長度不超過10 4。 保證所有字符都是可見的ASCII碼和空白,並且換行符表示字符串的結尾。

輸出規格:

對於每個測試用例,在一行中打印S 1-S 2。

輸入樣例:

They are students.
aeiou

樣本輸出:

Thy r stdnts.


思路:

用map<char,int> 來記錄哪些字符在s2中出現過,出現過爲標記爲1,再遍歷一遍s1,把沒出現過的字符輸出。

代碼:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;  
int main() 
{ 
  string s1,s2;
  getline(cin,s1);
  getline(cin,s2);
  map<char,int> m;
  for(int i=0;i<s2.size();i++)
   m[s2[i]]=1;
  for(int i=0;i<s1.size();i++)
  {
  	 if(m[s1[i]]==0)
  	  cout<<s1[i];
  }
  return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章