给出一组字符串D,要找一个字符串S使得D中一半小于等于S,另外一半大于S。输入保证一定有解。长度要尽量短,在此基础上字典序尽量小。

分类谈论,细节挺多的,比如'Z'。

其实直接暴就过了,没分类辣么麻烦。

#include<bits/stdc++.h>
using namespace std; vector<string> vec; int main()
{
// freopen("in.txt","r",stdin);
int n;
char s[];
while(scanf("%d",&n),n){
vec.resize(n);
for(int i = ; i < n; i++){
scanf("%s",s);
vec[i] = s;
}
sort(vec.begin(),vec.end());
int mid = n>>;
string &a = vec[mid-] ,&b = vec[mid];
int i;
int len1 = a.size()-, len2 = b.size()-;
if(len1 < len2){
for(i = ; i < len1; i++){
if(b[i] != a[i]) { s[i] = a[i]+; s[i+] = '\0'; break; }
s[i] = a[i];
}
if(i == len1) {
s[i] = a[i]; s[i+] = '\0';
}
}else if(len1>len2){
for(i = ; i < len2; i++){
if(b[i] != a[i]) { s[i] = a[i]+; s[i+] = '\0'; break; }
s[i] = a[i];
}
if(i == len2) {
if(b[i] != a[i]+) { s[i] = a[i]+; s[i+] = '\0'; }
else {
s[i] = a[i];
for(i++; i < len1; i++){
if(a[i] != 'Z'){
s[i] = a[i] + ; s[i+] = '\0'; break;
}
s[i] = a[i];
}
if(i == len1) { s[i] = a[i]; s[i+] = '\0'; }
}
}
}else {
for(i = ; i < len2; i++) {
if(b[i] != a[i]) { s[i] = a[i]+; s[i+] = '\0'; break; }
s[i] = a[i];
}
if(i == len2) { s[i] = a[i]; s[i+] = '\0'; }
}
printf("%s\n",s);
}
return ;
}
05-11 17:54