zoukankan      html  css  js  c++  java
  • HDU 2296 Ring

    Ring

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 2296
    64-bit integer IO format: %I64d      Java class name: Main
    For the hope of a forever love, Steven is planning to send a ring to Jane with a romantic string engraved on. The string's length should not exceed N. The careful Steven knows Jane so deeply that he knows her favorite words, such as "love", "forever". Also, he knows the value of each word. The higher value a word has the more joy Jane will get when see it.
    The weight of a word is defined as its appeared times in the romantic string multiply by its value, while the weight of the romantic string is defined as the sum of all words' weight. You should output the string making its weight maximal. 

     

    Input

    The input consists of several test cases. The first line of input consists of an integer T, indicating the number of test cases. Each test case starts with a line consisting of two integers: N, M, indicating the string's length and the number of Jane's favorite words. Each of the following M lines consists of a favorite word Si. The last line of each test case consists of M integers, while the i-th number indicates the value of Si.
    Technical Specification

    1. T ≤ 15
    2. 0 < N ≤ 50, 0 < M ≤ 100.
    3. The length of each word is less than 11 and bigger than 0.
    4. 1 ≤ Hi ≤ 100. 
    5. All the words in the input are different.
    6. All the words just consist of 'a' - 'z'.
     

    Output

    For each test case, output the string to engrave on a single line.
    If there's more than one possible answer, first output the shortest one. If there are still multiple solutions, output the smallest in lexicographically order.

    The answer may be an empty string. 
     

    Sample Input

    2
    7 2
    love
    ever
    5 5
    5 1
    ab
    5

    Sample Output

    lovever
    abab
    Hint
    Sample 1: weight(love) = 5, weight(ever) = 5, so weight(lovever) = 5 + 5 = 10 Sample 2: weight(ab) = 2 * 5 = 10, so weight(abab) = 10

    Source

     
    解题:Trie 图 + 动态规划。由于需要字典序最小,所以从后面倒着dp
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 using PII = pair<int,int>;
     4 const int maxn = 510;
     5 
     6 struct Trie{
     7     int ch[maxn][26],fail[maxn],cnt[maxn],tot;
     8     int dp[maxn][maxn];
     9     PII path[maxn][maxn];
    10     int newnode(){
    11         memset(ch[tot],0,sizeof ch[tot]);
    12         fail[tot] = cnt[tot] = 0;
    13         return tot++;
    14     }
    15     void init(){
    16         tot = 0;
    17         newnode();
    18     }
    19     void insert(char *str,int val,int rt = 0){
    20         for(int i = 0; str[i]; ++i){
    21             int &x = ch[rt][str[i]-'a'];
    22             if(!x) x = newnode();
    23             rt = x;
    24         }
    25         cnt[rt] += val;
    26     }
    27     void build(int rt = 0){
    28         queue<int>q;
    29         for(int i = 0; i < 26; ++i)
    30             if(ch[rt][i]) q.push(ch[rt][i]);
    31         while(!q.empty()){
    32             rt = q.front();
    33             q.pop();
    34             cnt[rt] += cnt[fail[rt]];
    35             for(int i = 0; i < 26; ++i){
    36                 int &x = ch[rt][i],y = ch[fail[rt]][i];
    37                 if(x){
    38                     fail[x] = y;
    39                     q.push(x);
    40                 }else x = y;
    41             }
    42         }
    43     }
    44     void solve(int n){
    45         memset(dp,0,sizeof dp);
    46         for(int i = n; i; --i){
    47             for(int j = 0; j < tot; ++j){
    48                 for(int k = 0; k < 26; ++k){
    49                     int x = ch[j][k],tmp = dp[i+1][x] + cnt[x];
    50                     if(tmp > dp[i][j]){
    51                         dp[i][j] = tmp;
    52                         path[i][j] = PII(x,k);
    53                     }
    54                 }
    55             }
    56         }
    57         int pos = n + 1,ans = 0,st = 0;
    58         for(int i = n + 1; i; --i)
    59             if(dp[i][0] > ans) ans = dp[pos = i][0];
    60         for(int i = pos; i <= n; ++i){
    61             printf("%c",path[i][st].second + 'a');
    62             st = path[i][st].first;
    63         }
    64         putchar('
    ');
    65     }
    66 }ac;
    67 char str[maxn][maxn];
    68 int main(){
    69     int kase,n,m,val;
    70     scanf("%d",&kase);
    71     while(kase--){
    72         scanf("%d%d",&n,&m);
    73         for(int i = 0; i < m; ++i)
    74             scanf("%s",str[i]);
    75         ac.init();
    76         for(int i = 0; i < m; ++i){
    77             scanf("%d",&val);
    78             ac.insert(str[i],val);
    79         }
    80         ac.build();
    81         ac.solve(n);
    82     }
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    环境变量设置set env exportFedora Centos日志我的搜狐
    Hadoop Streaming 编程
    业务开发测试HBase之旅三:通过Java Api与HBase交互
    hadoop+zookeeper+hbase安装_dekar_x的空间_百度空间
    HBase Java客户端编程
    Hadoop应用测试
    HBase vs Cassandra: 我们迁移系统的原因
    关于HBase的一些零碎事
    奔流 | 自由、奔放的技术刊物
    Paxos在大型系统中常见的应用场景
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4940619.html
Copyright © 2011-2022 走看看