NBUT 1222 English Game(trie树+DP)
[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)的更多相关文章
- POJ2004 Mix and build Trie树? dp?
学习Trie树中,所以上网搜一下Trie树的题,找到这个,人家写着是简单dp,那我就想着能学习到什么Trie树上的dp,但最后发现根本好像跟Trie树没有什么联系嘛... 题意就是给你很多个字符串(长 ...
- LA-3942(trie树+dp)
题意: 给出一个由多个不同单词组成的字典,和一个长字符串,把这个字符串分解成若干个单词的连接,问有多少种方法; 思路: dp[i]表示s[i,L]的方案数,d[i]=∑d[j];s[i,j-1]是一个 ...
- Remember the Word,LA3942(Trie树+DP)
Trie树基础题,记录下代码. #include <cstdio> #include <cstring> #define MaxNode 4005*100 #define No ...
- NBUT 1222 English Game 2010辽宁省赛
Time limit 1000 ms Memory limit 131072 kB This English game is a simple English words connection gam ...
- hdu4843(NOI2000) 古城之谜 (trie树+DP)
Description 著名的考古学家石教授在云梦高原上发现了一处古代城市遗址.让教授欣喜的是在这个他称为冰峰城(Ice-Peak City)的城市中有12块巨大石碑,上面刻着用某种文字书写的资料,他 ...
- BZOJ1212[HNOI2004]L语言——trie树+DP
题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...
- Codeforces 615C Running Track(DP + Trie树)
题目大概说给两个串,问最少要用多少个第一个串的子串(可以翻转)拼成第二个串. UVa1401,一个道理..dp[i]表示前缀i拼接成功所需最少的子串,利用第一个串所有子串建立的Trie树往前枚举转移. ...
- UVa1401 Remember the Word(DP+Trie树)
题目给定一个字符串集合有几种方式拼成一个字符串. dp[i]表示stri...strlen-1的方案数 dp[len]=1 dp[i]=∑dp[j](stri...strj-1∈SET) 用集合的字符 ...
- 【10.29校内测试】【线段树】【DP】【二进制Trie树求最小值最大】
Solution 标程太暴力惹QAQ 相当于是26棵线段树的说QAQ 不过我写了另一种写法,从大到小枚举每一个字母,标记字典序在这个字母之上的位置为1,每次都建一棵线段树,维护1的数量,即区间和. 修 ...
随机推荐
- iOS知识列表
Xib和StoryBoard,自动布局地图导航,实时公交,第三方地图应用本地推送通知,网络推送通知,真机调试,应用上传绘图,图表,曲线图 Xcode使用技巧多线程,runtime机制编码解码,加密设备 ...
- Spark与Pandas中DataFrame对比(详细)
Pandas Spark 工作方式 单机single machine tool,没有并行机制parallelism不支持Hadoop,处理大量数据有瓶颈 分布式并行计算框架,内建并行机制paral ...
- PAT天梯赛练习题——L3-005. 垃圾箱分布(暴力SPFA)
L3-005. 垃圾箱分布 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁 ...
- POJ 2914 Minimum Cut 全局最小割
裸的全局最小割了吧 有重边,用邻接矩阵的时候要小心 #include<iostream> #include<cstdio> #include<bitset> #in ...
- 49道Spring面试题和答案
49道Spring面试题和答案 Spring 概述 1. 什么是spring? Spring 是个Java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE ...
- Linux(9):期中架构(1)--- 集群构架 & 备份服务
01. 了解集群架构服务器组成 基本架构组成:(用于让用户进行访问) # 前端服务部分: 1)顾客-用户 是一个访问者,请求访问网站页面 2)保安-防火墙设备 对访问架构用户进行策略控制,正常访问网站 ...
- Subsequence(hdu 3530)
题意:给你一个长度为n的数列,要求一个子区间,使得区间的最大值与最小值的差s满足,m<=s<=k,求满足条件的最长子区间 /* 单调队列 我们可以用单调队列分别维护最大值和最小值 当差值大 ...
- 【ZOJ4053】Couleur(主席树,set,启发式)
题意: 有n个位置,每个位置上的数字是a[i],现在有强制在线的若干个单点删除操作,每次删除的位置都不同,要求每次删除之后求出最大的连续区间逆序对个数 n<=1e5,1<=a[i]< ...
- Elixir与编辑器安装
安装 Elixir 每个操作系统的安装说明可以在 elixir-lang.org 网站上 Installing Elixir 部分找到. 安装后你可以很轻松地确认所安装的版本. ~$:elixir - ...
- word2013 交叉引用添加参考文献的尾注编号,通过查找 ^# 替换为 [^&] 的方式添加中括号,在进行“更新域”操作后,中括号消失。
word2013 交叉引用添加的尾注编号,通过查找 ^# 替换为 [^&] 的方式添加中括号,用这个方法添加中括号很多次了,这次却出现问题:在进行“更新域”操作后,中括号消失. 详 ...