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是否大于等于前面的串小于后面的串,如果是,就输出,否则继续枚举下一位。 代码:
#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#define MAX 1000
using namespace std; string s[MAX];
string c; void check(int n)
{
int j;
c="";
j=;
while()
{
c+='A';
while(c[j]<='Z')
if(s[n]>c) c[j]++;
else break;
if(c[j]<='Z' && s[n]<=c && c<s[n+]) return ;
if(s[n][j]!=c[j]) c[j]--;
j++;
}
} int main()
{
int n,i;
while(cin>>n,n)
{
c.clear();
for(i=;i<n;i++)
{
s[i].clear();
cin>>s[i];
}
sort(s,s+n);
n=n/-;
check(n);
cout<<c<<endl;
}
return ;
}
6196
最新文章
- 第六课——UIDynamicAnimator
- Android(Java):jni源代码
- 百度图片爬虫-python版
- c++将引用作为函数的参数---6
- poj1733 带权并查集
- Oracle SQL 劈开字符串
- 编译时IOS Device 无法选择的情况
- thinkphp 使用过程中遇到的一个小函数
- $_FILES系统函数
- Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)的解决方法
- Linux sed command
- PHP大法
- 最大流 USTC1280
- Holer实现外网访问ARM嵌入式Linux系统
- 关于 Kubernetes 中的 Volume 与 GlusterFS 分布式存储
- Rancher2.0中邮件通知的设置
- easyui 改变单元格背景颜色
- 常用类及 LeetCode 每日一题
- 通过 onclick = ";test()";事件定义的事件 , 如何触发.
- 初始if..else 条件语句