1112 Stucked Keyboard (PAT甲级)

写完后看柳婼的解法,其实可以直接数重复了几次,我这个写法有点复杂化了。

原代码:

#include 
#include 
#include 

int main(){
    int k, j;
    bool flag;
    std::set st, printed;
    std::string str;
    std::cin >> k;
    std::cin >> str;
    for(int i = 1; i < str.size(); ++i){
        if(str[i] == str[i - 1]){
            flag = true;
            for(j = 1; j <= k - 2; ++j){
                if(str[i + j] != str[i]){
                    flag = false;
                    break;
                }
            }
            if(flag){
                st.insert(str[i]);
            }
            i += j;
        }
    }
    for(int i = 0; i < str.size(); ++i){
        if(st.find(str[i]) != st.end()){
            for(j = 1; j <= k - 1; ++j){
                if(str[i + j] != str[i]){
                    st.erase(str[i]);
                    break;
                }
            }
            i += j - 1;
        }
    }
    for(int i = 0; i < str.size(); ++i){
        if(st.find(str[i]) != st.end() && printed.find(str[i]) == printed.end()){
            std::cout << str[i];
            printed.insert(str[i]);
        }
    }
    std::cout << std::endl;
    for(int i = 0; i < str.size(); ++i){
        std::cout << str[i];
        if(st.find(str[i]) != st.end()){
            i += k - 1;
        }
    }
    return 0;
}

修改后的代码:

#include 
#include 
#include 

int main(){
    int k, cnt;
    std::set stucked, notStucked, printed;
    std::string str;
    std::cin >> k;
    std::cin >> str;
    cnt = 1;
    for(int i = 1; i < str.size(); ++i){
        if(str[i] == str[i - 1]){
            ++cnt;
        } else{
            if(cnt % k){
                notStucked.insert(str[i - 1]);
            } else{
                stucked.insert(str[i - 1]);
            }
            cnt = 1;
        }
    }
    if(cnt % k){
        notStucked.insert(str.back());
    } else{
        stucked.insert(str.back());
    }
    for(int i = 0; i < str.size(); ++i){
        if(stucked.find(str[i]) != stucked.end()
        && notStucked.find(str[i]) != notStucked.end()){
            stucked.erase(str[i]);
        }
    }
    for(int i = 0; i < str.size(); ++i){
        if(stucked.find(str[i]) != stucked.end()
        && printed.find(str[i]) == printed.end()){
            std::cout << str[i];
            printed.insert(str[i]);
        }
    }
    if(!printed.empty()){
        std::cout << std::endl;
    }
    for(int i = 0; i < str.size(); ++i){
        std::cout << str[i];
        if(stucked.find(str[i]) != stucked.end()){
            i += k - 1;
        }
    }
    return 0;
}

题目如下:

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_. 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

你可能感兴趣的:(PAT甲级,c++,pat考试)