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_teeeeeest
Sample Output:
ei
case1__this_isss_a_teest

一开始就是过不了测试点1,原来是aaabbbab这种,可能前面把a设置为错的,其实a是正确的。处理字符串一直是比较难的题,遍历字符串,将s[i]设为当前字符,s[j]为下一个字符,j一直++,直到s[j] != s[i]或者j >= 字符串长度,此时j指向的是第一个不等于i的字符,然后判断当前i键盘是否是坏的,如果是坏的,或者第一次出现,就用j-i % k,如果除得尽证明这个是坏的,设为1,如果不是设为-1.将i设为j。i在循环式中不要++

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
#include<string>
using namespace std;
//注意只要某字符有一次重复出现的个数≠K的倍数,此键算好键
int main(){
	string s;
	int k;
	cin>>k>>s;
	//==========处理hashTable===========
	int hashTable[128]={0};//表示是不是坏键
	//下标为字符,元素为1表示该字符是坏键,为-1表示该字符好键,0表示该字符还没遇到过
	for(int i = 0; i < s.length();){
        int j = i + 1;
       while(j<s.size()&&s[j]==s[i])        
			++j;//找到i位置后第一个不等于input[i]的字符位置
		if(hashTable[s[i]]>=0)//当前的input[i]是坏键
			hashTable[s[i]]=(j-i)%k==0?1:-1;
		//通过连续出现的input[i]的个数是否是K的倍数给hashTable赋值1、-1
        i = j;
    }
    int put[128] = {0};
    for(int i = 0; i < s.length(); i++){
        if(put[s[i]] == 0 && hashTable[s[i]] == 1){
            cout<<s[i];
            put[s[i]] = 1;
        }
    }
    cout<<endl;
    for(int i = 0; i < s.length(); i++){
        if(hashTable[s[i]] == -1) cout<<s[i];
        else{
            cout<<s[i];
            i += k -1;
        }
    }
	return 0;
}

主要是要理清思路,不要想到什么写什么,找相同位的个数时,记得加上限制条件,i<s.length()。最后输出坏键不是按字符大小输出的,是按照字符串中出现的顺序输出的

//第一次出现k的倍数次,直接设为false,如果后面且这个字符又是false又出现了,但是如果不是k的倍数,就重新设为true
//如果第一次出现不是k的倍数,后面再出现是k的倍数,也不用设为false
#include<iostream>
using namespace std;
int main(){
    int k;
    string s;
    int hashTable[256] = {0};//0代表正常,1代表异常
    int first[256]={0};//0代表第一次出现,1代表不是第一次
    cin>>k>>s;
    for(int i = 0; i < s.length();){
        int start = i;
        char c = s[i];
        while(s[i] == c && i < s.length()) i++;
        if((i-start) %k ==0){
            if(first[c] == 0){
                hashTable[c] = 1;
            }
        }
        else{//如果是正常的
            if(first[c] == 1 && hashTable[c] == 1){
                hashTable[c] = 0;
            }
        }
        first[c] = 1;
    }
    int appear[256] = {0};
    for(int i = 0; i < s.length(); i++){
        if(hashTable[s[i]] == 1 && appear[s[i]] == 0){
            cout<<s[i];
            appear[s[i]]=1;
        }
    }
    cout<<endl;
    for(int i = 0; i < s.length(); i++){
        cout<<s[i];
        if(hashTable[s[i]] == 1) i += k-1;//因为加完k又马上要加1,所以-1
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值