-
[1222] English Game
- 时间限制: 1000 ms 内存限制: 131072 K
- 链接:Click Here!
- 问题描述
- 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
- 来源
-
辽宁省赛2010
题意:
字典中有N个单词,每个单词都有一个权值,现在给你一个长度小于10000的字符串,你的任务就是在字典中找到若干个单词,组成这个字符串,并且要求权值最大。存在输出最大权值,不存在输出“-1”。
分析:
字典树+DP,用dp[i] 表示长度为i字符串的最大权值,那么很容易写出状态转移方程:
dp[i+j] = max{dp[i+j],dp[i] + 以第i+1个字符结尾的权值 | j > 0 并且i+j < 字符串的长度};
实现代码:
#include <cmath> #include <cstdio> #include <string> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define FIN freopen("input.txt","r",stdin) #define FOUT freopen("output.txt","w",stdout) typedef long long LL; int N, CNT, dp[10000+5]; char sbuf[10000 + 5], buf[35]; struct Node { int Num; Node* pNext[26]; Node() : Num(0) { memset(pNext, NULL, sizeof(pNext)); } } Memo[30 * 1000 + 5]; struct Trie { Node *pRoot, *ptr; void Insert(const char str[], const int Len, const int& w) { ptr = pRoot; int id; for(int i = 0; i < Len; i++) { id = str[i] - 'a'; if(NULL == ptr->pNext[id]) ptr->pNext[id] = &Memo[++CNT]; ptr = ptr->pNext[id]; } ptr->Num = w; } void Query(const char str[], const int L, const int p) { if(dp[p] < 0) return; ptr = pRoot; int id; for(int i = p, j = 1; i < L; i++, j++) { id = str[i] - 'a'; if(NULL == ptr->pNext[id]) return; ptr = ptr->pNext[id]; if(ptr->Num) dp[p + j] = max(dp[p + j], dp[p] + ptr->Num); } } } trie; void init() { memset(Memo, 0, sizeof(Memo)); memset(dp, -1, sizeof(dp)); CNT = 0; dp[0] = 0; trie.pRoot = &Memo[CNT++]; } int main() { // FIN; while(~scanf("%d %s", &N, sbuf)) { init(); int Len = strlen(sbuf), w; for(int i = 0; i < N; i++) { scanf("%s %d", buf, &w); trie.Insert(buf, strlen(buf), w); } for(int i = 0; i < Len; i++) { trie.Query(sbuf, Len, x); } printf("%d\n", dp[Len]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-28 00:48:14