1112 Stucked Keyboard (20分) 测试点2,3,4分析及测试样例

测试点3建议样例:

3
aaasssccc

测试点2,4:注意输出可能坏键的字符顺序是按照字符串中出现的顺序。

下面讲讲我的思路:
设置map<char, bool > map1;存储出现过的字符,false表示不是坏键,true表示是坏键。
然后处理字符串,字符每次和前一个比较,如果一样就len++。不一样就看map1中有没有出现过这个字符:
1.如果map1中有,并且为false,说明之前就出现了该字符,并且不是坏键。

3
eeeesseeeaaa
//扫描到最后一个e的时候,len==num,但是这不是坏键。因为之前已经出现连续4个e,所以这就是第一种情况;

2.map1中有,并且为true,不需要处理。
3.map1中没有,说明第一次出现,可能使坏键,设置成true;

当循环结束后,可能是测试点3那种情况。要判断最后一个字符加上去之后是不是len==num了。如果相等还要在进行判断最后出现的字符是不是坏键。

map2设置为了按字符出现顺序输出坏键。

#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <string>
#include <memory.h>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <iomanip>
#include <algorithm>
#include <cmath>
using namespace std;

int main(){
	int pos = 1, num, len = 1;
	cin >> num;
	string s;
	getchar();
	getline(cin, s);
	map<char, bool > map1;
	while (pos < s.length()) {
		if (s[pos] == s[pos - 1]){
			len++;
		}
		else {
			if (len%num != 0) {
				map1[s[pos-1]] = false;
			}
			else {
				if (map1.find(s[pos - 1])==map1.end()) {
					map1[s[pos-1]] = true;
				}
			}
			len = 1;
		}
		pos++;
	}
	if (len%num != 0) {
		map1[s[pos-1]] = false;
	}
	else {
		if (map1.find(s[pos-1]) == map1.end()) {
			map1[s[pos-1]] = true;
		}
	}
	map<char, bool > map2 = map1;
	for (int i = 0; i < s.length(); i++) {
		if (map2[s[i]] == true) {
			cout << s[i];
			map2[s[i]] = false;
		}
	}
	cout << endl;
	for (int i = 0; i < s.length(); i++) {
		if (map1[s[i]]== true) {
			i = i + num- 1;
		}
		cout << s[i];
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值