HDU2296-Ring
来源:互联网 发布:折800网站源码 编辑:程序博客网 时间:2024/06/07 17:14
Ring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4116 Accepted Submission(s): 1360
Problem Description
For the hope of a forever love, Steven is planning to send a ring to Jane with a romantic string engraved on. The string's length should not exceed N. The careful Steven knows Jane so deeply that he knows her favorite words, such as "love", "forever". Also, he knows the value of each word. The higher value a word has the more joy Jane will get when see it.
The weight of a word is defined as its appeared times in the romantic string multiply by its value, while the weight of the romantic string is defined as the sum of all words' weight. You should output the string making its weight maximal.
The weight of a word is defined as its appeared times in the romantic string multiply by its value, while the weight of the romantic string is defined as the sum of all words' weight. You should output the string making its weight maximal.
Input
The input consists of several test cases. The first line of input consists of an integer T, indicating the number of test cases. Each test case starts with a line consisting of two integers: N, M, indicating the string's length and the number of Jane's favorite words. Each of the following M lines consists of a favorite word Si. The last line of each test case consists of M integers, while the i-th number indicates the value of Si.
Technical Specification
1. T ≤ 15
2. 0 < N ≤ 50, 0 < M ≤ 100.
3. The length of each word is less than 11 and bigger than 0.
4. 1 ≤ Hi ≤ 100.
5. All the words in the input are different.
6. All the words just consist of 'a' - 'z'.
Technical Specification
1. T ≤ 15
2. 0 < N ≤ 50, 0 < M ≤ 100.
3. The length of each word is less than 11 and bigger than 0.
4. 1 ≤ Hi ≤ 100.
5. All the words in the input are different.
6. All the words just consist of 'a' - 'z'.
Output
For each test case, output the string to engrave on a single line.
If there's more than one possible answer, first output the shortest one. If there are still multiple solutions, output the smallest in lexicographically order.
The answer may be an empty string.
If there's more than one possible answer, first output the shortest one. If there are still multiple solutions, output the smallest in lexicographically order.
The answer may be an empty string.
Sample Input
27 2loveever5 55 1ab5
Sample Output
loveverababHintSample 1: weight(love) = 5, weight(ever) = 5, so weight(lovever) = 5 + 5 = 10Sample 2: weight(ab) = 2 * 5 = 10, so weight(abab) = 10
Source
The 4th Baidu Cup final
Recommend
lcy
题意:给你m个模板串,第i个模板串每出现一次会产生vi的价值,要求构造一个长度小于等于n的串使得价值最大,有多种方案的时候先选长度最小的,再选字典序最小的
解题思路:ac自动机,dp[i][j]表示长度问i的字符串到达j状态的最大值,a[i][j]表示对应的串
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cctype>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;char ch[15], a[55][1200][55], ans[55], temp[55];int n, m, ma;int dp[55][1200], x[120];bool cmp(char s1[], char s2[]){int len1 = strlen(s1);int len2 = strlen(s2);if (len1 != len2)return len1 < len2;else return strcmp(s1, s2) < 0;}struct Trie{int next[1200][26], fail[1200], flag[1200];int root, tot;int newnode(){for (int i = 0; i < 26; i++) next[tot][i] = -1;flag[tot++] = 0;return tot - 1;}void init(){tot = 0;root = newnode();}void insert(char ch[], int id){int k = root;for (int i = 0; ch[i]; i++){if (next[k][ch[i] - 'a'] == -1) next[k][ch[i] - 'a'] = newnode();k = next[k][ch[i] - 'a'];}flag[k] = id;}void build(){queue<int>q;fail[root] = root;for (int i = 0; i < 26; i++){if (next[root][i] == -1) next[root][i] = root;else{fail[next[root][i]] = root;q.push(next[root][i]);}}while (!q.empty()){int pre = q.front();q.pop();for (int i = 0; i < 26; i++){if (next[pre][i] == -1) next[pre][i] = next[fail[pre]][i];else{fail[next[pre][i]] = next[fail[pre]][i];q.push(next[pre][i]);}}}}void solve(){for (int i = 0; i <= n; i++)for (int j = 0; j < tot; j++)dp[i][j] = -INF;dp[0][0] = 0;strcpy(a[0][0], "");ma = 0;strcpy(ans, "");for (int i = 0; i < n; i++)for (int j = 0; j < tot; j++)if (dp[i][j] >= 0){strcpy(temp, a[i][j]);int len = strlen(temp);for (int k = 0; k < 26; k++){temp[len] = 'a' + k;temp[len + 1] = 0;int p = dp[i][j] + x[flag[next[j][k]]];if (p > dp[i + 1][next[j][k]] || (dp[i + 1][next[j][k]] == p&&cmp(temp, a[i + 1][next[j][k]]))){dp[i + 1][next[j][k]] = p;strcpy(a[i + 1][next[j][k]], temp);if (ma < p || (ma == p&&cmp(temp, ans))){strcpy(ans, temp);ma = p;}}}}printf("%s\n", ans);}void debug(){for (int i = 0; i < tot; i++){printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], flag[i]);for (int j = 0; j < 26; j++) printf("%2d", next[i][j]);printf("]\n");}}}ac;int main(){int t;scanf("%d", &t);while (t--){scanf("%d %d", &n, &m);ac.init();for (int i = 1; i <= m; i++){scanf("%s", ch);ac.insert(ch, i);}for (int i = 1; i <= m; i++) scanf("%d", &x[i]);ac.build();ac.solve();}return 0;}
阅读全文
0 0
- hdu2296 Ring
- HDU2296 Ring
- HDU2296-Ring
- HDU2296--Ring--AC自动机+DP
- hdu2296---Ring(AC自动机+dp)
- HDU2296 Ring AC自动机+DP
- hdu2296 Ring (AC自动机+dp)
- AC自动机+dp打印路径 hdu2296 Ring
- hdu2296
- hdu2296
- Ring
- ring
- hdu2296 AC自动机
- AC自动机+DP+hdu2296
- hdu2296(AC自动机+DP)
- hdu2296 AC自动机+DP
- hdu2296-(AC自动机+DP)
- HDU2296(AC自动机+DP)
- C++基础知识点补漏
- Android自定义View专题六-PathMeasure实战,仿小红书动态标签
- 朴素贝叶斯 续
- 6.4
- Android数据库面向对象之增、删、改、查
- HDU2296-Ring
- 判断1000年---2000年之间的闰年
- spring 自定义注解及使用
- 单片机C51汇编指令之寻址方式学习心得
- getopt 函数
- 最近的计划
- 五、java项目常用工具类之properties提取数据工具类
- Ptython编程中忽略警告信息
- JavaScript08 分支结构