题目:
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 keysi
ande
might be stucked, buts
is not even though it appears repeatedly sometimes. The original string could bethis 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
题目大意:由于键盘有的键坏了,坏了的键按下后悔打出k个来,找出坏键并输出原始字符串;
解题思路:用三个数组分别表示 存在,坏了,输出过,因为这里面有个坑,原始输出中没有坏的键他也可能手打三次,所以若在前面单独出现过一次的键,一定不是坏键。
但是我有一个案例内存超限了...扣2分....我太菜了(流泪)
代码:
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
using namespace std;
int main(){
int k;
string s;
//freopen("input.txt","r",stdin);
cin>>k;
getchar();
getline(cin,s);
int exist[256]={0};
int bad[256]={0};
int print[256]={0};
for(int i=0,j;i<s.length();i=j){
j=i+1;
exist[s[i]-'0']=1;
while(j<s.length()&&s[j]==s[j-1])j++;
if((j-i)%k==0&&print[s[i]-'0']==0)bad[s[i]-'0']=1;
print[s[i]-'0']=1;
}
//int i=0;
string res,huai;
int print1[256]={0};
for(int i=0;i<s.length();){
res+=s[i];
if(bad[s[i]-'0']==0){
i++;
}else{
if(print1[s[i]-'0']==0){
huai+=s[i];
print1[s[i]-'0']=1;
}
if(s[i]==s[i+1]){
i+=k;
}
}
}
cout<<huai<<endl<<res;
return 0;
}
我还花了好长时间来做这题...不如膜柳神....
https://www.liuchuo.net/archives/2075
解题思路:由于需要根据坏键发现的顺序输出,所以遍历整个字符串的方式输出~,并且确保不会重复输出~(用set集合确保,输出过了的放在set里面)
输出整个正确的字符串:如果当前s[i]是坏键,在输出一次后,令 i = i + k – 1,再输出,保证坏键出现k次只输出一次~
#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;
}