UVa1625 Color Length
来源:互联网 发布:全球最大电子书软件 编辑:程序博客网 时间:2024/04/20 07:56
是不是和最长公共子序列很像?对,就是那样dp,不过需要预处理每个颜色开始的位置和结束的位置,才能知道放入这个颜色后,会使结果增加多少。这题开始我一直TLE。。原因是我用memset()初始化5000*5000的数组,才知道那个函数在数据大的时候耗时是多么可怕!
#include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> #include <iomanip> #include <cstdlib> #include <string> #include <memory.h> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <ctype.h> #define INF 10000 #define ll long long #define max3(a,b,c) max(a,max(b,c)) #define MAXN 100010 using namespace std;char str1[5010];char str2[5010];int first[26][2];int last[26][2];int dp[5010][5010];int tab[5010][5010];int main(){ int t; cin>>t; while(t--){ scanf("%s%s",str1+1,str2+1); int len1=strlen(str1+1); int len2=strlen(str2+1); memset(first,-1,sizeof(first)); memset(last,-1,sizeof(last)); for(int i=1;i<=len1;i++){ if(first[str1[i]-65][1]==-1 ) first[str1[i]-65][1]=i; } for(int i=1;i<=len2;i++){ if(first[str2[i]-65][0]==-1 ) first[str2[i]-65][0]=i; } // for(int i=len1;i>=1;i--){ if(last[str1[i]-65][1]==-1 ) last[str1[i]-65][1]=i; } for(int i=len2;i>=1;i--){ if(last[str2[i]-65][0]==-1 ) last[str2[i]-65][0]=i; } // for(int i=0;i<=len1;i++){for(int j=0;j<=len2;j++){int re=0;for(int k=0;k<26;k++){ if( ( (i>=first[k][1]&&first[k][1]!=-1) || (j>=first[k][0]&&first[k][0]!=-1 )) && ( (i<last[k][1] &&last[k][1]!=-1)|| (j<last[k][0]&&last[k][0]!=-1) ) ){ re++; } } tab[i][j]=re;}} dp[0][0]=0; for(int i=1;i<=len1;i++){ dp[i][0]=dp[i-1][0]+tab[i][0]; } for(int i=1;i<=len2;i++){ dp[0][i]=dp[0][i-1]+tab[0][i]; } for(int i=1;i<=len1;i++){ for(int j=1;j<=len2;j++){ dp[i][j]=min( dp[i-1][j] , dp[i][j-1] )+tab[i][j]; } } cout<<dp[len1][len2]<<endl; } return 0;}
0 0
- UVA1625 - Color Length (DP)
- UVa1625 Color Length
- UVa1625 - Color Length
- uva1625 color length
- uva1625 Color Length
- UVa1625 Color Length
- uva1625 Color Length
- UVa1625--Color Length DP
- UVa1625 Color Length
- uva1625 Color Length
- 【动态规划】[UVa1625]Color Length
- UVa1625 Color Length(DP)
- Uva1625 -Color Length(DP)
- UVA1625 / UVALive 5841 Color Length DP
- uva1625 Color Length 线性动态规划
- 【DP模型:LCS】uva1625 Color Length
- UVA1625 Color Length(附 刘汝佳代码)
- UVa1625 Color length[DP][差分]
- 无 99 美金证书联机开发
- VPN简单汇总总结
- ocr文字识别软件ABBYY FineReader介绍
- GRE OVER IPSEC VPN
- 基于SDL的图像显示流程
- UVa1625 Color Length
- Android 设计模式之适配器模式
- 路由器SVTI 站点到站点VPN
- poj-3264
- JAVA学习笔记
- MySQL存储过程学习(http://zhanshenlvbu.iteye.com/blog/1162203)
- 基于ffmpeg的音视频解码原理
- 并发编程--线程的中断
- jquery 处理页面弹出层查询数据等待的操作(gif图片加载等待)