2010辽宁省赛 NBUT 1222 English Game【字典树+DP】

  • [1222] English Game

  • 时间限制: 1000 ms 内存限制: 131072 K
  • 链接:Click Here!
  • 问题描述
  • 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

题意:

字典中有N个单词,每个单词都有一个权值,现在给你一个长度小于10000的字符串,你的任务就是在字典中找到若干个单词,组成这个字符串,并且要求权值最大。存在输出最大权值,不存在输出“-1”。

分析:

字典树+DP,用dp[i] 表示长度为i字符串的最大权值,那么很容易写出状态转移方程:

dp[i+j] = max{dp[i+j],dp[i] + 以第i+1个字符结尾的权值 |  j > 0 并且i+j < 字符串的长度};

实现代码:

#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
typedef long long       LL;
int N, CNT, dp[10000+5];
char sbuf[10000 + 5], buf[35];
struct Node
{
    int Num;
    Node* pNext[26];
    Node() : Num(0)
    {
        memset(pNext, NULL, sizeof(pNext));
    }
} Memo[30 * 1000 + 5];
struct Trie
{
    Node *pRoot, *ptr;
    void Insert(const char str[], const int Len, const int& w)
    {
        ptr = pRoot;
        int id;
        for(int i = 0; i < Len; i++)
        {
            id = str[i] - 'a';
            if(NULL == ptr->pNext[id]) ptr->pNext[id] = &Memo[++CNT];
            ptr = ptr->pNext[id];
        }
        ptr->Num = w;
    }
    void Query(const char str[], const int L, const int p)
    {
        if(dp[p] < 0) return;
        ptr = pRoot;
        int id;
        for(int i = p, j = 1; i < L; i++, j++)
        {
            id = str[i] - 'a';
            if(NULL == ptr->pNext[id]) return;
            ptr = ptr->pNext[id];
            if(ptr->Num) dp[p + j] = max(dp[p + j], dp[p] + ptr->Num);
        }
    }
} trie;
void init()
{
    memset(Memo, 0, sizeof(Memo));
    memset(dp, -1, sizeof(dp));
    CNT = 0;
    dp[0] = 0;
    trie.pRoot = &Memo[CNT++];
}
int main()
{
//    FIN;
    while(~scanf("%d %s", &N, sbuf))
    {
        init();
        int Len = strlen(sbuf), w;
        for(int i = 0; i < N; i++)
        {
            scanf("%s %d", buf, &w);
            trie.Insert(buf, strlen(buf), w);
        }
        for(int i = 0; i < Len; i++)
        {
            trie.Query(sbuf, Len, x);
        }
        printf("%d\n", dp[Len]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 21:11:23

2010辽宁省赛 NBUT 1222 English Game【字典树+DP】的相关文章

2010年辽宁省赛 NBUT 1218【DFS实现树的遍历与更新】

[1218] You are my brother 时间限制: 1000 ms 内存限制: 131072 K 链接:NBUT 1218 问题描述 Little A gets to know a new friend, Little B, recently. One day, they realize that they are family 500 years ago. Now, Little A wants to know whether Little B is his elder, youn

zoj3013Word Segmenting (字典树+dp)

Word Segmenting Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge One key technology in Chinese search engine is Word Segmenting, which is more difficult than English Word Segmenting, as there is no space between words. A traditional solutio

UVALive 3942 Remember the Word 字典树+dp

/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5)和m个单词(每个单词长度最多100).单词都是不同的.该字符串可以由若干个单词组成,问最多有多少种组合方式. 思路:字典树+dp 用字典树处理好m个单词,定义dp[i]表示从i开始的字符串可以由单词组成的方式数. 那么dp[i] += dp[i+j]; j表示某个单词和字符串的[i,i+j-1]匹配

2010辽宁省赛F(字典树,动态规划)

#include<bits/stdc++.h>using namespace std;int n,x;char s[10010];char a[31010];int val[100010];int ch[100010][30];int dp[100010];int main(){    while(~scanf("%d",&n))    {        scanf("%s",s+1);        int len=strlen(s+1);  

【BZOJ2741】【FOTILE模拟赛】L 可持久化字典树+分块

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44496739"); } 题解: 首先我们处理出来sum[0,n]作为异或前缀和,然后答案就不再是[l,r]中间某段区间的异或和,而转化成求了[l?1,r]中任意两点异或和的最大值. 然后我们分块处理出fi,j表示 [第i块的开头,j

HDU5715 XOR 游戏 二分+字典树+dp

当时Astar复赛的时候只做出1题,赛后补题(很长时间后才补,懒真是要命),发现这是第二简单的 分析: 这个题,可以每次二分区间的最小异或和 进行check的时候用dp进行判断,dp[i][j]代表前i个元素分成j个区间,j是最后一个区间的最后一个元素 如果dp[i][j]为真,表明每个区间长度大于L,异或和大于mid 否则为假 返回dp[n][m]就好 复杂度度 O(30^2*nm) 吐槽:和异或相关的题总是和字典树贪心有关,又是一道,铭记铭记 #include <stdio.h> #inc

codeforces 456d (字典树+DP)

题目大意:给定n,表示字符串集合.给定k,表示进行了k次游戏,然后是n个字符串.每局开始,字符串为空串,然后两人轮流在末尾追加字符,保证新的字符串为集合中某字符串的前缀,不能操作者输,新一轮由上一句输的人先手. 思路:00 代表不能控制 01代表败,10代表胜,11代表能输能赢 转自  http://blog.csdn.net/blankcqk/article/details/38459033 附带字典树模版: void insert(){ int len = strlen(s); int no

LA 3942 - Remember the Word (字典树 + dp)

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1943 题目大意: 给定一个字符串和给定一个单词集合.问从给定单词集合中选取单词,有多少种选取方法刚好拼接成字符串. 例如: abcd 4 a b cd ab 有两种 a-b-cd ab-cd 这两种情况 解题思路: 因为给定的字符串的长度是3*10^5所以暴力就不能解决问题了

UVA 3942 -- Remember the Word (字典树+dp)

Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie. Since Jiejie can't remember numbers clear