PAT1112 Stucked Keyboard

个人学习记录,代码难免不尽人意。
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<cstdio>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool hashtable[128];
vector<char> v;
bool judge(string s,int i,int n){
    if(i+n-1>=s.size()){//注意,这个地方需要是i+n-1
        for(int k=i;k<s.size();k++){
            hashtable[s[k]]=false;
        }
        return false;
    }
    for(int k=i;k<i+n-1;k++){
        if(s[k]!=s[k+1]){
             if(hashtable[s[k]]) hashtable[s[k]]=false;
            return false;
        } 
    }
    return true;
}
bool find(char ch){
    for(int i=0;i<v.size();i++){
        if(v[i]==ch) return true;
    }
    return false;
}
int main(){
    fill(hashtable,hashtable+127,false);
    int n;
    scanf("%d",&n);
    string output;
    cin >> output;
    int i=0;
    while(i<output.size()){
        if(judge(output,i,n)){
            hashtable[output[i]]=true;
            if(!find(output[i]))
            v.push_back(output[i]);
            i+=n;
        }
        else i++;
    }
    for(int j=0;j<v.size();j++){
        if(hashtable[v[j]]){
            cout << v[j] ;
        }
    }
    cout << endl;
    i=0;
    while(i!=output.size()){
        if(hashtable[output[i]]){
            cout << output[i];
            i+=n;
        }
        else{
            cout << output[i];
            i++;
        }
    }
    cout << endl;
}

这道题比较简单,利用到了两次对字符串的遍历;逻辑如下:第一次遍历的时候判断三个连续的char是否相同,如果相同的话就将其的hashtable设置为true代表这个字符是stucked,然后将其加入到vector中。需要注意的是,如果三个连续的char不一样,则将两个相比较的前一个的hashtable设置为false再返回false,这样可以做到让某些虽然连续输出但是存在不超过n的节点不被记录。这样的话遍历不能采用简单的for循环,而是进行判断,如果三个相同则i+=3,不同则i++。
最后输出的时候,只有在vector中并且hashtable还等于true才被输出。
这道题比较容易做错的地方是在判断边界上,如果考虑不周到的话很容易处理错误(比如代码中注释的地方)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值