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

做法:dp[i][j]。前i个串的状态为j时的最小花费。j:状压表示已经合法的是哪些串。

能够知道。若j前有i个1,那么訪问它就是多余的,所以去掉i,枚举j就可以。

对于一个串的i位。若考虑它为这个串的唯一标识。那么无非是改变它为唯一字符,或者改变其它串在i位跟它同样的字符,又由于改变其它串的字符。能够贪心成顺便也都把它们变成合法的,所以若其它串有x个,能够再贪心成从这x+1个串中去掉代价最大的那个串,改变剩下x个串。得到x个合法串。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int val[30][30],cost[30][30],mark[30][30];
string s[30];
int dp[1<<20];
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>s[i];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>cost[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
int mx=0;
for(int k=0;k<n;k++)
if(s[i][j]==s[k][j])
{
val[i][j]+=cost[k][j];
mx=max(mx,cost[k][j]);
mark[i][j]|=1<<k;
}
val[i][j]-=mx;
}
int len=(1<<n)-1;
memset(dp,63,sizeof(dp));
dp[0]=0;
for(int i=0;i<len;i++)
{
for(int j=0;;j++)
if(!((i>>j)&1))
{
for(int k=0;k<m;k++)
{
dp[i|(1<<j)]=min(dp[i|(1<<j)],dp[i]+cost[j][k]);
dp[i|mark[j][k]]=min(dp[i|mark[j][k]],dp[i]+val[j][k]);
}
break;
}
}
cout<<dp[len];
}

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 thei-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 nm (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.

Sample test(s)
input
4 5
abcde
abcde
abcde
abcde
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
output
3
input
4 3
abc
aba
adc
ada
10 10 10
10 1 10
10 10 10
10 1 10
output
2
input
3 3
abc
ada
ssa
1 1 1
1 1 1
1 1 1
output
0

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

  1. 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 ...

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

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

  3. Codeforces 385B Bear and Strings

    题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...

  4. Codeforces 482C Game with Strings(dp+概率)

    题目链接:Codeforces 482C Game with Strings 题目大意:给定N个字符串,如今从中选定一个字符串为答案串,你不知道答案串是哪个.可是能够通过询问来确定, 每次询问一个位置 ...

  5. Codeforces 544E Remembering Strings 状压dp

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

  6. Codeforces 543C Remembering Strings(DP)

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

  7. Codeforces 543 B. World Tour

    http://codeforces.com/problemset/problem/543/B 题意: 给定一张边权均为1的无向图. 问至多可以删除多少边,使得s1到t1的最短路不超过l1,s2到t2的 ...

  8. 【24.34%】【codeforces 560D】Equivalent Strings

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. CodeForces 682D Alyona and Strings (四维DP)

    Alyona and Strings 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/D Description After re ...

随机推荐

  1. JavaScript-html标题滚动效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 用jquery控制表格奇偶行及活动行颜色

    虽然jquery流行已经很多年了,一直都感觉很难,也没有花时间去学习它,只是偶尔哪天心血来潮了去看一点点,时隔多日又会忘得一干二净.最近用到表格奇偶行不同色,不得不去再看jquery,虽然感觉还是难, ...

  3. 请问这个git上开源的node项目怎样才能在windows用Npm跑起来

    这个项目https://github.com/wechaty/we...以前都是用人家弄好的手脚架搞得es6,搞了2天搞起了es6还报错,错误信息在下面,然后我想请教大神:1我到底应该怎么弄才能在wi ...

  4. theano import error (win10 python2.7)

    因为项目需要,在win10-64位电脑上配置theano.但是一直有 import error的错误,找不到解决方法.作为一个python新手,实在搞不定,请大家不吝赐教!小女子不胜感激! 按照网上的 ...

  5. EasyUI 之 DataGrid利用用拉姆达表达式实现分页查询

      首先,我们在DataGrid的URL中加上我们要查询的条件:查询用户名不是“呵呵”的所有用户. <div> <table id="dg" class=&quo ...

  6. Python3基础笔记---re模块

    参考博客: Py西游攻关之模块 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列 ...

  7. ES6学习4 变量的解构赋值

    变量的解构赋值 一.数组结构赋值 1.数组结构赋值 let [a, b, c] = [1, 2, 3]; ES6 可以从数组中提取值,按照对应位置,对变量赋值. 1)  本质上,这种写法属于“模式匹配 ...

  8. Spring MVC框架一个实例的手动实现

    文件结构: SpringMVC05 // 应用程序名 ----index.html // 欢迎文件,主目录下的文件可以被URL直接访问到 ----WEB-INF // 这个目录下的文件将被保护起来不能 ...

  9. caioj 1071 动态规划入门(二维一边推4:相似基因) (最长公共子序列拓展)

    复制上一题总结 caioj 1069到1071 都是最长公共字序列的拓展,我总结出了一个模型,屡试不爽    (1) 字符串下标从1开始,因为0用来表示字符为空的情况,而不是第一个字符     (2) ...

  10. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)

    [题目链接]:http://codeforces.com/contest/822/problem/C [题意] 有n个旅行计划, 每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述; ...