https://cn.vjudge.net/problem/CodeForces-543C

题目

You have multiset of n strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some position i and some letter c of the English alphabet, such that this string is the only string in the multiset that has letter c in position i.

For example, a multiset of strings {"abc", "aba", "adc", "ada"} are not easy to remember. And multiset {"abc", "ada", "ssa"} is easy to remember because:

  • the first string is the only string that has character c in position 3;
  • the second string is the only string that has character d in position 2;
  • the third string is the only string that has character s in position 2.

You want to change your multiset a little so that it is easy to remember. For aij coins, you can change character in the j-th position of the i-th string into any other lowercase letter of the English alphabet. Find what is the minimum sum you should pay in order to make the multiset of strings easy to remember.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 20) — the number of strings in the multiset and the length of the strings respectively. Next n lines contain the strings of the multiset, consisting only of lowercase English letters, each string's length is m.

Next n lines contain m integers each, the i-th of them contains integers ai1, ai2, ..., aim (0 ≤ aij ≤ 106).

Output

Print a single number — the answer to the problem.

题解

把字符串一行一行地写出来

$n\leqslant 20$就暗示了可以二进制压位(压行)……

设$dp[k]$为容易记住的状态为k时的最小花费

有两种操作

  1. 改变记不住的行中的一个字符(n小于26,一定可以找到另外的字母),让这个字符串容易记
  2. 改变这一列中有与这个字符相同的字符的字符串,贪心保留最贵的位置,让这些字符串容易记

其中2是贪心多次1

但填表法可以按这种策略……

  1. 改变第一个记不住的行中的一个字符
  2. 改变第一个记不住的行中的一个字符的其他相同的行

反过来就是

  1. 从第一个行记不住的状态转移到现在的状态
  2. 从这些行记不住的转移到现在的状态

虽然其中2的这些行应该一次性记完,但一个字符串可以有多个独特的字符(算记多次),可能更省钱

5 2
aa
aa
ab
bb
bb
1 100
100 100
1 1
100 100
100 1

修改为

Ca
aa
DE
bb
bF

但是感觉还是有点牵强

(过了的代码)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<iomanip>
#include<queue> #define REP(r,x,y) for(register int r=(x); r<(y); r++)
#define REPE(r,x,y) for(register int r=(x); r<=(y); r++)
#define PERE(r,x,y) for(register int r=(x); r>=(y); r--)
#ifdef sahdsg
#define DBG(...) printf(__VA_ARGS__)
#else
#define DBG(...) (void)0
#endif using namespace std;
typedef long long LL;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii; template <class T>
inline void read(T& x) {
char c=getchar();
int f=1;x=0;
while(!isdigit(c)&&c!='-')c=getchar();
if(c=='-')f=-1,c=getchar();
while(isdigit(c)){x=x*10+c-'0';c=getchar();}x*=f;
}
template <class T,class... A>void read(T&t,A&...a){read(t);read(a...);} char st[27][27];
int c[27][27];
int dp[1<<20];
int ca[27][27],ka[27][27];
inline int lg2(int x) {
int k=0;
while(x) x>>=1, k++;
return k;
}
int main() {
int n,m; read(n,m);
REP(i,0,n) fgets(st[i],27,stdin);
REP(i,0,n) REP(j,0,m) {
read(c[i][j]);
}
memset(dp,0x3f,sizeof dp);
REP(j,0,m) {
REP(i,0,n) {
ca[i][j]=0;
int maxx=0;
REP(k,0,n) if(st[i][j]==st[k][j]) {
maxx=max(maxx,c[k][j]);
ca[i][j]+=c[k][j];
ka[i][j]|=1<<k;
}
ca[i][j]-=maxx;
}
}
dp[0]=0;
REP(k,0,1<<n) {
int lb=(k)&(-k);
int li=lg2(lb)-1;
REP(j,0,m) {
dp[k]=min(dp[k],dp[k^(k&ka[li][j])]+ca[li][j]);
dp[k]=min(dp[k],dp[k^lb]+c[li][j]);
}
}
printf("%d\n", dp[(1<<n)-1]);
return 0;
}

