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.
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).
Print a single number — the answer to the problem.
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
3
4 3 abc aba adc ada 10 10 10 10 1 10 10 10 10 10 1 10
2
3 3 abc ada ssa 1 1 1 1 1 1 1 1 1
0
状压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;
}

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

225

被折叠的 条评论
为什么被折叠?



