【PAT甲级】1077. Kuchiguse (20)

来源:互联网 发布:淘宝卖什么好 编辑:程序博客网 时间:2024/06/05 07:56
注意:1. getline()可能get到空行,需要过滤一下2. 头文件algorithm中的reverse可以对字符串进行反转
#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;int findMinLength(vector<string> v);int main() {    int n;    cin >> n;    vector<string> v;    string line;    for (int i = 0; i < n; i++) {        getline(cin, line);        if (line.empty()) {            i--;            continue;//////////        }        reverse(line.begin(), line.end());        v.push_back(line);    }    int m = findMinLength(v);    int count = 0;    bool flag = true;    for (int i = 0; i < m && flag; i++) {        char c = v[0].at(count);        for (int j = 0; j < n; j++) {            if (c != v[j].at(count)){                flag = false;                break;            }        }        if (flag) count++;    }    if (!count) cout << "nai" << endl;    else {        reverse(v[0].begin(), v[0].end());        cout << v[0].substr(v[0].length() - count) << endl;;    }    return 0;}int findMinLength(vector<string> v) {    int min = v[0].length();    for (int i = 0; i < v.size(); i++) {        if (v[i].length() < min)            min = v[i].length();    }    return min;}
原创粉丝点击