Codeforces 544E Remembering Strings 状压dp
题意:
给定n个长度均为m的字符串
以下n行给出字符串
以下n*m的矩阵表示把相应的字母改动成其它字母的花费。
问:
对于一个字符串,若它是easy to remembering 当 它存在一个字母。使得这个字母在这一列是独一无二的。
要使得n个字符串都是easy to remembering 的最小花费。
第一个例子是把第一列的4个a中3个a改动成别的字母。所以花费为3.
思路:
显然是个状压dp,但须要一点转化。
首先得到一个结论:
对于某一列,设这一列的字母是 a,a,b,b,a,d,c···
随意改动某种字母,都能使得包含该种字母的字符串变成unique
instance: 把全部的a字母都改动为别的字母,一定能使得改动后的字母与同列的其它字母不反复。
由于最多仅仅有20个字符串,也就是改动后的字母种类至多仅仅有20种。
然后状压已经easy to remembering的字符串的最小花费。
dp[i] 表示已经easy to remembering 的字符串状态为i时的最小花费。
两个转移:
1、直接改动字母
2、把这一列中全部与这个字母同样的字母都改动成别的字母。
当然能够剩下一个,剩下花费最大的那个就可以。
cost[i][j] 就表示除了花费最大的那个 同列中与str[i][j]字母同样的花费和。
bit[i][j] 表示哪些字符串 在第j列 与 a[i][j] 字母同样。
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x <0) {
putchar('-');
x = -x;
}
if (x>9) pt(x / 10);
putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<ll, ll> pii;
const int inf = 1e9;
const int N = 21;
int n, m;
char s[N][N];
int a[N][N];
int dp[1 << N];
int bit[N][N], cost[N][N];
int main() {
rd(n); rd(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++)rd(a[i][j]);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
int ans = 0, maxn = -inf;
for (int k = 0; k < n; k++)
if (s[i][j] == s[k][j])
{
ans += a[k][j];
maxn = max(maxn, a[k][j]);
bit[i][j] |= 1 << k;
}
ans -= maxn;
cost[i][j] = ans;
}
for (int i = 1; i < (1 << n); i++)dp[i] = inf;
dp[0] = 0;
for (int i = 0; i < (1 << n); i++)
{
for (int j = 0; j < n; j++)
if ((i & (1 << j)) == 0)
{
for (int k = 0; k < m; k++)
{
dp[i | (1 << j)] = min(dp[i | (1 << j)], dp[i] + a[j][k]);
dp[i | bit[j][k]] = min(dp[i | bit[j][k]], dp[i] + cost[j][k]);
}
}
}
pt(dp[(1 << n) - 1]);
return 0;
}
Codeforces 544E Remembering Strings 状压dp的更多相关文章
- Codeforces Round #302 (Div. 1) C - Remembering Strings 状压dp
C - Remembering Strings 思路:最关键的一点是字符的个数比串的个数多. 然后就能状压啦. #include<bits/stdc++.h> #define LL lon ...
- CF543C Remembering Strings 状压dp
Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...
- codeforces Diagrams & Tableaux1 (状压DP)
http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...
- Codeforces 917C - Pollywog(状压 dp+矩阵优化)
UPD 2021.4.9:修了个 typo,为啥写题解老出现 typo 啊( Codeforces 题目传送门 & 洛谷题目传送门 这是一道 *2900 的 D1C,不过还是被我想出来了 u1 ...
- Codeforces 79D - Password(状压 dp+差分转化)
Codeforces 题目传送门 & 洛谷题目传送门 一个远古场的 *2800,在现在看来大概 *2600 左右罢( 不过我写这篇题解的原因大概是因为这题教会了我一个套路罢( 首先注意到每次翻 ...
- codeforces 21D. Traveling Graph 状压dp
题目链接 题目大意: 给一个无向图, n个点m条边, 每条边有权值, 问你从1出发, 每条边至少走一次, 最终回到点1. 所走的距离最短是多少. 如果这个图是一个欧拉回路, 即所有点的度数为偶数. 那 ...
- Codeforces 895C - Square Subsets 状压DP
题意: 给了n个数,要求有几个子集使子集中元素的和为一个数的平方. 题解: 因为每个数都可以分解为质数的乘积,所有的数都小于70,所以在小于70的数中一共只有19个质数.可以使用状压DP,每一位上0表 ...
- CodeForces 327E Axis Walking(状压DP+卡常技巧)
Iahub wants to meet his girlfriend Iahubina. They both live in Ox axis (the horizontal axis). Iahub ...
- Codeforces ----- Kefa and Dishes [状压dp]
题目传送门:580D 题目大意:给你n道菜以及每道菜一个权值,k个条件,即第y道菜在第x道后马上吃有z的附加值,求从中取m道菜的最大权值 看到这道题,我们会想到去枚举,但是很显然这是会超时的,再一看数 ...
随机推荐
- C++中的static关键字的总结 (转载)
C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用. 1.面向过程设计中的st ...
- 【POJ1195】【二维树状数组】Mobile phones
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- 不使用ASP.NET服务器端控件(包括form表单不加runat="server")来触发.cs里的事件(方法),(适用于有代码洁癖者)。
很多时候,我们使用服务器端控件写出的代码,会给我们生成一些很多我们看不懂的代码(初学者),但是有时候我们并不需要这些代码(业务需求不同),对于生成的一些代码感到多余.所以我就开始想,有没有一种可能:不 ...
- kafka监控
做kafka相关项目,需要用到监控的东东,找到了两个方法 第一个: http://quantifind.com/KafkaOffsetMonitor/ 非常容易上手,效果也直观,可以显示各个topic ...
- 定义文字用em、rem,效果和px一样
1em=16px font-size: 2.4rem;/*2.4 × 10px = 24px*/
- 实战EntityFramework
删除对象一定要在同一个context 我尝试这在两个方法中使用两个context(Container)实例来进行一个获得一个删除,结果我获得的”The object cannot be deleted ...
- Gridview BottomPagerRow添加自定义控件
pager.ascx public partial class pager : System.Web.UI.UserControl{ } .aspx protected pager ctl; prot ...
- Unity的Profiler性能分析
1. CPU Usage A. WaitForTargetFPS: Vsync(垂直同步)功能所,即显示当前帧的CPU等待时间 B. Overhead: Profiler总体时间-所有单项的记录时间总 ...
- hadoop输出统计
- UpdateLayeredWindow, Layered Windows, codeproject
http://www.codeproject.com/Articles/16362/Bring-your-frame-window-a-shadow http://www.codeproject.co ...