赛后补题,还是要经常回顾,以前学过的匈牙利都忘记了,“猪队友”又给我讲了一遍。。。

怎么感觉二分图的匈牙利算法东西好多啊,啊啊啊啊啊啊啊啊啊(吐血。。。)

先传送一个写的很好的博客,害怕智障找不到了。大神膜%%%    Orz

H - Words from cubes

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

Input
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
Output
YES
Input
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
Output
No

 
这个题就是用积木拼单词,积木有6个面,面上写的有字母也有数字,问能不能用这些积木拼成单词。
建图的话,就是如果积木的某一面上有需要的字母或数字,就把这个积木和当前的单词中的字母或数字建立关系。其他的瞎写一下就可以了。
 
代码:
 #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-二分图最大匹配匈牙利的更多相关文章

  1. 51Nod 2006 飞行员配对(二分图最大匹配)-匈牙利算法

    2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 第二次世界大战时期,英国皇家空军从沦陷国 ...

  2. cogs 14. [网络流24题] 搭配飞行员 二分图最大匹配 匈牙利算法

    14. [网络流24题] 搭配飞行员 ★★   输入文件:flyer.in   输出文件:flyer.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述]     飞行大队有 ...

  3. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  4. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  5. HDU1068 (二分图最大匹配匈牙利算法)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. [POJ2446] Chessboard(二分图最大匹配-匈牙利算法)

    传送门 把所有非障碍的相邻格子彼此连一条边,然后求二分图最大匹配,看 tot * 2 + k 是否等于 n * m 即可. 但是连边不能重复,比如 a 格子 和 b 格子 相邻,不能 a 连 b ,b ...

  7. 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids

    题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...

  8. HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)

    (点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...

  9. ZOJ-3988 2017CCPC-秦皇岛 Prime Set 二分图最大匹配 匈牙利

    题面 题意:给你n个数,你可以选择2个和为质数的数为一对,每个数可以重复选择,你最多选k对,问你最多能选多少个不同数出来 题解:首先思考怎么样的数和为质数,2个偶数相加不行,除了1+1以外2个奇数相加 ...

随机推荐

  1. Qt:实现子线程发送信号父线程切换图片

    mainwindow.h中代码 #ifndef MAINWINDOW_H#define MAINWINDOW_H #include <QMainWindow>#include " ...

  2. python-numpy-pandas

    目录 numpy 模块 创建矩阵方法: 获取矩阵的行列数 切割矩阵 矩阵元素替换 矩阵的合并 通过函数创建矩阵 矩阵的运算 pandas模块 series (一维列表) DataFrame DataF ...

  3. Python学习day01

    age = 23 count=0 while count<3: guess_age = int (input("My age:")) if age ==guess_age: ...

  4. 【转】MySQL innodb_autoinc_lock_mode 详解 ,并发插入时主键冲突的解决方案

    本文转载于 http://www.cnblogs.com/JiangLe/p/6362770.html innodb_autoinc_lock_mode这个参数控制着在向有auto_increment ...

  5. CodeForces 109C 树形DP Lucky Tree

    赶脚官方题解写得挺清楚的说,=_= 注意数据范围用long long,否则会溢出. #include <iostream> #include <cstdio> #include ...

  6. day01_07.逻辑与字符串运算符

    &&(并且)====>发现&符号总是打错,记忆口令:&7(暗器),在数字7上面,在python中是and ||(或者)====>在python中是or . ...

  7. 04-python进阶-map&reduce

    Map --映射 Reduce -- 归纳 将大数据标准化的处理 Map 拆封任务,Reduce将结果合并 这样是不是能够将很多计算机组成一台超级计算机呢? 一些问题:如果任务本身就很复杂,那么拆解任 ...

  8. C# 数组 之间转换

    List<string> strLen = new List<string>();            if (!string.IsNullOrEmpty(group_id) ...

  9. 【NOIP2017】逛公园 拆点最短路+拓扑(记忆化搜索

    题目描述 策策同学特别喜欢逛公园.公园可以看成一张N个点M条边构成的有向图,且没有 自环和重边.其中1号点是公园的入口,N号点是公园的出口,每条边有一个非负权值, 代表策策经过这条边所要花的时间. 策 ...

  10. P1988 最大数

    最大数 08年江苏的一道省选题. 题目描述: 用两种操作维护一个数列: 1. 查询:查询当前数列中末尾L个数中的最大的数,并输出这个数的值. 2. 插入:将n加上t,其中t是最近一次查询操作的答案(如 ...