PAT甲级 坏掉的键盘 字符串模拟/哈希

坏掉的键盘

题目大意:
键盘上有些按键已经损坏了。
当你输入一些句子时,与坏掉的按键相对应的字符将不会出现在屏幕上。
现在给定你应该键入的字符串以及你实际键入的字符串,请找出哪些按键坏了。

输入格式
第一行包含应该键入的字符串。
第二行包含实际键入的字符串。

两个字符串中都只包含大小写英文字母,数字以及 _(表示空格)。

输出格式
共一行,按检测到的顺序,输出所有损坏的按键。
英文字母必须大写,每个损坏的按键只需要输出一次。

数据范围
给定字符串的长度均不超过 80。
保证至少有一个按键损坏。

输入样例:
7_This_is_a_test
_hs_s_a_es
输出样例:
7TI

解题思路:
双指针模拟一下,一个从s1开始跑一个从s2开始跑,碰到不一样的存入答案并且用哈希表或者bool数组记录一下

Code:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_set>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=1e5+7;

int main()
{
    string str,s,ans;
    cin>>str>>s;
    s+='*';               // 串1 :7_8  串2: _ 的情况 防止越界
    unordered_map<char,int> hash;               //hashmap
    for(int i=0,j=0;i<str.size()&&j<s.size();i++){
    	if(str[i]!=s[j]){
    		char x=toupper(str[i]);
    		if(!hash[x]){
    			ans+=toupper(str[i]);
    			hash[toupper(str[i])]++;
			}
		}
		else j++;
	}
    
	cout<<ans<<"\n";
    return 0;
}

set哈希表

unordered_set<char> hash;
    for(int i=0,j=0;i<str.size()&&j<s.size();i++){
    	if(str[i]!=s[j]){
    		char x=toupper(str[i]);
    		if(!hash.count(x)){
    			ans+=toupper(str[i]);
    			hash.insert(x);
			}
		}
		else j++;
	}

猜你喜欢

转载自blog.csdn.net/weixin_43872264/article/details/107842466
今日推荐