CodeForces Gym 100989B LCS (B)
来源:互联网 发布:linux 如何退出vim 编辑:程序博客网 时间:2024/06/05 00:51
给一个算LCS时得到的DP矩阵,然后根据这个矩阵让你算出两个符合条件的字符串
因为字符串长度<=25,然后可以先假设A字符串就是abcdef....,然后根据A算B就行,看B中哪个跟A一样,但是A中可能有两个或以上的字符跟B的某个是一样的,然后这时就要把这两个变成同一个,但是这个涉及到很多,因为可能还跟B中别的字符一样,所以就 用并查集维护他们,把他们并到一个集合里,最后输出时按集合的根输出就行。
或者不用并查集,每次遇到这 个问题时,就把AB字符串都扫一遍,遇到要改的改掉就行,反正长度 25,随便搞
#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>#include <queue>#include <cstring>#include <vector>using namespace std;#define ll long longint R, C;char A[30], B[30];int DP[30][30];int set[30];int stk[30];int findset(int x){if (x == set[x])return x;elsereturn set[x] = findset(set[x]);}void unionset(int x, int y){int fx = findset(x);int fy = findset(y);if (fx == fy)return;elseset[fy] = fx;findset(y);}int main(){//freopen("input.txt", "r", stdin);//freopen("output.txt", "w", stdout);scanf("%d%d", &R, &C);for (int i = 0; i <= R; ++i){for (int j = 0; j <= C; ++j){scanf("%d", &DP[i][j]);}}for (int i = 1; i <= R; ++i){A[i] = 'a' + i - 1;set[i] = i;}int cnt = 0;for (int j = 1; j <= C; ++j){cnt = 0;for (int i = 1; i <= R; ++i){if (DP[i][j] > DP[i - 1][j] && DP[i][j] > DP[i][j - 1]){stk[cnt++] = i;}}for (int i = 1; i < cnt; ++i)unionset(stk[0], stk[i]);}for (int i = 1; i <= R; ++i){findset(i);}for (int i = 1; i <= R; ++i)A[i] = A[findset(i)];for (int i = 1; i <= C; ++i){if (B[i] == 0)B[i] = 'z';}for (int j = 1; j <= C; ++j){cnt = 0;for (int i = 1; i <= R; ++i){if (DP[i][j] > DP[i - 1][j] && DP[i][j] > DP[i][j - 1]){B[j] = A[set[i]];}}}printf("%s\n", A + 1);printf("%s\n", B + 1);//system("pause");//while (1);return 0;}
0 0
- CodeForces Gym 100989B LCS (B)
- CodeForces Gym 100114B
- CodeForces Gym 100735B
- Codeforces GYM 100726B
- codeforces GYM 101431B (STL)
- Codeforces Gym 100623B Problem B. Billboard
- CodeForces Gym 100989D 1D Cafeteria (B)
- CodeForces Gym 100989G Mission in Amman (B)
- CodeForces Gym 100989A LCS (A)
- Codeforces Gym 100889 B Backward and Forward
- Codeforces Gym 101061B RGB plants
- 【Codeforces Gym】 100162B Circle of Stones
- Codeforces Gym 100199 B Reactor Cooling
- Codeforces Gym 100650B Countdown DFS
- Codeforces GYM 100960B: ForceField 题解
- Codeforces GYM 100503B: Kakuro 题解
- Codeforces Gym 101174 B. Bribing Eve
- codeforces GYM 101431B (后缀数据结构)
- 团体程序设计天梯赛-练习集L1-006. 连续因子
- Linux netstat命令详解
- 团体程序设计天梯赛-练习集L1-007. 念数字
- 团体程序设计天梯赛-练习集L1-008. 求整数段和
- 团体程序设计天梯赛-练习集L1-009. *N个数求和
- CodeForces Gym 100989B LCS (B)
- 团体程序设计天梯赛-练习集L1-010. 比较大小
- MySQL 索引及查询优化
- 团体程序设计天梯赛-练习集L1-011. A-B
- ImageView设计成圆角的形状
- 团体程序设计天梯赛-练习集L1-012. 计算指数
- 团体程序设计天梯赛-练习集L1-013. 计算阶乘和
- Python最好用的模板引擎Jinja
- 团体程序设计天梯赛-练习集L1-014. 简单题