Uva-1625-Color Length
来源:互联网 发布:cnabs数据库全称 编辑:程序博客网 时间:2024/04/30 21:07
这个题是白书上的一道题,转移方程式是dp[i][j]=min(dp[i+1][j],dp[i][j+1])+res[i][j](其中dp[i][j]表示第一个字符串移动了i个,第二个字符串移动了j个还需的最少费用,res[i][j]表示当前还有多少个颜色是已经出现但尚未结束状态),就是首先处理res[i][j]比较麻烦一点。
代码:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int inf=1<<29;const int maxn=5010;char stra[maxn],strb[maxn];int dp[maxn][maxn],res[maxn][maxn],first[27][2],last[27][2],l1,l2,tot;bool vis[27];void Init(){ memset(vis,0,sizeof(vis)); memset(last,-1,sizeof(last)); for(int i=0;i<27;i++) first[i][0]=first[i][1]=inf; tot=0; l1=strlen(stra); l2=strlen(strb); for(int i=0;i<l1;i++) { if(!vis[stra[i]-'A']) { vis[stra[i]-'A']=1; tot++; } if(first[stra[i]-'A'][0]==inf) first[stra[i]-'A'][0]=i; last[stra[i]-'A'][0]=i; } for(int i=0;i<l2;i++) { if(!vis[strb[i]-'A']) { vis[strb[i]-'A']=1; tot++; } if(first[strb[i]-'A'][1]==inf) first[strb[i]-'A'][1]=i; last[strb[i]-'A'][1]=i; } for(int i=0;i<=l1;i++) for(int j=0;j<=l2;j++) { int cnt=0; for(int k=0;k<26;k++) { if(first[k][0]==inf&&first[k][1]==inf) continue; if(first[k][0]>i-1&&first[k][1]>j-1) continue; if(last[k][0]<=i-1&&last[k][1]<=j-1) continue; cnt++; } res[i][j]=cnt; }}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%s%s",stra,strb); Init(); dp[l1][l2]=0; for(int i=l2-1;i>=0;i--) dp[l1][i]=dp[l1][i+1]+res[l1][i]; for(int i=l1-1;i>=0;i--) for(int j=l2;j>=0;j--) { if(j==l2) dp[i][j]=dp[i+1][j]+res[i][j]; else dp[i][j]=min(dp[i+1][j],dp[i][j+1])+res[i][j]; } printf("%d\n",dp[0][0]); } return 0;}
0 1
- 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
- BUPT Summer Journey #test1 E
- ACM-开灯问题
- 巧用excel创建层级参数化数据
- 程序的文件名
- StuanGou
- Uva-1625-Color Length
- 文章标题title优化技巧
- 如何在centos安装python-mysql?
- APUE第三章 文件I/O
- 灌水VS抽水
- 第十章 BIRT图表(续) .
- Android convertToBytes StringIndexOutOfBoundsException 调试小结
- jQuery扩展开发
- qemu及Guest OS环境搭建