题意:输入一个n(n是偶数)个字符串的集合,找一个长度最短的字符串S,使得集合当中恰好有一半的字符串小于等于S,有一半大于等于S,如果有多解的话,输出字典序最小的解。
思路:求出来中间的两个字符串然后贪心即可。
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define MAXN 200000
#define MAXE 210
#define INF 10000000
#define MOD 1000000007
#define LL long long
#define pi acos(-1.0)
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int n;
while (cin >> n && n) {
vector<string> vec;
string str;
for (int i = 0; i < n; ++i) {
cin >> str;
vec.push_back(str);
}
sort(vec.begin(), vec.end());
string temp1 = vec[n / 2 - 1];
string temp2 = vec[n / 2];
bool flag = false;
for (int i = 0; i < temp1.length(); ++i) {
string temp = temp1.substr(0, i);
string s = temp1.substr(0, i + 1);
if (s >= temp1 && s <= temp2) {
flag = true;
cout << s << endl;
break;
} else {
if (temp1[i] == 'Z') {
continue;
} else {
temp += (char)(temp1[i] + 1);
if (temp >= temp1 && temp < temp2) {
cout << temp << endl;
flag = true;
break;
}
}
}
}
if (!flag) {
cout << temp1 << endl;
}
}
}
/*
4
FRED
SAM
JOE
MARGARET
2
FRED
FREDDIE
2
JOSEPHINE
JERRY
2
LARHONDA
LARSEN
0
*/