您的位置:首页 > 产品设计 > UI/UE

【Codeforces 780 D Innokenty and a Football League 】+ 模拟 + 贪心

2017-03-12 12:27 417 查看
D. Innokenty and a Football League

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard 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:

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.

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.

把队名缩写 : 1. 第一种缩写为 第一个字符串的前三位

2. 第二种缩写为 第一个字符串的前两个位 + 第二个字符串的第一位

最后所有队名的缩写不同~且若若干个对第一个队名相同,者该若干队均不可使用第一个队名~

首先找出所有第一个队名一样的对,他们均只能使用第二中队名,多该队名已被使用,者不满足~

然后在处理第一种队名没有雷同队名,若该队名已被使用过,者判断第二种队名

贪心 + 模拟~所有对尽可能的使用第一种队名~

AC代码:

#include<cstdio>
#include<map>
#include<vector>
#include<iostream>
#include<cstring>
using namespace std;
const int K = 1e3 + 10;
string s1,s2,ss1[K],ss2[K];
map <string,vector <int> > mv;
map <string ,int> m1,m2,v;
map <int,int> x;
int main()
{
int N;
cin >> N;
for(int i = 1; i <= N; i++){
cin >> s1 >> s2;
ss1[i] = "",ss2[i] ="";
ss1[i] += s1.substr(0,3);
ss2[i] += s1.substr(0,2),ss2[i] += s2[0];
mv[ss1[i]].push_back(i);
m1[ss1[i]]++;
}
for(int i = 1; i <= N; i++)
if(!v[ss1[i]]){
int nl = mv[ss1[i]].size();
if(nl > 1){
m1[ss1[i]] = 0;
for(int j = 0; j < nl; j++){
int pl = mv[ss1[i]][j];
x[pl] = 1;
if(++m2[ss2[pl]] > 1){
cout << "NO" << endl;
return 0;
}
}
}
v[ss1[i]] = 1;
}
while(1){
int ok = 1;
for(int i = 1; i <= N; i++)
if(x[i] == 0 && m2[ss1[i]]){
ok = 0;
if(m2[ss2[i]]){
cout << "NO" << endl;
return 0;
}
else{
x[i] = 1;
m2[ss2[i]]++;
}
}
if(ok) break;
}
cout << "YES" << endl;
for(int i = 1; i <= N; i++)
if(x[i]) cout << ss2[i] << endl;
else cout << ss1[i] << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: