UVA1610 PartyGame 聚会游戏(细节题)
给出一组字符串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[142]; while(scanf("%d",&n),n){ vec.resize(n); for(int i = 0; i < n; i++){ scanf("%s",s); vec[i] = s; } sort(vec.begin(),vec.end()); int mid = n>>1; string &a = vec[mid-1] ,&b = vec[mid]; int i; int len1 = a.size()-1, len2 = b.size()-1; if(len1 < len2){ for(i = 0; i < len1; i++){ if(b[i] != a[i]) { s[i] = a[i]+1; s[i+1] = '\0'; break; } s[i] = a[i]; } if(i == len1) { s[i] = a[i]; s[i+1] = '\0'; } }else if(len1>len2){ for(i = 0; i < len2; i++){ if(b[i] != a[i]) { s[i] = a[i]+1; s[i+1] = '\0'; break; } s[i] = a[i]; } if(i == len2) { if(b[i] != a[i]+1) { s[i] = a[i]+1; s[i+1] = '\0'; } else { s[i] = a[i]; for(i++; i < len1; i++){ if(a[i] != 'Z'){ s[i] = a[i] + 1; s[i+1] = '\0'; break; } s[i] = a[i]; } if(i == len1) { s[i] = a[i]; s[i+1] = '\0'; } } } }else { for(i = 0; i < len2; i++) { if(b[i] != a[i]) { s[i] = a[i]+1; s[i+1] = '\0'; break; } s[i] = a[i]; } if(i == len2) { s[i] = a[i]; s[i+1] = '\0'; } } printf("%s\n",s); } return 0; }