Codeforces Round #302 (Div. 2) E. Remembering Strings(状压dp)

探讨如何通过最小的成本使一组字符串变得容易记忆,每一步修改都涉及字符替换或删除,旨在解决记忆难题。

E. Remembering Strings
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
一个字符串是好记的当且仅当它在第i位上的字符为c且其他字符串的第i位不为c。你可以将第i个字符串的第j列变成任意的字符,花费为a[i][j]。

状压dp,dp[S]表示使S好记的最小花费,转移有两种情况一种是任选一个字符改变他,代价为a[i][j],另一种是这一列有多个相同的字符c,保留其中一个,把其他为c的全部改掉,应该保留a[i][j]最大的那个c,这样才是最小花费。

#include <bits/stdc++.h>
#define foreach(it,v) for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
using namespace std;
typedef long long ll;
const int inf = 1e8;
const int maxn = 20;
int d[1<<(maxn+1)],a[maxn][maxn];
string s[maxn];
int lowzero(int S)
{
    for(int i = 0; i < maxn; i++)if(!((S>>i)&1))
        return i;
    return maxn-1;
}
int main(int argc, char const *argv[])
{
    int n,m;
    while(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>>a[i][j];

        for(int i = (1<<n)-1; i > 0; i--)d[i] = inf;
        d[0] = 0;
        int M = 1<<n;
        for(int S = 0; S < M; S++) {
            int bit = lowzero(S);
            for(int c = 0; c < m; c++) {
                d[S|(1<<bit)] = min(d[S|(1<<bit)],d[S] + a[bit][c]);
                int sum = 0, mw = 0, bits = 0;
                for(int r = 0; r < n; r++) if(s[bit][c]==s[r][c]) {
                    sum += a[r][c];
                    mw = max(mw,a[r][c]);
                    bits |= 1<<r;
                }
                d[S|bits] = min(d[S|bits],d[S] + sum - mw);
            }
        }
        cout<<d[M-1]<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值