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

    Problem Description
    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
     
     
    要求构造一个字符串使其包含的给定子串权值最大。
    dp维护最大价值在AC自动机里跑就好啦
    #include<iostream>
    #include<queue>
    #include<string>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    const int LO=26,NU=10005;
    inline int f(char u){
        return u-'a';
    }
    struct tree{
        int f;
        int q;
        int t[LO];
        int v[LO];
    }t[NU];
    int n,m,p,num,w;
    bool us[NU];
    char s[100][100];
    queue <int> q;
    int dp[51][NU];
    string ss[51][NU],str;
    inline int dfs(int x){
        if (us[x]) return t[x].q;
        us[x]=1;
        if (!x) return 0;
        return t[x].q+=dfs(t[x].f);
    }
    inline void in(int x){
        int p=0,l,m=strlen(s[x]);
        for (register int i=0;i<m;i++){
            l=f(s[x][i]);
            if (!t[p].t[l]) t[p].t[l]=++num;
            p=t[p].t[l];
        }
        t[p].q=w;
    }
    inline void mafa(){
        register int i;int k,p;
        q.push(0);t[0].f=0;
        while(!q.empty()){
            k=q.front();q.pop();
            for (i=0;i<LO;i++)
            if (t[k].t[i]){
                p=t[k].f;
                while((!t[p].t[i])&&p) p=t[p].f;
                t[t[k].t[i]].f=(k==p)?0:t[p].t[i];
                q.push(t[k].t[i]);
            }
        }
    }
    int main(){
        int tt;
        scanf("%d",&tt);
        register int i,j,k,l;int u;int ans;
        while(tt--){
            scanf("%d%d",&n,&m);
            num=u=ans=0;
            for (i=0;i<m;i++) scanf("%s",s[i]);
            for (i=0;i<m;i++){
                scanf("%d",&w);
                in(i);
            }
            mafa();
            for (i=0;i<=num;i++) us[i]=0;
            for (i=0;i<=num;i++) if (!us[i]) dfs(i);
            for (i=0;i<=num;i++)
            for (j=0;j<LO;j++){
                if (!t[i].t[j]){
                    u=t[i].f;
                    while(!t[u].t[j]&&u) u=t[u].f;
                    u=t[u].t[j];
                }else u=t[i].t[j];
                t[i].v[j]=u;
            }
            for (i=0;i<=n;i++)
            for (j=0;j<=num;j++) dp[i][j]=-1;
            dp[0][0]=0;ss[0][0]="";
            for (i=0;i<n;i++)
            for (j=0;j<=num;j++)
            if (dp[i][j]!=-1)
            for (l=0;l<LO;l++)
            if ((dp[i+1][t[j].v[l]]<t[t[j].v[l]].q+dp[i][j])||(dp[i+1][t[j].v[l]]==t[t[j].v[l]].q+dp[i][j]&&ss[i+1][t[j].v[l]]>ss[i][j]+(char)(l+'a')))
            dp[i+1][t[j].v[l]]=dp[i][j]+t[t[j].v[l]].q,ss[i+1][t[j].v[l]]=ss[i][j]+(char)(l+'a');
            ans=0;str="";
            for (i=0;i<=n;i++)
            for (j=0;j<=num;j++) if (dp[i][j]>ans||(dp[i][j]==ans&&(str.size()>ss[i][j].size()||(str.size()==ss[i][j].size()&&str>ss[i][j])))) ans=dp[i][j],str=ss[i][j];
            cout<<str<<endl;
            for (i=0;i<=num;i++)
            for (j=0;j<LO;j++) t[i].t[j]=t[i].v[j]=0;
            for (i=0;i<=num;i++) t[i].q=t[i].f=0;
        }
    }
    View Code
  • 相关阅读:
    Helpers Overview
    Validation
    Support Facades
    Session Store
    位运算(参考百科)
    开源项目_可能使用到的开源项目集合
    秒杀系统架构分析与实战(转)
    shell命令之根据字符串查询文件对应行记录
    MySQL做为手动开启事务用法
    spring 加载bean过程源码简易解剖(转载)
  • 原文地址:https://www.cnblogs.com/Enceladus/p/5312274.html
Copyright © 2011-2022 走看看