1084. Broken Keyboard (20)

題目鏈接:http://www.patest.cn/contests/pat-a-practise/1084
題目:

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

分析:
給出兩個字符串,原串和鍵盤壞後輸出顯示的字符串,找出壞掉的按鍵,就是找出兩個串包含的字符,做個差集就可以
注意一下大小寫字符的處理以及重複的處理

AC代碼:
#include<stdio.h> 
#include<iostream> 
#include<string> 
#include<vector> 
#include<algorithm> 
#include<string.h> 

using namespace std; 

int main(){ 
 freopen("F://Temp/input.txt", "r", stdin); 
 string str_in, str_out; 
 cin >> str_in >> str_out; 
 transform(str_in.begin(), str_in.end(), str_in.begin(), ::toupper);//先處理爲都是大寫的情況 
 transform(str_out.begin(), str_out.end(), str_out.begin(), ::toupper); 
 string str_in_2, str_out_2; 
 for (int i = 0; i < str_in.size(); ++i){ 
  if (str_in_2.find(str_in[i]) == string::npos){ 
   str_in_2 += str_in[i]; 
  } 
 } 
 for (int i = 0; i < str_out.size(); ++i){ 
  if (str_out_2.find(str_out[i]) == string::npos){ 
   str_out_2 += str_out[i]; 
  } 
 } 
 for (int i = 0; i < str_in_2.size(); ++i){ 
  if (str_out_2.find(str_in_2[i]) == string::npos){//輸出中沒有輸出的這個字符,那麼就是壞鍵盤,輸出 
   cout << str_in_2[i]; 
  } 
 } 
 cout << endl; 
 return 0; 
}


截圖:

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