HDU 2296 Ring [AC自动机 DP 打印方案]

Ring

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3536 Accepted Submission(s):
1153

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.



题意:

给出m个模式串,每个串有一定的分值,构造一个长度不超过n的串,使得分值最大,并输出该字符串
在分数同样大时,输出长度最小的
长度一样时输出字典序最小的



HDU2296是一道有Bug的题目...............题目本身有表述不清楚导致有歧义........没有说明字符串后缀重合应该怎么处理.........所以说有两种程序可以通过本题,一种是Trie树正着插入、不管后缀重合、然后在AC自动机上DP时记录整个字符串,还有一种是Trie树倒着插入、后缀重合时权值都算上、DP时只记录转移来的状态...................然后本题字典序判断用Tire树的DFS序绝对不对,这个DFS序不能表达出整个生成的字符串的字典序............并且我一开始求DFS序是在建完AC自动机后求的,我的AC自动机是用了Trie图优化的,所以我就眼睁睁的看着为什么Trie树上的dfs一直停不下来............................................不要问我为什么花了一晚上

冷静的说做法:套路DP,f[i][j]生成到i AC自动机上走到j 的最大权值,记录pa[i][j]转移来的状态 字典序最小所以倒着插入 遇到相同暴力往前比较就行了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=1005,M=105,INF=1e9;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x*f;
}
int n,m,w[N];
char s[15];
struct node{
    int ch[27],val,fail,w;
    char c;
}t[N];
int sz;
void ins(char s[],int id){
    int u=0,n=strlen(s+1);
    reverse(s+1,s+1+n);
    for(int i=1;i<=n;i++){
        int c=s[i]-‘a‘;
        if(!t[u].ch[c]) t[u].ch[c]=++sz;
        u=t[u].ch[c];
        t[u].c=s[i];
    }
    t[u].val=id;
}
int q[N],head,tail;
void getAC(){
    head=tail=1;
    for(int i=0;i<26;i++) if(t[0].ch[i]) q[tail++]=t[0].ch[i];
    while(head!=tail){
        int u=q[head++];
        t[u].w=w[t[u].val];
        t[u].w+=t[t[u].fail].w;
        //val ???
        for(int i=0;i<26;i++){
            int &v=t[u].ch[i];
            if(!v) v=t[t[u].fail].ch[i];
            else{
                t[v].fail=t[t[u].fail].ch[i];
                q[tail++]=v;
            }
        }
    }
}
//int dfn[N],dfc;
//void dfs(int u){
//    dfn[u]=++dfc; //printf("dfs %d %d %c\n",u,dfn[u],t[u].c);
//    for(int i=0;i<26;i++) if(t[u].ch[i]) dfs(t[u].ch[i]);
//}
int f[55][N],pa[55][N];
bool cmp(int a,int b,int i){//a<b
    //printf("cmp %d %d %d\n",a,b,i);
    while(t[a].c==t[b].c) a=pa[i][a],b=pa[i][b],i--;//,printf("cmp %d %d %d\n",a,b,i);
    return t[a].c<t[b].c;
}
void lalala(int i,int j){
    if(i==0) return;
    putchar(t[j].c);
    lalala(i-1,pa[i][j]);
}
void dp(){
    memset(f,-1,sizeof(f));
    f[0][0]=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<=sz;j++) if(f[i][j]!=-1){
//            printf("use %d %d %d  %d\n",i,j,f[i][j],pa[i][j]);
            for(int k=0;k<26;k++){
                int p=t[j].ch[k];
                int _=f[i][j]+t[p].w;//printf("k %d %d %d %d\n",k,p,_,f[i+1][p]);
                if(_>f[i+1][p]) f[i+1][p]=_,pa[i+1][p]=j;
                else if(_==f[i+1][p]&&j!=pa[i+1][p]&&cmp(j,pa[i+1][p],i)) pa[i+1][p]=j;
            }
        }
    int ans=-1,ai=0,aj=0;
    for(int i=1;i<=n;i++) for(int j=0;j<=sz;j++){//printf("f %d %d %d %d\n",i,j,f[i][j],pa[i][j]);
        if(f[i][j]>ans) ans=f[i][j],ai=i,aj=j;
        else if(f[i][j]==ans&&i==ai&&cmp(j,aj,i)) ans=f[i][j],aj=j;//,printf("dfn %d %d %d\n",i,j,f[i][j]);
    }
    //printf("ans %d %d %d\n",ans,ai,aj);
     if(ans!=0)
        lalala(ai,aj);
    puts("");
}
int main(){
    freopen("in","r",stdin);
    int T=read();
    while(T--){
        memset(t,0,sizeof(t));sz=0;
        n=read();m=read();
        for(int i=1;i<=m;i++) scanf("%s",s+1),ins(s,i);
        for(int i=1;i<=m;i++) w[i]=read();
        getAC();

        //for(int i=1;i<=sz;i++) printf("check %d %d %c\n",i,t[i].val,t[i].c);
        dp();
    }
}
时间: 2024-12-18 09:45:09

