A1084 Broken Keyboard

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

这道题让你找损坏的键盘字符

思路很简单,第一:对所有字符开散列;第二:把小写字母转变成大写;第三:一个个匹配,但是要注意当一个字符串匹配到最后时的特殊处理。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include<stack>
#include<queue>
using namespace std;

const int MaxN = 1000;
int str_w[MaxN];		//看看这个坏字符是不是已经被记下

void to_upper(string &s) {
	for (int i = 0; s[i] != '\0'; i++) {
		if (s[i] >= 'a' && s[i] <= 'z')
			s[i] = s[i] - 32;
	}
}
//'a'=='A'+32
int main() {
	fill(str_w, str_w + MaxN, -1);
	string s, s_worn;		//original string, typed-out string
	cin >> s >> s_worn;

	//全部变成大写字母
	to_upper(s);
	to_upper(s_worn);


	int i = 0, j = 0;
	vector<char> alpha;		//这个是坏掉的字符
	while (i < s.size() || j < s_worn.size()) {
		if (j == s_worn.size()) {	//特殊情况
			if (str_w[s[i]] == -1) {	//字符坏掉且未被记录
				str_w[s[i]] = 1;
				alpha.push_back(s[i]);
			}
			i++;
			continue;
		}
		if (s[i] == s_worn[j] ) {
			i++; j++;
			continue;
		}
		
		if (str_w[s[i]] == -1) {	//字符坏掉且未被记录
			str_w[s[i]] = 1;
			alpha.push_back(s[i]);
		}
		i++;
		
	}

	for (i = 0; i < alpha.size(); i++) {
		if (alpha[i] >= 'a' && alpha[i] <= 'z')
			cout << char(alpha[i] - 32);
		else {
			cout << alpha[i];
		}
	}
	cout << endl;
	

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值