Codeforces Gym100735 H.Words from cubes-二分图最大匹配匈牙利

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

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

先传送一个写的很好的博客,害怕智障找不到了。大神膜%%%    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个面,面上写的有字母也有数字,问能不能用这些积木拼成单词。
建图的话,就是如果积木的某一面上有需要的字母或数字,就把这个积木和当前的单词中的字母或数字建立关系。其他的瞎写一下就可以了。
 
代码:
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 const int N=1001;
 6 int n,k,len;
 7 //n1,n2为二分图的顶点集,其中x∈n1,y∈n2
 8 int map[N][N],vis[N],link[N];
 9 //link记录n2中的点y在n1中所匹配的x点的编号
10 struct node{
11     char t[N];
12 }a[N];
13 char s[N];
14 int find(int x){
15     int i;
16     for(i=0;i<n;i++){
17         if(map[x][i]&&!vis[i])//x->i有边,且节点i未被搜索
18         {
19             vis[i]=1;//标记节点已被搜索
20             //如果i不属于前一个匹配M或被i匹配到的节点可以寻找到增广路
21             if(link[i]==-1||find(link[i])){
22                 link[i]=x;//更新
23                 return 1;//匹配成功
24             }
25         }
26     }
27     return 0;
28 }
29 int main(){
30     while(cin>>s>>n){
31             len=strlen(s);
32             memset(link,-1,sizeof(link));
33             memset(map,0,sizeof(map));
34         for(int i=0;i<n;i++){
35             for(int j=0;j<6;j++)
36                 cin>>a[i].t[j];
37         }
38         for(int k=0;k<len;k++){
39             for(int i=0;i<n;i++){
40                 for(int j=0;j<6;j++){
41                     if(s[k]==a[i].t[j]){
42                         map[k][i]=1;
43                     }
44                 }
45             }
46         }
47         int num=0;
48         for(int i=0;i<len;i++){
49             memset(vis,0,sizeof(vis));
50             if(find(i))
51                 num++;
52         }
53         //cout<<num<<endl;
54         if(num==len)printf("YES
");
55         else printf("NO
");
56     }
57     return 0;
58 }

就这样吧,溜了溜了。。。

 


原文地址:https://www.cnblogs.com/ZERO-/p/8330901.html