UVALive Archive - 6196 - Party Games
题目:
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
题意是给你一些字符串,按照字典序排好以后用一个字符串c将它们平均分开为两半,这个c要大于等于一边的所有字符串,小于另一边的所有字符串,同时要求c是最短的。
排位赛没有做出来,后来看到盛爷的代码以后发现原来可以暴力枚举过。
做法是先对这些字符串排序,去中间的两个串,然后按照长度从短到长枚举c,对于每一个位置都从a到前面那个字符串的当前位的字母,然后判断当前的c是否大于等于前面的串小于后面的串,如果是,就输出,否则继续枚举下一位。
代码:
1 #include <iostream> 2 #include <string> 3 #include <stdio.h> 4 #include <algorithm> 5 #define MAX 1000 6 using namespace std; 7 8 string s[MAX]; 9 string c; 10 11 void check(int n) 12 { 13 int j; 14 c=""; 15 j=0; 16 while(1) 17 { 18 c+='A'; 19 while(c[j]<='Z') 20 if(s[n]>c) c[j]++; 21 else break; 22 if(c[j]<='Z' && s[n]<=c && c<s[n+1]) return ; 23 if(s[n][j]!=c[j]) c[j]--; 24 j++; 25 } 26 } 27 28 int main() 29 { 30 int n,i; 31 while(cin>>n,n) 32 { 33 c.clear(); 34 for(i=0;i<n;i++) 35 { 36 s[i].clear(); 37 cin>>s[i]; 38 } 39 sort(s,s+n); 40 n=n/2-1; 41 check(n); 42 cout<<c<<endl; 43 } 44 return 0; 45 }