HDU 2144 (最长连续公共子列 + 并查集) Evolution

我发现我一直理解错题意了,这里的子序列指的是连续子序列,怪不得我写的LCS一直WA

顺便复习一下并查集

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 const int maxn = 111;
 9 int dp[maxn][maxn], parent[maxn], len[maxn];
10 char DNA[maxn][maxn];
11
12 int LCS(int a, int b)
13 {
14     int ans = 0;
15     memset(dp,0,sizeof(dp));
16     for(int i = 1; i <= len[a]; ++i)
17         for(int j = 1; j <= len[b]; ++j)
18         {
19             if(DNA[a][i-1] == DNA[b][j-1])
20                 dp[i][j] = dp[i-1][j-1] + 1;
21             if(dp[i][j] > ans)
22                 ans = dp[i][j];
23         }
24     return ans;
25 }
26
27 int GetParent(int a)
28 {
29     return parent[a] == a ? a : parent[a] = GetParent(parent[a]);
30 }
31
32 int main(void)
33 {
34     #ifdef LOCAL
35         freopen("2144in.txt", "r", stdin);
36     #endif
37
38     int n, kase = 0;
39     double p;
40     while(scanf("%d %lf", &n, &p) == 2)
41     {
42         for(int i = 0; i < n; ++i)
43         {
44             scanf("%s", DNA[i]);
45             parent[i] = i;
46             len[i] = strlen(DNA[i]);
47         }
48         for(int i = 0; i < n; ++i)
49             for(int j = 0; j < i; ++j)
50             {
51                 int pi = GetParent(i);
52                 int pj = GetParent(j);
53                 if(pi == pj)    continue;
54                 double x = LCS(i, j) * 100.0;
55                 if(x/len[i]>p && x/len[j]>p)
56                     parent[pi] = pj;
57             }
58         int ans = 0;
59         for(int i = 0; i < n; ++i)
60             if(parent[i] == i)
61                 ++ans;
62         printf("Case %d:\n%d\n", ++kase, ans);
63     }
64     return 0;
65 }

代码君

时间: 2024-08-26 17:54:19

HDU 2144 (最长连续公共子列 + 并查集) Evolution的相关文章

最长连续公共子串、最长公共子串(可以非连续)、最长回文串(连续)、最长回文串(可以不连续)、最长递增数组的求解

问题:最长连续公共子串.最长公共子串(可以非连续).最长回文串(连续).最长回文串(可以不连续).最长递增数组.长方形镶嵌最多的求解 方法:上述问题有相似性,都可以采用动态规划进行求解. (1)最长连续公共子串: 如果A[i]==B[j], dp[i][j]=dp[i-1][j-1]+1; 否则,dp[i][j]=0; (2)最长公共子串(可非连续): 如果A[i]==B[j], dp[i][j]=dp[i-1][j-1]+1; 否则,dp[i][j]=dp[i-1][j-1]; (3)最长回文

hdu 1829-A Bug&#39;s LIfe(简单带权并查集)

题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有,按题目要求输出.这题有点坑的地方在于输出上多了一行空行,不PE都没注意到. 思路: 用一个数组gender[i] 记录当前节点 i 与根节点的关系,parent[i]数组记录当前节点的父节点. 因为是带权并查集,在Find_Parent 时更新当前节点与根节点的关系,且路径压缩至根节点下, 所以不用

hdu 1829 A Bug&#39;s Life(分组并查集(偏移量))

A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9204    Accepted Submission(s): 2961 Problem Description Background Professor Hopper is researching the sexual behavior of a rare sp

HDU 3038 How Many Answers Are Wrong(种类并查集)

题目链接 食物链类似的题,主要是在于转化,a-b的和为s,转换为b比a-1大s.然后并查集存 此节点到根的差. 假如x的根为a,y的根为b: b - y = rank[y] a - x = rank[x] y - x = s 可以推出b - a = rank[y] - rank[x] + s; 并查集 延迟更新什么的,都忘了啊. 还有这题,如果是x--的话,记得更新0的根. #include <cstring> #include <cstdio> #include <stri

hdu 1811 Rank of Tetris (拓扑排序+并查集)

Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4931 Accepted Submission(s): 1359 Problem Description自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他

HDU 1829 A Bug&#39;s Life (种类并查集)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1829 A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 19429    Accepted Submission(s): 6206 Problem Description Background Professor H

两个字符串的最长连续公共子串

LCS(Longest Common Subsequence) 就是求两个字符串最长公共子串的问题.引入: LCS(Longest Common Subsequence) 就是求两个字符串最长公共子串的问题. 比如: String str1 = new String("adbccadebbca");  String str2 = new String("edabccadece");str1与str2的公共子串就是bccade. 解法就是用一个矩阵来记录两个字符串中所

HDU 1423 最长上升公共子序列(LCIS)

题目大意: 给定两个数字数组a[] , b[],在这两个数组中找一个最长的公共上升子序列,输出最长的长度 #include <cstdio> #include <cstring> using namespace std; const int N = 1005; #define max(a,b) a>b?a:b int dp[N] , a[N] , b[N]; /*可以看作是每次在第一个数据中提取一个数字,然后在第二个数组中 根据相同的数字来查找最长上升子序列,f[i][j],

HDU 2586 How far away? Tarjan算法 并查集 LCA

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 23506    Accepted Submission(s): 9329 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every da