PAT A1077 Kuchiguse

总的来说,这道题也没什么新鲜的,就是从后向前判断是否有相同的结尾;

但是值得注意的是输入的问题,这里用cin的话,如果后面在输入新的字符串, getline会将空格或者回车读入,所以一定要注意,在cin单个字符后,一定要getchar()来代表换行。

剩下的也就没什么,主要的就是从后往前比较,如果发现不同字符,截取。迭代的时候一定要注意选取最小的字符个数进行迭代,从而使得不会出现非法的地址访问

#include<string>
#include<iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    getchar();
    string res;
    getline(cin, res);
    string temp;
    for (int i = 1; i < n; i++) {
        getline(cin, temp);
        int same = 0;
        for (same = 0; same<res.size()&&same<temp.size(); same++) {
            if (res[res.size() - 1 - same] != temp[temp.size() - 1 - same]) {
                break;
            }
        }
        if (same == 0) {
            res = "nai";
            break;
        }
        res = res.substr(res.size()-same);
        //cout << res << endl;
    }
    cout << res;
    cin >> temp;
}

 

相关文章:

  • 2021-09-23
  • 2021-10-28
  • 2021-09-04
  • 2021-04-20
  • 2021-06-09
  • 2022-12-23
  • 2021-12-16
  • 2021-10-11
猜你喜欢
  • 2021-08-20
  • 2021-05-11
  • 2021-11-09
  • 2021-12-07
  • 2022-12-23
  • 2022-01-05
  • 2021-12-16
相关资源
相似解决方案