UVA - 1610 - Party Games(模拟or枚举)

来源:互联网 发布:整套python教学视频 编辑:程序博客网 时间:2024/06/06 18:35

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 (2 ≤ n ≤ 1,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


题意:给出偶数个名字,保证每个名字都不同,求一个字符串使得可以通过比较将名字分成两个team。

最开始用模拟做,排序后比较中间两个字符串,比的快哭了QAQ 结果还脑子糊想不清楚,难过。

队长大大做完后说你个渣渣,直接暴力搜啊23333

暴力枚举每个位置的字符,枚举一次比一次,一旦满足条件(大于等于n/2+1位置的,小于n/2位置的),直接输出。队长你厉害orzzzzzzzzzzz


#include <stdio.h>#include <math.h>#include <string.h>#include <algorithm>#include <queue>#include <iostream>#include <assert.h>#define INF 0x3f3f3f3fusing namespace std;int main(){  int n;  string s[1005];  while (cin >> n) {    if(n == 0) break;    for (int i = 0; i < n; i++) {      cin >> s[i];    }    sort(s, s + n); //最开始用char做不会排序2333  发现string真方便    int m1 = n / 2 - 1;    string ans = "";    bool flag = 0;    int cnt = 0;    while (1) {      string s2 = "";      for (char i = 'A'; i <= 'Z'; i++) {        s2 = ans + i;        if (s2 >= s[m1] && s2 < s[m1 + 1]) {          cout << s2 << endl;          flag = 1;          break;        }      }      if (flag) break;      ans += s[m1][cnt++];    }  }  return 0;}



0 0