C. Remembering Strings

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/543/problem/C

Description

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.

Sample 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

Sample Output

3

HINT

题意

给你一堆字符串,如果这个字符的第i个字符在所有字符串的第i个都是不同的,那么我们就说这个字符串很好记

然后你可以花费 a[i][j]使得这个字符变成任意字符

然后问你最小花费,使得所有字符串都很好记

题解:

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=<<;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int a[][];
string s[];
int d[<<];
int p[]; int main()
{
int n=read(),m=read();
for(int i=;i<n;i++)
cin>>s[i];
for(int i=;i<n;i++)
for(int j=;j<m;j++)
cin>>a[i][j];
for(int j=;j<<<n;j++)
d[j]=inf;
d[]=;
for(int i=;i<m;i++)
{
for(int j=;j<;j++)
p[j]=;
for(int j=;j<n;j++)
p[s[j][i]-'a']|=<<j;
for(int j=;j<;j++)
{
int t=,mx=;
for(int k=;k<n;k++)
{
if(p[j]>>k&)
{
for(int l=;l<<<n;l++)
d[l|(<<k)]=min(d[l|(<<k)],d[l]+a[k][i]);
t+=a[k][i];
mx=max(mx,a[k][i]);
}
}
for(int l=;l<<<n;l++)
d[l|p[j]]=min(d[l|p[j]],d[l]+t-mx);
}
}
cout<<d[(<<n)-]<<endl;
return ;
}

Codeforces Round #302 (Div. 1) C. Remembering Strings DP的更多相关文章

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

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

  2. Codeforces Round #302 (Div. 2).C. Writing Code (dp)

    C. Writing Code time limit per test 3 seconds memory limit per test 256 megabytes input standard inp ...

  3. 水题 Codeforces Round #302 (Div. 2) A Set of Strings

    题目传送门 /* 题意:一个字符串分割成k段,每段开头字母不相同 水题:记录每个字母出现的次数,每一次分割把首字母的次数降为0,最后一段直接全部输出 */ #include <cstdio> ...

  4. 完全背包 Codeforces Round #302 (Div. 2) C Writing Code

    题目传送门 /* 题意:n个程序员,每个人每行写a[i]个bug,现在写m行,最多出现b个bug,问可能的方案有几个 完全背包:dp[i][j][k] 表示i个人,j行,k个bug dp[0][0][ ...

  5. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 /* 题意:在n^n的海洋里是否有k块陆地 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 输出完k个L后,之后全部输出S:) 5 10 的例子可以是这样的: LSLS ...

  6. Codeforces Round #302 (Div. 2)

    A. Set of Strings 题意:能否把一个字符串划分为n段,且每段第一个字母都不相同? 思路:判断字符串中出现的字符种数,然后划分即可. #include<iostream> # ...

  7. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  8. Codeforces Round #302 (Div. 2) A. Set of Strings 水题

    A. Set of Strings Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/544/pr ...

  9. Codeforces Round #313 (Div. 1) B. Equivalent Strings

    Equivalent Strings Problem's Link: http://codeforces.com/contest/559/problem/B Mean: 给定两个等长串s1,s2,判断 ...

随机推荐

  1. 122.Best Time to Buy and Sell Stock II---dp

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ 题目大意:基本定义与121类似,不 ...

  2. 关于"轉淚點"与"轉捩點"

    经常看台湾偶像剧或台湾综艺节目的人,一定听过"转泪点"这个词,虽然我一直不知道这三个字具体是怎么写, 但其意思很容易明白,就是"转折点"的意思.今天无聊在看凤凰 ...

  3. Linux中涉及到环境变量的文件

    1. 系统级 (a) /etc/profile : 在用户登录操作系统时,定制用户环境的第一个文件,应用于登录的每一个用户 ==> 该文件一般调用/etc/bash.bashrc文件 (b)/e ...

  4. freemarker模板引擎的使用

    freemarker是一套前端模板引擎,在使用时,要先在web项目中添加freemarker.jar的依赖. 我在这里主要演示spring-mvc整合freemarker模板引擎.项目案例的文件包结构 ...

  5. JSP、EL表达式、JSTL

    JSP 1.什么是jsp? Java Server Pages: java服务器端页面.可以理解为一个特殊的页面,其中既可以指定定义html标签,又可以定义java代码.其本质就是一个Servlet. ...

  6. Nginx 502错误:upstream sent too big header while reading response header from upstream

    原因: 在使用Shiro的rememberMe功能时,服务器返回response的header部分过大导致. 解决方法: https://stackoverflow.com/questions/238 ...

  7. python基础(9)--递归、二叉算法、多维数组、正则表达式

    1.递归 在函数内部,可以调其他函数,如果一个函数在内部调用它本身,这个函数就是递归函数.递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于裂解 递归算法解决问题的特点: 1)递归是 ...

  8. 深度学习方法(七):最新SqueezeNet 模型详解,CNN模型参数降低50倍,压缩461倍!

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 继续前面关于深度学习CNN经典模型的 ...

  9. csu 1798(树上最远点对,线段树+lca)

    1798: 小Z的城市 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 60  Solved: 16[Submit][Status][Web Board] ...

  10. jmeter-----GUI运行和非GUI运行的区别

    gui:界面会消耗很多资源,并且运行的结果是保存在Jmeter运行的内存中.当时间一长,内存增长到一定程度,就会报错,甚至假死. 非gui:实时的将运行log文件保存到本地文件中,不会撑爆内存.并且对 ...