題目:ios
題目來源牛客網:https://www.nowcoder.com/practice/f0db4c36573d459cae44ac90b90c6212?tpIdspa
輸入兩個字符串,從第一字符串中刪除第二個字符串中全部的字符。例如,輸入」They are students.」和」aeiou」,則刪除以後的第一個字符串變成」Thy r stdnts.」3d
解答:code
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 int main(){ 6 string s1, s2; 7 getline(cin, s1); 8 getline(cin, s2); 9 int a[256] = { 0 }; 10 for (int i = 0; i < s2.size(); i++){ 11 a[s2[i]]++; 12 } 13 string res; 14 for (int i = 0; i < s1.size(); i++){ 15 if (a[s1[i]] == 0){ 16 res += s1[i]; 17 } 18 } 19 cout << res << endl; 20 return 0; 21 }
此題的解答關鍵是對String類接口的使用。blog