1112 Stucked Keyboard (20分)

On a broken keyboard, some of the keys are always stucked. So when you type some sentences, the characters corresponding to those keys will appear repeatedly on screen for k times.

Now given a resulting string on screen, you are supposed to list all the possible stucked keys, and the original string.

Notice that there might be some characters that are typed repeatedly. The stucked key will always repeat output for a fixed k times whenever it is pressed. For example, when k=3, from the string thiiis iiisss a teeeeeest we know that the keys i and e might be stucked, but s is not even though it appears repeatedly sometimes. The original string could be this isss a teest.

Input Specification:

Each input file contains one test case. For each case, the 1st line gives a positive integer k (1<k≤100) which is the output repeating times of a stucked key. The 2nd line contains the resulting string on screen, which consists of no more than 1000 characters from {a-z}, {0-9} and _. It is guaranteed that the string is non-empty.

Output Specification:

For each test case, print in one line the possible stucked keys, in the order of being detected. Make sure that each key is printed once only. Then in the next line print the original string. It is guaranteed that there is at least one stucked key.

Sample Input:

3
caseee1__thiiis_iiisss_a_teeeeeest

Sample Output:

ei
case1__this_isss_a_teest

题⽬⼤意:

键盘某些键卡住了,按⼀次重复k次,要求找出可能的键,并且输出正确的字符串顺序。可
能的键要求按照被发现的顺序输出。

分析:

考察STL的应⽤ map<char, bool> broken存储出现的键是否坏,map<char, bool> printed存储输出可能坏的键的时候,当前字符是否已经被输出过,输出过的键放在set⾥⾯.
寻找坏键:遍历字符串的每个字符的时候,与pre(字符串当前字符s[i]的前⼀个字符)相⽐较,如果相等就继续计数cnt++,如果不相等,令cnt =1表示当前字符出现了⼀次如果cnt % k等于0 则令s[i]可能是坏键,置map对应的字符的bool值为true
输出坏键:由于需要根据坏键发现的顺序输出,所以遍历整个字符串的⽅式输出,并且确保不会重复输出(⽤set集合确保,输出过了的放在set⾥⾯)输出整个正确的字符串:如果当前s[i]是坏键,在输出⼀次后,令 i = i + k – 1,再输出,保证坏键出现k次只输出⼀次
tips:如果出现了先不是坏建后⼜判断是坏键的情况,这种情况会出现错误,因为前⾯已经认为它不是坏键了,说明它⼀定不是坏建,所以要加⼀个sureNoBroken,把确定不是坏键的键标记出来,在map都设置完成后把确定不是坏键的m标记为false。虽然测试⽤例⾥⾯没有考虑到这种情况,但是如果输⼊3 aabbaaa,应该输出没有坏键。

参考了大神题解

//1112 Stucked Keyboard (20分)
#include<bits/stdc++.h>
using namespace std;
int k,cnt=1;
string str;
map<char,bool> broken;
map<char,bool> printed;
bool nobroken[256];
int main()
{
	cin>>k;
	cin>>str;
	char pre='$';
	str+='$';//用于最后一个字母和$对比 
	for(int i=0; i<str.length(); i++)
	{
		if(str[i]==pre)
		{
			cnt++;
		}
		else
		{
			if(cnt%k!=0)
				nobroken[pre]=true;
			cnt=1;
		}
		if(i!=str.length()-1)
		{
			if(cnt%k==0)
				broken[str[i]]=true;
		}
		pre=str[i];
	}
	for(int i=0; i<str.length()-1; i++)
	{
		if(nobroken[str[i]])
			broken[str[i]]=false;
	}
	for(int i=0; i<str.length()-1; i++)
	{
		if(broken[str[i]]&&printed[str[i]]==false)
		{
			cout<<str[i];
			printed[str[i]]=true;
		}
	}
	cout<<endl;
	for(int i=0; i<str.length()-1; i++)
	{
		cout<<str[i];
		if(broken[str[i]])
			i=i+k-1;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值