PAT 甲级 1084 Broken Keyboard (20 分)

题目描述

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.

输入

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.

输出

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.

思路

用数组存那些出现的字符,之后比对,没出现的即为缺失的

代码

#include<cstdio>
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int all[200] = {
    
     0 };
int last[200] = {
    
     0 };
char str[100] = {
    
     0 };
int main()
{
    
    
	cin.getline(str, 100);
	for (int i = 0; str[i] != 0; i++)
	{
    
    
		if (str[i] >= 'a' && str[i] <= 'z')
		{
    
    
			str[i] = str[i] - 32;
		}
		all[str[i]] = 1;
	}
	char str2[100] = {
    
     0 };
	cin.getline(str2, 100);
	for (int i = 0; str2[i] != 0; i++)
	{
    
    
		if (str2[i] >= 'a' && str2[i] <= 'z')
		{
    
    
			str2[i] = str2[i] - 32;
		}
		last[str2[i]] = 1;
	}
	for (int i = 0; str[i] != 0; i++)
	{
    
    
		if (all[str[i]] == 1 && last[str[i]]==0)
		{
    
    
			printf("%c", str[i]);
			all[str[i]] = 0;
		}
	}
	cout << endl;
}

猜你喜欢

转载自blog.csdn.net/qq_45478482/article/details/120096586