CF 543C Remembering Strings的更多相关文章

  1. Codeforces 543C Remembering Strings(DP)

    题意比较麻烦 见题目链接 Solution: 非常值得注意的一点是题目给出的范围只有20,而众所周知字母表里有26个字母.于是显然对一个字母进行变换后是不影响到其它字符串的. 20的范围恰好又是常见状 ...

  2. Codeforces Round #302 (Div. 1) C - Remembering Strings 状压dp

    C - Remembering Strings 思路:最关键的一点是字符的个数比串的个数多. 然后就能状压啦. #include<bits/stdc++.h> #define LL lon ...

  3. Codeforces Round #302 (Div. 1) C. Remembering Strings DP

    C. Remembering Strings Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  4. CF Set of Strings

    Set of Strings time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. CF Vitaly and Strings

    Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. Codeforces 544E Remembering Strings 状压dp

    题目链接 题意: 给定n个长度均为m的字符串 以下n行给出字符串 以下n*m的矩阵表示把相应的字母改动成其它字母的花费. 问: 对于一个字符串,若它是easy to remembering 当 它存在 ...

  7. codeforces 543 C Remembering Strings

    题意:若一个字符串集合里的每一个字符串都至少有一个字符满足在i位上,仅仅有它有,那么这个就是合法的,给出全部串的每一个字符修改的花费,求变成合法的最小代价. 做法:dp[i][j].前i个串的状态为j ...

  8. CF 149E Martian Strings 后缀自动机

    这里给出来一个后缀自动机的题解. 考虑对 $s$ 的正串和反串分别建后缀自动机. 对于正串的每个节点维护 $endpos$ 的最小值. 对于反串的每个节点维护 $endpos$ 的最大值. 这两个东西 ...

  9. CF543C Remembering Strings 状压dp

    Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...

随机推荐

  1. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.3版本全新发布

    1.RDIFramework.NET框架介绍 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,为企业或个人快速开发系统提供了强大的支持,开发人员不需要开发系统的基础功能和 ...

  2. 【我们一起写框架】MVVM的WPF框架(四)—DataGrid

    前言 这个框架写到这里,应该有很多同学发现,框架很多地方的细节,其实是违背了MVVM的设计逻辑的. 没错,它的确是违背了. 但为什么明知道违背设计逻辑,还要这样编写框架呢? 那是因为,我们编写的是框架 ...

  3. 【Javascript】JS遍历数组的三种方法:map、forEach、filter

    前言 近一段时间,因为项目原因,会经常在前端对数组进行遍历.处理,JS自带的遍历方法有很多种,往往不加留意,就可能导致知识混乱的现象,并且其中还存在一些坑.前端时间在ediary中总结了js原生自带的 ...

  4. [JavaScript] 函数节流(throttle)和函数防抖(debounce)

    js 的函数节流(throttle)和函数防抖(debounce)概述 函数防抖(debounce) 一个事件频繁触发,但是我们不想让他触发的这么频繁,于是我们就设置一个定时器让这个事件在 xxx 秒 ...

  5. Spring异常分析

    异常报错 2019-01-14 10:40:18.427 ERROR 11776 --- [ost-startStop-1] o.s.b.w.e.t.TomcatStarter : Error sta ...

  6. Revit通过API创建共享参数

    Revit共享参数是通过创建一个.txt类型的文件来保存相关信息,一旦与项目保存完毕之后,共享参数也就变成了项目参数(项目参数无法通过API创建),项目参数是保存在Revit项目里面的,所以此时这个. ...

  7. while,for,if输入账号密码判断(还请各位大牛能够优化,本人刚学习一周)

    AccountNumber1 = [] password1 = [] flag = True while flag: num = 0 a = 1 print('-----------这是个欢迎界面-- ...

  8. Editplus的扩展程序的删除

    好久没用Editplus,然后准备删除,结果发现有进程正在运行,删除不掉 ,于是,用软件管家删除 用软件管家删除结束之后,有残留文件,就是Editplus的扩展程序        1.于是找到Edit ...

  9. 忘记Linux登录密码怎么办?

    1.启动虚拟机,出现下面倒计时界面时,按e键.进入启动前编辑. 2.进入如下界面,再按e键. 3.进入如下页面后,选中第二项kernel开头的项,选中后再按e键. 4.进入如下界面后,在最后面输入空格 ...

  10. SAP MM '独立/集中'等于1的MTS物料MRP运行后合并需求触发PR

    SAP MM '独立/集中'等于1的MTS物料MRP运行后合并需求触发PR Test data 独立与集中: 1 (仅个别需求) STO 1, 这是一个公司间STO,从国内生产基本转入香港贸易公司, ...