仔细分析题意,其实很容易发现,如果将整个序列按照字典序排序之后,最终的结果其实仅仅只与最中间的两个字符串序列是有关的,所以后续的求解就很简单了,取出两个最靠中间的字符串,a和b,逐步取a的子串,然后枚举‘A’ 到‘Z’的字符,将二者拼接,假设拼接之后得到的是temp,如果temp>=a&&temp<b,那么temp其实也就是我们想要的结果了,具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;
const int maxn = 1000 + 10;
string arr[maxn];
int main(){
int n;
while (cin >> n){
if (n == 0) break;
for (int i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
string a = arr[n / 2 - 1];
string b = arr[n / 2];
bool flag = true;
int length = 0;
string temp;
while (flag){
temp = a.substr(0, length);
for (int i = 0; i < 26; i++){
char t = 'A' + i;
string temp2 = temp + t;
if (temp2 >= a&&temp2 < b){
temp = temp2;
flag = false;
break;
}
}
length++;
}
cout << temp << endl;
}
return 0;
}