A1084 Broken Keyboard (20 point(s))

输出不匹配的坏键,全大写,不重复

1. 原文

原题

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:

1
2
7_This_is_a_test
_hs_s_a_es

Sample output:

1
7TI

2. 解析

统计第二个字符串的每个字符,exist[str2[i]]++;

遍历第一个字符串的每个字符,没被访问过,答案中也没有则加入答案

1
2
3
if(exist[str1[i]]==0&&ans.find(str1[i])==string::npos){
ans+=str1[i];
}

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<cstdio>
#include<map>
#include<cctype>
#include<string>
using namespace std;
map<char,int> exist;
char str1[100],str2[100];
int main()
{
scanf("%s%s",str1,str2);
for (int i = 0; str2[i] != '\0'; ++i)
{
str2[i]=toupper(str2[i]);
exist[str2[i]]++;
}
string ans;
for (int i = 0; str1[i] != '\0'; ++i)
{
str1[i]=toupper(str1[i]);
if(exist[str1[i]]==0&&ans.find(str1[i])==string::npos){
ans+=str1[i];
}
}
printf("%s\n",ans.c_str());
return 0;
}

Way2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<cstdio>
#include<cctype>
char s1[100],s2[100];
int exist[130]={};
int main(){
scanf("%s%s",s1,s2);
for (int i = 0; s2[i] != '\0'; ++i)
{
s2[i]=toupper(s2[i]);
exist[s2[i]]++;
}
for (int i = 0; s1[i] != '\0'; ++i)
{
s1[i] = toupper(s1[i]);
if (exist[s1[i]]==0)
{
printf("%c",s1[i]);
exist[s1[i]]++;
}
}
printf("\n");
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1084/
  • 发布时间: 2019-09-03 12:31
  • 更新时间: 2021-10-29 14:04
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!