zoukankan      html  css  js  c++  java
  • Codeforces Round #302 (Div. 1) C. Remembering Strings DP

    C. Remembering Strings

    Time Limit: 20 Sec  Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/543/problem/C

    Description

    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.

    Sample 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

    Sample Output

    3

    HINT

     

    题意

    给你一堆字符串,如果这个字符的第i个字符在所有字符串的第i个都是不同的,那么我们就说这个字符串很好记

    然后你可以花费 a[i][j]使得这个字符变成任意字符

    然后问你最小花费,使得所有字符串都很好记

    题解:

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=1<<29;
    /*
    
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    */
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    int a[40][40];
    string s[40];
    int d[1<<20];
    int p[40];
    
    int main()
    {
        int n=read(),m=read();
        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 j=0;j<1<<n;j++)
            d[j]=inf;
        d[0]=0;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<26;j++)
                p[j]=0;
            for(int j=0;j<n;j++)
                p[s[j][i]-'a']|=1<<j;
            for(int j=0;j<26;j++)
            {
                int t=0,mx=0;
                for(int k=0;k<n;k++)
                {
                    if(p[j]>>k&1)
                    {
                        for(int l=0;l<1<<n;l++)
                            d[l|(1<<k)]=min(d[l|(1<<k)],d[l]+a[k][i]);
                        t+=a[k][i];
                        mx=max(mx,a[k][i]);
                    }
                }
                for(int l=0;l<1<<n;l++)
                    d[l|p[j]]=min(d[l|p[j]],d[l]+t-mx);
            }
        }
        cout<<d[(1<<n)-1]<<endl;
        return 0;
    }
  • 相关阅读:
    WDNMD--冲刺日志(第二天)
    spring boot: filter/interceptor/aop在获取request/method参数上的区别(spring boot 2.3.1)
    spring boot:使用validator做接口的参数、表单、类中多字段的参数验证(spring boot 2.3.1)
    spring boot:使接口返回统一的RESTful格式数据(spring boot 2.3.1)
    spring boot:使用log4j2做异步日志打印(spring boot 2.3.1)
    spring boot:给接口增加签名验证(spring boot 2.3.1)
    intellij idea:设置java方法注释模板(intellij idea 2019.2)
    linux(centos8):zabbix配置邮件报警(监控错误日志)(zabbix5.0)
    linux(centos8):lnmp环境编译安装zabbix5.0
    spring boot:使用多个redis数据源(spring boot 2.3.1)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4487622.html
Copyright © 2011-2022 走看看