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

对于每一个字符串,可以预处理出其到达数字,字母,和特殊符号所需的最小步数。

然后就是在n个东西中,选出数字、字母和特殊符号,至少一个,所需的最少步数。

因为第一个选了数字的话,它就不能选字母的了,然后比赛的时候发现这就是二分图,然后就上了km的模板。(太笨了我)

虽然过了,但是明显正解不是这个。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
char str[maxn][maxn];
int e[ + ][ + ]; int match[maxn];//match[col] = row
int vx[maxn], vy[maxn];
int fx[maxn], fy[maxn];
int dfs (int u, int m) {
vx[u] = ;
int i;
for (i = ; i <= m; i++) { //筛选n个 导航员 col的值
if (vy[i] == && fx[u] + fy[i] == e[u][i]) {
vy[i] = ;
if (match[i] == || dfs(match[i], m)) {
match[i] = u; // match[col]=row;
return ;//搭配成功
}
}
}
return ;//我找不到啊,后面,就会执行km
}
void do_km(int n, int m) { //
int i, j;
int d = -inf;
for (i = ; i <= n; i++) { //遍历每一个驾驶员 row的值
if (vx[i] == ) {
for (j = ; j <= m; j++) { //对他进行遍历导航员 col的值
if (!vy[j]) {
if (d < (fx[i] + fy[j] - e[i][j])) {
d = fx[i] + fy[j] - e[i][j];
}
}
}
}
}
for (i = ; i <= n; i++) {
if (vx[i] == ) {
fx[i] -= d;
vx[i] = ; //请0
}
if (vy[i] == ) { //
fy[i] += d;
vy[i] = ; //情0
}
}
return ;
}
int anskm(int n, int m) {
memset(vx, , sizeof(vx));
memset(vy, , sizeof(vy));
memset(fx, , sizeof(fx));
memset(fy, , sizeof(fy));
memset(match, , sizeof(match));
//km算法的一部分,先初始化fx,fy
for (int i = ; i <= n; i++) { //遍历每一个驾驶员 row的值
fy[i] = ;
fx[i] = inf; //无穷小
for (int j = ; j <= m; j++) { //遍历每一个导航员 col的值
if (fx[i] > e[i][j]) { //默契值
fx[i] = e[i][j];
}
}
}
for (int i = ; i <= n; i++) { //遍历每一个驾驶员 row的值
memset(vx, , sizeof(vx));
memset(vy, , sizeof(vy));
while (!dfs(i, m)) {//如果他找不到搭配,就实现km算法
do_km(n, m);//km完后,还是会对这个想插入的节点进行dfs的,因为他还没搭配嘛
}
}
int ans = ;
for (int i = ; i <= m; i++) //遍历导航员,col的值
ans += e[match[i]][i];//输入的row是驾驶员,col是导航员
//match[i]:导航员i和驾驶员match[i]搭配了 match[col]=row;
return ans;
} void work() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= ; ++i) {
for (int j = ; j <= ; ++j) {
e[i][j] = ;
}
}
for (int i = ; i <= n; ++i) {
scanf("%s", str[i] + );
int a = , b = , c = ;
int len = m;
for (int j = ; str[i][j]; ++j) {
if (str[i][j] >= '' && str[i][j] <= '') {
a = min(a, j - );
} else if (str[i][j] == '#' || str[i][j] == '*' || str[i][j] == '&') {
b = min(b, j - );
} else {
c = min(c, j - );
}
}
for (int j = len; j >= ; j--) {
if (str[i][j] >= '' && str[i][j] <= '') {
a = min(a, len - j + );
} else if (str[i][j] == '#' || str[i][j] == '*' || str[i][j] == '&') {
b = min(b, len - j + );
} else {
c = min(c, len - j + );
}
}
e[i][] = a;
e[i][] = b;
e[i][] = c;
}
// for (int i = 1; i <= n; ++i) {
// for (int j = 1; j <= n; ++j) {
// cout << e[i][j] << " ";
// }
// cout << endl;
// }
cout << anskm(n, n) - (n - ) * << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

km

正解因该是一个dp

dp[i][2][2]][2]表示在前i个字符串中,选出了数字(0 or 1)、字母、特殊符号所需的最小步数。

转移

dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a][b][c]); //保留上次的最小值。

然后对于每一个a、b、c

dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a - 1][b][c] + e[i][1]);之类的。

一开始还担心会不会使得同一个字符串,既选了数字,也选了字母。

