题意:
给出n个串(n为偶数);
要构造一个串,使n串中有一半小于等于它,另外一半大于它;
要求这个串长度尽量小,同时字典序小;
思路:
把所有串排个序;
然后拿出中间的两个串比较;
AC
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
const int N = 1005;
string str[N];
string res;
int n;
int main() {
while(scanf("%d",&n) && n) {
getchar();
for(int i = 0; i < n; i++) {
getline(cin ,str[i]);
}
sort(str, str + n);
string a = str[n / 2 - 1];
string b = str[n / 2];
res = "";
for(int i = 0; i < a.size() && i < b.size(); i++) {
if(a[i] == b[i])
res += a[i];
else {
if(i == a.size() - 1) {
res += a[i];
}else if((b[i] - a[i] > 1) || i != b.size() - 1) {
res += (a[i] + 1);
}else {
res += a[i];
for(int j = i + 1; j < a.size(); j++) {
if(j == a.size() - 1) {
res += a[j];
break;
}
if(a[j] != 'Z') {
res += (a[j] + 1);
break;
}
res += 'Z';
}
}
break;
}
}
cout << res << endl;
}
}原文:http://blog.csdn.net/yeyeyeguoguo/article/details/45047301