hdoj 2296 Ring 【AC自动机 + dp】



Ring

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


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
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
 


如:串ab价值为5,串bc价值为5,那么串abc的价值为10。


题意:给出m个串以及每个串的价值,现在让你找到一个长度不超过n的串,使得其价值最大。若有多个串满足,输出所有串中最短的,若依旧有多个串满足,输出字典序最小的串。


思路:构建AC自动机上的Trie状态转移图,在串的节点用数组记录串的编号。

用dp[i][j]表示在Trie上走i步达到j节点获得的最大价值。

每走一步——就在上一节点的所有孩子中维护具有最大价值、长度最短且字典序最小的串,并更新我们需要的结果。




AC代码:


#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 1200
#define INF 0x3f3f3f3f
using namespace std;
int val[110];
int n, m;
bool cmp(char *s1, char *s2)
{
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    if(len1 != len2)
        return len1 < len2;//优先长度短的
    else
        return strcmp(s1, s2) < 0;//优先字典序小的
}
struct Trie
{
    int next[MAXN][30], fail[MAXN], End[MAXN];
    int L, root;
    int newnode()
    {
        for(int i = 0; i < 26; i++)
            next[L][i] = -1;
        End[L++] = -1;
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void Insert(char *s, int id)
    {
        int now = root;
        for(int i = 0; s[i]; i++)
        {
            if(next[now][s[i]-'a'] == -1)
                next[now][s[i]-'a'] = newnode();
            now = next[now][s[i]-'a'];
        }
        End[now] = id;
    }
    void Build()
    {
        queue<int> Q;
        fail[root] = root;
        for(int i = 0; i < 26; i++)
        {
            if(next[root][i] == -1)
                next[root][i] = root;
            else
            {
                fail[next[root][i]] = root;
                Q.push(next[root][i]);
            }
        }
        while(!Q.empty())
        {
            int now = Q.front();
            Q.pop();
            for(int i = 0; i < 26; i++)
            {
                if(next[now][i] == -1)
                    next[now][i] = next[fail[now]][i];
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    Q.push(next[now][i]);
                }
            }
        }
    }
    int dp[60][MAXN];//在Trie上从root出发走i步到达j节点获得的最大价值
    char str[60][MAXN][60];
    char ans[60], temp[60];
    void solve()
    {
        int Mval = 0;//价值不能为负
        memset(dp, -INF, sizeof(dp));
        dp[0][0] = 0;
        memset(ans, 0, sizeof(ans));
        strcpy(str[0][0], "");
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < L; j++)
            {
                if(dp[i][j] == -INF) continue;
                strcpy(temp, str[i][j]);
                int len = strlen(temp);
                for(int k = 0; k < 26; k++)
                {
                    int nextnode = next[j][k];
                    int have = dp[i][j];
                    if(End[nextnode] != -1)
                        have += val[End[nextnode]];
                    temp[len] = 'a' + k;
                    temp[len+1] = '\0';
                    if(dp[i+1][nextnode] < have || (dp[i+1][nextnode] == have && cmp(temp, str[i+1][nextnode])))
                    {
                        dp[i+1][nextnode] = have;
                        strcpy(str[i+1][nextnode], temp);
                        if(Mval < have || (Mval == have && cmp(temp, ans)))
                        {
                            Mval = have;
                            strcpy(ans, temp);
                        }
                    }
                }
            }
        }
        printf("%s\n", ans);
    }
};
Trie ac;
char str[60];
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &n, &m);
        ac.init();
        for(int i = 1; i <= m; i++)
            scanf("%s", str), ac.Insert(str, i);
        for(int i = 1; i <= m; i++)
            scanf("%d", &val[i]);
        ac.Build(); ac.solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值