codeforces 543 C Remembering Strings

探讨了如何通过最小的代价将一组字符串调整为易于记忆的状态。每串字符串的每个字符都有修改费用,目标是在不违反唯一性规则的前提下,使整个集合变得容易记忆。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:若一个字符串集合里的每个字符串都至少有一个字符满足在i位上,只有它有,那么这个就是合法的,给出所有串的每个字符改动的花费,求变成合法的最小代价。


做法:dp[i][j],前i个串的状态为j时的最小花费。j:状压表示已经合法的是哪些串。


可以知道,若j前有i个1,那么访问它就是多余的,所以去掉i,枚举j即可。


对于一个串的i位,若考虑它为这个串的唯一标识,那么无非是改变它为唯一字符,或者改变其他串在i位跟它相同的字符,又因为改变其他串的字符,可以贪心成顺便也都把它们变成合法的,所以若其他串有x个,可以再贪心成从这x+1个串中去掉代价最大的那个串,改变剩下x个串,得到x个合法串。




#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 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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值