【1084】Broken Keyboard (20 分)

简介: 在第二个字符串中出现,则跳出内层for循环;内层for循环结束时,如果第二个字符串未出现c1,且c1未被输出过,则输出c1。 对于上面的判断c1是否输出过:hashtable数

https://pintia.cn/problem-sets/994805342720868352/problems/994805382902300672

输入格式:

7_This_is_a_test
_hs_s_a_es

输出格式:

7TI

【大致思路】

     分别读入两个字符串,第一个for循环枚举第一个字符串,内部嵌套的第二个for枚举第二个字符串,如果是小写字母则都转化为大写,若c1在第二个字符串中出现,则跳出内层for循环;内层for循环结束时,如果第二个字符串未出现c1,且c1未被输出过,则输出c1。

    对于上面的判断c1是否输出过:hashtable数组元素为false时则代表c1未被输出过;而如果输出c1后则令对应的hashtable[c1]=true。

【2个关键点】

(1)在不分大小写英文下,判断在第一个字符串中出现,但在第二个字符串中没出现的字符。

(2)同一个字符(不区分大小写)在最后均用大写字母输出一次。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<algorithm>   
using namespace std;  
注意是两个"有联系"的字符串的比较
另外,可用bool型字数组HashTable[]表示字符是否已经输出
int main(){   
  string str1,str2;
  bool HashTable[128]={false};  //HashTable数组用来标记字符是否已被输出
  getline(cin,str1);
  getline(cin,str2);
  int len1=str1.length();
  int len2=str2.length();
  for(int i=0;i<len1;i++){   //枚举第一个字符串中的每个字符
    int j;
    char c1,c2;
    for(j=0;j<len2;j++){  //枚举第二个字符串的每个字符
      c1=str1[i];
      c2=str2[j];
      if(c1 >= 'a' && c1 <= 'z') c1-=32;  //如果是小写字母,则转化为大写
      if(c2 >= 'a' && c2 <= 'z') c2-=32;  //如果是小写字母,则转化为大写
      if(c1 == c2)  break;  //如果c1在第二个字符串中出现,则跳出
    }
    if( j == len2 && HashTable[c1] == false) {
      printf("%c",c1);  //在第二个字符串中未出现c1,且c1未被输出过
      HashTable[c1]=true;
    }
  }
  system("pause");
    return 0;   
}

注意点

(1)空格也作为需要判断的字符;

(2)大小写不区分,且小写字母均输出其大写形式;

(3)HashTable数组的大小只要能把题目给出的字符包括即可——一般可直接设置ASCII码的个数128作为数组的长度。

相关文章
|
11月前
|
C++
【PAT甲级 - C++题解】1084 Broken Keyboard
【PAT甲级 - C++题解】1084 Broken Keyboard
48 0
UVA11988 破损的键盘 Broken Keyboard (a.k.a. Beiju Text)
UVA11988 破损的键盘 Broken Keyboard (a.k.a. Beiju Text)
PAT (Advanced Level) Practice - 1112 Stucked Keyboard(20 分)
PAT (Advanced Level) Practice - 1112 Stucked Keyboard(20 分)
108 0
|
索引
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
91 0
PAT (Advanced Level) Practice - 1014 Waiting in Line(30 分)
PAT (Advanced Level) Practice - 1014 Waiting in Line(30 分)
98 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
96 0
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
PAT (Advanced Level) Practice - 1060 Are They Equal(25 分)
82 0
PAT (Advanced Level) Practice - 1013 Battle Over Cities(25 分)
PAT (Advanced Level) Practice - 1013 Battle Over Cities(25 分)
97 0
PAT (Advanced Level) Practice - 1082 Read Number in Chinese(25 分)
PAT (Advanced Level) Practice - 1082 Read Number in Chinese(25 分)
81 0
SAP WM 执行VL06P为交货单创建组,报错-TO for multiple deliveries only with delayed delivery update-
SAP WM 执行VL06P为交货单创建组,报错-TO for multiple deliveries only with delayed delivery update-
SAP WM 执行VL06P为交货单创建组,报错-TO for multiple deliveries only with delayed delivery update-