Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League

地址:http://codeforces.com/contest/782/problem/D

题目:

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 3 and 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
2
DINAMO BYTECITY
FOOTBALL MOSCOW
output
YES
DIN
FOO
input
2
DINAMO BYTECITY
DINAMO BITECITY
output
NO
input
3
PLAYFOOTBALL MOSCOW
PLAYVOLLEYBALL SPB
GOGO TECHNOCUP
output
YES
PLM
PLS
GOG
input
3
ABC DEF
ABC EFG
ABD OOO
output
YES
ABD
ABE
ABO
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.

 

 思路:先找出并标记第一类队名有重复的所有队,这些队只能使用第二类队名(这些队名暂记为B),如果这些队的第二类队名重复就无解。

  然后处理剩下的第一类队名没重复的队(记为A),本来是这些队是可以直接选第一类队名的,但是因为这些第一类队名可能和先处理的第二类队名重复,所以要进行以下处理:

  1.判断有无第一队名和先前处理的第二类队名重复的,有则判断此时选择第二类是否可行,可行就将该队加入B中,不行就无解。无重复的队先不处理。

  2.因为新B队中可能和A队中有重复的,所以要重复执行步骤1,直到AB中没重复的。

 

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-6;
10 const double pi=acos(-1.0);
11 const int K=1e5+7;
12 const int mod=1e9+7;
13 
14 int n,ans,same[1001],sh[1001];
15 char sa[1001][50],sb[1001][50];
16 map<string,int>hs;//hs哈希队名,hs[x]记录哈希值为x的第一个出现的队的序号
17 string ss,sc[1001];
18 
19 int main(void)
20 {
21     cin>>n;
22     ss="sac";
23     for(int i=1,cnt=1;i<=n;i++)
24     {
25         scanf("%s%s",&sa[i][1],&sb[i][1]);
26         ss[0]=sa[i][1],ss[1]=sa[i][2],ss[2]=sa[i][3];
27         if(hs[ss])  same[i]=1,same[sh[hs[ss]]]=1;
28         else    hs[ss]=cnt,sh[cnt++]=i;
29     }
30     hs.clear();
31     for(int i=1;i<=n;i++)
32     if(same[i])//先处理第二类
33     {
34         ss[0]=sa[i][1],ss[1]=sa[i][2],ss[2]=sb[i][1];
35         if(hs[ss])
36         {
37             ans=1;break;
38         }
39         hs[ss]=1,sc[i]=ss;
40     }
41     while(1)//再循环处理第一类队名和第二类队名中重复的
42     {
43         bool ff=1;
44         for(int i=1;i<=n;i++)
45         if(!same[i])
46         {
47             ss[0]=sa[i][1],ss[1]=sa[i][2],ss[2]=sa[i][3];
48             if(hs[ss])
49             {
50                 ss[2]=sb[i][1];
51                 if(hs[ss])
52                 {
53                     printf("NO
");
54                     return 0;
55                 }
56                 same[i]=1;
57                 ff=0;
58                 hs[ss]=1;
59             }
60             sc[i]=ss;
61         }
62         if(ff)
63             break;
64     }
65     for(int i=1;i<=n;i++)//处理剩下的
66     if(!same[i])
67     {
68         ss[0]=sa[i][1],ss[1]=sa[i][2],ss[2]=sa[i][3];
69         sc[i]=ss;
70     }
71     if(ans)
72         printf("NO
");
73     else
74     {
75         printf("YES
");
76         for(int i=1;i<=n;i++)
77             cout<<sc[i]<<endl;
78     }
79     return 0;
80 }
81 
82 /*
83 3
84 abc abc
85 abc dce
86 abd ebc
87 */

 

原文地址:https://www.cnblogs.com/weeping/p/6516532.html