PATB1033 old keyboard typing (20 points)

First, the technical summary

  1. Use an array of characters segmentation fault that is char str []; change the string str; the problem is resolved. Try to use later in C ++ string
  2. Use cin >>, the answer to an error, possibly because the input is an input spaces, leading to wrong answer, into getline (cin, str); problem solved.
    Details Reference: https://www.cnblogs.com/tsruixi/p/11781506.html

Two, C ++ reference code

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int maxn = 10010;
bool hashTable[256];
int main(){
    memset(hashTable,true,sizeof(hashTable));
    string str;
    getline(cin,str);
    int len = str.length();
    for(int i = 0; i < len; i++){
        if(str[i] >= 'A' && str[i] <= 'Z'){
            str[i] = str[i] - 'A' + 'a';
        }
        hashTable[str[i]] = false;
    }
    cin >> str;
    len = str.length();
    int flag = 0;
    for(int i = 0; i < len; i++){
        if(str[i] >= 'A' && str[i] <= 'Z'){
            int low = str[i] - 'A' + 'a';
            if(hashTable[low] == true && hashTable['+'] == true){
                cout << str[i];
                flag = 1;
            }
        }else if(hashTable[str[i]] == true){
            cout << str[i];
            flag = 1;
        }
    }
    if(flag == 0) cout << endl;
    return 0;
} 

Guess you like

Origin www.cnblogs.com/tsruixi/p/11785213.html