【动态规划】[UVa1625]Color Length
来源:互联网 发布:香港大学 知乎 编辑:程序博客网 时间:2024/04/27 20:43
分析一下发现如果每一次放进去一个新,它就会对所有覆盖这个点的所有颜色的L加1那么另
int add(int x, int y) { int cnt = 0; for(int i=0;i<26;i++){ if(pos[i][1] <= x && pos2[i][1] <= y) continue; if(pos[i][0] > x && pos2[i][0] > y) continue; if(pos[i][0] > x && pos2[i][0] == -1) continue; if(pos2[i][0] > y && pos[i][0] == -1) continue; cnt++; } return cnt;}
之前可以先预处理一下每一种颜色在每一段中的起始位置和结束位置
void Init(){ memset(pos, -1, sizeof pos); memset(pos2, -1, sizeof pos2); int len = strlen(s1+1); for(int i=1;i<=len;i++) pos[s1[i]-'A'][1] = i; for(int i=len;i>=1;i--) pos[s1[i]-'A'][0] = i; len = strlen(s2+1); for(int i=1;i<=len;i++) pos2[s2[i]-'A'][1] = i; for(int i=len;i>=1;i--) pos2[s2[i]-'A'][0] = i;}
然后很容易发现
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 100000;int pos[26][2], pos2[26][2];char s1[MAXN+10], s2[MAXN+10];void Init(){ memset(pos, -1, sizeof pos); memset(pos2, -1, sizeof pos2); int len = strlen(s1+1); for(int i=1;i<=len;i++) pos[s1[i]-'A'][1] = i; for(int i=len;i>=1;i--) pos[s1[i]-'A'][0] = i; len = strlen(s2+1); for(int i=1;i<=len;i++) pos2[s2[i]-'A'][1] = i; for(int i=len;i>=1;i--) pos2[s2[i]-'A'][0] = i;}int add(int x, int y) { int cnt = 0; for(int i=0;i<26;i++){ if(pos[i][1] <= x && pos2[i][1] <= y) continue; if(pos[i][0] > x && pos2[i][0] > y) continue; if(pos[i][0] > x && pos2[i][0] == -1) continue; if(pos2[i][0] > y && pos[i][0] == -1) continue; cnt++; } return cnt;}const int INF = 999999999;int dp[2][MAXN+5];int solve(){ memset(dp, 0, sizeof dp); int now = 0, len1 = strlen(s1+1), len2 = strlen(s2+1); for(int i=1;i<=len2;i++) dp[now][i] = INF; for(int i=0;i<=len1;i++){ now ^= 1; for(int j=0;j<=len2;j++) dp[now][j] = min(((j-1)>=0?dp[now][j-1]:INF), dp[now^1][j]) + add(i, j); } return dp[now][len2];}int main(){ int T; scanf("%d", &T); while(T--){ scanf("%s%s", s1+1, s2+1); Init(); printf("%d\n", solve()); }}
0 0
- 【动态规划】[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
- UVa1625 Color Length
- uva1625 Color Length
- UVa1625--Color Length DP
- UVa1625 Color Length
- uva1625 Color Length
- UVa1625 Color Length(DP)
- Uva1625 -Color Length(DP)
- 动态规划_线性结构例题_Color Length(UVA1625)
- UVA1625 / UVALive 5841 Color Length DP
- 【DP模型:LCS】uva1625 Color Length
- Android 雪花飘落效果
- js跨域及解决方案
- UITableView使用reloadData的几种动画方法
- Chrome禁用NPAPI插件(包括 Silverlight、Java 和 Unity)
- Android网络框架-Volley(三) CacheDispatcher和NetworkDispatcher源码分析
- 【动态规划】[UVa1625]Color Length
- 开源工作流BPM软件JFlow安装配置视频教程
- 3D缩水过滤工具 手机版
- 微信开发,对象转换为xml时候引用XStream这个类报错处理方案
- springMVC下的json数据的传递
- GRE写作必备句型
- maven和ant 的区别
- java PKCS7Padding 加密Cannot find any provider supporting AES/CBC/PKCS7Padding 解决办法
- Oracle 11g