• [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. 【数位DP】HDU 6156 Palindrome Function

    http://acm.hdu.edu.cn/showproblem.php?pid=6156 [AC] #include<bits/stdc++.h> using namespace st ...

  2. oracle连接封装方法

    public static void updateSqlOracle(String sqlstr,String linkIP,String username,String password) thro ...

  3. 如何禁止虚拟机自动获取DHCP分配的ip地址

    今天在看Hadoop视频学习的时候跟着视频里面修改ip地址,将虚拟机的ip地址修改为192.168.2.3,结果ifconfig显示ip地址为192.168.2.128,用物理主机去ping这两个ip ...

  4. angular中ng-class的一些用法

    在前面Angularjs开发一些经验总结中我们说到在angular开发中angular controller never 包含DOM元素(html/css),在controller需要一个简单的POJ ...

  5. localStorage增删改查

    /** * 设置 本地缓存 */ export function setStorage(key, obj) { if (typeof obj === 'string') { localStorage. ...

  6. Android SurfaceView与View

    SurfaceView介绍 SurfaceView是视图(View)的继承类,这个视图里面内嵌了一个专门用于绘制的Surface.你可以控制这个Surface的格式和尺寸,而SurfaceView控制 ...

  7. 标准C程序设计七---11

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  8. Java的常用对象

    PO:持久对象 (persistent object),po(persistent object)就是在Object/Relation Mapping框架中的Entity,po的每个属性基本上都对应数 ...

  9. 多个ajax执行混乱问题

    多个ajax执行混乱问题,之前拿ajax取代iframe做响应布局(左侧点击,右侧展示),当执行多个点击事件时会造成一个页面的初始化触发另一个页面的on click的function, 将ajax调为 ...

  10. 2716 [Violet 3] 天使玩偶

    @(BZOJ)[CDQ分治] Sample Input 100 100 81 23 27 16 52 58 44 24 25 95 34 2 96 25 8 14 97 50 97 18 64 3 4 ...