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. 配置启动MySQL的Docker容器

    docker run -d -p : --name mysql -e MYSQL_ROOT_PASSWORD= mysql:

  2. HBase轻松入门之HBase架构图解析

    2018-12-13 2018-12-20 本篇文章旨在针对初学者以我本人现阶段所掌握的知识就HBase的架构图中各模块作一个概念科普.不对文章内容的“绝对.完全正确性”负责. 1.开胃小菜 关于HB ...

  3. Codeforces 1077F1 Pictures with Kittens (easy version)(DP)

    题目链接:Pictures with Kittens (easy version) 题意:给定n长度的数字序列ai,求从中选出x个满足任意k长度区间都至少有一个被选到的最大和. 题解:$dp[i][j ...

  4. 深入理解JVM(3)——类加载机制

    1.类加载时机 类的整个生命周期包括了:加载( Loading ).验证( Verification ).准备( Preparation ).解析( Resolution ).初始化( Initial ...

  5. 自动化运维工具Ansible介绍

    一个由 Python 编写的强大的配置管理解决方案.尽管市面上已经有很多可供选择的配置管理解决方案,但他们各有优劣,而 ansible 的特点就在于它的简洁. 让 ansible 在主流的配置管理系统 ...

  6. 关于百度地图(离线)使用过程报“Cannot read property 'jb' of undefined ”错误的解决办法

    使用百度地图(离线)API时,地图无法显示,f12查看报错: BaiduApi_2.0.js:1056 Uncaught TypeError: Cannot read property 'jb' of ...

  7. VUE-Windows系统下搭建vue环境

    一.安装node.js(https://nodejs.org/en/) 下载完毕后,可以安装node,建议不要安装在系统盘(如C:).注意记下路径..   此处默认安装这4项即可,点击Next按钮. ...

  8. LaTeX技巧561:LaTeX如何让每一章带有目录?

    转自: http://blog.sina.com.cn/s/blog_5e16f1770102ds8b.html LaTeX技巧561:LaTeX如何让每一章带有目录? [问题描述] 当前章节列出该章 ...

  9. 使用Notepad++开发Java程序

    安装NppExec插件(已安装可跳过) 插件下载地址 我选择了最新的RC2 根据软件位数下载对应的版本,我直接下载了32位对应的dll 解压后里面有两个文件夹和一个dll文件 拷贝到Notepad++ ...

  10. Ch02 课堂作业

    测试一:运行结果: 测试二:运行结果: 测试三:运行结果: