1084 Broken Keyboard (20 分)has雜湊
1084 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, 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 _
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
題目大意:舊鍵盤上壞了幾個鍵,於是在敲一段文字的時候,對應的字元就不會出現。現在給出應該輸入的一段文字、以及實際被輸入的文字,請你列出肯定壞掉的那些鍵~
注意: insensitive :不敏感 所以 case insensitive 是大小寫不敏感
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int arr[255] = {0};
string a,b,ans = "";
cin>>a>>b;
for(int i=0;i<b.size();i++){
if(isalpha(b[i])){
if(b[i] >= 'a' && b[i] <= 'z'){
arr[b[i]]++;
arr[65 + (b[i]-'a')]++;
}
if(b[i] >= 'A' && b[i] <= 'Z'){
arr[b[i]]++;
arr[97 + (b[i]-'A')]++;
}
}else
arr[b[i]]++;
}
for(int i=0;i<a.size();i++)
if(arr[a[i]] == 0)
{
if(isalpha(a[i])){
if(a[i] >= 'a' && a[i] <= 'z'){
arr[a[i]]++;
arr[65 + (a[i]-'a')]++;
}
if(a[i] >= 'A' && a[i] <= 'Z'){
arr[a[i]]++;
arr[97 + (a[i]-'A')]++;
}
ans += toupper(a[i]);
}
else{
arr[a[i]]++;
ans += a[i];
}
}
cout<<ans;
return 0;
}
或:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int arr[255] = {0};
string a,b,ans = "";
cin>>a>>b;
for(int i=0;i<b.size();i++){
if(b[i] >= 'a' && b[i] <= 'z'){
b[i] -= 32;
}
arr[b[i]]++;
}
for(int i=0;i<a.size();i++){
if(a[i] >= 'a' && a[i] <= 'z')
a[i] -= 32;
if(arr[a[i]] == 0){
arr[a[i]]++;
ans += a[i];
}
}
cout<<ans;
return 0;
}