D.Innokenty and a football league
来源:互联网 发布:淘宝宝贝截图 编辑:程序博客网 时间:2024/06/06 17:44
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 3and 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.
2DINAMO BYTECITYFOOTBALL MOSCOW
YESDINFOO
2DINAMO BYTECITYDINAMO BITECITY
NO
3PLAYFOOTBALL MOSCOWPLAYVOLLEYBALL SPBGOGO TECHNOCUP
YESPLMPLSGOG
3ABC DEFABC EFGABD OOO
YESABDABEABO
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 第一个串的前两个+ 第二个串的前一个
然后问你 这些队伍能不能全部取到简称,并且不能重复,
题目有个限制: 如果有x 个队伍的第一种简称相同, 则这个x队伍必须选择第二种, 如果第二种也有重复的, 则NO; 并且选择第二种的 同时会对 之后的队伍选择产生影响;
解题思路; 先处理第一类相同的 (设为x), 相同的去选择第二种, x 结束后 判断这些x 是否存在相同; 如果没有相同,去处理 剩下第一类不同的(设为y),
此时对于剩下的 要判断, 之前的(x)的 是不是和现在(y)存在相同, 如果相同NO 不同 为答案,存下来
题目要求:所有的队名皆不能重复,若多个队的第一类相同, 则全部选择第二类; 若
他们的第二队也有相同的 NO
在对于第一类不重复的情况下, 要对之前重复的选择第二类是否有影响
第二类 通过回溯 回溯回去看看有没有 和之前的重复 则NO
*/
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#include <stdio.h>
typedef long long ll;
const int MAXN=10010;
{
string first,second,ans;// 前后两个串
int id;
} num[MAXN];
int n;
{
return cnt_first[a.first]>cnt_first[b.first];// 用 统计的值 排序
}
int cmp_cnt(node a,node b)
{
return cnt[a.first]>cnt[b.first];
}
int cmp_id(node a,node b)
{
return a.id<b.id;
}
int finds(int k)
{
int i;
sort(num+k,num+n+1,cmp_cnt);// 按照答案中第一类个数排序
if(!cnt[num[k].first]) return k;// 如果第一类和第二类没有重复 结束
while(k<=n)
{
if(cnt[num[k].first])// 如果第一类的答案有
{
if(cnt[num[k].second]) return -1;//并且第二类的答案被占用 返回-1 NO
cnt[num[k].ans]=1;//标记第二类答案为1
else
break;// 没有的话结束就可以
k++;
}
return finds(k);
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{
for(i=1;i<=n; i++)
{
cin>>num[i].first>>num[i].second;
num[i].id=i;
num[i].first=num[i].first.substr(0,3);//取前三位
num[i].second=num[i].first.substr(0,2)+num[i].second[0];//取前两位+后面的第一位
cnt_first[num[i].first]++;// 统计第一种 相同的字符串 个数 ++;
}
sort(num+1,num+n+1,cmp);//按照第一个统计数值排序 第一类数值排序
int flag=0;
for(i=1;i<=n;i++)//此时已经按照第一种相同的多少从大到小排序
{
if(cnt_first[num[i].first]>1)//如果 第一种的 队伍有多个的话
{
if(cnt_second[num[i].second]==1)//判断第二种 是否被占用 no
flag=1;
else
{
num[i].ans=num[i].second;// 此时 当第一类多个, 第二类不同, 是 选择第二类为答案 全部选择第二类
cnt_second[num[i].ans]=1;// 把第二类的 这个 标记为 已用
cnt[num[i].ans]=1;// 把这个第二类的结果 在答案中标记
}
else// 此时说明数目相同 已经处理完了, 到i 跳出即可
break;
}
i=finds(i);// 从i 开始到结束 查找是否存在和前面有相同的部分
if(i==-1) flag=1;
if(!flag)// 第一类的 没有重复的
{
while(i<=n)
{
num[i].ans=num[i].first;
i++;
}
printf("%s\n",flag ?"NO":"YES");
if(!flag)
{
sort(num,num+n+1,cmp_id);//答案从小到大排序
for(i=1;i<=n;i++)
cout<<num[i].ans<<endl;
}
}
}
- codeforces D. Innokenty and a Football League
- D. Innokenty and a Football League----map
- D. Innokenty and a Football League
- D.Innokenty and a football league
- 【codeforces 782D】 Innokenty and a Football League
- Codeforces 782D Innokenty and a Football League【贪心】
- Codeforces 780D-Innokenty and a Football League
- Codeforce 403 D. Innokenty and a Football League
- 【Codeforces 780 D Innokenty and a Football League 】+ 模拟 + 贪心
- CodeForces 780D Innokenty and a Football League【模拟+贪心】
- CodeForces 782D Innokenty and a Football League
- codeforce D. Innokenty and a Football League 贪心,模拟
- Codeforce 780D Innokenty and a Football League (贪心+模拟)
- Codeforces Round #403 D. Innokenty and a Football League(思维+模拟)
- CF 782D. Innokenty and a Football League 贪心,思维,模拟
- codeforces 782D Innokenty and a Football League(2-SAT)
- codeforces 781B. Innokenty and a Football League(贪心)
- 【Codeforces Round #403】Codeforces 781B Innokenty and a Football League
- NYOJ 5 Binary String Mathing (substr函数)
- poj 2586
- 5_android的界面编程(UI)
- auto和decltype
- C++迭代器iterator段错误segmentation fault (core dumped)
- D.Innokenty and a football league
- 07-python学习笔记-列表生成式
- java多线程
- 程序员面试金典——n皇后问题_____(S)
- UDA1341(asoc)分析
- 【IO流】FileReader,FileWriter完成文件的copy
- Python入门学习(3)
- MySQL大数据量分页查询方法及其优化
- Java Lock的使用