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最大的,就是最终答案。

  • 相关阅读:
    mybatis 乐观锁和逻辑删除
    JAVA实现DES加密实现详解
    axios 全攻略之基本介绍与使用(GET 与 POST)
    Ajax json 数据格式
    CentOS 7安装Hadoop 3.0.0
    <p>1、查询端口号占用,根据端口查看进程信息</p>
    CentOS查询端口占用和清除端口占用的程序
    Spring Boot Maven 打包可执行Jar文件!
    linux下运行jar
    maven 工程mybatis自动生成实体类
  • 原文地址:https://www.cnblogs.com/shanyr/p/5676727.html
Copyright © 2011-2022 走看看