Words from cubes

Time Limit: Unknown ms
Memory Limit: 65536KB

This problem will be judged on CodeForcesGym. Original ID: 100735H
64-bit integer IO format: %I64d      Java class name: (Any)

 

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.

 

Sample Input

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

Source

 
解题:最大匹配,匈牙利算法
 
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
char str[maxn];
int n,Link[maxn];
bool w[maxn][maxn],used[maxn];
bool match(int u){
for(int i = ; i < n; ++i){
if(used[i] || !w[u][i]) continue;
used[i] = true;
if(Link[i] == - || match(Link[i])){
Link[i] = u;
return true;
}
}
return false;
}
int main(){
while(~scanf("%s",str)){
scanf("%d",&n);
memset(w,false,sizeof w);
char tmp[];
for(int i = ; i < n; ++i){
for(int j = ; j < ; ++j){
scanf("%s",tmp);
for(int k = ; str[k]; ++k)
if(str[k] == tmp[]) w[k][i] = true;
}
}
memset(Link,-,sizeof Link);
int ret = ;
for(int i = ; i < ; ++i){
memset(used,false,sizeof used);
if(match(i)) ++ret;
}
printf("%s\n",strlen(str) == ret?"YES":"NO");
}
return ;
}

CodeForcesGym 100735H Words from cubes的更多相关文章

  1. 水泡动画模拟(Marching Cubes)

    Marching Cubes算法是三维离散数据场中提取等值面的经典算法,其主要应用于医学领域的可视化场景,例如CT扫描和MRI扫描的3D重建等. 算法主要的思想是在三维离散数据场中通过线性插值来逼近等 ...

  2. Codeforces525E Anya and Cubes(双向搜索)

    题目 Source http://codeforces.com/contest/525/problem/E Description Anya loves to fold and stick. Toda ...

  3. [saiku] 系统登录成功后查询Cubes

    一.系统启动时初始化ds和conn 1.查询出目前系统拥有的Datasources和Connections放入内存中 2.比对saiku-datasources中的ds是否有新增的,如果有,创建新的d ...

  4. UVa 10601 (Polya计数 等价类计数) Cubes

    用6种颜色去染正方体的12条棱,但是每种颜色都都限制了使用次数. 要确定正方体的每一条棱,可以先选择6个面之一作为顶面,然后剩下的四个面选一个作为前面,共有24种. 所以正方体的置换群共有24个置换. ...

  5. poj 1543 Perfect Cubes(注意剪枝)

    Perfect Cubes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14901   Accepted: 7804 De ...

  6. OpenJudge 2810(1543) 完美立方 / Poj 1543 Perfect Cubes

    1.链接地址: http://bailian.openjudge.cn/practice/2810/ http://bailian.openjudge.cn/practice/1543/ http:/ ...

  7. 组合数学(Pólya计数原理):UvaOJ 10601 Cubes

    Cubes You are given 12 rods of equal length. Each of them is colored in certain color. Your task is ...

  8. cf492A Vanya and Cubes

    A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. uva 10051 Tower of Cubes(DAG最长路)

    题目连接:10051 - Tower of Cubes 题目大意:有n个正方体,从序号1~n, 对应的每个立方体的6个面分别有它的颜色(用数字给出),现在想要将立方体堆成塔,并且上面的立方体的序号要小 ...

随机推荐

  1. hibernate缓存机制详细分析(一级、二级、查询缓存,非常清晰明白)

    本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session级别).二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论下我们的N+1的问题. 随笔虽长,但我相信 ...

  2. sql 查询出当天记录数据

    select updatetime,NewComment,HistoryID,sum(1) over(partition by UpdateTime) from LPProjectHistoryord ...

  3. 用Python一键搭建Http服务器的方法

    用Python一键搭建Http服务器的方法 Python3请看 python -m http.server 8000 & Python2请看 python -m SimpleHTTPServe ...

  4. linux命令(001) -- chkconfig

    一.准备知识 在说明chkconfig命令的用途之前,有必要先了解一下Linux系统中/etc/rc[0-6].d目录的用途. 众所周知,在Linux系统定义了7种不同的启动级别,这7种启动级别的含义 ...

  5. C++ 由虚基类 虚继承 虚函数 到 虚函数表

    //虚基类:一个类可以在一个类族中既被用作虚基类,也被用作非虚基类. class Base1{ public: Base1(){cout<<"Construct Base1!&q ...

  6. jq-文本框只能输入数字

    <input type="text" onKeyUp="value=value.replace(/\D/g,'')"  /> onKeyUp: 当输 ...

  7. 大数据插入Excel报错处理

    发现问题: 最近运行程序时,发现了一个问题,就是在导出excel时,报了一下错误 分析问题: 原来是由于NPOI这个动态库导致的,然后看了下版本,发现是1.2.5.然后百度了下,发现这个版本的NPOI ...

  8. [ CodeForces 865 D ] Buy Low Sell High

    \(\\​\) \(Description\) 给出\(N\)天股票的价钱\(A_1,...,A_N\),每天可以什么都不做,或者买入或卖出\(1\)支股票,分别花出或收入\(A_i\)元,求最大收益 ...

  9. wordpress登录账号之后才能查看页面,实例

    函数: <?php auth_redirect(); ?> 例子: 要求用户登录才能查看页面 if(!is_user_logged_in()){ auth_redirect(); } 源文 ...

  10. Hive扩展功能(七)--Hive On Spark

    软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...