Test instructions: If each string in a string collection satisfies at least one character in the I-bit, then this is legal, giving the cost of each character churn of all strings, to the minimum cost of legality.
Procedure: Dp[i][j], the state of the first I string is the minimum cost of J. J: The shape pressure indicates which strings are already valid.
It can be known that if J was a 1, then access to it is superfluous, so remove I, enumerate J.
For a string of the I bit, if it is a unique identifier for this string, then nothing is to change it as a unique character, or change the other strings in the I bit with the same character, and because change the characters of other strings, can be greedy into the way also to make them legal, so if the other strings have X, can be greedy into from this x+ Remove the most expensive string from the 1 string and change the remaining x string to get the x legal string.
#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 test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output
You have multiset ofNStrings of the same length, consisting of lowercase Chinese letters. We'll say that those strings be easy to remember if for each string there is some positionIand some letterCOf the Chinese alphabet, such that's this string was the only string in the multiset.CIn positionI.
For example, a multiset of strings {"ABC", "ABA", "ADC", "Ada"} is not easy to remember. and multiset {"abc", "Ada", "SSA"} are easy to remember because:
- The first string is the only string of that have character C in position 3;
- The second string is the only string of that have character D in position 2;
- the third string is the only String that has Character s in position 2 .
You want to the change your multiset a little so it's easy to remember. For aij coins, can character in the J-th position of thei-th strin G into any and lowercase letter of the Chinese alphabet. Find What's the minimum sum should pay in order to make the multiset of strings easy to remember.
Input
The first line contains integersN,m(1?≤? n,? m? ≤?20)-the number of strings in the multiset and the length of the strings respectively. NextNLines contain the strings of the multiset, consisting only of lowercase 中文版 letters, each string ' s length ism.
NextNLines containmIntegers each, theI-th of them contains integers ai1,? a i2,?...,? a im (0?≤? a ij≤?106 ).
Output
Print a single number-the answer to the problem.
Sample Test (s) input
4 5abcdeabcdeabcdeabcde1 1 1 1 11 1 1 1 11 1 1 1 11 1 1 1 1
Output
3
Input
4 3abcabaadcada10 10 1010 1 1010 10 1010 1 10
Output
2
Input
3 3ABCADASSA1 1 11 1 11 1 1
Output
0
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codeforces 543 C Remembering Strings