Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League
地址:http://codeforces.com/contest/782/problem/D
题目:
2 seconds
256 megabytes
standard input
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:
- 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.
2
DINAMO BYTECITY
FOOTBALL MOSCOW
YES
DIN
FOO
2
DINAMO BYTECITY
DINAMO BITECITY
NO
3
PLAYFOOTBALL MOSCOW
PLAYVOLLEYBALL SPB
GOGO TECHNOCUP
YES
PLM
PLS
GOG
3
ABC DEF
ABC EFG
ABD OOO
YES
ABD
ABE
ABO
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的更多相关文章
- 【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和¬ ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...
- 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 ...
- 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 ...
- 树的性质和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步,且这个步数是向上取 ...
- 2-sat Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D
http://codeforces.com/contest/782/problem/D 题意: 每个队有两种队名,问有没有满足以下两个条件的命名方法: ①任意两个队的名字不相同. ②若某个队 A 选用 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)
Ivan comes again! Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...
- hdu 1756:Cupid's Arrow(计算几何,判断点在多边形内)
Cupid's Arrow Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 自定义控件_StickyNavLaout
关注我一.View结构原理1.extends linearLayout 继承想要用的布局,首先完成布局的填充在 onFinishInflate 方法中 findViewById(); @Overrid ...
- Android 切换主题 (二)
Android 切换主题 (二) 背景 我原来写过一篇文章关于 android 切换主题的文章 -- Android 切换主题以及换肤的实现 , 里面介绍了如何使用 setTheme() 来切换主题, ...
- 【BZOJ1135】[POI2009]Lyz 线段树
[BZOJ1135][POI2009]Lyz Description 初始时滑冰俱乐部有1到n号的溜冰鞋各k双.已知x号脚的人可以穿x到x+d的溜冰鞋. 有m次操作,每次包含两个数ri,xi代表来了x ...
- 【BZOJ3238】[Ahoi2013]差异 后缀数组+单调栈
[BZOJ3238][Ahoi2013]差异 Description Input 一行,一个字符串S Output 一行,一个整数,表示所求值 Sample Input cacao Sample Ou ...
- datatables如何把列设置成hidden隐藏域?
官网:https://datatables.net/reference/option/设置: visible: false如下: <!DOCTYPE html><html>&l ...
- Cocos2d-x学习笔记之常用的宏
NS_CC_BEGIN:Cocos2D-x命名空间开始,属于Cocos2D-x的类的定义以该宏开头,用以代替C++中的“namespace cocos2d{”. NS_CC_END:Cocos2D-x ...
- 技术宅之flappy bird 二逼鸟
师雪坤和刘阳 风靡一时的虐心小游戏<Flappy Bird>,以玩法简单.难度超高著称,不过,最近这款让全世界玩家几欲怒摔手机的游戏,被两位中国技术宅设计的"玩鸟机器人" ...
- JRebel插件安装配置与破解激活(多方案)详细教程
JRebel 介绍 IDEA上原生是不支持热部署的,一般更新了 Java 文件后要手动重启 Tomcat 服务器,才能生效,浪费不少生命啊.目前对于idea热部署最好的解决方案就是安装JRebel插件 ...