2010辽宁省赛 NBUT 1222 English Game【字典树+DP】
来源:互联网 发布:js格式化代码插件 编辑:程序博客网 时间:2024/05/22 06:30
[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 aaaaa 23 aaaa 2aa 5aaa 64 abca 1bc 2ab 4c 13 abcdab 10bc 20cd 303 abcdcd 100abc 1000bcd 10000
- 样例输出
87540-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;}
2 1
- 2010辽宁省赛 NBUT 1222 English Game【字典树+DP】
- NBUT 1222 English Game(trie树+DP)
- NUBT 1222 English Game 【字典树 + dp】
- NEFU English Game 字符串 dp 字典树
- 2010年辽宁省赛 NBUT 1218【DFS实现树的遍历与更新】
- English Game 字典树排序 字符串处理
- NEU1007: English Game 字典树排序 字符串处理
- 辽宁省赛2010
- 辽宁省赛2010
- 辽宁省赛2010 G
- dp+trie nbut1222 English Game
- 2014辽宁省赛 Picking Cabbage 状压dp
- 2010 辽宁省省赛 题解
- Friends number 2010辽宁省赛
- NOJ1222-English Game(AC自动机+dp)
- 2015 辽宁省赛C题 dp+单调队列优化
- 2010 ACM/ICPC 辽宁省赛 Dinner
- 【9018p1316】SPY(辽宁省赛2010)
- 如何有效地记忆与学习
- uva 1175 Ladies' Choice (稳定婚姻问题)
- Android 笔记1
- jQuery转义
- Android启动界面之SplashActivity的设计
- 2010辽宁省赛 NBUT 1222 English Game【字典树+DP】
- hdu 2566 统计硬币
- 黑马程序员----异常处理机制的概括
- Java基础50问与答
- spring的IOC和DI
- Kafka命令行常用命令说明
- RMQ(st在线算法模板)
- 01 java.lang.Object
- xxxccc