PAT Advanced 1112 Stucked Keyboard

Overview

PAT Advanced 1112 Stucked Keyboard: tests mod operation, count consecutive characters and test if it can be divided by k, if so, print it in the original order.

PAT Advenced 1112 Stucked Keyboard

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

PAT Advenced 1112 Stucked Keyboard Solution

The algorithm is not too difficult, scan the input, count all the consecutive appearances of each same characters, if all of them could be divided by k exactly, then it is a stucked key, so for eaxmaple, 2 aabaaa, a is not stucked, because we say three a’s.

Note the stucked keys need to be printed in the order of being detected, since we normally would use map to store the occurrence, we can’t directly use the order of the keys in the map data structre, be careful about this, then it should pass the OJ.

The following is the C++ source code that could be accepted by PAT OJ to pass this PAT Advenced 1112 Stucked Key:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <string>
#include <iostream>
#include <cstdio>
#include <map>
#include <vector>

using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::vector;

int main() {
	int k;
	cin >> k;
	getchar();

	string line;
	std::getline(cin, line);

	map<char, vector<int> > cs;
	map<char, bool> flags;
	for (int i = 0, len = line.size(), cnt = 1; i < len; i += cnt) {
		cnt = 1;
		for (int j = i + 1; j < len && line.at(j - 1) == line.at(j); ++j, ++cnt);
		cs[line.at(i)].push_back(cnt);
	}

	for(auto const& ele : cs) {
		char c = ele.first;
		bool f = true;
		for(auto const& cnt : ele.second) {
			f = f && (cnt % k == 0);
		}
		flags[c] = f;
	}

	map<char, bool> pp;
	for (auto & c: line) {
		if (!pp[c]) {
			if (flags[c]) {
				cout << c;
			}
			pp[c] = true;
		}
	}
	cout << endl;

	for (int i = 0, len = line.size(), cnt = 1; i < len; i += cnt) {
		cnt = 1;
		for (int j = i + 1; j < len && line.at(j - 1) == line.at(j); ++j, ++cnt);

		if (flags[line.at(i)]) {
			cout << std::string(cnt / k, line.at(i));
		} else {
			cout << std::string(cnt, line.at(i));
		}
	}

	return 0;
}

Summary

PAT Advanced 1112 Stucked Keyboard: tests mod operation, count consecutive characters and test if it can be divided by k, if so, print it in the original order.

Written on November 19, 2016