PAT Advanced 1050 Broken Keyboard (20) [Hash散列]

题目

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
hssaes

题意

输入s1(应该输入的字符串),s2(显示出来的字符串),判断键盘坏掉的键

题目分析

  1. 定义int asc[256],记录s2中字符出现的次数
  2. 定义int bk[256],记录坏键是否已打印
  3. 遍历s1找到在s1中未出现在s2中的字符(asc[s1[i]]==0)
    • 若bk[asc[s1[i]]]==0表示未打印过,则打印,并将其值置为1,表示该坏键已打印
    • 若bk[asc[s1[i]]]==1表示已打印过,不再打印

Code

#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char * argv[]) {
    char s1[81],s2[81];
    cin.getline(s1,81);
    cin.getline(s2,81);
    int asc[256]= {0},bk[256]= {0};
    int len1=strlen(s1),len2=strlen(s2);
    for(int i=0; i<len2; i++) {
        asc[toupper(s2[i])]++;
    }
    for(int i=0; i<len1; i++) {
        char temp = toupper(s1[i]);
        if(asc[temp]==0&&bk[temp]==0){
            printf("%c",temp);
            bk[temp]=1;
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/houzm/p/12238782.html

时间: 2024-11-07 10:43:55

PAT Advanced 1050 Broken Keyboard (20) [Hash散列]的相关文章

PAT Advanced 1050 String Subtraction (20) [Hash散列]

题目 Given two strings S1 and S2, S = S1 – S2 is defined to be the remaining string afer taking all the characters in S2 from S1. Your task is simply to calculate S1 – S2 for any given strings. However, it might not be that simple to do it fast. Input

PAT Advanced 1041 Be Unique (20) [Hash散列]

题目 Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1, 10^4]. The first one who bets on a unique number wins. For example, if there ar

PAT Advanced 1092 To Buy or Not to Buy (20) [Hash散列]

题目 Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence Eva must c

PAT Advanced 1050 String Subtraction (20 分)

Given two strings S?1?? and S?2??, S=S?1??−S?2?? is defined to be the remaining string after taking all the characters in S?2?? from S?1??. Your task is simply to calculate S?1??−S?2?? for any given strings. However, it might not be that simple to do

1084. Broken Keyboard (20)【字符串操作】——PAT (Advanced Level) Practise

题目信息 1084. Broken Keyboard (20) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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

pat1084. Broken Keyboard (20)

1084. Broken Keyboard (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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.

PAT 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 o

A1084. 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, p

PAT甲级——A1084 Broken Keyboard

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, p