题目:题目链接
思路:排序后处理到第一个不同的字符,贪心一下就可以了
AC代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <string> 6 #include <vector> 7 #include <map> 8 #include <set> 9 10 #define FRER() freopen("in.txt", "r", stdin) 11 #define FREW() freopen("out.txt", "w", stdout) 12 13 #define INF 0x3f3f3f3f 14 15 using namespace std; 16 17 int main() 18 { 19 //FRER(); 20 //FREW(); 21 vector<string> vec; 22 string str, ptr; 23 int n; 24 while(cin >> n && n) { 25 vec.clear(); 26 for(int i = 0; i < n; ++i) { 27 cin >> str; 28 vec.push_back(str); 29 } 30 sort(vec.begin(), vec.end()); 31 int b = n / 2, a = b - 1; 32 str = vec[a]; 33 ptr = vec[b]; 34 int len = min(str.length(), ptr.length()); 35 bool ok = true; 36 string s = "", tmp; 37 for(int i = 0; i < len; ++i) { 38 if(str[i] == ptr[i]) 39 s += str[i]; 40 else { 41 tmp = s + (char)(str[i] + 1); 42 if(tmp == ptr) { 43 s += str[i++]; 44 while(i < str.length() - 1 && str[i] == 'Z') 45 s += str[i++]; 46 if(i < str.length() - 1) 47 s += (char)(str[i] + 1); 48 else 49 s = str; 50 } 51 else { 52 if(tmp.length() == str.length()) 53 s = str; 54 else 55 s = tmp; 56 } 57 break; 58 } 59 } 60 cout << s << endl; 61 } 62 return 0; 63 }