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. ACMG遗传变异分类标准与指南

    2015年,美国权威机构——美国医学遗传学与基因组学学会(ACMG)编写和发布了<ACMG遗传变异分类标准与指南>.为帮助我国医疗工作者和遗传咨询从业者更好地理解ACMG遗传变异分类标准. ...

  2. diff详解,读懂diff结果-转载

    阅读目录 1.概述 2.diff如何工作,如何理解diff的执行结果 3.Normal模式 4.Context模式 5.Unified模式 6.比较目录 7.一些有用的参数 回到顶部 1.概述 本文将 ...

  3. 【Python】【环境搭建】

    [环境配置] Windows : http://blog.csdn.net/zhunianguo/article/details/53524792 [Pycharm] pyCharm最新2018激活码 ...

  4. Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并

    题目链接:http://codeforces.com/contest/381/problem/E  E. Sereja and Brackets time limit per test 1 secon ...

  5. c语言 快速排序

    #include<stdio.h> #include<stdlib.h> #define BUF_SIZE 10 void display(int array[], int m ...

  6. 百度编辑器 Ueditor 上传图片时打开文件夹的延迟问题,点击上传图片弹窗打开慢问题

      在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/ima ...

  7. Qt5.3.2(VS2010)_调试_进入Qt源码

    1.必须是 Debug模式 2. http://blog.csdn.net/mayenjoy/article/details/42535789 http://blog.csdn.net/goforwa ...

  8. C#中简单的文件操作实例

    using System; using System.IO; namespace Demo { class Program { static string tmpPath = @"D:/Lg ...

  9. delphi文件名获取方法

    取文件名 ExtractFileName(FileName); 取文件扩展名: ExtractFileExt(filename); 取文件名,不带扩展名: 方法一:   Function Extrac ...

  10. CF1082G Petya and Graph

    题意 定义图权 = 图中边权总和 - 图中点权总和(空图的图权=0),求 n 个点 m 条边的无向图最大权子图. 把边看成点,这个点与两个原图中的点连边.直接最小割求最大闭合子图即可.