CodeForcesGym 100735H Words from cubes

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

 
解题:最大匹配,匈牙利算法
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 256;
 4 char str[maxn];
 5 int n,Link[maxn];
 6 bool w[maxn][maxn],used[maxn];
 7 bool match(int u){
 8     for(int i = 0; i < n; ++i){
 9         if(used[i] || !w[u][i]) continue;
10         used[i] = true;
11         if(Link[i] == -1 || match(Link[i])){
12             Link[i] = u;
13             return true;
14         }
15     }
16     return false;
17 }
18 int main(){
19     while(~scanf("%s",str)){
20         scanf("%d",&n);
21         memset(w,false,sizeof w);
22         char tmp[20];
23         for(int i = 0; i < n; ++i){
24             for(int j = 0; j < 6; ++j){
25                 scanf("%s",tmp);
26                 for(int k = 0; str[k]; ++k)
27                     if(str[k] == tmp[0]) w[k][i] = true;
28             }
29         }
30         memset(Link,-1,sizeof Link);
31         int ret = 0;
32         for(int i = 0; i < 26; ++i){
33             memset(used,false,sizeof used);
34             if(match(i)) ++ret;
35         }
36         printf("%s\n",strlen(str) == ret?"YES":"NO");
37     }
38     return 0;
39 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4812524.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值