别把题目理解的太复杂
多复习字符串处理函数
reverse
在algorithm
里
取字符串函数是 substr()
读入时 第一个数后面把 \n读了,不然字符串会读入的不正确
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
int main(){
scanf("%d\n",&n);
string s,ans,a;
for(int i=0; i<n; i++){
getline(cin,s);
reverse(s.begin(), s.end());
if(i==0){
ans = s;
continue;
}
else {
int k = min(ans.length(),s.length());
for(int j=0; j<k; j++){
if(s[j] != ans[j]){
ans = ans.substr(0,j);
break;
}
}
}
}
reverse(ans.begin(),ans.end());
if(ans.length() == 0) printf("nai\n");
else cout << ans;
return 0;
}