Uva 1610 - Party Games(模拟)
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;
}