HDU 4681 String
来源:互联网 发布:小米手环抢购软件 编辑:程序博客网 时间:2024/06/07 16:18
http://acm.hdu.edu.cn/showproblem.php?pid=4681
蛮好……
一开始有这样的想法:
其实
具体说来就是:
lcs_lft[i][j]=lcs(A[1..i],B[1..j])lcs_rit[i][j]=lcs(A[i..n],B[j..m]) A 的每个位置i 有一个j=nexta[i] 的位置,表示A[i..j] 有一个C 做子序列并且j 尽量小;同理有nextb[] 。result=max(lcs_lft[i−1][j−1]+lcs_rit[nexta[i]+1][nextb[j]+1])+strlen(C)
#include <bits/stdc++.h>const int N = 1024;char a[N];char b[N];char c[N];int lcs_lft[N][N];int lcs_rit[N][N];int nexta[N];int nextb[N];void lcs_table(int n, int m){ memset(lcs_lft, 0, sizeof(lcs_lft)); memset(lcs_rit, 0, sizeof(lcs_rit)); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { lcs_lft[i][j] = std::max(lcs_lft[i - 1][j], lcs_lft[i][j - 1]); if (a[i] == b[j]) lcs_lft[i][j] = std::max(lcs_lft[i - 1][j - 1] + 1, lcs_lft[i][j]); } } for (int i = n; i >= 1; --i) { for (int j = m; j >= 1; --j) { lcs_rit[i][j] = std::max(lcs_rit[i + 1][j], lcs_rit[i][j + 1]); if (a[i] == b[j]) lcs_rit[i][j] = std::max(lcs_rit[i + 1][j + 1] + 1, lcs_rit[i][j]); } }}void next_table(int n, int m){ for (int i = 1; i <= n; i++) { int len = 0; nexta[i] = -1; for (int j = i; j <= n; j++) { if (a[j] == c[len + 1]) { len++; if (c[len + 1] == '\0') { nexta[i] = j; break; } } } } for (int i = 1; i <= m; i++) { int len = 0; nextb[i] = -1; for (int j = i; j <= m; j++) { if (b[j] == c[len + 1]) { len++; if (c[len + 1] == '\0') { nextb[i] = j; break; } } } }}int max_length(int n, int m){ int res = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int ii = nexta[i]; int jj = nextb[j]; if (ii < 0 || jj < 0) continue; int tmp = lcs_lft[i - 1][j - 1] + lcs_rit[ii + 1][jj + 1]; res = std::max(res, tmp); } } res += strlen(c + 1); return res;}int main(){ int casc; scanf("%d", &casc); for (int casi = 1; casi <= casc; casi++) { scanf("%s %s %s", a + 1, b + 1, c + 1); int n = strlen(a + 1); int m = strlen(b + 1); lcs_table(n, m); next_table(n, m); printf("Case #%d: %d\n", casi, max_length(n, m)); } return 0;}
0 0
- HDU 4681 String
- HDU 4681 String
- hdu 4681 String
- hdu-4681-String
- hdu 4681 string
- hdu 4681 string
- HDU - 4681 String
- hdu 4681 String (LCS)
- HDU 4681 String(DP)
- HDU 4681 String
- HDU 4681 String dp
- string string string HDU
- hdu 4681 String(dp)
- HDU 4681 String (动态规划)
- HDU 4681 String 解题报告
- 简单dp-hdu-4681-String
- 【HDU】4681 String(DP)
- String HDU
- BZOJ 3238 [Ahoi2013]差异 后缀数组+单调栈
- mac 复制到剪贴板
- sql server 2008Windows防火墙
- UVA 10817 Headmaster's Headache(状压DP)
- 原码,反码,补码 详解
- HDU 4681 String
- CXF自定义拦截器的使用
- 2015.9.4第三场组队赛
- 欢迎使用CSDN-markdown编辑器
- Linux下编程-----文件与IO(二)stat函数
- Codeforces Gym 100339I Sum vs Product 剪枝搜索
- php pdo学习笔记
- Device eth0 does not seem to be present,delaying initialization.
- MFC中获得各个类的指针/句柄 ID的总结