• [1222] English Game

  • 时间限制: 1000 ms 内存限制: 131072 K
  • 问题描写叙述
  • 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

题目大意:

给出一个目标字符串和n个字符串及其相应的权值,求用这些字符串中的1个或多个组成目标字符串的最大权值。

解题思路:

用trie树保存b个字符串及其权值,接下来就是动态规划了。

到达某一点i,遍历trie树找到i+1为第一个字符的字符串。在结束位置j,dp[j]=max(dp[j],dp[i]+val[i+1到j之间的字符串])。除第0位之外,假设dp[i]=0说明没有字符串可以组成0到i之间的字符串,那么就不用查询。

參考代码:

#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const double eps=1e-10;
const int INF=0x3f3f3f3f;
const int MAXN=1e4+50;
typedef long long LL;
struct node
{
node* next[26];
int val;
node()
{
val=0;
memset(next,0,sizeof(next));
}
}*root; int n,L,dp[MAXN];
char s[MAXN]; void trie_insert(node* start,char* word,int v)
{
node* now=start;
int l=strlen(word);
for(int i=0; i<l; i++)
{
int id=word[i]-'a';
if(now->next[id]==NULL)
now->next[id]=new node();
now=now->next[id];
}
now->val=v;
} int trie_query(node* start,int pos)
{
node* now=start;
int i;
for(i=pos+1; i<=L; i++)
{
int id=s[i]-'a';
if(now->next[id]==NULL)
break;
now=now->next[id];
if(now->val)
dp[i]=max(dp[i],dp[pos]+now->val);
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(scanf("%d%s",&n,s+1)!=EOF)
{
root=new node();
L=strlen(s+1);
memset(dp,0,sizeof(dp));
for(int i=0; i<n; i++)
{
char tmp[MAXN];
int d;
scanf("%s%d",tmp,&d);
trie_insert(root,tmp,d);
}
for(int i=0; i<=L; i++)
if(dp[i]||i==0)//假设dp[i]=0说明没有字符串可以组成0到i之间的字符串,那么就不用查询
trie_query(root,i);
printf("%d\n",dp[L]==0?-1:dp[L]);
}
return 0;
}

NBUT 1222 English Game(trie树+DP)的更多相关文章

  1. POJ2004 Mix and build Trie树? dp?

    学习Trie树中,所以上网搜一下Trie树的题,找到这个,人家写着是简单dp,那我就想着能学习到什么Trie树上的dp,但最后发现根本好像跟Trie树没有什么联系嘛... 题意就是给你很多个字符串(长 ...

  2. LA-3942(trie树+dp)

    题意: 给出一个由多个不同单词组成的字典,和一个长字符串,把这个字符串分解成若干个单词的连接,问有多少种方法; 思路: dp[i]表示s[i,L]的方案数,d[i]=∑d[j];s[i,j-1]是一个 ...

  3. Remember the Word,LA3942(Trie树+DP)

    Trie树基础题,记录下代码. #include <cstdio> #include <cstring> #define MaxNode 4005*100 #define No ...

  4. NBUT 1222 English Game 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB This English game is a simple English words connection gam ...

  5. hdu4843(NOI2000) 古城之谜 (trie树+DP)

    Description 著名的考古学家石教授在云梦高原上发现了一处古代城市遗址.让教授欣喜的是在这个他称为冰峰城(Ice-Peak City)的城市中有12块巨大石碑,上面刻着用某种文字书写的资料,他 ...

  6. BZOJ1212[HNOI2004]L语言——trie树+DP

    题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...

  7. Codeforces 615C Running Track(DP + Trie树)

    题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...

  8. UVa1401 Remember the Word(DP+Trie树)

    题目给定一个字符串集合有几种方式拼成一个字符串. dp[i]表示stri...strlen-1的方案数 dp[len]=1 dp[i]=∑dp[j](stri...strj-1∈SET) 用集合的字符 ...

  9. 【10.29校内测试】【线段树】【DP】【二进制Trie树求最小值最大】

    Solution 标程太暴力惹QAQ 相当于是26棵线段树的说QAQ 不过我写了另一种写法,从大到小枚举每一个字母,标记字典序在这个字母之上的位置为1,每次都建一棵线段树,维护1的数量,即区间和. 修 ...

随机推荐

  1. iOS学习笔记14-网络(三)WebView

    一.WebView WebView就是一个内嵌浏览器控件,在iOS中主要有两种WebView:UIWebView和WKWebView,UIWebView是iOS2之后开始使用,WKWebView是在i ...

  2. 【Luogu】P1462通往奥格瑞玛的道路(二分答案+SPFA)

    题目链接 导致我WA十几遍的原因居然是最大值不够大……以后再也不相信memset(dis,127/3,sizeof(dis))了. 此题先将花费排序,然后二分最大花费,spfa判断解是否可行.spfa ...

  3. BZOJ 4719 [Noip2016]天天爱跑步 ——树链剖分

    一直以为自己当时是TLE了,但是再看发现居然WA? 然后把数组扩大一倍,就A掉了.QaQ 没什么好说的.一段路径分成两段考虑,上升的一段深度+时间是定值,下降的一段深度-时间是定值,然后打标记统计即可 ...

  4. 【2018.10.20】CXM笔记(思维)

    1. 给你个环状字符串,问从哪个地方拆开能使它的字典序最小. 先预处理任意子串的哈希值. 然后枚举拆点,将它与当前最优的拆点比较谁更优(就是从哪拆的字典序更小),具体方法是二分+哈希找出两串最长的相同 ...

  5. msp430项目编程57

    msp430综合项目---扩展项目七57 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结

  6. HUNAN -11566 Graduation Examination(找规律)

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11566&courseid=0 输入n,求出第n个fi ...

  7. TortoiseSVN如何更换或重置登录用户

    昨天手贱把svn重新卸载了,再安装后便与之前的项目断了,因为第一次使用这个,也不清楚再怎么登录,还有就是上次是使用别人的账号,也不知道怎么清除别人的账号. 鼠标右键找到settings,点击打开 找到 ...

  8. Java到底是值传递还是引用传递

    什么是按值传递,什么是按引用传递 按值调用(call by value) : 在参数传递过程中,形参和实参占用了两个完全不同的内存空间.形参所存储的内容是实参存储内容的一份拷贝. 按引用调用:在参数传 ...

  9. WIP - 离散任务点击组件-错误:LOCATOR.CONTROL 的变元无效:ORG_LOCATOR_CONTROL=''

    Getting Error "Invalid Argument to LOCATOR.CONTROL: ORG_LOCATOR_CONTROL='' in Material Requirem ...

  10. [转]PHP并发IO编程之路(深度长文)

    原文:https://www.imooc.com/article/8449 -------------------------------------------------------------- ...