UVA - 1610 - Party Games(模拟or枚举)
2016-07-26 10:11
561 查看
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
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; }
相关文章推荐
- MongoDB Replica set + sharding
- MySQL用变量的方法添加伪序号列
- 《Android开发艺术探索读书笔记一》
- PHP 读取/导出 CSV文件
- iOS: 如何调节UITabbarItem的图片和文字位置
- Chrome 控制台console的用法
- 获取微信公众号一键关注链接
- 解决问题的思路(如故事版里面有东西,却不见了)(swift里面开发比较多)
- 谈谈对spring的理解
- Exchange 2013的邮件队列中存在大量的InboundProxy@contoso.com邮件原因
- 按照某一列统计数据
- 为a标签设置行宽高无效?
- vim相关文章整理
- 【HDU】1087 - Super Jumping! Jumping! Jumping!(dp)
- POJ1422
- Android提醒微技巧,你真的了解Dialog、Toast和Snackbar吗?
- Do not concatenate text displayed with setText. Use resource string with placeholders. 提示
- ReflectionToStringBuilder的使用分析与优化
- Scala入门到精通——第十四节 Case Class与模式匹配(一),scala入门到精通
- arm linux 移植usb转串口驱动