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:
- the short name is the same as three first letters of the team's name, for example, for the mentioned club it is "DIN",
- 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.
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.
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.
把队名缩写 : 1. 第一种缩写为 第一个字符串的前三位
2. 第二种缩写为 第一个字符串的前两个位 + 第二个字符串的第一位
最后所有队名的缩写不同~且如果一些队第一个队名相同,则这些队均不可使用第一个队名~
思路:
1.先找出第一种队名相同的,如果存在第二种队名也相同直接输出NO。然后把他们的第二种队名标记上。
2.然后开始循环这些第一种队名不同的,我们想,只要第一种队名都不曾标记过就可以了。所以如果第一种队名被标记过,我们看第二种队名,如果也被标记了,直接NO。否则,我们就把这第二种队名标记上,然后这些都是必须选第二种队名的。
这样做一次之后可能会对之前的第一种队名有影响(标记增多)所以我们要重复执行上一个步骤,知道所有的不再有重复。
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1005;
int n;
string s1,s2;
map<string,bool>q;
struct node
{
string fst,sec;
int num;
int ans=0;
bool operator<(const node &a)const
{
if(fst!=a.fst)return fst<a.fst;
else return sec<a.sec;
}
} p[MAXN];
bool cmp(node a,node b)
{
return a.num<b.num;
}
int main()
{
scanf("%d",&n);
for(int i=0; i<n; ++i)
{
cin>>s1>>s2;
p[i].fst=s1.substr(0,3);
p[i].sec=s1.substr(0,2)+s2[0];
p[i].num=i;
}
sort(p,p+n);
for(int i=1; i<n; ++i)
{
if(p[i].fst==p[i-1].fst)
{
if(p[i].sec==p[i-1].sec)
{
puts("NO");
return 0;
}
p[i].ans=p[i-1].ans=2;
q[p[i].sec]=q[p[i-1].sec]=1;
}
}
bool ok=1;
while(ok)
{
ok=0;
for(int i=0; i<n; ++i)
{
if(p[i].ans)continue;
bool &t=q[p[i].fst];
bool &tt=q[p[i].sec];
if(t)
{
if(!tt)
{
p[i].ans=2;
tt=1;
ok=1;
}
else
{
puts("NO");
return 0;
}
}
}
}
puts("YES");
sort(p,p+n,cmp);
for(int i=0; i<n; ++i)
{
if(p[i].ans==2)cout<<p[i].sec<<endl;
else cout<<p[i].fst<<endl;
}
return 0;
}