You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<bits/stdc++.h>
using namespace std;
const int N = 210;
string s[N];
//n是每个输入的字符串的个数
int n;
//len是用来控制有多少个字符是相同的
int len;
int main() {
//优化输入
ios::sync_with_stdio(false);
while (cin >> n && n) {
//记录最大长度
int sz = 0;
//接收字符串数组,并且反转
for (int i = 0; i < n; i++) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
sz = max(sz, (int) s[i].size());
}
//标识
bool flag = false;
//遍历每一个可能的长度,没有终止边界内部肯定存在break
for (len = 0; len < sz; len++) {
//遍历每一个字符串
for (int i = 0; i < n; i++) {
//以每一个字符串的这一位与每一个字符串的这一位进行对比
if (s[0][len] != s[i][len]) {
flag = true;
break;
}
}
if (flag) break;
}
//字符串的相同后缀长度
if (len > 0) {
//还是拿第一个字符串为操作样例,把它反转回来
reverse(s[0].begin(), s[0].end());
//利用substr截取它的后面数len位
cout << s[0].substr((int) s[0].size() - len) << endl;
} else cout << "" << endl;
}
return 0;
}