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
#include<iostream>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using namespace std;
int main(){
unordered_map<char, int> un_map;
unordered_set<char> un_set;
vector<char> vec[10000];
int k;
cin >> k;
string s;
cin >> s;
un_map[s[0]]++;
vec[0].push_back(s[0]);
int flag = 0;
for(int i = 1; i < s.length(); i++){
un_map[s[i]]++;
if(s[i - 1] != s[i]){
flag++;
}
vec[flag].push_back(s[i]);
}
for(auto i : un_map){
if(i.second % k == 0){
un_set.insert(i.first);
}
}
for(int i = 0; i <= flag; i++){
if(un_set.find(vec[i].front()) != un_set.end() && vec[i].size() % k != 0){
un_set.erase(vec[i].front());
}
}
string out_s = "";
bool a[300] = {false};
int q = 0;
while (q < s.length())
{
out_s += s[q];
if(un_set.find(s[q]) != un_set.end()){
if(!a[int(s[q])]){
printf("%c", s[q]);
a[int(s[q])] = true;
}
q = q + k;
}else
{
q++;
}
}
printf("\n%s\n", out_s.c_str());
return 0;
}
本文介绍了一种算法,用于检测因键盘某些按键故障而引起的字符重复现象,并还原原始输入字符串。通过分析屏幕上的输出字符串,该算法能识别可能卡住的按键及其重复次数,最后输出所有可能的故障按键及原始输入的正确字符串。
442

被折叠的 条评论
为什么被折叠?



