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

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

先传送一个写的很好的博客,害怕智障找不到了。大神膜%%%    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个面,面上写的有字母也有数字,问能不能用这些积木拼成单词。
建图的话,就是如果积木的某一面上有需要的字母或数字,就把这个积木和当前的单词中的字母或数字建立关系。其他的瞎写一下就可以了。
 
代码:
 #include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=;
int n,k,len;
//n1,n2为二分图的顶点集,其中x∈n1,y∈n2
int map[N][N],vis[N],link[N];
//link记录n2中的点y在n1中所匹配的x点的编号
struct node{
char t[N];
}a[N];
char s[N];
int find(int x){
int i;
for(i=;i<n;i++){
if(map[x][i]&&!vis[i])//x->i有边,且节点i未被搜索
{
vis[i]=;//标记节点已被搜索
//如果i不属于前一个匹配M或被i匹配到的节点可以寻找到增广路
if(link[i]==-||find(link[i])){
link[i]=x;//更新
return ;//匹配成功
}
}
}
return ;
}
int main(){
while(cin>>s>>n){
len=strlen(s);
memset(link,-,sizeof(link));
memset(map,,sizeof(map));
for(int i=;i<n;i++){
for(int j=;j<;j++)
cin>>a[i].t[j];
}
for(int k=;k<len;k++){
for(int i=;i<n;i++){
for(int j=;j<;j++){
if(s[k]==a[i].t[j]){
map[k][i]=;
}
}
}
}
int num=;
for(int i=;i<len;i++){
memset(vis,,sizeof(vis));
if(find(i))
num++;
}
//cout<<num<<endl;
if(num==len)printf("YES\n");
else printf("NO\n");
}
return ;
}

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

 

最新文章

  1. Vim魔法堂:认识快捷键绑定
  2. JavaScript Date对象 日期获取函数
  3. MySQL 数据库设计 笔记与总结(1)需求分析
  4. TCP握手
  5. PHP安装环境,服务器不支持curl_exec的解决办法
  6. spoj 416
  7. 一个基于Qt的截屏程序
  8. Upgrade Ver 4.3.x from 4.2.x
  9. (89c51)16x16点阵屏幕的实现
  10. 关闭Pycharm拼写检查(Mac)
  11. [编织消息框架][网络IO模型]BIO
  12. 理解C++ lvalue与rvalue
  13. JVM的GC(概念与深入)
  14. 什么是DevOps?
  15. Notepad++ 的函数参数提示错误的问题终于解决了
  16. #6【bzoj4321】queue2 dp
  17. [No0000129]WPF(1/7)开始教程[译]
  18. 让Logstash每次都从头读文件及常见问题
  19. 【DevExpress v17.2新功能预告】改进DevExtreme编辑器
  20. sencha touch 入门系列 (五)sencha touch运行及代码解析(上)

热门文章

  1. Pyhon从入门到致命
  2. python numpy模块
  3. Hard problem CodeForces - 706C
  4. IAR调试时出现IAR one or more breakpoints could not be set and have been disabled的解决办法
  5. SpringBoot接收前端参数的三种方法
  6. 国内外移动端web适配屏幕方案总结
  7. 【整理】python中re的match、search、findall、finditer区别
  8. Spring 4.3.11.RELEASE文档阅读(一):overview
  9. js 抓取页面数据
  10. iOS学习笔记43-Swift(三)类