Party Games(uva 1610) 卡细节

来源:互联网 发布:微表情测试软件 编辑:程序博客网 时间:2024/06/07 16:40

You’ve been invited to a party. The hostwants to divide the guests into 2 teams for party games, with exactly the samenumber of guests on each team. She wants to be able to tell which guest is onwhich team as she greets them when they arrive. She’d like to do so as easilyas possible, without having to take the time to look up each guest’s name on alist. Being a good computer scientist, you have an idea: give her a singlestring, and all she has to do is compare the guest’s name alphabetically tothat string. To make this even easier, you would like the string to be as shortas possible. Given the unique names of n party guests (n is even), find theshortest possible string S such that exactly half the names are less than orequal to S, and exactly half are greater than S. If there are multiple stringsof the same shortest possible length, choose the alphabetically smallest stringfrom among them.

 

Input There may be multiple test cases inthe input. Each test case will begin with an even integer n (2 ≤ n ≤ 1, 000) onits own line. On the next n lines will be names, one per line. Each name willbe a single word consisting only of capital letters and will be no longer than30 letters. The input will end with a ‘0’ on its own line.

 

Output For each case, print a single linecontaining the shortest possible string (with ties broken in favor of thealphabetically smallest) that your host could use to separate her guests. Thestrings 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

 

题意

给定偶数n个字符串,要求找出一个最短的字符串使得有一半的字符串字典序小于等于该串,另一半大于该串,保证有解。

 

思路

   思路很简单,就是给字符串排序考虑最中间的两个串就行。别的不多说,给几组坑爹的数据就会做了。卡了2小时想哭。

 

Sample input

2

ABCDEFGH

AC

Sample output

ABD

 

Sample input

2

XXXXA

XXXXB

Sample output

XXXXA

 

Sample input

2

XXXXAHHHH

XXXXBP

Sample output

XXXXB

 

Sample input

2

XXXXAHHHH

XXXXB

Sample output

XXXXAI

 

Sample input

2

XXXXAZZZZ

XXXXB

Sample output

XXXXAZZZZ

 

Sample input

2

XXXXAZZZZHKLJ

XXXXB

Sample output

XXXXAZZZZI

 

最坑的是这个:

Sample input

2

XXXXAZZZZZA

XXXXB

Sample output

XXXXAZZZZZA

 

代码

#include<bits/stdc++.h>using namespace std;const int maxn = 50;int n;vector<string> s;char ans[maxn], in[maxn];int main() {while (scanf("%d", &n) == 1 && n) {s.clear();memset(ans, 0, sizeof(ans));for (int i = 0; i < n; i++) {scanf("%s", in);s.push_back(string(in));}sort(s.begin(), s.end());int i;string s1 = s[n/2 - 1], s2 = s[n/2];int len1 = s1.length(), len2 = s2.length();if (len1 <= len2) {for (i = 0; i < len1; i++) {if (s1[i] == s2[i]) ans[i] = s1[i];else break;}if (i != len1) {if (i + 1 == len1) ans[i] = s1[i];else ans[i] = s1[i] + 1;}}else {for (i = 0; i < len2; i++) {if (s1[i] == s2[i]) ans[i] = s1[i];else break;}if (i + 1 == len2) {if (s1[i] + 1 < s2[i]) ans[i] = s1[i] + 1;else {ans[i] = s1[i];for (i++; i < len1; i++) {if (s1[i] == 'Z') ans[i] = s1[i];else break;}if (i < len1 - 1) ans[i] = s1[i] + 1;else if (i == len1 - 1) ans[i] = s1[i];}}else { ans[i] = s1[i] + 1; }}printf("%s\n", ans);}return 0;}

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 捐衣服 红衣服 化缘 和尚化缘 化缘是什么意思 化肥绕口令 中国化肥网 化肥 化肥网 黑化肥 化肥拼音 化肥的拼音 化肥行情 史丹利化肥 化肥价格 化肥品牌 化肥造句 假化肥名单 绕口令化肥 化肥厂 灰化肥 农用化肥 化肥代理商 洋丰化肥 国光化肥 化肥化验 化肥包装秤 化肥图片 中国化肥 化肥是什么 化肥招聘 中国化肥网化肥价格 化肥的作用 化肥报价 化肥价 化肥种类 化肥厂家 化肥袋 化肥袋厂家 化肥价格表 化肥管