1084. Broken Keyboard (20)

来源:互联网 发布:知乎周刊是什么 编辑:程序博客网 时间:2024/05/18 12:03
题目链接: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陈小旭
0 0