【贪心】codeforces45E Director

来源:互联网 发布:电动牙刷 博朗 知乎 编辑:程序博客网 时间:2024/05/21 14:57
E. Director
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya is a born Berland film director, he is currently working on a new blockbuster, "The Unexpected". Vasya knows from his own experience how important it is to choose the main characters' names and surnames wisely. He made up a list of n names and n surnames that he wants to use. Vasya haven't decided yet how to call characters, so he is free to match any name to any surname. Now he has to make the list of all the main characters in the following format: "Name1 Surname1Name2 Surname2...NamenSurnamen", i.e. all the name-surname pairs should be separated by exactly one comma and exactly one space, and the name should be separated from the surname by exactly one space. First of all Vasya wants to maximize the number of the pairs, in which the name and the surname start from one letter. If there are several such variants, Vasya wants to get the lexicographically minimal one. Help him.

An answer will be verified a line in the format as is shown above, including the needed commas and spaces. It's the lexicographical minimality of such a line that needs to be ensured. The output line shouldn't end with a space or with a comma.

Input

The first input line contains number n (1 ≤ n ≤ 100) — the number of names and surnames. Then follow n lines — the list of names. Then follow n lines — the list of surnames. No two from those 2n strings match. Every name and surname is a non-empty string consisting of no more than 10 Latin letters. It is guaranteed that the first letter is uppercase and the rest are lowercase.

Output

The output data consist of a single line — the needed list. Note that one should follow closely the output data format!

Sample test(s)
input
4AnnAnnaSabrinaJohnPetrovIvanovaStoltzAbacaba
output
Ann Abacaba, Anna Ivanova, John Petrov, Sabrina Stoltz
input
4AaAbAcBaAdAeBbBc
output
Aa Ad, Ab Ae, Ac Bb, Ba Bc
【翻译】

问题描述

Vasya是一位天生的Berland电影导演,他现在正在努力制作一部新的大片"The Unexpected"。根据自己的经验,Vasya懂得为主角明智地选择一个名字(name)和姓氏(surname)是多么的重要。他制作了一个含有他想要使用的n个名字和n个姓氏的列表,但是Vasya还没有决定这些角色的具体姓名,所以他可以自由的将任何名字和姓氏匹配起来。现在,他需要以"name_1 surname_1, name_2 surname_2, ..., name_n surname_n"(即所有姓名之间用一个逗号一个空格隔开,名和姓之间用一个空格隔开)这样的格式写出一个含有所有主角的姓名的列表。首先Vasya想使名和姓的首字母相同的姓名对数最多,如果有多解,Vasya希望得到字典序最小的,帮帮他吧。
  答案应该严格按照上述格式输出,包括逗号和空格,应保证输出的一行是所有可能的解中字典序最小的,行末不能有多余的逗号或空格。

输入格式(direct.in)

  输入第一行是一个整数n ( 1n ≤100 ),表示有nnamensurname
  接下来n行是name,再有n行是surname
  这2n个字符串中任意两个字符串都不同。每个name或者surname是一个不超过10个字母的非空字符串,保证首字母大写且剩余部分都是小写。

输出格式(direct.out)

  输出应有一行,即所要求的列表,注意这一行要严格遵守输出格式。

样例输入

样例输入1
4
Ann
Anna
Sabrina
John
Petrov
Ivanova
Stoltz
Abacaba
样例输入2
4
Aa
Ab
Ac
Ba
Ad
Ae
Bb
Bc

样例输出

样例输出1
Ann Abacaba, Anna Ivanova, John Petrov, Sabrina Stoltz
样例输出2
Aa Ad, Ab Ae, Ac Bb, Ba Bc

数据规模和约定

  对于 20%数据保证所有字符串首字母一样。
  对于 100% 数据保证 n ≤ 100

 

【题解】先排序,用need、offer数组分别记录需要的量和提供的量,为匹配做准备

       按顺序匹配,能将j(姓)匹配给i(名)的情况是

           1、j有剩余而i不足

           2、j有剩余且j>=i(首字母大小) j>=i保证j已经扫过了i能匹配的位置或正好处于i能匹配的位置

           3、i=j 且 i不足

           4、i=j 且 j>=i

#include<iostream>#include<algorithm>#include<string>using namespace std;string a[500],b[500];int i,n,j,with[500],use[500],need[500],k,offer[500];int main(){    while(cin>>n){        for(i=1;i<=n;++i)            cin>>a[i];        for(i=1;i<=n;++i)            cin>>b[i];        sort(a+1,a+n+1);        sort(b+1,b+n+1);        for(i=1;i<=n;++i)            ++need[a[i][0]],++offer[b[i][0]];        j=1;k=1;        for(i=1;i<=n;++i){            for(j=1;j<=n;++j){                if(use[j]==0)                if((need[b[j][0]]<offer[b[j][0]]||a[i][0]==b[j][0]))                 if(offer[a[i][0]]<need[a[i][0]]||b[j][0]>=a[i][0])break;            }                    with[i]=j;use[j]=1;--offer[b[j][0]];--need[a[i][0]];        }        cout<<a[1]<<" "<<b[with[1]];        for(i=2;i<=n;++i)cout<<", "<<a[i]<<" "<<b[with[i]];    }    return 0;}


0 0
原创粉丝点击