D.Innokenty and a football league

来源:互联网 发布:淘宝宝贝截图 编辑:程序博客网 时间:2024/06/06 17:44
http://blog.csdn.net/sizaif/article/details/72846578
D. Innokenty and a Football League
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Innokenty is a president of a new football league in Byteland. The first task he should do is to assign short names to all clubs to be shown on TV next to the score. Of course, the short names should be distinct, and Innokenty wants that all short names consist of three letters.

Each club's full name consist of two words: the team's name and the hometown's name, for example, "DINAMO BYTECITY". Innokenty doesn't want to assign strange short names, so he wants to choose such short names for each club that:

  1. the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "DIN",
  2. or, the first two letters of the short name should be the same as the first two letters of the team's name, while the third letter is the same as the first letter in the hometown's name. For the mentioned club it is "DIB".

Apart from this, there is a rule that if for some club x the second option of short name is chosen, then there should be no club, for which the first option is chosen which is the same as the first option for the club x. For example, if the above mentioned club has short name "DIB", then no club for which the first option is chosen can have short name equal to "DIN". However, it is possible that some club have short name "DIN", where "DI" are the first two letters of the team's name, and "N" is the first letter of hometown's name. Of course, no two teams can have the same short name.

Help Innokenty to choose a short name for each of the teams. If this is impossible, report that. If there are multiple answer, any of them will suit Innokenty. If for some team the two options of short name are equal, then Innokenty will formally think that only one of these options is chosen.

Input

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of clubs in the league.

Each of the next n lines contains two words — the team's name and the hometown's name for some club. Both team's name and hometown's name consist of uppercase English letters and have length at least 3and at most 20.

Output

It it is not possible to choose short names and satisfy all constraints, print a single line "NO".

Otherwise, in the first line print "YES". Then print n lines, in each line print the chosen short name for the corresponding club. Print the clubs in the same order as they appeared in input.

If there are multiple answers, print any of them.

Examples
input
2DINAMO BYTECITYFOOTBALL MOSCOW
output
YESDINFOO
input
2DINAMO BYTECITYDINAMO BITECITY
output
NO
input
3PLAYFOOTBALL MOSCOWPLAYVOLLEYBALL SPBGOGO TECHNOCUP
output
YESPLMPLSGOG
input
3ABC DEFABC EFGABD OOO
output
YESABDABEABO
Note

In the first sample Innokenty can choose first option for both clubs.

In the second example it is not possible to choose short names, because it is not possible that one club has first option, and the other has second option if the first options are equal for both clubs.

In the third example Innokenty can choose the second options for the first two clubs, and the first option for the third club.

In the fourth example note that it is possible that the chosen short name for some club x is the same as the first option of another club y if the first options of x and y are different.


题目大意:  现在给你 n 组数据  每组里面有 两个串;  现在 要给 n队伍 取简称;

简称的方法只有两个:  1   第一个串的前三个, 2  第一个串的前两个+ 第二个串的前一个

然后问你 这些队伍能不能全部取到简称,并且不能重复,  

题目有个限制:  如果有x 个队伍的第一种简称相同, 则这个x队伍必须选择第二种,  如果第二种也有重复的, 则NO;  并且选择第二种的 同时会对 之后的队伍选择产生影响;


解题思路;  先处理第一类相同的 (设为x), 相同的去选择第二种,  x 结束后 判断这些x 是否存在相同; 如果没有相同,去处理 剩下第一类不同的(设为y), 

此时对于剩下的 要判断,  之前的(x)的 是不是和现在(y)存在相同,  如果相同NO  不同 为答案,存下来

/*
题目要求:所有的队名皆不能重复,若多个队的第一类相同, 则全部选择第二类; 若
他们的第二队也有相同的 NO
在对于第一类不重复的情况下, 要对之前重复的选择第二类是否有影响
 第二类 通过回溯 回溯回去看看有没有 和之前的重复  则NO

*/
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <stdio.h>
typedef long long ll;
using namespace std;
const int MAXN=10010;
map<string,int> cnt_first,cnt_second,cnt;//cnt 存的是正确答案
struct node
{
    string  first,second,ans;// 前后两个串
    int id;
} num[MAXN];
int n;
int cmp(node a,node b)
{
    return cnt_first[a.first]>cnt_first[b.first];// 用 统计的值 排序
}
int cmp_cnt(node a,node b)
{
    return cnt[a.first]>cnt[b.first];
}
int cmp_id(node a,node b)
{
    return a.id<b.id;
}
int  finds(int k)
{
    int i;
    sort(num+k,num+n+1,cmp_cnt);// 按照答案中第一类个数排序
    if(!cnt[num[k].first]) return k;//  如果第一类和第二类没有重复  结束
    while(k<=n)
    {
        if(cnt[num[k].first])// 如果第一类的答案有
        {
            if(cnt[num[k].second]) return -1;//并且第二类的答案被占用  返回-1 NO
            num[k].ans=num[k].second;// 没有的话  答案存第二类
            cnt[num[k].ans]=1;//标记第二类答案为1
        }
        else
            break;// 没有的话结束就可以
        k++;
    }
    return finds(k);
}
int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n; i++)
        {
            cin>>num[i].first>>num[i].second;
            num[i].id=i;
            num[i].first=num[i].first.substr(0,3);//取前三位
            num[i].second=num[i].first.substr(0,2)+num[i].second[0];//取前两位+后面的第一位
            cnt_first[num[i].first]++;// 统计第一种 相同的字符串 个数 ++;
        }
        sort(num+1,num+n+1,cmp);//按照第一个统计数值排序  第一类数值排序
        int flag=0;
        for(i=1;i<=n;i++)//此时已经按照第一种相同的多少从大到小排序
        {
            if(cnt_first[num[i].first]>1)//如果 第一种的 队伍有多个的话
            {
                if(cnt_second[num[i].second]==1)//判断第二种 是否被占用 no
                    flag=1;
                else
                {
                    num[i].ans=num[i].second;// 此时 当第一类多个,  第二类不同, 是 选择第二类为答案 全部选择第二类
                    cnt_second[num[i].ans]=1;// 把第二类的 这个 标记为 已用
                    cnt[num[i].ans]=1;// 把这个第二类的结果 在答案中标记
                }
            }
            else// 此时说明数目相同 已经处理完了,  到i  跳出即可
                break;
        }
        i=finds(i);// 从i 开始到结束 查找是否存在和前面有相同的部分
        if(i==-1) flag=1;
        if(!flag)// 第一类的  没有重复的
        {
             while(i<=n)
            {
                num[i].ans=num[i].first;
                i++;
            }
        }
        printf("%s\n",flag ?"NO":"YES");
        if(!flag)
        {
            sort(num,num+n+1,cmp_id);//答案从小到大排序
            for(i=1;i<=n;i++)
                cout<<num[i].ans<<endl;
        }
    }
    return 0;
}