然后是不会的,因为需要选数字的时候,是从dp[!now][a - 1][b][c]转移过来的,也就是从上一唯的b、c转过来。所以不会有重复。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
char str[maxn][maxn];
int e[ + ][ + ];
int dp[][][][];
void work() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= ; ++i) {
for (int j = ; j <= ; ++j) {
e[i][j] = ;
}
}
for (int i = ; i <= n; ++i) {
scanf("%s", str[i] + );
int a = , b = , c = ;
int len = m;
for (int j = ; str[i][j]; ++j) {
if (str[i][j] >= '' && str[i][j] <= '') {
a = min(a, j - );
} else if (str[i][j] == '#' || str[i][j] == '*' || str[i][j] == '&') {
b = min(b, j - );
} else {
c = min(c, j - );
}
}
for (int j = len; j >= ; j--) {
if (str[i][j] >= '' && str[i][j] <= '') {
a = min(a, len - j + );
} else if (str[i][j] == '#' || str[i][j] == '*' || str[i][j] == '&') {
b = min(b, len - j + );
} else {
c = min(c, len - j + );
}
}
e[i][] = a;
e[i][] = b;
e[i][] = c;
}
memset(dp, 0x3f, sizeof dp);
int now = ;
dp[now][][][] = ;
for (int i = ; i <= n; ++i) {
now = !now;
for (int a = ; a <= ; ++a) {
for (int b = ; b <= ; ++b) {
for (int c = ; c <= ; ++c) {
dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a][b][c]);
if (a) {
dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a - ][b][c] + e[i][]);
// break;
}
if (b) {
dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a][b - ][c] + e[i][]);
// break;
}
if (c) {
dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a][b][c - ] + e[i][]);
// break;
}
}
}
}
}
cout << dp[now][][][] << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

C. Dasha and Password 预处理 + dp的更多相关文章

  1. Codeforces Round #394 (Div. 2) C. Dasha and Password(简单DP)

    C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力

    C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...

  3. Codeforces Round #394 (Div. 2) C. Dasha and Password

    C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  4. Codeforces 761C Dasha and Password(枚举+贪心)

    题目链接 Dasha and Password 题目保证一定有解. 考虑到最多只有两行的指针需要移动,那么直接预处理出该行移动到字母数字或特殊符号的最小花费. 然后O(N^3)枚举求最小值即可. 时间 ...

  5. Div.2 C. Dasha and Password

    C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举

    题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ...

  7. codeforces 761 C. Dasha and Password(多维dp)

    题目链接:http://codeforces.com/contest/761/problem/C 题意:给出n行的字符串每一列都从第一个元素开始可以左右移动每一行字符串都是首位相连的. 最后问最少移动 ...

  8. BZOJ-1587|前缀和 预处理 dp||叶子合并leaves

    叶子合并leaves Description 在一个美丽的秋天,丽丽每天都经过的花园小巷落满了树叶,她决定把树叶堆成K堆,小巷是笔直的 共有N片树叶(树叶排列也是笔直的),每片树叶都有一个重量值,并且 ...

  9. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem H. Password Service dp

    Problem H. Password Service 题目连接: http://www.codeforces.com/gym/100253 Description Startups are here ...

随机推荐

  1. AngularJS自己定义标签加入回调函数eval()

    function helloworld(name){ console.log("hello!!!!!"+name) } var name="zhangsan"; ...

  2. 第3周课后实践&#183;程序阅读(4)-利用引用訪问私有数据成员

    /* * Copyright (c) 2015, 烟台大学计算机学院 * All rights reserved. * 文件名:test.cpp * 作 者:刘畅 * 完毕日期:2015年 3 月 2 ...

  3. IOS开发之----常用的基本GDB命令【转】

    原文地址:http://blog.sina.com.cn/s/blog_71715bf801016d2y.html gdb不是万能的,可是没有gdb却是万万不能的.这里给大家简单介绍下iOS开发中最基 ...

  4. ExtJs里表格自动显隐滚动条

    ExtJs里面,layout:'border'这种布局应该很常用,但我用的时候,因为不熟,走了一些弯路.比如说,一个页面,大体布局是这样的: 上:查询输入框 中+下:查询结果(表格,底部有分页控件) ...

  5. iOS开发——高级篇——线程保活

    线程保活: 顾名思义,就是保护线程不死(保证线程处于激活状态,生命周期没有结束) 正常情况,当线程执行完一次任务之后,需要进行资源回收,也就意味着生命周期结束 应用场景: 当有一个任务,随时都有可能去 ...

  6. Koa2学习(二)async/await

    Koa2学习(二)async/await koa2中用到了大量的async/await语法,要学习koa2框架,首先要好好理解async/await语法. async/await顾名思义是一个异步等待 ...

  7. 如何写好react组件

    react 组件方面: 总结 React 组件的三种写法 及最佳实践 [涨经验] React组件编写思路(一) 使用react-router实现单页面应用时设置页面间过渡的两种方式 [翻译]基于 Cr ...

  8. Android vector Path Data画图详解

    SVG是一种矢量图格式,是Scalable Vector Graphics三个单词的首字母缩写.在xml文件中的标签是,画出的图形可以像一般的图片资源使用,例子如下: <vector xmlns ...

  9. CSYZDay2模拟题解

    T1.rotate [问题描述] ZYL有N张牌编号分别为1, 2,……,N.他把这N张牌打乱排成一排,然后他要做一次旋转使得旋转后固定点尽可能多.如果第i个位置的牌的编号为i,我们就称之为固定点.旋 ...

  10. 后缀数组 hash求LCP BZOJ 4310: 跳蚤

    后缀数组的题博客里没放进去过..所以挖了一题写写 充实下博客 顺便留作板子.. 一个字符串S中 内容不同的子串 有 sigma{n-sa[i]+1-h[i]}   (噢 这里的h[]就是大家熟知的he ...