PAT A 1112. Stucked Keyboard (20)

字符串的题,不难,但粗心就容易错。要注意题目的意思,其中有一段“The stucked key will always repeat output for a fixed k times whenever it is pressed”,意思就是说卡住的键是每被敲一次是100%会出现k个对应字符的,例如k=3时,对于”eeeeee"来讲,e就是被卡住了,原串为“ee",但对于”eeeee"来讲,e就是没被卡住。这个题代码我写得很麻烦,不喜欢做这种题。

#include <string>
#include <iostream>
#include <map>
#include <vector>
#include <set>
using namespace std;

map<char,bool> alp;
set<char> res;

int main(void){
	//freopen("in.log","r",stdin);
	int k;
	string st;
	cin>>k>>st;
	for(int i=0;i<st.size();++i){
		if((!alp.count(st[i]))||alp[st[i]]){//仅检查当前被标记为卡住的键,和未层出现的字符
			int j=i+1;
			bool flag=true;//字符初始为被卡住
			while(j-i<k&&j<st.size()){
				if(st[i]!=st[j]){
					flag=false;
					break;
				}
				++j;
			}
			if(j==st.size()&&j-i<k){//这是特殊情况,即当前字符后面剩下的字符个数已经不够k-1个了,那必然不是卡住的键
				flag=false;
			}	
			alp[st[i]]=flag;
			if(flag)i=j-1;
		}	
	}
	for(int i=0;i<st.size();++i){
		if(alp[st[i]]&&!res.count(st[i])){
			cout<<st[i];
			res.insert(st[i]); 
		}
	}
	cout<<endl;
	for(int i=0;i<st.size();++i){
		if(alp[st[i]])i+=k-1;
		cout<<st[i];
	}
	cout<<endl; 
	return 0;
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值