hdu 2296 Ring AC自动机+DP

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

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 

 

比较普通的AC自动机+DP,和之前做的类型题差不多,这道题不同之处在于需要沿途记录字符串,并且字符串要是字典序最小的。

可以用STL里的string记录,速度感觉还行,不是很慢。

题意和思路这里就不说了,比较简单,和前面的都一样,可以参考我之前的博客。

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int ch[102*12][26],End[102*12],cur,fail[102*12],last[102*12],h[102];
char str[55],str0[102][12];
void get_fail()
{
    int now,tmpFail,Next;
    queue<int> q;
    for(int j=0; j<26; j++)
    {
        if(ch[0][j])
        {
            q.push(ch[0][j]);
            fail[ch[0][j]] = 0;
            last[ch[0][j]] = 0;
        }
    }
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int j=0; j<26; j++)
        {
            if(!ch[now][j]) continue;
            Next = ch[now][j];
            q.push(Next);
            tmpFail = fail[now];
            while(tmpFail&&!ch[tmpFail][j]) tmpFail = fail[tmpFail];
            fail[Next] = ch[tmpFail][j];
            last[Next] = End[fail[Next]] ? fail[Next]:last[fail[Next]];
        }
    }
}
string ans_str[102*12][55];
int dp[102*12][55];
int main()
{
    int n,m,now,T,kase=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(ch,0,sizeof(ch));
        memset(End,0,sizeof(End));
        memset(last,0,sizeof(last));
        cur = 1;
        int len;
        for(int i=1; i<=m; i++)
        {
            scanf("%s",str0[i]);
            len = strlen(str0[i]);
            now = 0;
            for(int j=0; j<len; j++)
            {
                str0[i][j]-='a';
                if(ch[now][str0[i][j]]==0) ch[now][str0[i][j]] = cur++;
                now = ch[now][str0[i][j]];
                str0[i][j]+='a';
            }
            End[now] = i;
        }
        get_fail();

        for(int i=1; i<=m; i++) scanf("%d",&h[i]);

        memset(dp,-1,sizeof(dp));

        dp[0][0]=0;
        ans_str[0][0]=string("");
        int ans=-1,ans_now,ans_len;
        for(int i=0; i<n; i++)
        {
            for(int tnow=0; tnow<cur; tnow++)
            {
                if(dp[tnow][i]!=-1)
                for(int c=0; c<26; c++)
                {
                    int now = tnow,tsum=0;
                    while(now&&!ch[now][c]) now = fail[now];
                    now = ch[now][c];
                    if(End[now]) tsum+=h[End[now]];
                    int tmp = now;
                    while(last[tmp])
                    {
                        tsum += h[End[last[tmp]]];
                        tmp = last[tmp];
                    }
                    string tmp_s = string("a");
                    tmp_s[0]=c+'a';
                    if(dp[tnow][i]+tsum>dp[now][i+1]||
                       (dp[tnow][i]+tsum==dp[now][i+1] && ans_str[tnow][i]+tmp_s < ans_str[now][i+1])
                       ) {
                        ans_str[now][i+1] = ans_str[tnow][i]+tmp_s;
                        dp[now][i+1] = dp[tnow][i]+tsum;
                    }
                    if(dp[now][i+1]>ans || (
                       dp[now][i+1]==ans && ans_len==i+1 && ( ans_str[ans_now][ans_len] > ans_str[now][i+1] )
                       )) {
                        ans = dp[now][i+1];
                        ans_now = now;
                        ans_len = i+1;
                    }
                }
            }
        }
        if(ans>0) cout<<ans_str[ans_now][ans_len];
        puts("");
    }
}
posted @ 2016-04-09 19:11  ZhMZ  阅读(180)  评论(0编辑  收藏  举报