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 ...
随机推荐
- lua基础(一)
参考链接: http://blog.csdn.net/lyh916/article/details/49719697 一.注释 --这是行注释 --[[ 这是块注释 这是块注释 这是块注释 --]] ...
- 应用开发之WinForm环境
本章简言 上一章笔者讲到关于IO文件操作类,了解如何处理文件流.从这一章开始笔者将讲解相对比较高级的知识点.而本章笔者就对WinForm开发的知识点进行讲解和引导.现在很多业务都是面向于B/S模式的开 ...
- jquery对象转dom对象
jq取兄弟级的上移个元素 jquery对象: var a = $(this).prev("a"); console.log(a); 输出: 转dom对象: var a = $(th ...
- 【BZOJ3232】圈地游戏 分数规划+最小割
[BZOJ3232]圈地游戏 Description DZY家的后院有一块地,由N行M列的方格组成,格子内种的菜有一定的价值,并且每一条单位长度的格线有一定的费用. DZY喜欢在地里散步.他总是从任意 ...
- Map中存放数组
Map<String,Object> map = new HashMap<String, Object>(); Map<String,String> agentMa ...
- ps -aux | egrep 多个值
ps -aux |egrep "(schedule.jar|positec.jar|time_server.jar|tomcat-xweb/)"
- 170308、oracle查看被锁的表和解锁
--以下几个为相关表SELECT * FROM v$lock;SELECT * FROM v$sqlarea;SELECT * FROM v$session;SELECT * FROM v$proce ...
- java方法的理解、调用栈与异常处理
一.流程分支 If/else :基于boolean值的双分支 Switch:基于数字(整数.char.byte.枚举).字符串 类型的多分支 Int month =5; Switch 二.方法meth ...
- SignalR循序渐进(三)简易的集群通讯组件
上一篇演示了泛型Hub的实现,微软于6月17日更新了SignalR 2.1.0,然后自带了泛型Hub,于是就不需要自己去实现了…(微软你为啥不早一个月自带啊…).不过没关系,SignalR出彩之处不在 ...
- Markdown安装与简单使用
早就听过Markdown的大名了,说是最适合程序员的编辑器,一点也不为过.平时写文章,写博客.除了内容以外,还要被一堆繁琐的样式困扰,毕竟样式太难看,既是自己的文章,也会懒得看的.今天正好看到博客上面 ...