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.
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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
2010辽宁省赛 NBUT 1222 English Game【字典树+DP】
原文地址:http://blog.csdn.net/acmore_xiong/article/details/47759921