zoukankan      html  css  js  c++  java
  • HDU 3341 Lost's revenge

    Lost's revenge

    Time Limit: 5000ms
    Memory Limit: 65535KB
    This problem will be judged on HDU. Original ID: 3341
    64-bit integer IO format: %I64d      Java class name: Main
    Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is the man called "nuclear weapon of FZU,descendant of Jingrun", because of his talent in the field of number theory. So Lost had never won the game. He was so ashamed and angry, but he didn't know how to improve his level of number theory.

    One noon, when Lost was lying on the bed, the Spring Brother poster on the wall(Lost is a believer of Spring Brother) said hello to him! Spring Brother said, "I'm Spring Brother, and I saw AekdyCoin shames you again and again. I can't bear my believers were being bullied. Now, I give you a chance to rearrange your gene sequences to defeat AekdyCoin!".

    It's soooo crazy and unbelievable to rearrange the gene sequences, but Lost has no choice. He knows some genes called "number theory gene" will affect one "level of number theory". And two of the same kind of gene in different position in the gene sequences will affect two "level of number theory", even though they overlap each other. There is nothing but revenge in his mind. So he needs you help to calculate the most "level of number theory" after rearrangement.
     

    Input

    There are less than 30 testcases.
    For each testcase, first line is number of "number theory gene" N(1<=N<=50). N=0 denotes the end of the input file.
    Next N lines means the "number theory gene", and the length of every "number theory gene" is no more than 10.
    The last line is Lost's gene sequences, its length is also less or equal 40.
    All genes and gene sequences are only contains capital letter ACGT.
     

    Output

    For each testcase, output the case number(start with 1) and the most "level of number theory" with format like the sample output.
     

    Sample Input

    3
    AC
    CG
    GT
    CGAT
    1
    AA
    AAA
    0

    Sample Output

    Case 1: 3
    Case 2: 2

    Source

     
    解题:Trie 图 + 变进制hash + 状压dp
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 510;
     4 struct Trie {
     5     int ch[maxn][4],fail[maxn],cnt[maxn],tot;
     6     int id[256];
     7     void init() {
     8         tot = 0;
     9         newnode();
    10         for(int i = 0; i < 4; ++i) id["ACGT"[i]] = i;
    11     }
    12     int newnode() {
    13         memset(ch[tot],0,sizeof ch[tot]);
    14         fail[tot] = cnt[tot] = 0;
    15         return tot++;
    16     }
    17     void insert(char *str,int rt = 0) {
    18         for(int i = 0; str[i]; ++i) {
    19             int &x = ch[rt][id[str[i]]];
    20             if(!x) x = newnode();
    21             rt = x;
    22         }
    23         cnt[rt]++;
    24     }
    25     void build(int rt = 0) {
    26         queue<int>q;
    27         for(int i = 0; i < 4; ++i)
    28             if(ch[rt][i]) q.push(ch[rt][i]);
    29         while(!q.empty()) {
    30             rt = q.front();
    31             q.pop();
    32             cnt[rt] += cnt[fail[rt]];
    33             for(int i = 0; i < 4; ++i) {
    34                 int &x = ch[rt][i],y = ch[fail[rt]][i];
    35                 if(x) {
    36                     fail[x] = y;
    37                     q.push(x);
    38                 } else x = y;
    39             }
    40         }
    41     }
    42     int dp[maxn][11*11*11*11 + 5];
    43     int solve(char *str) {
    44         int bt[4] = {0,0,0,1},num[4] = {};
    45         for(int i = 0; str[i]; ++i) ++num[id[str[i]]];
    46         for(int i = 2; i >= 0; --i)
    47             bt[i] = bt[i + 1]*(num[i] + 1);
    48         memset(dp,-1,sizeof dp);
    49         int ans = dp[0][0] = 0;
    50         for(int a = 0; a <= num[0]; ++a)
    51             for(int b = 0; b <= num[1]; ++b)
    52                 for(int c = 0; c <= num[2]; ++c)
    53                     for(int d = 0; d <= num[3]; ++d) {
    54                         int hs = a*bt[0] + b*bt[1] + c*bt[2] + d;
    55                         for(int i = 0; i < tot; ++i) {
    56                             if(dp[i][hs] == -1) continue;
    57                             for(int k = 0; k < 4; ++k) {
    58                                 if(a == num[0] && k == 0) continue;
    59                                 if(b == num[1] && k == 1) continue;
    60                                 if(c == num[2] && k == 2) continue;
    61                                 if(d == num[3] && k == 3) continue;
    62                                 int x = dp[ch[i][k]][hs + bt[k]];
    63                                 int y = dp[i][hs] + cnt[ch[i][k]];
    64                                 dp[ch[i][k]][hs + bt[k]] = max(x,y);
    65                             }
    66                         }
    67                     }
    68         int st = num[0]*bt[0] + num[1]*bt[1] + num[2]*bt[2] + num[3]*bt[3];
    69         for(int i = 0; i < tot; ++i)
    70             ans = max(ans,dp[i][st]);
    71         return ans;
    72     }
    73 } ac;
    74 char str[maxn];
    75 int main() {
    76     int n,cs = 1;
    77     while(scanf("%d",&n),n){
    78         ac.init();
    79         while(n--){
    80             scanf("%s",str);
    81             ac.insert(str);
    82         }
    83         scanf("%s",str);
    84         ac.build();
    85         printf("Case %d: %d
    ",cs++,ac.solve(str));
    86     }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    vue,如何每次访问同样路由都可以刷新
    CentOS 7.X 关闭SELinux
    Centos7安装Redis
    ubuntu 18.04 安装 MySql,并配置远程连接
    CentOS7设置阿里镜像源
    ubuntu 18.04 设置静态ip方法
    发现网络产品漏洞后,应立即通知上游开发者,并及时通知下游用户
    ApacheCN 数据库译文集 20211112 更新
    ApacheCN Linux 译文集(二) 20211206 更新
    ApacheCN C# 译文集 20211124 更新
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4943336.html
Copyright © 2011-2022 走看看