uva1625 颜色的长度
2016-08-05 01:00
176 查看
另一个递推模板,遍历完上一层,然后下一层就可以利用i-1或者j-1.
两个队列的类似最长公共子序列问题常定义状态为,分别移动了多少个元素。
复杂代价预处理,然后状态转换时直接调用。
滚动数组可以利用多层来表示相邻层次关系,利用t^=1 来进行0和1的状态转换。
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51186
两个队列的类似最长公共子序列问题常定义状态为,分别移动了多少个元素。
复杂代价预处理,然后状态转换时直接调用。
滚动数组可以利用多层来表示相邻层次关系,利用t^=1 来进行0和1的状态转换。
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51186
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 5000 + 5; const int INF = 1000000000; char p[maxn], q[maxn]; // starts from position 1 int sp[26], sq[26], ep[26], eq[26]; // sp[i] start positions of character i in p int d[2][maxn], c[2][maxn]; // c[i][j]: how many "incomplete" colors in the mixed sequence int main() { int T; scanf("%d", &T); while(T--) { scanf("%s%s", p+1, q+1); int n = strlen(p+1); int m = strlen(q+1); for(int i = 1; i <= n; i++) p[i] -= 'A';//inti the elements to convenient to express for(int i = 1; i <= m; i++) q[i] -= 'A'; // calculate s and e for(int i = 0; i < 26; i++) { sp[i] = sq[i] = INF; ep[i] = eq[i] = 0; } for(int i = 1; i <= n; i++) { sp[p[i]] = min(sp[p[i]], i);//need init to INF ep[p[i]] = i; } for(int i = 1; i <= m; i++) { sq[q[i]] = min(sq[q[i]], i); eq[q[i]] = i; } // dp int t = 0; memset(c, 0, sizeof(c)); memset(d, 0, sizeof(d)); for(int i = 0; i <= n; i++){ for(int j = 0; j <= m; j++){ if(!i && !j) continue; // calculate d int v1 = INF, v2 = INF; //use scrollable array, t and t^1 represent the relationship of level if(i) v1 = d[t^1][j] + c[t^1][j]; // remove from p if(j) v2 = d[t][j - 1] + c[t][j - 1]; // remove from q d[t][j] = min(v1, v2); // calculate c if(i) { c[t][j] = c[t^1][j]; //if i is end,--,other wise ++ if(sp[p[i]] == i && sq[p[i]] > j) c[t][j]++; if(ep[p[i]] == i && eq[p[i]] <= j) c[t][j]--; } else if(j) { c[t][j] = c[t][j - 1]; if(sq[q[j]] == j && sp[q[j]] > i) c[t][j]++; if(eq[q[j]] == j && ep[q[j]] <= i) c[t][j]--; } } t ^= 1;//exchange the state } printf("%d\n", d[t^1][m]); } return 0; }
相关文章推荐
- UVa - 1625 Color Length 颜色的长度 动态规划 重庆一中高2018级竞赛班第十次测试 2016.9.16 Problem 2
- [UVa1625]颜色的长度
- UVA1625 颜色的长度(color length)(重庆一中高2018级信息学竞赛测验10) 解题报告
- UVA 1625 Color Length 颜色的长度 (预处理+dp)
- 颜色的长度(uva 1625)
- 动态规划(颜色的长度,uva 1625)
- UVa 1625 颜色的长度
- Uva 1625,颜色的长度
- UVA1625颜色的长度
- 【UVa1625】颜色的长度
- UITableView Cell 间隔线颜色长度
- Color Length - UVa 1625 dp
- CSS——NO.9(颜色值和长度值)
- Color Length UVA - 1625 (线性dp)
- uva 10615 二分图匹配 每个点上的不同边必须染上不同颜色 求最小染色数
- 找出第二个文本抄袭第一个文本的所有位置和长度 后缀数组 UVA 10526 - Intellectual Property
- UITextField限制输入长度,修改placeholder颜色和大小
- Uva1625 Color Length
- iOS 自定义UITextField中的光标颜色、长度或高度
- UVA1593 不定量不定长度单词,左对齐