[CF544E]Remembering Strings_状压dp

E. Remembering Strings

Subject to the effect :

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.

Data range :

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


Problem solution :

$ Lijinnn $ thief was put God, look at problem solution feel better.

First, we need to find that every character of each column may only have two changes in circumstances.

The first is to get rid of a character on the line.

The second is a character in this column then get rid of all the right to leave a maximum value.

It is obvious that it is not necessarily the only .... but it must be optimal.

Then there is the $ dp $, and assume $ f [S] $ to $ S $ indicates the status of a legitimate string, consider each column of each row have a kind of way of transfer, transfer updates to violence.

In fact, there is a small optimization, but relied on $ CF $ quick evaluation machine is not necessary.

Is such that we find, first change a column in a row, then change to another line in another column, and replace the back is the same as their order.

So, when we use $ S $ transferred back, just either take a $ 0 $ bit later transferred to.

Each intermediate can not be guaranteed $ f [S] $ is optimal, but to ensure $ f [(1 << n) -1] $ is optimal, and an update to the final answer is the most that one strand excellent.

Code :( optimization is reflected in this small, judging that $ break $ $ 0 $ that $ if $ in)

#include <bits/stdc++.h>

#define N 21 

using namespace std;

char s[N][N];

int w[N][N], sm[N][N], cs[N][N];

int dp[1 << N], n, m;

void dispose() {
    for (int i = 0; i < n; i ++ ) {
        for (int j = 0; j < m; j ++ ) {
            int al = 0, mx = 0;
            for (int k = 0; k < n; k ++ ) {
                if (s[i][j] == s[k][j]) {
                    al += w[k][j];
                    mx = max(mx, w[k][j]);
                    sm[i][j] |= (1 << k);
                }
            }
            cs[i][j] = al - mx;
        }
    }

    memset(dp, 0x3f, sizeof dp);

    dp[0] = 0;
    for (int s = 0; s < (1 << n); s ++ ) {
        for (int i = 0; i < n; i ++ ) {
            if(!(s >> i & 1)) {
                for (int j = 0; j < m; j ++ ) {
                    dp[s | (1 << i)] = min(dp[s | (1 << i)], dp[s] + w[i][j]);
                    dp[s | sm[i][j]] = min(dp[s | sm[i][j]], dp[s] + cs[i][j]);
                }
                break;
            }
        }
    }
}

int main() {
    cin >> n >> m ;
    for (int i = 0; i < n; i ++ ) {
        scanf("%s", s[i]);
    }
    for (int i = 0; i < n; i ++ ) {
        for (int j = 0; j < m; j ++ ) {
            scanf("%d", &w[i][j]);
        }
    }
    dispose();
    printf("%d\n", dp[(1 << n) - 1]);
    return 0;
}

Summary : The last of the optimization is actually a good idea, a method is also very difficult to think of, looking forward to master.

Guess you like

Origin www.cnblogs.com/ShuraK/p/11240627.html