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

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 0x3f3f3f3f
using 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值