算法08——patB旧键盘打字(散列)

题目描述:
  旧键盘上有几个键坏了,于是在输入一段文字时,对应的字符就不会出现。现在给出应该输入的一段文字以及坏掉的那些键,结果出现的文字会是怎样?

输入格式:
  在两行中分别给出坏掉的那些键以及该输入的文字。其中对应英文字母的坏键以大写字母给出。每段文字是不超过10^5个字符的串。可用字符包括[a ~ z ,A ~ Z]、数字0~9、下划线"_“代表空格,”,",".","-",以及"+"(上档键)。题目保证第二行输入的字符串非空。注意:如果上档键坏了,那么大写的英文字母将无法出现。

输出格式:
  在一行中输出能够出现结果的文字。如果没有一个字符出现,则输出空行。

输入样例:

7+IE.
7_This_is_a_test.

输出样例:

_hs_s_a_tst

思路:

  1. 以bool型数组hashTable[256]表示键盘上的字符是否完好,处置全为true。读入第一个字符串,将其中出现的所有字符c,另hashTable[c] = false,表示该键失效。需要注意的是,如果c是大写字母,应先转换为小写字母
  2. 读入第二个字符串,遍历其中的字符,如果当前字符c是大写字母,那么先转换为小写字母,判断该小写字母和上档键“+”是否均有效;如果均有效则输出该大写字母。如果当前字符c是除了大写字母以外的其他字符,那么只要判断该键是否有效即可

代码:

#include<cstdio>
#include<cstring>
const int maxn = 100010;
bool hashTable[256];
char str[maxn];
int main(){
    
    
    memset(hashTable,true,sizeof(hashTable));
    gets(str);
    int len = strlen(str);
    for(int i = 0 ; i < len ; i++){
    
    
        if(str[i] >= 'A' && str[i] <= 'Z')
            str[i] = str[i] - 'A' + 'a';
        hashTable[str[i]] = false;
    }
    gets(str);
    len = strlen(str);
    for(int i = 0 ; i < len ; i++){
    
    
         if(str[i] >= 'A' && str[i] <= 'Z'){
    
    
            int low = str[i] - 'A' + 'a';
            if(hashTable[low]==true && hashTable['+']==true)
                printf("%c",str[i]);
            }else if(hashTable[str[i]]==true)
            printf("%c",str[i]);
    }
    printf("\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46025531/article/details/122761749
今日推荐