http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11396&courseid=0
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 as they arrive, and 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 determine whether the guest's name is alphabetically less than or greater than
that string.
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’s more than one string of the shortest length, output the one that comes
first alphabetically.
Input
There will 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 at least one
letter and no longer than 30 letters. All of the names in a test case will be unique. The
input will end with a 0 on its own line.
Output
For each case, output the alphabetically first of all of the shortest possible strings that
your host could use to separate her guests. Output this string using all upper case
letters. Do not output any spaces. Do not put a blank line between outputs.
Sample Input
4
FRED
SAM
JOE
MARGARET
2
FRED
FREDDIE
2
JOSEPHINE
JERRY
0
Sample Output
K
FRED
JF
题意:要求输出一个字符串,这个字符串大于等于其中一半给定的字符串,并小于另一半,要求长度最短,长度同样短的话就找字典序最小的
思路:这道题卡了好久,经过无数次想测试数据终于是过了
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; struct node { char name[50]; int len; } a[1005]; int cmp(node x,node y) { return strcmp(x.name,y.name)<0; } int main() { int n,i,l,k,minn,j; char ans[35]; while(~scanf("%d",&n),n) { memset(ans,'\0',sizeof(ans)); for(i = 0; i<n; i++) { scanf("%s",a[i].name); a[i].len = strlen(a[i].name); } l = 0; sort(a,a+n,cmp);//先把给定的字符串按字典序升序排列 k = n/2;//中间的字符串 for(i = 0; i<a[k-1].len; i++) { if(a[k].name[i] == a[k-1].name[i])//相同的直接放入ans内 ans[l++] = a[k].name[i]; else if(a[k].name[i] > a[k-1].name[i] && (i+1)!=a[k-1].len)//如果此时第k个串的字符大于k-1的字符,并且没有到k-1的末尾 { if(a[k].name[i]-a[k-1].name[i] == 1 && (i+1) == a[k].len)//如果此时k与k-1的相应位置字符相差1并且已经到了k串的结尾 { ans[l++] = a[k-1].name[i];//先将此时k-1串的字符放入ans for(j = i+1; j<a[k-1].len; j++) { if(a[k-1].name[j] == 'Z')//如果是z,放入 ans[l++] = a[k-1].name[j]; else { if(j!=a[k-1].len-1)//不是Z且没有到末尾,这个字符串增大1,得出答案 ans[l++] = a[k-1].name[j]+1; else//不是Z但是到了末尾,那么直接放入ans得出答案 ans[l++] = a[k-1].name[j]; break; } } break; } ans[l++] = a[k-1].name[i]+1;//相差大于1,那么可以直接将k-1的那个字符+1得出答案 break; } else if(a[k].name[i] > a[k-1].name[i] && (i+1) == a[k-1].len)//如果k大于k-1的字符,并且k-1到了结尾 { ans[l++] = a[k-1].name[i];//那么直接将k-1的字符放入ans即可得出答案 break; } } ans[l] = '\0'; printf("%s\n",ans); } return 0; }