原文链接: PAT A1084. Broken Keyboard (20)
上一篇: PAT A1012. The Best Rank (25)
下一篇: 笨蛋难题四
https://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
思路:
1,如何在英文字母不区分大小写的情况下判断在第一个字符串中有哪些字符没有在第二个字符串中出现
2,如何保证同一个字符不区分大小写的情况下只输出一次,且均用大写输出
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
char a[111],b[111];
int cmp(char a,char b){
return toupper(a)==toupper(b);
}
bool flag[200]={0};
int main(int argc, char *argv[])
{
int lena,lenb;
scanf("%s %s",a,b);
lena = strlen(a);
lenb=strlen(b);
for(int i=0;i<lenb;i++){
for(int j=0;j<lena;j++){
if(a[j] && cmp(a[j],b[i]) )
a[j]=0;
}
}
for(int i=0;i<lena;i++)
if(a[i] && !flag[ toupper(a[i])] ){
printf("%c",toupper(a[i]));
flag[ toupper(a[i])]=1;
}
printf("\n");
return 0;
}