Color Length UVA
来源:互联网 发布:淘宝正义哥奶粉高仿 编辑:程序博客网 时间:2024/06/03 08:49
记录在两个序列当中每个字母的起始位置和结束位置,然后按照紫书思路进行递推,注意一个技巧,将一个空字符分别放到两个字符串的开始,并且二维数组的[0][0]位置的值记为0,具体实现见如下代码:
#include<iostream>#include<vector>#include<string>#include<set>#include<stack>#include<queue>#include<map>#include<algorithm>#include<cmath>#include<iomanip>#include<cstring>#include<sstream>#include<cstdio>#include<deque>using namespace std;int T;string s1, s2;int start1[30], start2[30];int end1[30], end2[30];int n, m;int Distance(int index1,int index2){int amount = 0;for (char var = 'A'; var <= 'Z'; var++){int ind = var - 'A';if ((start1[ind] && start1[ind] <= index1 || start2[ind] && start2[ind] <= index2)&&(end1[ind]>index1||end2[ind]>index2)) amount++;}return amount;}int main(){cin >> T;while (T--){cin >> s1 >> s2;memset(start1, 0, sizeof(start1));memset(start2, 0, sizeof(start2));memset(end1, 0, sizeof(end1));memset(end2, 0, sizeof(end2));n = s1.size(); m = s2.size();s1 = " " + s1;s2 = " " + s2;for (int i = 1; i <= n; i++){int index1 = s1[i] - 'A';if (start1[index1] == 0) start1[index1] = i;end1[index1] = i;}for (int i = 1; i <= m; i++){int index2 = s2[i] - 'A';if (start2[index2] == 0) start2[index2] = i;end2[index2] = i;}int arr[500][500];arr[0][0] = 0;for (int i = 0; i <= n; i++){for (int j = 0; j <= m; j++){if(i||j) arr[i][j] = 1 << 20;if (i) arr[i][j] = min(arr[i][j], arr[i - 1][j] + Distance(i - 1, j));if (j) arr[i][j] = min(arr[i][j], arr[i][j - 1] + Distance(i, j - 1));}}cout << arr[n][m] << endl;}return 0;}
阅读全文
0 0
- 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
- Color Length UVA
- Color Length UVA
- Color Length UVA
- Color Length - UVa 1625 dp
- UVa OJ 1625 - Color Length
- uva 1625 Color Length (dp)
- UVa #1625 Color Length (例题9-8)
- 每日一题--二叉树的深度
- 论文笔记——CVPR 2017 Dilated Residual Networks
- hihocoder 1033 交错和 (数位dp)
- JavaScript中的Dom
- 如何在Ubuntuh环境下配置Android NDK
- Color Length UVA
- qt 实现画树功能
- IDEA使用maven的进行mybatis的逆向工程
- MySQL数据库引擎详解
- JAVA设计模式总结
- CSS3动画(360度旋转、旋转放大、放大、移动)
- unity学习——协程的停止StopCoroutine
- 八皇后问题的解和动态演示过程
- “一致性”架构设计