PTA甲级 1112 Stucked Keyboard (20 point(s))

强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬

本文由参考于柳神博客写成

柳神的CSDN博客,这个可以搜索文章

柳神的个人博客,这个没有广告,但是不能搜索

还有就是非常非常有用的 算法笔记 全名是

算法笔记  上级训练实战指南		//这本都是PTA的题解
算法笔记

PS 今天也要加油鸭

在这里插入图片描述

题目原文

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

生词如下:

PS:这题反应了我的英文水平.垃圾到不行.题目有点看不懂.最后靠着时间给怼了上去.

stuck 结巴 corresponding 相应的

题目大意:

就是键盘有些键坏了.然后你一按下去.就会自动的重复K个.
然后告诉你K.和一段文章.

要你算出坏掉的键.和原文是啥.

思路如下:

这题应该算 简单模拟.

随便一讲.我是真的菜.

我的思路,就是先找出坏掉的键.然后一个一个删除掉多余的.

代码如下:

#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
int main(void) {
	string data;
	char result[1010];
	int IsPut[1010] = { 0 };
	bool ResultPut[1010] = { false };
	int k = 0, i = 0;
	cin >> k >> data;									//Put data
	for (i = 0; i < data.length(); ++i) {
		for (int j = 1; j < k; ++j) {
			if (data[i] != data[i + j]||i+j>=data.length()) {	//this respend the data[i] isn't the stucked key
				IsPut[data[i]] = 1;
				break;
			}
			if (j == k - 1) {
				//0 respend the key don't be checked.2 is respends the key has checked
				if (IsPut[data[i]]==0||IsPut[data[i]]==2) {
					IsPut[data[i]] = 2;					//2 is representative The key is stucked
					i = i + k - 1;
				}
			}
		}
	}
	int index = 0;
	for (i = 0; i < data.length(); ++i) {
		if (IsPut[data[i]] != 2) {
			result[index] = data[i];
			index++;
		}
		else {
			if (!ResultPut[data[i]]) {
				cout << data[i];
				ResultPut[data[i]] = true;
			}
			result[index] = data[i];
			index++;
			i = i + k - 1;					//Because of when the loop i++
		}
	}
	cout << "\n";
	//finall put the result
	for (int i = 0; i < index; ++i) cout << result[i];
}

下面请和我一起欣赏柳神的代码:

柳神的思路:

分析:考察STL的应用~
map<char, bool>存储出现的键是否坏,set存储输出可能坏的键的时候,当前字符是否已经被输出过,输出过的键放在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:谢谢在csdn博客的评论里的同学友情提醒,如果出现了先不是坏建后又判断是坏键的情况,这种情况会出现错误,因为前面已经认为它不是坏键了,说明它一定不是坏建,所以要加一个sureNoBroken,把确定不是坏键的键标记出来,在map都设置完成后把确定不是坏键的m标记为false。虽然测试用例里面没有考虑到这种情况,但是如果输入3 aabbaaa,应该输出没有坏键~

#include <iostream>
#include <map>
#include <cstdio>
#include <set>
using namespace std;
bool sureNoBroken[256];
int main() {
    int k, cnt = 1;
    scanf("%d", &k);
    string s;
    cin >> s;
    map<char, bool> m;
    set<char> printed;
    char pre = '#';
    s = s + '#';
    for(int i = 0; i < s.length(); i++) {
        if(s[i] == pre) {
            cnt++;
        } else {
            if(cnt % k != 0) {
                sureNoBroken[pre] = true;
            }
            cnt = 1;
        }
        if(i != s.length() - 1) m[s[i]] = (cnt % k == 0);
        pre = s[i];
    }
    for(int i = 0; i < s.length() - 1; i++) {
        if(sureNoBroken[s[i]] == true)
            m[s[i]] = false;
    }
    for(int i = 0; i < s.length() - 1; i++) {
        if(m[s[i]] && printed.find(s[i]) == printed.end()) {
            printf("%c", s[i]);
            printed.insert(s[i]);
        }
    }
    printf("\n");
    for(int i = 0; i < s.length() - 1; i++) {
        printf("%c", s[i]);
        if(m[s[i]])
            i = i + k - 1;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值