地址: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中没重复的。

 

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e5+;
const int mod=1e9+; int n,ans,same[],sh[];
char sa[][],sb[][];
map<string,int>hs;//hs哈希队名,hs[x]记录哈希值为x的第一个出现的队的序号
string ss,sc[]; int main(void)
{
cin>>n;
ss="sac";
for(int i=,cnt=;i<=n;i++)
{
scanf("%s%s",&sa[i][],&sb[i][]);
ss[]=sa[i][],ss[]=sa[i][],ss[]=sa[i][];
if(hs[ss]) same[i]=,same[sh[hs[ss]]]=;
else hs[ss]=cnt,sh[cnt++]=i;
}
hs.clear();
for(int i=;i<=n;i++)
if(same[i])//先处理第二类
{
ss[]=sa[i][],ss[]=sa[i][],ss[]=sb[i][];
if(hs[ss])
{
ans=;break;
}
hs[ss]=,sc[i]=ss;
}
while()//再循环处理第一类队名和第二类队名中重复的
{
bool ff=;
for(int i=;i<=n;i++)
if(!same[i])
{
ss[]=sa[i][],ss[]=sa[i][],ss[]=sa[i][];
if(hs[ss])
{
ss[]=sb[i][];
if(hs[ss])
{
printf("NO\n");
return ;
}
same[i]=;
ff=;
hs[ss]=;
}
sc[i]=ss;
}
if(ff)
break;
}
for(int i=;i<=n;i++)//处理剩下的
if(!same[i])
{
ss[]=sa[i][],ss[]=sa[i][],ss[]=sa[i][];
sc[i]=ss;
}
if(ans)
printf("NO\n");
else
{
printf("YES\n");
for(int i=;i<=n;i++)
cout<<sc[i]<<endl;
}
return ;
} /*
3
abc abc
abc dce
abd ebc
*/

Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League的更多相关文章

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

    先反复地扫(不超过n次),把所有可以确定唯一取法的给确定下来. 然后对于剩下的不能确定的,跑2-SAT.输出可行解时,对于a和¬a,如果a所在的强连通分量序号在¬a之前,则取a,否则不取a.如果a和¬ ...

  2. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)

    Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...

  3. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)A模拟 B三分 C dfs D map

    A. Andryusha and Socks time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

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

    D. Innokenty and a Football League time limit per test 2 seconds memory limit per test 256 megabytes ...

  5. 树的性质和dfs的性质 Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E

    http://codeforces.com/contest/782/problem/E 题目大意: 有n个节点,m条边,k个人,k个人中每个人都可以从任意起点开始走(2*n)/k步,且这个步数是向上取 ...

  6. 2-sat Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D

    http://codeforces.com/contest/782/problem/D 题意: 每个队有两种队名,问有没有满足以下两个条件的命名方法: ①任意两个队的名字不相同. ②若某个队 A 选用 ...

  7. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E Underground Lab

    地址:http://codeforces.com/contest/782/problem/E 题目: E. Underground Lab time limit per test 1 second m ...

  8. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C Andryusha and Colored Balloons

    地址:http://codeforces.com/contest/782/problem/C 题目: C. Andryusha and Colored Balloons time limit per ...

  9. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Changed

    地址:http://codeforces.com/contest/782/problem/B 题目: B. The Meeting Place Cannot Be Changed time limit ...

随机推荐

  1. 数据库已有时间索引,想再添加ID索引

    将时间索引的代码复制进去后,将编辑框的变量改为m_QueryID. 准备先以时间索引查找出大概数据,再直接使用ID索引精确查找. 于是想直接精确查ID为105的数据信息. 出现错误: 发现错误原因是忘 ...

  2. ionic 弹窗(alert, confirm)

    直接上代码吧,不解释了 控制器: angular.module('app.controllers', []) .controller('categoryCtrl', ['$scope', '$http ...

  3. 点击input选中文本

    选文本: $(".unline-ipt").click(function () { $(this).focus().select(); this.selectionStart = ...

  4. python3-requests库的使用

    同步请求库requests用来做测试和简单爬虫其实非常好用的,今天来讲一讲,毕竟不熟悉就用,吃了很大亏啊,文档一定要好好看 http://docs.python-requests.org/zh_CN/ ...

  5. nginx提高加载静态文件速度

    1.本来对于静态网页,我们不需要放在应用容器中,原因一时由于应用服务器是用来解析动态网页的,针对静态网页本来就性能不高,而且还会占用应用容器的资源,所以我们专门使用nginx用来解析静态网页.     ...

  6. SaltStack数据系统-Pillar

    上一篇:SaltStack数据系统-Grains 使用saltstack进行配置管理可以使用pillar定义主机假如是Openstack修改了一下nova的密码就需要修改很多配置文件 pillar很安 ...

  7. Oracle之rman常用命令及维护(51CTO风哥rman课程)

    list 查看数据库备份的信息 查询数据库对应物 list incarnation; list backup summary; 列出当前备份信息及汇总 B是备份 F是全备 A是归档 第三个A是是否有效 ...

  8. Windows的445端口(文件共享)

    周鸿祎:教育网大量电脑445端口暴露,导致中招_科技_腾讯网 http://tech.qq.com/a/20170513/016133.htm 互联网周鸿祎2017-05-13 12:04   据36 ...

  9. Webpack4.x 入门

    概览 新建项目 npm init -y 安装webpack & webpack-cli (c)npm install -D webpack (c)npm install -D webpack- ...

  10. Zipline入门教程

    Zipline Beginner Tutorial Basics 基础 Zipline is an open-source algorithmic trading simulator written ...