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. [SCOI2015]小凸想跑步

    题目描述 小凸晚上喜欢到操场跑步,今天他跑完两圈之后,他玩起了这样一个游戏. 操场是个凸 n 边形, nn 个顶点按照逆时针从 0 ∼n−1 编号.现在小凸随机站在操场中的某个位置,标记为p点.将 p ...

  2. 解决类似umount target is busy挂载盘卸载不掉问题

    问题描述: Linux下挂载后的分区或者磁盘某些时候需要umount的时候出现类似“umount: /mnt: target is busy.”等字样,或者“umount: /xxx: device ...

  3. yii2 or查询

    // 我们要查询id等于1或者id等于3的数据 $userInfo = User::find()->where(['or' , 'id = 1' , 'id = 3'])->all(); ...

  4. markdown笔记实现页内目录跳转

    方法一 使用Markdown的语法来增加跳转链接:[名称](#id). 1. 只要()内 #号 后面的内容和锚点处标签内的id对应即可,可以任意使用标签支持html语法 2. id不可以有括号和空格, ...

  5. java String转int int转化为String

    String转int String str = "123"; int a = Integer.parseInt(str); System.out.println(a); Integ ...

  6. 一篇文章了解云安全领域的新宠CCSK

    这是一个人人谈"云"."大数据"的时代,作为一个IT民工,如果与同行间聊天时,不谈及这方面的内容,有人可能会觉得你落伍了,跟不上这个时代了.随云计算.云存储之后 ...

  7. 任意模数NTT学习笔记

    这两天有点颓,所以东西学的也很慢...这个一眼就能推出来的活生生卡了我两天.. 说几个细节: 柿子: \[f*g = (\frac{f}{M} +f\%m)*(\frac{g}{M} +g\%m) \ ...

  8. Java网络通信协议、UDP、TCP类加载整理

    网络通信协议 网络通信协议 网络通信协议有很多种,目前应用最广泛的是TCP/IP协议(Transmission Control Protocal/Internet Protoal传输控制协议/英特网互 ...

  9. GO语言系列(一)- 初识go语言

    一.golang语言的特性 1.垃圾回收 a.内存自动回收,再也不需要开发人员管理内存 b.开发人员专注业务实现,降低了心智负担 c.只需要new分配内存,不需要释放 2.天然并发 a.从语言层面支持 ...

  10. idea中的一些快捷键,未完待续......

    1.快速查看注释的渲染效果 在keymap中查找“Quick Documentation”并设置自己喜欢的快捷键即可 2.全局搜索 在keymap中查找“Replace in Path”并设置自己喜欢 ...