码迷,mamicode.com
首页 > 其他好文 > 详细

hdu2296---Ring(AC自动机+dp)

时间:2015-03-04 16:59:08      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:ac自动机   dp   

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

Source
The 4th Baidu Cup final

Recommend
lcy | We have carefully selected several similar problems for you: 3247 3341 2825 2243 2457

dp[i][j]表示长度为i,在节点j时的最大权值,开一个数组来记录到达这个状态时的字符串

/*************************************************************************
    > File Name: hdu2296.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月03日 星期二 17时11分54秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int MAX_NODE = 1200;
const int CHILD_NUM = 26;

int dp[100][MAX_NODE];
string path[100][MAX_NODE];
char buf[110][15];
int w[110];
int n, m;

struct AC_Automation
{
    int next[MAX_NODE][CHILD_NUM];
    int fail[MAX_NODE];
    int end[MAX_NODE];
    int root, L;

    int newnode ()
    {
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            next[L][i] = -1;
        }
        end[L++] = 0;
        return L - 1;
    }

    void init ()
    {
        L = 0;
        root = newnode();
    }

    void Build_Trie (char buf[], int w)
    {
        int now = root;
        int len = strlen (buf);
        for (int i = 0; i < len; ++i)
        {
            if (next[now][buf[i] - ‘a‘] == -1)
            {
                next[now][buf[i] - ‘a‘] = newnode();
            }
            now = next[now][buf[i] - ‘a‘];
        }
        end[now] = w;
    }

    void Build_AC ()
    {
        queue <int> qu;
        fail[root] = root;
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            if (next[root][i] == -1)
            {
                next[root][i] = root;
            }
            else
            {
                fail[next[root][i]] = root;
                qu.push (next[root][i]);
            }
        }

        while (!qu.empty())
        {
            int now = qu.front ();
            qu.pop();
            for (int i = 0; i < CHILD_NUM; ++i)
            {
                if (next[now][i] == -1)
                {
                    next[now][i] = next[fail[now]][i];
                }
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    qu.push (next[now][i]);
                }
            }
        }
    }

    void solve ()
    {
        dp[0][0] = 0;
        for (int i = 0; i <= n; ++i)
        {
            for (int j = 0; j <= L; ++j)
            {
                path[i][j] = "";
            }
        }
        string ans;
        ans = "";
        int maxs = 0;
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < L; ++j)
            {
                if (dp[i][j] >= 0)
                {
                    for (int k = 0; k < CHILD_NUM; ++k)
                    {
                        int l = next[j][k];
                        string cur = path[i][j];
                        cur += (‘a‘ + k);
                        int tmp = dp[i][j];
                        if (end[l] != 0)
                        {
                            tmp += end[l];
                        }
                        if (dp[i + 1][l] < tmp)
                        {
                            dp[i + 1][l] = tmp;
                            path[i + 1][l] = cur;
                        }
                        else if (dp[i + 1][l] == tmp)
                        {
                            if (path[i + 1][l].length() > cur.length())
                            {
                                path[i + 1][l] = cur;
                            }
                            else if (path[i + 1][l].length() == cur.length() && cur < path[i + 1][l])
                            {
                                path[i + 1][l] = cur;
                            }
                        }
                        if (maxs < dp[i + 1][l])
                        {
                            maxs = dp[i + 1][l];
                            ans = path[i + 1][l];
                        }
                        else if (maxs == dp[i + 1][l])
                        {
                            int len1 = ans.length();
                            int len2 = path[i + 1][l].length();
                            if (len1 > len2)
                            {
                                ans = path[i + 1][l];
                            }
                            else if (len1 == len2 && ans > path[i + 1][l])
                            {
                                ans = path[i + 1][l];
                            }
                        }
                    }
                }
            }
        }
        cout << ans << endl;
    }
}AC;

int main ()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        AC.init();
        memset (dp, -1, sizeof(dp));
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= m; ++i)
        {
            scanf("%s", buf[i]);
        }
        for (int i = 1; i <= m; ++i)
        {
            scanf("%d", &w[i]);
        }
        for (int i = 1; i <= m; ++i)
        {
            AC.Build_Trie (buf[i], w[i]);
        }
        AC.Build_AC();
        AC.solve ();
    }
    return 0;
}

hdu2296---Ring(AC自动机+dp)

标签:ac自动机   dp   

原文地址:http://blog.csdn.net/guard_mine/article/details/44061631

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!