1084. Broken Keyboard (20)

来源:互联网 发布:windows 10可以投屏吗 编辑:程序博客网 时间:2024/06/13 19:30


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.

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

解析:本题并没有使用双重循环一遍遍的查找缺失的字母,而是采用效率较高的索引方法,只需要单层循环就可以搞定了,运用了HASH的思想;另外PAT题目本身的测试数据有时会有坑,譬如1 2 3后面可能有10个空格和一个换行,而且这种现象在PAT题目中还很普遍,从程序的角度看这不算是测试数据问题,因为你的程序必须对不可见字符有屏蔽能力,有一定的鲁棒性。解决的办法很简单,只需要2行代码:

while ((ch = getchar()) != '\n')        ;


/*************************************************************************> File Name: 1084.c> Author: YueBo> Mail: yuebowhu@163.com> Created Time: Sun 14 May 2017 08:40:44 AM CST ************************************************************************/#include<stdio.h>#include <stdlib.h>#include <string.h>int main(){    int characters[10+26+1];    char input[128];    char ch;    int loopi;    int len;    for (loopi = 0; loopi < 10+26+1; loopi++)        characters[loopi] = 0;        scanf("%s", input);       while ((ch = getchar()) != '\n')        ;    while ((ch = getchar()) != '\n')    {        if (ch == '_')            characters[10+26] = 1;        else if (ch >= '0' && ch <= '9')            characters[ch-'0'] = 1;        else        {            if (ch >= 'A' && ch <= 'Z')                characters[10+ch-'A'] = 1;            else                characters[10+ch-'a'] = 1;        }    }    len = strlen(input);    for (loopi = 0; loopi < len; loopi++)    {        if (input[loopi] >= 'a' && input[loopi] <= 'z')            input[loopi] -= 32;        if (input[loopi] == '_')        {            if (characters[10+26] == 0)            {                printf("%c", input[loopi]);                characters[10+26] = 1;            }        }        else if (input[loopi] >= '0' && input[loopi] <= '9')        {            if (characters[input[loopi]-'0'] == 0)            {                printf("%c", input[loopi]);                characters[input[loopi]-'0'] = 1;            }        }        else        {            if (characters[10+input[loopi]-'A'] == 0)            {                printf("%c", input[loopi]);                characters[10+input[loopi]-'A'] = 1;                           }        }    }    printf("\n");    return 0;}
                                             
0 0
原创粉丝点击