Gym100735H - Words from cubes-二分图最大匹配匈牙利算法
2018-01-22 20:09
302 查看
赛后补题,还是要经常回顾,以前学过的匈牙利都忘记了,“猪队友”又给我讲了一遍。。。
H-Wordsfromcubes
Informikaswascleaninghisdrawerswhilehefoundatoyofhischildhood.Well,it'snotjustatoy,it'sabunchofcubeswithlettersanddigitswrittenonthem.
Informikasremembersthathecouldhavemakeanywordhecouldthinkofusingthesecubes.Heisnotsureaboutthatnow,becausesomeofthecubeshavebeenlost.
Informikashasalreadycomeupwithawordhewouldliketomake.Couldyouhelphimbysayingifthewordcanbebuiltfromthecubesinthedrawer?
Input
OnthefirstlineofinputthereisastringS,consistingoflowercaseEnglishletters,andanintegerN(4 ≤ |S| ≤ 20,1 ≤ N ≤ 100)–thewordInformikaswanttobuildandthenumberofcubes.OntheeveryofthefollowingNlinesthereare6characters.EveryofthosecharactersiseitheralowercaseEnglishletteroradigit.
ItisguaranteedthatthestringSconsistsonlyoflowercaseEnglishletters.
Output
Outputoneword,either"YES",ifthewordcanbebuiltusinggivencubes,or"NO"otherwise.
Example
Input
Output
Input
OutputNO
就这样吧,溜了溜了。。。
Informikaswascleaninghisdrawerswhilehefoundatoyofhischildhood.Well,it'snotjustatoy,it'sabunchofcubeswithlettersanddigitswrittenonthem.
Informikasremembersthathecouldhavemakeanywordhecouldthinkofusingthesecubes.Heisnotsureaboutthatnow,becausesomeofthecubeshavebeenlost.
Informikashasalreadycomeupwithawordhewouldliketomake.Couldyouhelphimbysayingifthewordcanbebuiltfromthecubesinthedrawer?
Input
OnthefirstlineofinputthereisastringS,consistingoflowercaseEnglishletters,andanintegerN(4 ≤ |S| ≤ 20,1 ≤ N ≤ 100)–thewordInformikaswanttobuildandthenumberofcubes.OntheeveryofthefollowingNlinesthereare6characters.EveryofthosecharactersiseitheralowercaseEnglishletteroradigit.
ItisguaranteedthatthestringSconsistsonlyoflowercaseEnglishletters.
Output
Outputoneword,either"YES",ifthewordcanbebuiltusinggivencubes,or"NO"otherwise.
Example
Input
dogs4 d1we79 o2have g3cook s3ies5
Output
YES
Input
banana6 ba7891 n17776 a96378 n82479 a78913 s71127
OutputNO
这个题就是用积木拼单词,积木有6个面,面上写的有字母也有数字,问能不能用这些积木拼成单词。 建图的话,就是如果积木的某一面上有需要的字母或数字,就把这个积木和当前的单词中的字母或数字建立关系。其他的瞎写一下就可以了。。。 代码:
1#include<iostream> 2#include<cstring> 3#include<cstdio> 4usingnamespacestd; 5constintN=1001; 6intn,k,len; 7//n1,n2为二分图的顶点集,其中x∈n1,y∈n2 8intmap ,vis ,link ; 9//link记录n2中的点y在n1中所匹配的x点的编号 10structnode{ 11chart ; 12}a ; 13chars ; 14intfind(intx){ 15inti; 16for(i=0;i<n;i++){ 17if(map[x][i]&&!vis[i])//x->i有边,且节点i未被搜索 18{ 19vis[i]=1;//标记节点已被搜索 20//如果i不属于前一个匹配M或被i匹配到的节点可以寻找到增广路 21if(link[i]==-1||find(link[i])){ 22link[i]=x;//更新 23return1;//匹配成功 24} 25} 26} 27return0; 28} 29intmain(){ 30while(cin>>s>>n){ 31len=strlen(s); 32memset(link,-1,sizeof(link)); 33memset(map,0,sizeof(map)); 34for(inti=0;i<n;i++){ 35for(intj=0;j<6;j++) 36cin>>a[i].t[j]; 37} 38for(intk=0;k<len;k++){ 39for(inti=0;i<n;i++){ 40for(intj=0;j<6;j++){ 41if(s[k]==a[i].t[j]){ 42map[k][i]=1; 43} 44} 45} 46} 47intnum=0; 48for(inti=0;i<len;i++){ 49memset(vis,0,sizeof(vis)); 50if(find(i)) 51num++; 52} 53//cout<<num<<endl; 54if(num==len)printf("YES\n"); 55elseprintf("NO\n"); 56} 57return0; 58}
就这样吧,溜了溜了。。。
相关文章推荐
- 用匈牙利算法求二分图的最大匹配
- poj 3894 System Engineer (二分图最大匹配--匈牙利算法)
- 二分图的最大匹配、完美匹配和匈牙利算法
- 它处资料:二分图最大匹配的匈牙利算法
- 二分图的最大匹配、完美匹配和匈牙利算法
- POJ 1469 COURSES【匈牙利算法入门 二分图的最大匹配 模板题】
- 匈牙利算法,二分图最大基数匹配(过山车,hdu 2063)
- 最大二分图最大匹配的匈牙利算法
- 二分图的最大匹配2—匈牙利算法
- 二分图最大匹配(匈牙利算法)
- HIHO #1122 : 二分图二•二分图最大匹配之匈牙利算法
- 二分图基数最大匹配 匈牙利算法
- 二分图的最大匹配(匈牙利算法)HDU1083
- 二分图的最大匹配、完美匹配和匈牙利算法
- 二分图的最大匹配 匈牙利算法
- 二分图最大匹配(匈牙利算法-DFS增广模板)
- 二分图最大匹配的匈牙利算法
- 关于二分图最大匹配--匈牙利算法的理解
- 二分图的最大匹配——匈牙利算法
- 用匈牙利算法求二分图的最大匹配(转载)