1112. Stucked Keyboard (20)
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_teeeeeestSample Output:
ei case1__this_isss_a_teest
https://www.patest.cn/contests/pat-a-practise/1112
思路:
扫描原字符串
如果与前一位相同,累加重复个数,如果达到阈值N,且没有淘汰标记,则次数清零,加入备选数组。
如果与前一位不同,若前一位不是0(恰好累加到阈值N),则说明前一位的字符不符合要求,直接打淘汰标记。
最后将备选数组中的元素再筛选一遍即可
输出时,用while,遇到坏的,加阈值N,遇到好的,加1。
CODE:
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<vector>
using namespace std;
map<char,int> ma;
map<char,int> ma1;
vector<char> vec;
int main()
{
int n;
cin>>n;
string t;
cin>>t;
for (int i=0;i<t.length();i++)
{
if (i==0)
{
ma[t[i]]=1;
}
else if (t[i]!=t[i-1])
{
ma[t[i]]=1;
if (ma[t[i-1]]!=0) ma1[t[i-1]]=-1;
ma[t[i-1]]=0;
}
else
{
ma[t[i]]++;
if (ma[t[i]]==n)
{
//cout<<t[i]<<endl;
if(ma1[t[i]]!=-1)
{
if (ma1[t[i]]!=1)
vec.push_back(t[i]);
ma1[t[i]]=1;
}
ma[t[i]]=0;
}
}
}
for (int i=0;i<vec.size();i++)
{
if (ma1[vec[i]]!=-1)
{
cout<<vec[i];
}
}
cout<<endl;
int j=0;
while (j<t.length())
{
cout<<t[j];
if (ma1[t[j]]==-1)
j++;
else
j+=n;
}
return 0;
}