Allowed Letters

最直观的想法是贪心取, 然后网络流取check可不可行, 然后T了。

想到最大流可以等于最小割, 那么我们状压枚举字符代表的6个点连向汇点是否断掉,

然后再枚举64个本质不同的位置, 是否需要切段原点联想它的边, 单次check复杂度64 * 64

用sosdp能优化到64 * 6

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end() using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, m, mask[N], c[N], cnt[N], sos[N];
char s[N], t[N], ans[N]; bool check() {
int sum = , ret = inf;
for(int i = ; i < ; i++) sum += c[i];
for(int i = ; i < ; i++) sos[i] = cnt[i];
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
if(j >> i & ) sos[j] += sos[j ^ ( << i)];
for(int s1 = ; s1 < ; s1++) {
int tmp = ;
for(int i = ; i < ; i++)
if(s1 >> i & ) tmp += c[i];
tmp += sum - sos[s1];
chkmin(ret, tmp);
}
return sum == ret;
} inline int getId(char c) {
return c - 'a';
} int main() {
scanf("%s", s + );
n = strlen(s + );
for(int i = ; i <= n; i++) c[getId(s[i])]++;
scanf("%d", &m);
while(m--) {
int p; scanf("%d%s", &p, t + );
for(int i = ; t[i]; i++) mask[p] |= << getId(t[i]);
}
for(int i = ; i <= n; i++) {
if(!mask[i]) mask[i] = ;
cnt[mask[i]]++;
}
for(int i = ; i <= n; i++) {
bool flag = false;
for(int j = ; j < ; j++) {
if(c[j] && mask[i] >> j & ) {
c[j]--;
cnt[mask[i]]--;
flag = check();
if(flag) {
ans[i] = 'a' + j;
break;
}
c[j]++;
cnt[mask[i]]++;
}
}
if(!flag) return puts("Impossible"), ;
}
ans[n + ] = '\0';
puts(ans + );
return ;
} /*
*/

Codeforces 1009G Allowed Letters 最大流转最小割 sosdp的更多相关文章

  1. Codeforces 1009G Allowed Letters FMT,二分图,二分图匹配,霍尔定理

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF1009G.html 题目传送门 - CF1009G 题意 给定一个长度为 $n$ 的字符串 $s$ .并给定 ...

  2. Codeforces 628F 最大流转最小割

    感觉和昨天写了的题一模一样... 这种题也能用hall定理取check, 感觉更最小割差不多. #include<bits/stdc++.h> #define LL long long # ...

  3. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E - Goods transportation 最大流转最小割转dp

    E - Goods transportation 思路:这个最大流-> 最小割->dp好巧妙哦. #include<bits/stdc++.h> #define LL long ...

  4. 【bzoj1001】【最短路】【对偶图】【最大流转最小割】狼抓兔子题解

    [BZOJ1001]狼抓兔子 1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 18872  Solved ...

  5. Codeforces 724E Goods transportation(最小割转DP)

    [题目链接] http://codeforces.com/problemset/problem/724/E [题目大意] 每个城市有pi的物品可以运出去卖,si个物品可以买, 编号小的城市可以往编号大 ...

  6. Codeforces 786E. ALT 最小割+倍增

    E. ALT http://codeforces.com/problemset/problem/786/E 题意: 给出一棵 n 个节点的树与 m 个工人.每个工人有一条上下班路线(简单路径),一个工 ...

  7. Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)

    Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...

  8. Yet Another Maxflow Problem CodeForces - 903G (最小割,线段树)

    大意: 两个n元素集合$A$, $B$, $A_i$与$A_{i+1}$连一条有向边, $B_i$与$B_{i+1}$连一条有向边, 给定$m$条从$A_i$连向$B_j$的有向边, 每次询问修改$A ...

  9. CodeForces E. Goods transportation【最大流+dp最小割】

    妙啊 首先暴力建图跑最大流非常简单,s向每个i连流量为p[i]的边,每个i向t连流量为s[i]的边,每个i向j连流量为c的边(i<j),但是会又T又M 考虑最大流=最小割 然后dp求最小割,设f ...

随机推荐

  1. 【数学建模】数模day13-灰色系统理论I-灰色关联与GM(1,1)预测

    接下来学习灰色系统理论. 0. 什么是灰色系统? 部分信息已知而部分信息未知的系统,我们称之为灰色系统.相应的,知道全部信息的叫白色系统,完全未知的叫黑色系统. 为什么采用灰色系统理论? 在给定信息不 ...

  2. @RequestParam、@RequestBody和@ModelAttribute区别

    一.@RequestParamGET和POST请求传的参数会自动转换赋值到@RequestParam 所注解的变量上1. @RequestParam(org.springframework.web.b ...

  3. php curl报错:417 - Expectation Failed

    当我在post提交的数据增加一段内容后会报错:417 - Expectation Failed. 查资料发现在使用curl做POST时,当post的数据大于1024字节时,curl并不会直接发起pos ...

  4. Spring MVC 使用介绍(六)—— 注解式控制器(二):请求映射与参数绑定

    一.概述 注解式控制器支持: 请求的映射和限定 参数的自动绑定 参数的注解绑定 二.请求的映射和限定 http请求信息包含六部分信息: ①请求方法: ②URL: ③协议及版本: ④请求头信息(包括Co ...

  5. CRLF在过滤XSS语句后打Cookie方式

    看了很长时间的漏洞奖励计划,学到了不少骚姿势,我觉得这个姿势很不错,总结下写篇文章发出来.针对CRLF漏洞打cookie的方法. 这里不讲概念挖掘方式了,以实战为主: 阅读文章前先参考文章:http: ...

  6. 20175221 2018-2019-2 《Java程序设计》第二周学习总结

    20175221   <Java程序设计>第2周学习总结 教材学习内容总结 教材方面 本周学习了第二章的“基本数据类型与数组”的内容,以及粗略地看了一下第三章“运算符.表达式和语句”的内容 ...

  7. cobbler批量安装操作

    打开mirrors.aliyun.com/epel http://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm rpm -ivh h ...

  8. Python终极coding

    作为一名程序员,除了需要具备解决问题的思路以外,代码的质量和简洁性也很关键.因为从一个人的代码可以直接看出你的基本功.对于Python而言,这就意味着你需要对Python的内置功能和库有很深入的了解. ...

  9. NOI-OJ 2.2 ID:8758 2的幂次方表示

    思路 可以把任意一个数转化为2^a+2^b+2^c+...+2^n 例如137的二进制为10001001,这就等效于2^7+2^3+2^0 以上结果如何通过程序循环处理呢?需要把数字n分解为上述公式, ...

  10. SpringBoot系列: 如何优雅停止服务

    ============================背景============================在系统生命周期中, 免不了要做升级部署, 对于关键服务, 我们应该能做到不停服务完成 ...