Time limit 1000 ms

Memory limit 131072 kB

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.

Input

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.

Output

For each test case, output the biggest sum weight, if you could not form the string X, output -1.

Sample Input

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

Sample Output

8
7
5
40
-1 用给的每个小字符串的价值拼出给的大字符串的最大价值 我自己还不会,先记一下同校大牛的代码,再研究
 #include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
const int N = ;
struct node
{
int l,r,v;
int nt;
}p[];
int h[],cnt;
void add(int l,int r,int v)
{
p[++cnt].l = l;
p[cnt].r = r;
p[cnt].v = v;
p[cnt].nt = h[l];
h[l]=cnt;
} char s[];
char c[][];
int dp[];
int l[],v[],n,len; int next[];
int slen; void getNext(int x)
{
int j, k;
j = ; k = -; next[] = -;
while(j < l[x])
if(k == - || c[x][j] == c[x][k])
next[++j] = ++k;
else
k = next[k];
}
void KMP_Count(int x)
{
int ans = ;
int i, j = ; if(slen == && l[x] == )
{
if(s[] == c[x][])
add(,,v[x]);
else return;
}
getNext(x);
for(i = ; i < slen; i++)
{
while(j > && s[i] != c[x][j])
j = next[j];
if(s[i] == c[x][j])
j++;
if(j == l[x])
{
add(i-l[x]+,i+,v[x]);
j = next[j];
}
}
}
int main()
{
while(scanf("%d%s",&n,s)!=EOF)
{
cnt = ;
memset(h,,sizeof(h));
slen = len = strlen(s);
memset(dp,-,sizeof(dp));
dp[]=;
for(int i=;i<=n;i++)
{
scanf("%s%d",c[i],&v[i]);
l[i] = strlen(c[i]);
KMP_Count(i);
}
for(int i=;i<len;i++)
{
if(dp[i]==-) continue;
for(int j=h[i];j>;j=p[j].nt)
{
dp[p[j].r] = max(dp[p[j].r], dp[i]+p[j].v);
}
}
printf("%d\n",dp[len]);
}
return ;
}
												

NBUT 1222 English Game 2010辽宁省赛的更多相关文章

  1. NBUT 1224 Happiness Hotel 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB The life of Little A is good, and, he managed to get enoug ...

  2. NBUT 1223 Friends number 2010辽宁省赛

    Time limit  1000 ms Memory limit   131072 kB Paula and Tai are couple. There are many stories betwee ...

  3. NBUT 1222 English Game(trie树+DP)

    [1222] English Game 时间限制: 1000 ms 内存限制: 131072 K 问题描写叙述 This English game is a simple English words ...

  4. NBUT 1221 Intermediary 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB It is widely known that any two strangers can get to know ...

  5. NBUT 1225 NEW RDSP MODE I 2010辽宁省赛

    Time limit  1000 ms Memory limit  131072 kB Little A has became fascinated with the game Dota recent ...

  6. NBUT 1218 You are my brother 2010辽宁省赛

    Time limit 1000 ms Memory limit 131072 kB Little A gets to know a new friend, Little B, recently. On ...

  7. NBUT 1220 SPY 2010辽宁省赛

    Time limit  1000 ms Memory limit  131072 kB The National Intelligence Council of X Nation receives a ...

  8. NBUT 1219 Time 2010辽宁省赛

    Time limit   1000 ms Memory limit   131072 kB Digital clock use 4 digits to express time, each digit ...

  9. NBUT 1217 Dinner 2010辽宁省赛

    Time limit  1000 ms Memory limit  32768 kB Little A is one member of ACM team. He had just won the g ...

随机推荐

  1. python 多线程队列

    ##Using Queue with multiprocessing – Chapter : Process Based Parallelism import multiprocessing impo ...

  2. js 几个重要的特性

    背景: 语法借鉴    java 函数借鉴    scheme 原型继承借鉴    self 正则表达式借鉴    Perl 1.动态语言 函数的定义和调用  形参与实参不需要一致 形参可由 argu ...

  3. 解决RDP连接不上

    1,开始->运行 regedit 删除注册表项目:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft, 删除MSLicensing.2,开始--运行 mstsc /v:( ...

  4. JS中Ajax的同步和异步

    ajax同步 : 意味着此时请求Server后,JS代码不再继续执行,等待Server返回后才继续往下执行. ajax异步 : 意味着此时请求Server后,JS代码继续执行,不管Server什么时候 ...

  5. 消息队列Kafka学习记录

    Kafka其实只是众多消息队列中的一种,对于Kafka的具体释义我这里就不多说了,详见:http://baike.baidu.com/link?url=HWFYszYuMdP_lueFH5bmYnlm ...

  6. SGE:qsub/qstat/qdel/qhost 任务投递和监控

    参考: Oracle Grid Engine qsub命令 SGE - qsub使用范例 SGE作业基本用法 qsub是最为稳定的底层任务投递系统,就是把一个脚本投递到集群的计算节点上运行. 注意,只 ...

  7. python模块——random模块(简单验证码实现)

    实现一个简单的验证码生成器 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" # Usage: 验证 ...

  8. 2月9日 Time and Date(Ruby基础) \n 2月10日 ,使用Proc,block对象化。

    20.1Time类 ,Data类 Time.now或者Time.new:获取当前时间. 相关Method: year, month, hour, min ,sec, wday: 一周中的第几天,返回整 ...

  9. Jersey 2.x 从Maven Archetype 创建一个新项目

    创建 Jersey 工程需要使用 Apache 的 Maven 软件工程和管理工具.所有的Jersey产品模块都可以在 Maven中央库 中找到.这样的话 Jersey 可以非常容易和其他基于 Mav ...

  10. Razor及HtmlHelper学习笔记

    Razor 不是编程语言.它是服务器端标记语言. 什么是Razor? Razor 是一种允许您向网页中嵌入基于服务器的代码(Visual Basic 和 C#)的标记语法. 当网页被写入浏览器时,基于 ...