zoukankan      html  css  js  c++  java
  • CF 543C Remembering Strings

    https://cn.vjudge.net/problem/CodeForces-543C

    题目

    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.

    Input

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

    Output

    Print a single number — the answer to the problem.

    题解

    把字符串一行一行地写出来

    $nleqslant 20$就暗示了可以二进制压位(压行)……

    设$dp[k]$为容易记住的状态为k时的最小花费

    有两种操作

    1. 改变记不住的行中的一个字符(n小于26,一定可以找到另外的字母),让这个字符串容易记
    2. 改变这一列中有与这个字符相同的字符的字符串,贪心保留最贵的位置,让这些字符串容易记

    其中2是贪心多次1

    但填表法可以按这种策略……

    1. 改变第一个记不住的行中的一个字符
    2. 改变第一个记不住的行中的一个字符的其他相同的行

    反过来就是

    1. 从第一个行记不住的状态转移到现在的状态
    2. 从这些行记不住的转移到现在的状态

    虽然其中2的这些行应该一次性记完,但一个字符串可以有多个独特的字符(算记多次),可能更省钱

    5 2
    aa
    aa
    ab
    bb
    bb
    1 100
    100 100
    1 1
    100 100
    100 1

     修改为

    Ca
    aa
    DE
    bb
    bF

    但是感觉还是有点牵强

    (过了的代码)

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<set>
    #include<iomanip>
    #include<queue>
    
    #define REP(r,x,y) for(register int r=(x); r<(y); r++)
    #define REPE(r,x,y) for(register int r=(x); r<=(y); r++)
    #define PERE(r,x,y) for(register int r=(x); r>=(y); r--)
    #ifdef sahdsg
    #define DBG(...) printf(__VA_ARGS__)
    #else
    #define DBG(...) (void)0
    #endif
    
    using namespace std;
    typedef long long LL;
    typedef pair<LL, LL> pll;
    typedef pair<int, int> pii;
    
    template <class T>
    inline void read(T& x) {
    	char c=getchar();
    	int f=1;x=0;
    	while(!isdigit(c)&&c!='-')c=getchar();
    	if(c=='-')f=-1,c=getchar();
    	while(isdigit(c)){x=x*10+c-'0';c=getchar();}x*=f;
    }
    template <class T,class... A>void read(T&t,A&...a){read(t);read(a...);}
    
    char st[27][27];
    int c[27][27];
    int dp[1<<20];
    int ca[27][27],ka[27][27];
    inline int lg2(int x) {
    	int k=0;
    	while(x) x>>=1, k++;
    	return k;
    }
    int main() {
    	int n,m; read(n,m);
    	REP(i,0,n) fgets(st[i],27,stdin);
    	REP(i,0,n) REP(j,0,m) {
    		read(c[i][j]);
    	}
    	memset(dp,0x3f,sizeof dp);
    	REP(j,0,m) {
    		REP(i,0,n) {
    			ca[i][j]=0;
    			int maxx=0;
    			REP(k,0,n) if(st[i][j]==st[k][j]) {
    				maxx=max(maxx,c[k][j]);
    				ca[i][j]+=c[k][j];
    				ka[i][j]|=1<<k;
    			}
    			ca[i][j]-=maxx;
    		}
    	}
    	dp[0]=0;
    	REP(k,0,1<<n) {
    		int lb=(k)&(-k);
    		int li=lg2(lb)-1;
    		REP(j,0,m) {
    			dp[k]=min(dp[k],dp[k^(k&ka[li][j])]+ca[li][j]);
    			dp[k]=min(dp[k],dp[k^lb]+c[li][j]);
    		}
    	}
    	printf("%d
    ", dp[(1<<n)-1]);
    	return 0;
    }
    
  • 相关阅读:
    Apache Shiro 的三大核心组件
    MyBatis 与 Hibernate 有哪些不同?
    #{}和${}的区别是什么?
    Java Web中前端提交的表单两种方式(处理中文乱码问题)
    Http中200、302、304、404和500等响应状态码所表示的意义?
    jsp三大指令标记,七大动作标记、详解。
    session 的工作原理
    JSP的四种作用域
    jsp有哪些内置对象?作用分别是什么?
    什么是session?什么是cookie?session和cookie有什么区别?
  • 原文地址:https://www.cnblogs.com/sahdsg/p/10707387.html
Copyright © 2011-2022 走看看