You've been invited to a party. The host wants to divide the guests into 2 teams for party games, with exactly the same number of guests on each team. She wants to be able to tell which guest is on which team as she greets them when they arrive. She'd like to do so as easily as possible, without having to take the time to look up each guest's name on a list.
Being a good computer scientist, you have an idea: give her a single string, and all she has to do is compare the guest's name alphabetically to that string. To make this even easier, you would like the string to be as short as possible.
Given the unique names of n party guests (n is even), find the shortest possible string S such that exactly half the names are less than or equal to S, and exactly half are greater than S. If there are multiple strings of the same shortest possible length, choose the alphabetically smallest string from among them.
Input There may be multiple test cases in the input.
Each test case will begin with an even integer n (2n1, 000) on its own line.
On the next n lines will be names, one per line. Each name will be a single word consisting only of capital letters and will be no longer than 30 letters.
The input will end with a `0' on its own line.
Output For each case, print a single line containing the shortest possible string (with ties broken in favor of the alphabetically smallest) that your host could use to separate her guests. The strings should be printed in all capital letters.
Sample Input
4
FRED
SAM
JOE
MARGARET
2
FRED
FREDDIE
2
JOSEPHINE
JERRY
2
LARHONDA
LARSEN
0
Sample Output
K
FRED
JF
LARI 題目大意:給你n個不相同的字符串,讓你找出一個字符串s,s要滿足以下條件:1、把n個字符串分為兩部分,s >= (n / 2)個字符串, 並且 s < (n / 2) 個字符串;2、s 的長度盡可能短。 3、如果有多個長度相同的符合條件的s,輸出字典序最小的s。 解題思路:先對字符串排序,分析第(n / 2)個字符串a和第(n / 2 + 1)個字符串b。此題需要想法全面,注意細節。 請看代碼:
#include <iostream> #include<cstring> #include<algorithm> #include<string> #include<cmath> #include<cstdio> using namespace std; const int MAXN = 1005 ; string s[MAXN] ; string a , b ; char ans[35] ; int we(string c , string d) { int len = min(c.length() , d.length() ) ; int i ; for(i = 0 ; i < len ; i ++) { if(c[i] != d[i]) { return i ; } } return -1 ; } int main() { int n ; while (scanf("%d" , &n) != EOF) { memset(ans , 0 , sizeof(ans)) ; if(n == 0) break ; int i ; for(i = 0 ; i < n ; i ++) { cin >> s[i] ; } sort(s , s + n) ; a = s[n / 2 - 1] ; b = s[n / 2] ; int lena , lenb ; lena = a.length() ; lenb = b.length() ; int pos = -1 ; if(lena < lenb) { pos = we(a , b) ; if(pos == -1) { cout << a << endl ; } else { if(pos < lena - 1) { for(i = 0 ; i <= pos - 1 ; i ++) { ans[i] = a[i] ; } ans[pos] = a[pos] + 1 ; ans[pos + 1] = '\0' ; } else { for(i = 0 ; i <= pos ; i ++) { ans[i] = a[i] ; } ans[pos + 1] = '\0' ; } printf("%s\n" , ans) ; } } else if(lena == lenb) { pos = we(a , b) ; if(pos < lena - 1) { for(i = 0 ; i <= pos - 1 ; i ++) { ans[i] = a[i] ; } ans[pos] = a[pos] + 1 ; ans[pos + 1] = '\0' ; } else { for(i = 0 ; i <= pos ; i ++) { ans[i] = a[i] ; } ans[pos + 1] = '\0' ; } printf("%s\n" , ans) ; } else { pos = we(a , b) ; if(pos < lenb - 1) { for(i = 0 ; i < pos ; i ++) { ans[i] = a[i] ; } ans[pos] = a[pos] + 1 ; ans[pos + 1] = '\0' ; } else { if(a[pos ] != b[pos] - 1) { for(i = 0 ; i < pos ; i ++) { ans[i] = a[i] ; } ans[pos] = a[pos] + 1 ; ans[pos + 1] = '\0' ; } else { for(i = 0 ; i <= pos ; i ++) { ans[i] = a[i] ; } pos ++ ; while (pos < lena ) { if(a[pos] < 'Z') { if(pos == lena - 1) { ans[pos] = a[pos] ; ans[pos + 1] = '\0' ; // cout << "NO" << endl ; break ; } else { ans[pos] = a[pos] + 1 ; ans[pos + 1] = '\0' ; break ; } } else { if(pos == lena - 1) { ans[pos] = a[pos] ; ans[pos + 1] = '\0' ; break ; } else { ans[pos] = a[pos] ; pos ++ ; } } } } } printf("%s\n" , ans) ; } } return 0; }