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. Jmeter性能测试之一 性能测试的流程和步骤介绍

    Step1: 知道在架构上,你要做的性能测试要cover几个部分,如下图,性能测试从用户角度,PC端之后都要要考虑进行的,例如网络,app server,Database等等 N1+N2+N3+N4 ...

  2. 使用 data.table 包操作数据

    在第一节中,我们回顾了许多用于操作数据框的内置函数.然后,了解了 sqldf 扩展包,它使得简单的数据查询和统计变得更简便.然而,两种方法都有各自的局限性.使用内置函数可能既繁琐又缓慢,而相对于各式各 ...

  3. [ios]sqlite轻量级数据库学习连接

    SQLLite (一)基本介绍 http://blog.csdn.net/lyrebing/article/details/8224431 SQLLite (二) :sqlite3_open, sql ...

  4. 【Golang 接口自动化05】使用yml管理自动化用例

    我们在前面几篇文章中学习怎么发送数据请求,怎么处理解析接口返回的结果,接下来我们一起来学习怎么进行测试用例管理,今天我们介绍的是使用yml文件进行用例管理,所以首先我们一起来了解一下YAML和它的简单 ...

  5. RabbitMQ入门_06_深入了解ack

    A. Delivery Tag 参考资料:https://www.rabbitmq.com/confirms.html 仔细查看一下 Consumer 的回调方法: public void handl ...

  6. Codeforces 847I - Noise Level

    847I - Noise Level 思路:bfs. 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...

  7. Unity 代码优化

    1.不用的代码删除掉,因为即使不用的代码也会 IL2Cpp. 2.MonoBehaviour 的方法是空方法,特别是Update方法,删除掉,会有性能消耗. 3.Unity 中 override 的方 ...

  8. codeforces 559a//Gerald's Hexagon// Codeforces Round #313(Div. 1)

    题意:面积是sqrt(3)/4的多少倍? 做延长线 #pragma comment(linker,"/STACK:1024000000,1024000000") #include& ...

  9. 『PyTorch x TensorFlow』第八弹_基本nn.Module层函数

    『TensorFlow』网络操作API_上 『TensorFlow』网络操作API_中 『TensorFlow』网络操作API_下 之前也说过,tf 和 t 的层本质区别就是 tf 的是层函数,调用即 ...

  10. 关于最短路的想法&&问题

    今天本想水几道floyd却被坑了:注意考虑<重边>!!!!!!!!!!!!!!! 小心图里出现的重边,如果不处理的话,必然WA!构图时一定要仔细!