http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1550

很久以前做的一题,当时队友用最大流做,现在我也是

这个转化为二分图多重匹配,就是一样的意思了。

设出一个原点S,两个人1,和2,S-->1的流量是n表明只能流出n个字母,s-->2的流量是n,一样含义。

然后再设一个汇点T,表明需要有多少流进来,跑一发就好。

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = * ;
struct Edge {
int u, v, w, tonext;
int id;
}e[maxn * ];
int first[maxn], num;
void addEdge(int u, int v, int w) {
e[num].u = u, e[num].v = v, e[num].w = w, e[num].tonext = first[u], e[num].id = num;
first[u] = num++;
}
int flow[maxn], pre[maxn];
int bfs(int be, int en) { //O(n * E * E)
queue<int> que;
memset(flow, false, sizeof flow);
pre[be] = -, flow[be] = inf;
que.push(be);
while (!que.empty()) {
int cur = que.front();
que.pop();
for (int i = first[cur]; ~i; i = e[i].tonext) {
int v = e[i].v;
if (flow[v] == && e[i].w > ) {
pre[v] = e[i].id;
flow[v] = min(flow[cur], e[i].w);
que.push(v);
}
}
if (flow[en]) break; //找到了增广路
}
if (flow[en] == ) return -;
else return flow[en];
} int maxFlow(int be, int en) {
int sumFlow = ;
while (true) {
int res = bfs(be, en);
if (res == -) { //找不到增广路
break;
}
int edgeID = pre[en];
while (edgeID != -) {
e[edgeID].w -= res;
e[edgeID ^ ].w += res;
edgeID = pre[e[edgeID].u];
}
sumFlow += res;
}
return sumFlow;
}
char s1[maxn], s2[maxn], s3[maxn];
int cnt1[], cnt2[], cnt3[];
void work() {
memset(cnt1, , sizeof cnt1);
memset(cnt2, , sizeof cnt2);
memset(cnt3, , sizeof cnt3);
memset(first, -, sizeof first);
num = ;
int lenstr = strlen(s1 + );
addEdge(, , lenstr / );
addEdge(, , );
addEdge(, , lenstr / );
addEdge(, , );
for (int i = ; i <= lenstr; ++i) {
cnt1[s1[i]]++;
cnt2[s2[i]]++;
cnt3[s3[i]]++;
}
for (int i = 'A'; i <= 'Z'; ++i) {
addEdge(, i - 'A' + , cnt1[i]);
addEdge(i - 'A' + , , );
addEdge(, i - 'A' + , cnt2[i]);
addEdge(i - 'A' + , , cnt2[i]);
}
for (int i = 'A'; i <= 'Z'; ++i) {
addEdge(i - 'A' + , , cnt3[i]);
addEdge(, i - 'A' + , );
}
int res = maxFlow(, );
if (res == lenstr) {
printf("YES\n");
} else printf("NO\n");
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
while (scanf("%s%s%s", s1 + , s2 + , s3 + ) > ) work();
return ;
}

1550: Simple String 最大流解法的更多相关文章

  1. 1550: Simple String (做得少的思维题,两个字符串能否组成另外一个字符串问题)

    1550: Simple String Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 256 Mb     Submitt ...

  2. Water --- CSU 1550: Simple String

    Simple String Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1550 Mean: 略. analy ...

  3. CSU - 1550 Simple String —— 字符串

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1550 题解: 1.A+B 与C的交集必须>=n 2.A与C的交集必须>= ...

  4. 【stanford C++】字符串(String)与流(Stream)

    字符串(String)与流(Stream) 一.C++中字符串(String) 字符串(String):就是(可能是空的)字符序列. C++中的字符串在概念上和Java中的字符串类似. C++字符串用 ...

  5. 线性规划费用流解法(Bzoj1061: [Noi2008]志愿者招募)

    题面 传送门 Sol 线性规划费用流解法用与求解未知数为非负数的问题 这道题可以列出一堆形如 \(x[i]+x[j]+x[k]+...>=a[p]\) 的不等式 我们强行给每个式子减去一个东西, ...

  6. (比赛)A - Simple String Problem

    A - Simple String Problem Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%lld & ...

  7. FZU - 2218 Simple String Problem(状压dp)

    Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...

  8. D - Simple String CSU - 1550

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1550 很久都没补这题,最近想学网络流,就看看,队友以前用网络流过的,Orz, 但是这题只需要简 ...

  9. delphi 怎么将一个文件流转换成字符串(String到流,String到文件,相互转化)

    //from   http://kingron.myetang.com/zsfunc0d.htm (*//   标题:充分利用pascal字符串类型   说明:和PChar不同,string可以保存# ...

随机推荐

  1. js图片上传及显示

    html部分: <form action='' method='post' name='myform'> <input type='file' id='iptfileupload' ...

  2. Pycharm用鼠标滚轮控制字体大小的

    Pycharm用鼠标滚轮控制字体大小的   一.pycharm字体放大的设置 File —> setting —> Keymap —>在搜寻框中输入:increase —> I ...

  3. 用OpenLayers开发地图应用

    项目背景 最近有一个使用全球地图展示数据的项目,用地图展示数据本身没什么难度,但出于安全和保密的考虑,甲方单位要求项目不能连接外网,只能在内网使用,也就是说,我们不得不在内网中部署一个地图服务器,在这 ...

  4. UE4 框架

    转自:http://www.cnblogs.com/NEOCSL/p/4059841.html 有很多人是从UE3 接触到Unreal,如果你也对UE3非常了解,便能很快的上手UE4.但是,UE4的开 ...

  5. Poco 编译mysql

    POCO mysql需要自己添加connecter的header和lib MySQL Client: For the MySQL connector, the MySQL client librari ...

  6. 如何分析一个QT类

    作者:gnuhpc  出处:http://www.cnblogs.com/gnuhpc/ 我们以QLineEdit这个类为例来看看如何学习分析一个QT类. 1.Public Types: 这是一个在这 ...

  7. SQL Replication

    http://www.cnblogs.com/CareySon/archive/2012/06/20/IntroductToSQLServerReplicationPart1.html http:// ...

  8. 从0起步 BI 第一课

    收集 BI 学习资料(了解,书籍,示例demo) 专业人士: http://www.cnblogs.com/aspnetx/p/3189029.html http://www.flybi.net/ h ...

  9. C# 32位程序在64位系统下运行中解决重定向问题

    在64位的Windows操作系统中,为了兼容32位程序的运行,64位的Windows操作系统采用重定向机制.目的是为了能让32位程序在64位的操作系统不仅能操作关键文件文夹和关键的注册表并且又要避免与 ...

  10. iview组件select之默认展示label,并传空value做方法入参

    要求: 默认查询操作日期在当日的数据:(打开页面时默认选中时间.全部) 后台约定:选定“全部”这个条件,传的值是空"" 综上:使用select选择框的v-model绑定数据,使用: ...