Loading

1112 Stucked Keyboard (20 分)

1. 题目

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

2. 题意

有一个坏掉的键盘,如果键盘中某个键卡住了,那么按下该键,固定会连续输出k次改键字符。

题目给出一串键盘打出的字符串,从该字符串中找出卡住的键,并输出该字符串在键盘正常情况下打出的结果。

3. 思路——字符串+set

  1. 这里创建两个set,一个用来存正常键(goodKeys),一个用来存卡住的键(brokenKeys)。一开始遍历要检查的字符串,如果某个字符成k倍出现,说明可能是卡住的键(因为可能在字符串其他位置,该字符并不是成k倍出现,此时说明该键是正常的),先加入到结果res字符串中,并将检测到的正常键加入goodKeys的set中。

  2. 找好可能卡住的键的字符串res后,遍历res,如果发现该字符存在于goodKeys的set中,则不输出;如果不存在于goodKeys的set中,则输出,并将该字符加入goodKeys的set中,保证后面不再输出该字符。在遍历过程中,要顺便存储卡住的键到brokenKeys的set中。

  3. 2步骤主要输出了卡住的键。而题目还要求输出键盘正常情况下字符串输出情况。这时只要遍历原字符串,碰到卡住的键的字符,只输出该字符一次,并跳过后面k-1个字符,继续往下遍历即可。

4. 代码

#include <iostream>
#include <string>
#include <set> 

using namespace std;

int main()
{
	int k;
	cin >> k;
	string str;
	getchar();
	getline(cin, str);
	
	int cnt = 1;
	char ch = str[0];
	set<char> goodKeys;	// 记录正常的键 
	string res = "";	// 记录暂时卡住的键 
	for (int i = 1; i < str.length(); ++i)
	{
		if (str[i] == ch)
			cnt++;
		else
		{
			
			if (cnt % k == 0)		// 如果键被卡住,则该键输出的连续字符个数为k的倍数 
				res += ch;
			else					// 否则该键就是正常的 
				goodKeys.insert(ch);
			cnt = 1;				//重新计数 
			ch = str[i];			// 计数下一个键输出的个数 
		}
	}
	// 主要是为了检查最后一个键是正常还是卡住的 
	if (cnt % k == 0)
		res += ch;
	else
		goodKeys.insert(ch);
	
	set<int> brokenKeys;	// 记录被卡住的键 
	for (int i = 0; i < res.length(); ++i)
	{
		// 如果键正常,则不输出 
		if (goodKeys.count(res[i])) continue; 
		cout << res[i];
		brokenKeys.insert(res[i]);
		// 该键卡住,加入好的键的set中,后面不再重复输出 
		goodKeys.insert(res[i]);
	}
	cout << endl;
	for (int i = 0; i < str.length(); ++i)
	{
		// 如果该键被卡住,则连续k个改键字符只输出一个,跳过后面的k-1个 
		if (brokenKeys.count(str[i])) i += k - 1;
		cout << str[i];
	}
	return 0;
}
 
posted @ 2021-10-29 13:31  August_丶  阅读(30)  评论(0编辑  收藏  举报