Color Length UVA
来源:互联网 发布:数控车床螺纹编程 编辑:程序博客网 时间:2024/06/06 04:58
紫书上的题目,讲解的很清楚,附代,以后要仔细思考.
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>using namespace std;int c[5010][5010];int dp[5010][5010];char s1[5010];char s2[5010];int p[5010];int q[5010];int py[5010];int qy[5010];int px[5010];int qx[5010];int main(){int T;scanf("%d", &T);while(T--){ scanf("%s %s",s1+1,s2+1); int len1 = strlen(s1+1); int len2 = strlen(s2+1);for(int i=1; i<=len1; i++) s1[i] -= 'A';for(int i=1; i<=len2; i++) s2[i] -= 'A'; for(int i=0; i<26; i++) { px[i] = qx[i] = 99999; py[i] = qy[i] = 0; }for(int i=1; i<=len1; i++){ px[s1[i]] = min(px[s1[i]],i); py[s1[i]] = i;}for(int i=1; i<=len2; i++){ qx[s2[i]] = min(qx[s2[i]],i); qy[s2[i]] = i;}//memset(c, 0, sizeof(c));c[0][0] = 0;dp[0][0] = 0;for(int i=0; i<=len1; i++){ for(int j=0; j<=len2; j++) { int v1 = 99999; int v2 = 99999; if(i==0 && j==0)continue; if(i) v1 = dp[i-1][j] + c[i-1][j]; if(j) v2 = dp[i][j-1] + c[i][j-1]; dp[i][j] = min(v1,v2); if(i) { c[i][j] = c[i-1][j]; if(i==px[s1[i]] && j<qx[s1[i]])c[i][j]++; if(i==py[s1[i]] && j>=qy[s1[i]])c[i][j]--; } else if(j) {c[i][j] = c[i][j-1];if(j==qx[s2[j]] && i < px[s2[j]])c[i][j]++;if(j==qy[s2[j]] && i >=py[s2[j]])c[i][j]--; }}}printf("%d\n", dp[len1][len2]);}return 0;}
水波.
阅读全文
0 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
- Color Length UVA
- Color Length UVA
- Color Length UVA
- Color Length - UVa 1625 dp
- UVa OJ 1625 - Color Length
- uva 1625 Color Length (dp)
- UVa #1625 Color Length (例题9-8)
- 初学HTML(二)
- HDU1560 DNA sequence(IDA星) (E)
- Qoo10开店,为什么你不应该担心推广问题,而应该担心销售问题
- Vue实例详解
- Shiro笔记(二)----shiro源码与默认提供的示例
- Color Length UVA
- Universal TUN and TAP device driver
- Apache系列—httpd.conf文件指令详解
- 嵌入式Linux--基于物联网实验环境的温湿度实时显示系统
- 在eclipse中安装groovy插件详细步骤
- glfw在vs2013中的配置记录
- java工厂类理解
- verilog时钟频率对应关系
- Fragment 基类的常规抽取