地址: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. mathtype免费版下载及序列号获取地址

    在编辑公式这个方面来说,MathType是使用最多的一个工具,因为它操作简单,不需要复杂的学习过程就可以很快地掌握操作技巧,并且功能也比Office自带的公式编辑器完善很多,可以对公式进行批量修改.编 ...

  2. 安装wampserver时提示丢失MSVCR110.dll

    安装Wampserver 2后启动的时候提示系统错误:MSVCR110.dll丢失. 在wampserver官网上有例如以下提示: 于是卸载原来的WAMPSERVER 2 ,在http://www.m ...

  3. 各种API中文文档下载地址

    转发: http://www.aseoe.com/api-download/download.html jquery easyui 帮助文档: http://download.csdn.net/dow ...

  4. ios开发之--swift下Alamofire的使用

    1,首先使用cocoapods导入,如果有不会的同学,可以去看我写的关于cocopods使用的那篇博客 2,直接上代码: a 先看下文件结构 CommonFile.swift import UIKit ...

  5. php 实现Iterator 接口

    <?php class MyIterator implements Iterator{ private $var = array(); public function __construct($ ...

  6. Android之Handler与AsyncTask的区别

    1 ) AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可 ...

  7. SpringMVC笔记——Spring+MyBatis组合开发简单实例

    简介 SSH框架很强大,适合大型项目开发.但学无止境,多学会一门框架组合开发会让自己增值许多. SSM框架小巧精致,适合中小型项目快速开发,对于新手来说也是简单上手的.在SSM框架搭建之前,我们先学习 ...

  8. Linuxyum源切换阿里云软件源

    备份本机软件源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 下载新的CentOS-Bas ...

  9. Content-Negotiation Header Quality Values

    HTTP: The Definitive Guide 17.3.2 Content-Negotiation Header Quality Values The HTTP protocol define ...

  10. Preparing Olympiad---cf550B(DFS或者状态压缩模板)

    比赛链接:http://codeforces.com/problemset/problem/550/B 给你n个数,选出来只是2个然后求他们的和在L和R的区间内,并且选出来的数中最大值和最小值的差不得 ...