• [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;
}

最新文章

  1. 清空Github上某个文件的历史版本
  2. smarty模块引擎
  3. Lambda表达式详解
  4. Aspectj 实现Method条件运行
  5. 使用 SVG 实现一个漂亮的页面预加载效果
  6. 经典的Java基础面试题集锦
  7. 为什么在Mac中无法用k web运行ASP.NET 5程序
  8. zstu.4189: 逻辑运算(构建 &amp;&amp; 前缀表达式入门)
  9. sharepoint bcs (bussiness connectivity services)
  10. C#程序大打开
  11. epub、ocf等常用电子书格式浅析----附JAVA示例程序
  12. [深入React] 8.refs
  13. DevExpress GridView使用技巧之列标题点击事件
  14. ios正在使用NSDateComponents、NSDate、NSCalendar它的结论是在当前时间是在一段时间在一天。
  15. php提供service总结---wsdl篇
  16. SpringMVC的filter怎么使用Autowired依赖注入bean
  17. Js表单验证控件(使用方便,无需编码)-01使用说明
  18. 斑马打印机的安装调试,生成PDF
  19. ES6 解构
  20. 简单的页面互点Javascript代码

热门文章

  1. 【Android】监听viewpager子页面里面的Button按钮
  2. BZOJ2707 [SDOI2012]走迷宫 【概率dp + tarjan + 高斯消元】
  3. Codeforces633G - Yash And Trees
  4. 算法复习——树链剖分模板(bzoj1036)
  5. 前端ui框架---ant 蚂蚁金服开源
  6. Android Studio升级到3.0,抛出Aapt2Exception异常
  7. css解析规则
  8. Struts2的验证框架简单吗?
  9. python多线程实践小结
  10. 转:C#并口热敏小票打印机打印位图