UVa 1663 净化器
2024-08-28 20:36:21
https://vjudge.net/problem/UVA-1663
题意:
给m个长度为n的模板串,每个模板串包含字符0,1和最多一个星号"*",其中星号可以匹配0或1。例如,模板01*可以匹配010和011两个串。改写这个模板集合,使得模板的个数最少。
思路:
一个模板只能匹配两个字符串。所以要减少次数的话,我们就要尽量把字符串一一配对,这样每两个字符串都可以用一个模板串来表示。那么这就很明显的是求二分图的最大匹配。下面的代码中因为重复匹配了,所以最后需要除2。
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int maxn = ;
int n, m;
int c[maxn];
int g[maxn][maxn];
int vis[maxn];
int match[maxn]; int dfs(int x)
{
for (int i = ; i < maxn; i++)
{
if (g[x][i] && !vis[i])
{
vis[i] = ;
if (match[i] == - || dfs(match[i]))
{
match[i] = x;
return ;
}
}
}
return ;
} int main()
{
char s[];
ios::sync_with_stdio(false);
//freopen("D:\\txt.txt", "r", stdin);
while (~scanf("%d%d",&n,&m)&&(n||m))
{
memset(c, , sizeof(c));
memset(g, , sizeof(g));
for (int i = ; i < m; i++)
{
int num= ;
int pos = -;
scanf("%s", s);
for (int j = ; j < n; j++)
{
if (s[j] == '') num |= << j;
else if (s[j] == '*') pos = j;
}
c[num] = ;
if (pos != -)
{
num |= << pos;
c[num] = ;
}
}
int cnt = ;
for (int i = ; i < maxn; i++)
{
if (c[i])
{
cnt++;
for (int j = ; j < n; j++)
{
int temp = i ^ ( << j);
if (c[temp]) g[i][temp] = ;
}
}
}
int tot = ;
memset(match, -, sizeof(match));
for (int i = ; i < maxn; i++)
{
memset(vis, , sizeof(vis));
tot += dfs(i);
}
cout << cnt - tot / << endl;
}
}
最新文章
- 【Silverlight】打开Silverlight程序报错,";未找到导入的项目......请确认<;Import>;声明中的路径正确,且磁盘上存在该文件";
- 并发下常见的加锁及锁的PHP具体实现-转载
- [BZOJ3144][HNOI2013]切糕(最小割)
- 找回Win8.1(windows server 2012 R2)的双拼
- 如何用crontab运行一个图形化界面的程序
- AS3.0函数定义的方法
- JS生成不重复随机数
- ubuntu Linux离线安装软件包
- jsp 页面取值
- 连接SQLServer OLEDB数据库(ACCESS) ODBC Oracle
- 使用 flow.ci 快速发布你的项目文档
- Linux学习 用户管理
- day 10 函数名的运用,闭包,迭代器
- [HNOI2012]矿场搭建 BZOJ2730 点双+结论
- 8款世界级Webmail工具推荐
- cmake重新编译
- ASP.net-空白页的问题
- How to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux
- Codeforces Round #521 (Div. 3) D. Cutting Out 【二分+排序】
- review12
热门文章
- Andrew Ng-ML-第十章-应用机器学习的建议
- [LeetCode] 415. Add Strings_Easy tag: String
- MySQL用户授权 和 bin-log日志 详解和实战(http://www.cnblogs.com/it-cen/p/5234345.html)
- 翻译[RFC6238] TOTP: Time-Based One-Time Password Algorithm
- 公司里面用的iTextSharp(教程)---生成一个简答的PDF的语法
- SoapUI 使用变量
- sql server数据库备份单个表的结构和数据生成脚本
- python-安装,设置环境变量(win10)
- STM32f103C8T6 Bootloader设计(转)
- ES6学习--对象属性的遍历