zoukankan      html  css  js  c++  java
  • LightOJ DNA Prefix(字典树+dfs)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/F

    F - DNA Prefix
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
    Submit Status

    Description

    Given a set of n DNA samples, where each sample is a string containing characters from {A, C, G, T}, we are trying to find a subset of samples in the set, where the length of the longest common prefix multiplied by the number of samples in that subset is maximum.

    To be specific, let the samples be:

    ACGT

    ACGTGCGT

    ACCGTGC

    ACGCCGT

    If we take the subset {ACGT} then the result is 4 (4 * 1), if we take {ACGT, ACGTGCGT, ACGCCGT} then the result is 3 * 3 = 9 (since ACG is the common prefix), if we take {ACGT, ACGTGCGT, ACCGTGC, ACGCCGT} then the result is 2 * 4 = 8.

    Now your task is to report the maximum result we can get from the samples.

    Input

    Input starts with an integer T (≤ 10), denoting the number of test cases.

    Each case starts with a line containing an integer n (1 ≤ n ≤ 50000) denoting the number of DNA samples. Each of the next n lines contains a non empty string whose length is not greater than 50. And the strings contain characters from {A, C, G, T}.

    Output

    For each case, print the case number and the maximum result that can be obtained.

    Sample Input

    3

    4

    ACGT

    ACGTGCGT

    ACCGTGC

    ACGCCGT

    3

    CGCGCGCGCGCGCCCCGCCCGCGC

    CGCGCGCGCGCGCCCCGCCCGCAC

    CGCGCGCGCGCGCCCCGCCCGCTC

    2

    CGCGCCGCGCGCGCGCGCGC

    GGCGCCGCGCGCGCGCGCTC

    Sample Output

    Case 1: 9

    Case 2: 66

    Case 3: 20

    先给代码吧:

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 char Str[55];
     6 typedef struct Trie{
     7     int val;
     8     Trie *Next[4];
     9 }Trie;
    10 Trie root;
    11 
    12 void creatTrie()
    13 {
    14     int len = strlen(Str);
    15     Trie *p = &root;
    16     Trie *q;
    17     int id;
    18     for(int i = 0; i < len; i++){
    19         if(Str[i]=='A') id = 0;
    20         else if(Str[i]=='C') id = 1;
    21         else if(Str[i]=='G') id = 2;
    22         else if(Str[i]=='T') id = 3;
    23         if(p->Next[id]==NULL) {
    24             q = new Trie();
    25             q->Next[0] = q->Next[1] = q->Next[2] = q->Next[3] = NULL;
    26             q->val=1;
    27             p->Next[id] = q;
    28             p = q;
    29         }
    30         else {
    31             q = p->Next[id];
    32             q->val++;
    33             p = q;
    34         }
    35     }
    36     return;
    37 }
    38 int ans;
    39 void sol(Trie *p,int dep)
    40 {
    41     int tm = 0;
    42     Trie *q;
    43     for(int i = 0; i < 4; i++)
    44     {
    45         if(p->Next[i]!=NULL){
    46             q = p->Next[i];
    47             tm = (q->val)*(dep+1);
    48             ans = max(tm,ans);
    49             sol(q,dep+1);
    50         }
    51     }
    52 }
    53 void clearTree(Trie *t)
    54 {
    55     if(t==NULL) return;
    56     for(int i = 0; i < 4; i++) {
    57         if(t->Next[i] != NULL) {
    58             clearTree(t->Next[i]);
    59         }
    60     }
    61     delete t;
    62 }
    63 int main()
    64 {
    65     int T;
    66     int cnt = 1;
    67     scanf("%d",&T);
    68     while(T--)
    69     {
    70         root.Next[0] = root.Next[1] = root.Next[2] = root.Next[3] = NULL;
    71         ans = 0;
    72         int n;
    73         scanf("%d", &n);
    74         for(int i = 0; i < n; i++)
    75         {
    76             scanf("%s",Str);
    77             creatTrie();
    78         }
    79         sol(&root,0);
    80         printf("Case %d: %d
    ",cnt++,ans);
    81         for(int i = 0; i < 4; i++)clearTree(root.Next[i]);
    82     }
    83     return 0;
    84 }

    题意:

    有n和DNA序列,求出他们中公共前缀长度和有相同公共前缀DNA序列乘积的最大值。

    解析:

    这题用trie来搞,用trie存储所有的DNA序列,然后每个节点有个val值,还有一个length表示的是存储以该节点结尾的DNA序列的数目,最后求出length*val最大的,就是最终答案。

  • 相关阅读:
    2020 Java开发者数据分析:中国已成为 Java 第一大国
    居然仅用浏览器,就完成了Spring Boot应用的开发与部署!
    Serverless 初体验:快速开发与部署一个Hello World(Java版)
    聊聊算法——回文字符串
    Redis Lua脚本完全入门
    Mock测试你的Spring MVC接口
    HTTPS证书知识扫盲
    Java中类型判断的几种方式
    山寨一个Spring的@Component注解
    如何自动填充SQL语句中的公共字段
  • 原文地址:https://www.cnblogs.com/shanyr/p/5676727.html
Copyright © 2011-2022 走看看