现在的位置: 首页 > 综合 > 正文

BNU 29045 Party Games – from lanshui_Yang

2019年01月07日 ⁄ 综合 ⁄ 共 2910字 ⁄ 字号 评论关闭

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.

\epsfbox{p6196.eps}

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$ \le$n$ \le$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
     题目大意:给你n个不相同的字符串,让你找出一个字符串s,s要满足以下条件:1、把n个字符串分为两部分,s >= (n / 2)个字符串, 并且 s < (n / 2) 个字符串;2、s 的长度尽可能短。 3、如果有多个长度相同的符合条件的s,输出字典序最小的s。
     解题思路:先对字符串排序,分析第(n / 2)个字符串a和第(n / 2 + 1)个字符串b。此题需要想法全面,注意细节。
     请看代码:
#include <iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdio>
using namespace std;
const int MAXN = 1005 ;
string s[MAXN] ;
string a , b ;
char ans[35] ;
int  we(string c , string d)
{
    int len = min(c.length() , d.length() ) ;
    int i ;
    for(i = 0 ; i < len ; i ++)
    {
        if(c[i] != d[i])
        {
            return i ;
        }
    }
    return -1 ;
}
int main()
{
    int  n ;
    while (scanf("%d" , &n) != EOF)
    {
        memset(ans , 0 , sizeof(ans)) ;
        if(n == 0)
            break ;
        int i ;
        for(i = 0 ; i < n ; i ++)
        {
            cin >> s[i] ;
        }
        sort(s , s + n) ;
        a = s[n / 2 - 1] ;
        b = s[n / 2] ;
        int lena , lenb ;
        lena =  a.length() ;
        lenb = b.length() ;
        int pos = -1 ;
        if(lena < lenb)
        {
            pos = we(a , b) ;
            if(pos == -1)
            {
                cout << a << endl ;
            }
            else
            {
                if(pos < lena - 1)
                {
                    for(i = 0 ; i <= pos - 1 ; i ++)
                    {
                        ans[i] = a[i] ;
                    }
                    ans[pos] = a[pos] + 1 ;
                    ans[pos + 1] = '\0' ;
                }
                else
                {
                    for(i = 0 ; i <= pos ; i ++)
                    {
                        ans[i] =  a[i] ;
                    }
                    ans[pos + 1] = '\0' ;
                }
                printf("%s\n" , ans) ;
            }
        }
        else if(lena == lenb)
        {
            pos = we(a , b) ;
            if(pos < lena - 1)
            {
                for(i = 0 ; i <= pos - 1 ; i ++)
                {
                    ans[i] = a[i] ;
                }
                ans[pos] = a[pos] + 1 ;
                ans[pos + 1] = '\0' ;
            }
            else
            {
                for(i = 0 ; i <= pos ; i ++)
                {
                    ans[i] = a[i] ;
                }
                ans[pos + 1] = '\0' ;
            }
            printf("%s\n" , ans) ;
        }
        else
        {
            pos = we(a , b) ;
            if(pos < lenb - 1)
            {
                for(i = 0 ; i < pos ; i ++)
                {
                    ans[i] = a[i] ;
                }
                ans[pos] = a[pos] + 1 ;
                ans[pos + 1] = '\0' ;
            }
            else
            {
                if(a[pos ] != b[pos] - 1)
                {
                    for(i = 0 ; i < pos ; i ++)
                    {
                        ans[i] = a[i] ;
                    }
                    ans[pos] = a[pos] + 1 ;
                    ans[pos + 1] = '\0' ;
                }
                else
                {
                    for(i = 0 ; i <= pos ; i ++)
                    {
                        ans[i] = a[i] ;
                    }
                    pos ++ ;
                    while (pos < lena )
                    {
                        if(a[pos] < 'Z')
                        {
                            if(pos == lena  - 1)
                            {
                                ans[pos] = a[pos] ;
                                ans[pos + 1] = '\0' ;
                               // cout << "NO" << endl ;
                                break ;
                            }
                            else
                            {
                                ans[pos] = a[pos] + 1 ;
                                ans[pos + 1] = '\0' ;
                                break ;
                            }
                        }
                        else
                        {
                            if(pos == lena - 1)
                            {
                                ans[pos] = a[pos] ;
                                ans[pos + 1] = '\0' ;
                                break ;
                            }
                            else
                            {
                                ans[pos] = a[pos] ;
                                pos ++ ;

                            }
                        }
                    }
                }
            }
            printf("%s\n" , ans) ;
        }
    }
    return 0;
}

抱歉!评论已关闭.