uva 1625 color length
来源:互联网 发布:java基础知识txt下载 编辑:程序博客网 时间:2024/04/30 14:37
d[i][j] 表示拿了前i个和前j个后,还所需的“最少费用“。
d[m][n] = 0;
d[i][j] = res[i][j] + min(d[ i+1 ][ j ], d[ i ][ j+1 ] );
计算res可以用一维的,因为每次都可以由前一个推算。
代码
#include <stdio.h>int min(int i, int h) {return i<h?i:h;}int max(int i, int h) {return i>h?i:h;} const int TNT = 2000000000, Alpha = 26;char str[5050], strl[5050];int m, n, res[5050], d[5050][5050];void G(char *s,int & a) {a = 0;char ch;do ch = getchar(); while(ch < 'A' || ch > 'Z');do s[++a] = ch-'A', ch = getchar(); while(ch >= 'A' && ch <= 'Z');s[a+1] = '\0';ungetc(ch,stdin);}int fead[26], fast[26], sead[26], sast[26];void commando() {for(int i = 0; i < Alpha; i++) {fead[i] = sead[i] = TNT; fast[i] = sast[i] = -1;}for(int i = 1; i <= m; i++ ) {if(fead[str[i]] == TNT) fead[str[i]] = i;fast[str[i]] = i;}for(int i = 1; i <= n; i++) {if(sead[strl[i]] == TNT) sead[strl[i]] = i;sast[strl[i]] = i;}}int main(){int _case ; scanf("%d",&_case);while(_case--) {G(str, m); G(strl, n);commando();d[m][n] = 0; res[n] = 0;for(int i = m; i >= 0; i--) {for(int j = n; j >= 0; j--) {if( j < n) {char ch = strl[j+1];if(fead[ch]<=i || sead[ch]<=j){if(fast[ch] <= i && sast[ch] == j+1) res[j] = res[j+1]+1;else res[j] = res[j+1];}else {if(fast[ch] > 0 || sast[ch] > j+1)res[j] = res[j+1]-1;else res[j] = res[j+1];}d[i][j] = d[i][j+1] + res[j];if( i < m) d[i][j] = min( d[i][j], d[i+1][j] + res[j]);}}//for(int t = 0; t <= n; t++) printf("%d ",res[t]); putchar('\n');if(i > 0) {char c = str[i];if(fead[c] < i || sead[c] <= n){if(fast[c] == i) res[n]++;}else if(fast[c] > i) res[n]--;d[i-1][n] = d[i][n] + res[n];}}printf("%d\n",d[0][0]);}return 0;}
1 0
- Uva-1625-Color Length
- UVA 1625 Color Length
- UVA-1625 Color Length
- uva 1625 color length
- UVA 1625 Color Length
- UVA 1625 Color Length
- UVA 1625 Color Length
- UVa 1625 Color Length
- UVA 1625 Color Length
- UVA 1625 Color Length
- Color Length - UVa 1625 dp
- UVa OJ 1625 - Color Length
- uva 1625 Color Length (dp)
- UVa #1625 Color Length (例题9-8)
- UVA 1625 Color Length (DP)
- UVa 1625:Color Length(DP)
- uva 1625——Color Length
- Color Length UVA
- 【Android开发小记--15】录制视频
- find()和filter()函数的用法区别
- 大数据 IMF传奇行动 如何 搭建 8台设备的 hadoop分布式集群
- 华为手机 logcat不输入错误日志
- [1651] Red packet【二分】
- uva 1625 color length
- HDU 1007 (分治递归)
- NYOJ 题目58 最小步数
- Linux命令行学习之路(七)——定时执行
- POJ 2112 Optimal Milking(二分+floyd+最大流)
- 【Android开发小记--16】数据存储1--assets、raw、内部存储、外部存储——文件的读写
- hdu 5624 KK's Reconstruction
- 适配器模式(C++)
- HTML5开发移动web应用——Sencha Touch篇(10)