Codeforces Gym100735 H.Words from cubes-二分图最大匹配匈牙利
赛后补题,还是要经常回顾,以前学过的匈牙利都忘记了,“猪队友”又给我讲了一遍。。。
怎么感觉二分图的匈牙利算法东西好多啊,啊啊啊啊啊啊啊啊啊(吐血。。。)
先传送一个写的很好的博客,害怕智障找不到了。大神膜%%% Orz
Informikas was cleaning his drawers while he found a toy of his childhood. Well, it's not just a toy, it's a bunch of cubes with letters and digits written on them.
Informikas remembers that he could have make any word he could think of using these cubes. He is not sure about that now, because some of the cubes have been lost.
Informikas has already come up with a word he would like to make. Could you help him by saying if the word can be built from the cubes in the drawer?
Input
On the first line of input there is a string S, consisting of lowercase English letters, and an integer N (4 ≤ |S| ≤ 20, 1 ≤ N ≤ 100) – the word Informikas want to build and the number of cubes. On the every of the following N lines there are 6 characters. Every of those characters is either a lowercase English letter or a digit.
It is guaranteed that the string S consists only of lowercase English letters.
Output
Output one word, either "YES", if the word can be built using given cubes, or "NO" otherwise.
Example
dogs 4
d 1 w e 7 9
o 2 h a v e
g 3 c o o k
s 3 i e s 5
YES
banana 6
b a 7 8 9 1
n 1 7 7 7 6
a 9 6 3 7 8
n 8 2 4 7 9
a 7 8 9 1 3
s 7 1 1 2 7
No
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=;
int n,k,len;
//n1,n2为二分图的顶点集,其中x∈n1,y∈n2
int map[N][N],vis[N],link[N];
//link记录n2中的点y在n1中所匹配的x点的编号
struct node{
char t[N];
}a[N];
char s[N];
int find(int x){
int i;
for(i=;i<n;i++){
if(map[x][i]&&!vis[i])//x->i有边,且节点i未被搜索
{
vis[i]=;//标记节点已被搜索
//如果i不属于前一个匹配M或被i匹配到的节点可以寻找到增广路
if(link[i]==-||find(link[i])){
link[i]=x;//更新
return ;//匹配成功
}
}
}
return ;
}
int main(){
while(cin>>s>>n){
len=strlen(s);
memset(link,-,sizeof(link));
memset(map,,sizeof(map));
for(int i=;i<n;i++){
for(int j=;j<;j++)
cin>>a[i].t[j];
}
for(int k=;k<len;k++){
for(int i=;i<n;i++){
for(int j=;j<;j++){
if(s[k]==a[i].t[j]){
map[k][i]=;
}
}
}
}
int num=;
for(int i=;i<len;i++){
memset(vis,,sizeof(vis));
if(find(i))
num++;
}
//cout<<num<<endl;
if(num==len)printf("YES\n");
else printf("NO\n");
}
return ;
}
就这样吧,溜了溜了。。。
Codeforces Gym100735 H.Words from cubes-二分图最大匹配匈牙利的更多相关文章
- 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法
2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 第二次世界大战时期,英国皇家空军从沦陷国 ...
- cogs 14. [网络流24题] 搭配飞行员 二分图最大匹配 匈牙利算法
14. [网络流24题] 搭配飞行员 ★★ 输入文件:flyer.in 输出文件:flyer.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 飞行大队有 ...
- UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法
二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...
- HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU1068 (二分图最大匹配匈牙利算法)
Girls and Boys Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [POJ2446] Chessboard(二分图最大匹配-匈牙利算法)
传送门 把所有非障碍的相邻格子彼此连一条边,然后求二分图最大匹配,看 tot * 2 + k 是否等于 n * m 即可. 但是连边不能重复,比如 a 格子 和 b 格子 相邻,不能 a 连 b ,b ...
- 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids
题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...
- HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)
(点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...
- ZOJ-3988 2017CCPC-秦皇岛 Prime Set 二分图最大匹配 匈牙利
题面 题意:给你n个数,你可以选择2个和为质数的数为一对,每个数可以重复选择,你最多选k对,问你最多能选多少个不同数出来 题解:首先思考怎么样的数和为质数,2个偶数相加不行,除了1+1以外2个奇数相加 ...
随机推荐
- python-time模块--pickle模块
目录 time 模块 为什么要有time模块,time模块有什么用? time模块的三种格式 时间戳(timestamp) 格式化时间(需要自己定义格式) 结构化时间(struct-time) 结构化 ...
- Running OOM killer script for process 32248 for Solr on port 8983
Running OOM killer script for process 32248 for Solr on port 8983 分析1 https://blog.csdn.net/qq_41665 ...
- poj1523赤裸裸的割点
这题真是没什么好说的...赤裸裸的求割点直接模板上 #include<cstdio> #include<cstring> #include<iostream> #i ...
- ogre3D学习基础6---场景管理器的使用
场景管理器的使用 最常使用的坐标系统空间(同时也是Ogre程序所能提供的)即是世界空间(World).父节点空间(Parent)以及本地空间(Local). 1.世界空间 就是物体所存在的地方,当我们 ...
- 《完美应用Ubuntu》第3版 何晓龙 著
系统篇 用好Ubuntu掌握这些就够了 第1章 Ubuntu的进化 1.1 GNU/Linux的历史和文化 1.1.1 GNU/Linux是Linux的全称 1.1.2 Linux的诞生 1.2 Li ...
- 《Effective Java》笔记 :(一)创建和销毁对象
一 .考虑用静态工厂方法代替构造器 1. 静态工厂方法与设计模式中的工厂方法模式不同,注意不要混淆 例子: public static Boolean valueOf(boolean b){ retu ...
- python随机数的产生
导入 random模块 >>> import random 1. random.random random.random()用于生成一个0到1的随机浮点数: 0 <= n ...
- Shape,expand_dims,slice基本用法
import tensorflow as tf t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]], [[5, 5, 5], ...
- [python学习篇][廖雪峰][1]高级特性--列表生成式
>>> import os >>> [d for d in os.listdir(r"d:\temp")] ['0.png', '0.xml', ...
- [python测试框架] http接口测试框架
https://testerhome.com/topics/5631 Http 接口测试框架 (思路 + 实现中 + 开源 + 可能难产) Http 接口测试框架疑问解答 Fiddler 保存会话 ( ...