PAT-B: 1029 old keyboard (20 points)



1. Topic

1029 Old keyboard (20 points)
Several keys are broken on the old keyboard, so when typing a paragraph of text, the corresponding characters will not appear. Now give a piece of text that should be entered, and the actual text that was entered, please list the keys that must be broken

Input format:

Enter the text that should be entered and the text that is actually entered in two lines. Each text is a string of no more than 80 characters, consisting of letters AZ (including upper and lower case), numbers 0-9, and underscore _ (representing spaces). The title guarantees that both strings are non-empty.

Output format:

In the order of discovery, output the broken keys in one line. English letters are only output in uppercase, and each bad key is output only once. The question is guaranteed to have at least 1 bad key.

Input sample:

7_This_is_a_test
_hs_s_a_es

Sample output:

7TI


2. Ideas and precautions

Idea: First change the two input strings to uppercase. The outermost loop controls the string str1 , which is the normal output string. Each time the loop is executed , one of the characters in str1 is compared with all the characters in str2 (with bad keys).
Defined . FLAG1, FLAG2
FLAG1
for marking str1 a key is present in str2 ; if it does not exist bond indicates that the bond is broken.
flag2 is used to mark str1Whether the character marked as a bad key in is repeated in the char array where the answer is stored.


Three, AC code

#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
int main()
{
    
    
 	char str1[1001];
 	char str2[1001];
    char sub[100]={
    
    'n'}; 
    int count=0;
    	cin>>str1>>str2;
    int len1=strlen(str1);
    int len2=strlen(str2);
    
    for(int i=0;i<len1;i++)
	str1[i]=toupper(str1[i]);
	
    for(int j=0;j<len2;j++)
    str2[j]=toupper(str2[j]);
    
    //对比部分
	for(int i=0;i<len1;i++)
	{
    
    		
		bool flag1=0;
		bool flag2=0;
		
	  	for(int j=0;j<len2;j++)
		{
    
    
		    if (str1[i]==str2[j])   
			{
    
    
				flag1=1;  	//该键不是坏键
			}
		}
			
		if (flag1==0)    //经过遍历所有的str2元素,都没找到,该键已坏             
		{
    
    
			for(int k=0;k<100;k++)
			{
    
    
				if (sub[k]==str1[i])  //在存储坏键的数组中找这个坏键之前存了没
				{
    
    
					flag2=1;
				}
			}
			if (flag2==0)  //如果没存,就放进去
			{
    
    
				sub[count++]=str1[i];		//从0开始加避免数组出现空格			
			}
		} 	
	}
	for(int i=0;i<count;i++)	//输出
	{
    
    
		cout<<sub[i];
	} 
} 

result:
Insert picture description here

Guess you like

Origin blog.csdn.net/SKMIT/article/details/113914130