PAT A1112 Stucked Keyboard (20point(s))

Scroll Down

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 , {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:


Sample Output:





使用unordered_map<char, set>类型存储每个字符在每个字串中出现的次数(这里的字串指的是重复字符字符串),每次统计都将出现次数除以n,即坏键出现次数,这样如果某个键是坏的,则出现次数的集合大小一定为1,且数值为n。


using namespace std;
unordered_map<char, set<int>> count1;
int n = 0;

void getInput(char pre, char *str)
	char c;
	int cnt = 1;
	*str++ = pre;
	while ((c = getchar()) != '\n' && pre == c) {
		*str++ = c;
	cnt = cnt % n == 0 ? n : cnt;
	if (c == '\n') {
		*str = '\0';
	else getInput(c, str);
int main()
	char str[1024];
	scanf("%d\n", &n);
	char c = getchar();
	getInput(c, str);
	char *p = str;
	unordered_map<char, bool> mp;
	while (*p) {
		if (count1[*p].size() == 1 && *count1[*p].begin() == n && mp.find(*p) == mp.end()) {
			mp[*p] = true;
		else p++;
	p = str;
	while (*p) {
		if (count1[*p].size() == 1 && *count1[*p].begin() == n) p += n;
		else p++;
	return 0;