Maximum match + Hungarian algorithm

Source: Internet
Author: User

Title: Http://codeforces.com/gym/100735/problem/H

H. Words from Cubestime limit per test0.25 smemory limit per test64 mbinputstandard inputoutputstandard output

Informikas was cleaning he 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 has make any word he could think of using these cubes. He is not sure on that so now, because some of the cubes has been lost.

Informikas have already come up with a word he would like to make. Could him by saying if the word can is built from the cubes in the drawer?

Input

On the first line of input there is a string S, consisting of lowercase 中文版 letters, and an integer C3>n (4?≤?| S|? ≤?20, 1?≤? N? ≤?100), Haven Word Informikas want to build and the number of cubes. On the every of the following N Lines there is 6 characters. Every of those characters is either a lowercase 中文版 letter or a digit.

It is guaranteed, the string S consists only of lowercase Chinese letters.

Output

Output one word, either "YES", if the word can be built using given cubes, or "NO" otherwise.

Examplesinput
Dogs 4
D 1 W e 7 9
o 2 h a v E
G 3 c 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


Test instructions is very simple, not much elaborated.
Own ideas:
DFS burst + A little pruning is over, and complexity is not going to happen.
AC Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>#include<Set>#include<algorithm>#include<map>#defineMAXN 10005using namespaceStd;typedefstruct{    intx, y;} Zuobiao;zuobiao ZZ[MAXN];intVis[maxn],ans,len,n,num;Charstr[ -];Charmp[ the][Ten];voidDfs () {if(Ans==len)return ; Else{         for(intI=0; i<n;i++){                if(Vis[i])Continue;  for(intj=0;j<6; j + +)        {            if(mp[i][j]==Str[ans]) {ans++; Vis[i]=1; if(Ans==len)return;               DFS (); if(Ans==len)return; Ans--; Vis[i]=0;  Break; }        }    }}}intMain () {scanf ("%s", str); CIN>>N; Num=0;  for(intI=0; i<n;i++)        {             for(intj=0;j<6; j + +)            {                if(j!=n-1) scanf ("%c",&Mp[i][j]); Elsescanf"%c",&Mp[i][j]); if(mp[i][j]==str[0]) {zz[num].x=i; Zz[num].y=J; Num++;        }} getchar (); } memset (Vis,0,sizeof(VIS)); Len=strlen (str);  for(intI=0; i<num;i++) {vis[zz[i].x]=1; Ans=1;          DFS (); if(len==ans) {cout<<"YES"<<Endl; return 0; } vis[zz[i].x]=0; Ans=0; } cout<<"NO"<<Endl; return 0;}

Anyway, it's incredible.

Then, attach the ideas of the big boys!

Binary matching + Hungarian algorithm http://www.renfei.org/blog/bipartite-matching.html

#include <bits/stdc++.h>using namespacestd;Const intMAXN = the;CharSTR[MAXN];intN,LINK[MAXN];BOOLW[MAXN][MAXN],USED[MAXN];BOOLMatchintu) {     for(inti =0; I < n; ++i) {        if(Used[i] | |!w[u][i])Continue; Used[i]=true; if(Link[i] = =-1||match (Link[i])) {Link[i]=u; return true; }    }    return false;}intMain () { while(~SCANF ("%s", str)) {scanf ("%d",&N); Memset (W,false,sizeofW); Chartmp[ -];  for(inti =0; I < n; ++i) {             for(intj =0; J <6; ++j) {scanf ("%s", TMP);  for(intK =0; STR[K]; ++k)if(Str[k] = = tmp[0]) W[k][i] =true; }} memset (Link,-1,sizeofLink); intRET =0;  for(inti =0; I < -; ++i) {memset (used,false,sizeofused); if(Match (i)) + +ret; } printf ("%s\n", strlen (str) = = ret?"YES":"NO"); }    return 0;}

Maximum match + Hungarian algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.