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

NOJ1222-English Game(AC自动机+dp)

时间:2015-04-16 14:24:45      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:ac自动机   dp   

问题描述

This English game is a simple English words connection game.
The rules are as follows: there are N English words in a dictionary, and every word has its own weight v. There is a weight if the corresponding word is used. Now there is a target string X. You have to pick some words in the dictionary, and then connect them to form X. At the same time, the sum weight of the words you picked must be the biggest.

输入
There are several test cases. For each test, N (1<=n<=1000) and X (the length of x is not bigger than 10000) are given at first. Then N rows follow. Each row contains a word wi (the length is not bigger than 30) and the weight of it. Every word is composed of lowercases. No two words in the dictionary are the same.
输出
For each test case, output the biggest sum weight, if you could not form the string X, output -1.
样例输入

1 aaaa
a 2
3 aaa
a 2
aa 5
aaa 6
4 abc
a 1
bc 2
ab 4
c 1
3 abcd
ab 10
bc 20
cd 30
3 abcd
cd 100
abc 1000
bcd 10000

样例输出

8
7
5
40
-1

提示

来源

辽宁省赛2010

先用AC自动机把串每一个位置可以成功匹配的模式串的长度和权值存起来,然后一个水的dp就行
dp[i] 表示长度为i时可以得到的最大的权值,转移方程见代码

/*************************************************************************
    > File Name: NOJ1222.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年04月15日 星期三 21时57分33秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

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

const int MAX_NODE = 30010;
const int CHILD_NUM = 26;
vector <PLL> use[10010];

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

    int newnode()
    {
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            next[L][i] = -1;
        }
        size[L] = 0;
        W[L] = 0;
        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‘];
        }
        size[now] = len;
        end[now] = 1;
        W[now] = max(W[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 Match (char buf[])
    {
        int now = root;
        int len = strlen (buf);
        for (int i = 0; i < len; ++i)
        {
            now = next[now][buf[i] - ‘a‘];
            int tmp = now;
            while (tmp != root)
            {
                if (end[tmp])
                {
                    use[i].push_back(make_pair(size[tmp], W[tmp]));
                }
                tmp = fail[tmp];
            }
        }
    }
}AC;

char str[10010];
char buf[33];
int dp[10010];

int main()
{
    int n, w;
    while (~scanf("%d%s", &n, str))
    {
        AC.init();
        for (int i = 1; i <= n; ++i)
        {
            scanf("%s%d", buf, &w);
            AC.Build_Trie(buf, w);
        }
        int len = strlen(str);
        for (int i = 0; i < len; ++i)
        {
            use[i].clear();
        }
        AC.Build_AC();
        AC.Match(str);
        memset(dp, -1, sizeof(dp));
        dp[0] = 0;
        for (int i = 1; i <= len; ++i)
        {
            int size = use[i - 1].size();
            for (int j = 0; j < size; ++j)
            {
                PLL tmp = use[i - 1][j];
                if (dp[i - tmp.first] != -1)
                {
                    dp[i] = max(dp[i], dp[i - tmp.first] + tmp.second);
                }
            }
        }
        printf("%d\n", dp[len]);
    }
    return 0;
}

NOJ1222-English Game(AC自动机+dp)

标签:ac自动机   dp   

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

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