HDU 2296 Ring [AC自动机 DP 打印方案]的相关文章

HDU 2296 Ring AC自动机 + DP

题意:给你n个模式串,每个模式串有一个得分,让你构造出一个长度为N之内且分数最高的文本串;输出字典序列最小的. 解题思路:  AC自动机 + DP , 不过要输出字典序列最小,多开一个 一个三维字符串来辅助二维DP(新思路) , DP[i][j] ,表示到i位置状态为j的最大得分. 解题代码: 1 // File Name: temp.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月11日 星期四 15时18分4秒 4 5 #inclu

[hdu2296]Ring(AC自动机+dp)

题意:你M个单词构成一个词典,每个单词有一个权值(单词出现多次算多个权值),现在要你构造一个不超过长度N的字符串,使得该字符串权值最大.如果出现多个答案,输出最短的,如果依然有多解,输出字典序最小的. 解题关键:最典型的AC自动机上跑dp. 令$dp[i][j] = x$表示走了i步到达j点的最大价值,则 转移方程:$dp[i][j] = \max (dp[i][j],dp[i-1][k] + val[j])$ $val[i]$代表以某前缀的价值总和. 注意这里是多对多的关系,需用从遍历起点时更

hdu 2296 aC自动机+dp(得到价值最大的字符串)

Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3180    Accepted Submission(s): 1033 Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a rom

hdu 2457 AC自动机+dp

DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2004    Accepted Submission(s): 1085 Problem Description Biologists finally invent techniques of repairing DNA that contains segments c

HDU2296 Ring(AC自动机+DP)

题目是给几个带有价值的单词.而一个字符串的价值是 各单词在它里面出现次数*单词价值 的和,问长度不超过n的最大价值的字符串是什么? 依然是入门的AC自动机+DP题..不一样的是这题要输出具体方案,加个字符数组记录每个状态最优情况的字符串即可. 另外题目字典序是先考虑长度再考虑每一位单词:特别要注意,有一个非常坑的地方看了Disscus才知道——单词A包含单词B,那么只计算单词A不计算单词B. dp[i][j]表示长度i(自动机上转移k步)后缀状态是自动机第j个结点的字符串的最大价值 dp[0][

HDU 3341 Lost&#39;s revenge AC自动机+dp

Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3757    Accepted Submission(s): 1020 Problem Description Lost and AekdyCoin are friends. They always play "number game"(A bor

Hdu 3341 Lost&#39;s revenge (ac自动机+dp+hash)

题目大意: 给出很多个DNA串,每一个串的价值为1,最后给出一个长串,要你重新排列最后的串使之它所有的子串的权值和最大. 思路分析: 最先容易想到的思路就是搜!管她3721..直接一个字符一个字符的码,然后在AC自动机上判断最后的权值.TLE哟. 然后发现搜过不去,那就dp咯.再容易想到的就是dp[i][a][b][c][d] 表示此时遍历AC自动机的节点在i,然后构成了a个A,b个G,c个C,d个T的权值. 再一看内存,500*40*40*40*40...然后...就没有然后了 再想,因为它说

hdu 2825 Wireless Password(ac自动机&amp;dp)

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4022    Accepted Submission(s): 1196 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

hdu 4878 ZCC loves words(AC自动机+dp+矩阵快速幂+中国剩余定理)

hdu 4878 ZCC loves words(AC自动机+dp+矩阵快速幂+中国剩余定理) 题意:给出若干个模式串,总长度不超过40,对于某一个字符串,它有一个价值,对于这个价值的计算方法是这样的,设初始价值为V=1,假如这个串能匹配第k个模式串,则V=V*prime[k]*(i+len[k]),其中prime[k]表示第k个素数,i表示匹配的结束位置,len[k]表示第k个模式串的长度(注意,一个字符串可以多次匹配同意个模式串).问字符集为'A'-'Z'的字符,组成的所有的长为L的字符串,