HDU2296--Ring--AC自动机+DP
来源:互联网 发布:社保卡照片制作软件 编辑:程序博客网 时间:2024/06/07 23:14
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题意:给出串的长度上限,和带权的单词。求最大权的串,长度尽量小的前提下字典序尽量小。思路:dp[i][j]表示当前状态为j,在考虑第i个位置填什么字符。先将所给带权字符串构建AC自动机,那么对于每个状态添加一个字符会转移到另一个状态也就是dp[i][j]的值可以根据dp[i+1][j添加字符后转到的状态]得到。也就是说,从后面忘前面填字母(这样可以满足字典序最小)#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 1080int dp[58][maxn];int pos[58][maxn];char str[108][12];int Cnt,first,rear;int key[108];struct node{node * fail;node * next[26];int end,cnt;node(){end = 0;cnt = Cnt++;for(int i = 0;i < 26;i++)next[i] = NULL;}}*q[maxn],*qq[maxn];void Creat_Tire(char * s,node * root,int num){node * p = root;int len = strlen(s);for(int i = 0;i < len;i++){int id = s[i] - 'a';if(p -> next[id] == NULL)p -> next[id] = qq[Cnt-1] = new node();p = p -> next[id];}p -> end += num;}void build_ac_automation(node * root){node * p = NULL;node * temp = NULL;q[rear++] = root;while(first < rear){p = q[first++];for(int i = 0;i < 26;i++){if(p -> next[i] != NULL){if(p == root){p -> next[i] -> fail = root;}else {temp = p -> fail;while(temp != NULL){if(temp -> next[i] != NULL){p -> next[i] -> fail = temp -> next[i];p -> next[i] -> end += temp -> next[i] -> end;break;}temp = temp -> fail;}}q[rear++] = p -> next[i];}else {if(p == root){p -> next[i] = root;}else {p -> next[i] = p -> fail -> next[i];}}}}}int main(){//freopen("in.txt","r",stdin);int t;scanf("%d",&t);while(t--){Cnt = first = rear = 0;node * root = new node();qq[0] = root;int n,m;scanf("%d%d",&n,&m);for(int i = 1;i <= m;i++)scanf("%s",str[i]);for(int i = 1;i <= m;i++){scanf("%d",&key[i]);Creat_Tire(str[i],root,key[i]);}build_ac_automation(root);memset(dp,0,sizeof(dp));for(int i = n;i >= 1;i--){for(int j = 0;j < Cnt;j++){int add = 0,set = 0;for(int k = 0;k < 26;k++){if(qq[j]->next[k]->end+dp[i+1][qq[j]->next[k]->cnt] > add){set = k;add = qq[j]->next[k]->end+dp[i+1][qq[j]->next[k]->cnt];}}pos[i][j] = set;dp[i][j] += add;}}int fuckpos = n+1,sum = 0;for(int i = n+1;i >= 1;i--){if(dp[i][0] > sum){sum = dp[i][0];fuckpos = i;}}int state = 0;for(int i = fuckpos;i <= n;i++){printf("%c",pos[i][state]+'a');state = qq[state] -> next[pos[i][state]] -> cnt;}puts("");}return 0;}
0 0
- HDU2296--Ring--AC自动机+DP
- hdu2296---Ring(AC自动机+dp)
- HDU2296 Ring AC自动机+DP
- hdu2296 Ring (AC自动机+dp)
- AC自动机+dp打印路径 hdu2296 Ring
- AC自动机+DP+hdu2296
- hdu2296(AC自动机+DP)
- hdu2296 AC自动机+DP
- hdu2296-(AC自动机+DP)
- HDU2296(AC自动机+DP)
- hdu2296 AC自动机
- HDU 2296 Ring(AC自动机+DP)
- HDU 2296 Ring(AC自动机+DP)
- HDU 2296 Ring(AC自动机+DP)
- 【HDU】2296 Ring AC自动机+DP
- hdu 2296 Ring(AC自动机+DP)
- hdu 2296 Ring AC自动机+DP
- hdu 2296 Ring (ac自动机+dp)
- IE和火狐等浏览器html标签显示不同的解决办法
- Matlab 集群计算使用心得
- 谈ObjC对象的两段构造模式【转】
- 关于Cocos2d-x 3.0正式版 粒子问题在IOS上正常显示,在Android下有问题的解决方案
- 国防科大校赛I题: Prime Summation
- HDU2296--Ring--AC自动机+DP
- 当腾讯把微信开发者逼到绝境,你们如何突围?
- C/C++文件操作转载自http://www.cnblogs.com/kzloser/archive/2012/07/16/2593133.html#b1_2
- 项目记录
- struts2国际化
- AptGet/Howto
- 关于回车和换行
- 黑马程序员 IO流 文件的分割与合并
- 实验一 JSP基本语法