hdu 2296 aC自动机+dp(得到价值最大的字符串)

时间:2023-01-12 07:57:48

Ring

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3180    Accepted Submission(s): 1033

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.
 
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'.

 
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.

 
Sample Input
2
7 2
love
ever
5 5
5 1
ab
5
 
Sample Output
lovever
abab
/*
hdu 2296 aC自动机+dp(得到价值最大的字符串) 给你m个子串,每个子串有自己的价值,让你求出长度为小于等于n的价值最大的字符串.
要求字符串的长度尽可能的小,长度相同时字典序最小即可 在生成状态转换图之后用,dp的思想解决.
用dp[i][j]记录长度为i时且状态为j时的最大值,与此同时用str[i][j][55]记录这个字符串
当价值相同时,对字符串进行比较即可. hhh-2016-04-24 17:13:36
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef unsigned long long ll;
typedef unsigned int ul;
const int mod = 20090717;
const int INF = 0x3f3f3f3f;
const int N = 12*105;
int tot;
int n,m;
char tp[55];
int dp[55][N];
char ans[55][N][55]; struct Matrix
{
int len;
int ma[111][111];
Matrix() {};
Matrix(int L)
{
len = L;
}
}; int Compare(char a[],char b[])
{
int len1 = strlen(a);
int len2 = strlen(b);
if(len1 != len2) return len1 > len2;
return strcmp(a,b);
} struct Tire
{
int nex[N][26],fail[N],ed[N];
int root,L;
int newnode()
{
for(int i = 0; i < 26; i++)
nex[L][i] = -1;
ed[L++] = -1;
return L-1;
} void ini()
{
L = 0,root = newnode();
memset(ed,-1,sizeof(ed));
} int cal(char ch)
{
if(ch == 'A')
return 0;
else if(ch == 'C')
return 1;
else if(ch == 'G')
return 2;
else if(ch == 'T')
return 3;
} void inser(char buf[],int val)
{
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++)
{
int ta = buf[i] - 'a';
if(nex[now][ta] == -1)
nex[now][ta] = newnode();
now = nex[now][ta];
}
ed[now] = val;
} void build()
{
queue<int >q;
fail[root] = root;
for(int i = 0; i < 26; i++)
if(nex[root][i] == -1)
nex[root][i] = root;
else
{
fail[nex[root][i]] = root;
q.push(nex[root][i]);
}
while(!q.empty())
{
int now = q.front();
q.pop();
// if(ed[fail[now]])
// ed[now] = ed[fail[now]];
for(int i = 0; i < 26; i++)
{
if(nex[now][i] == -1)
nex[now][i] = nex[fail[now]][i];
else
{
fail[nex[now][i]] = nex[fail[now]][i];
q.push(nex[now][i]);
}
}
}
} Matrix to_mat()
{
Matrix mat(L);
memset(mat.ma,0,sizeof(mat.ma));
for(int i = 0; i < L; i++)
{
for(int j = 0; j < 4; j++)
{
if(!ed[nex[i][j]])
mat.ma[i][nex[i][j]] ++;
}
}
return mat;
} void solve()
{
for(int j = 0; j <= n; j++)
{
for(int i = 0; i < N; i++)
dp[j][i] = -1;
}
dp[0][0] = 0;
char tan[55] = {""};
int tMax = 0;
strcpy(ans[0][0],"");
strcpy(tp,"");
for(int i = 1; i <= n; i++)
for(int j = 0; j < N; j++)
{
if(dp[i-1][j] >= 0)
{
strcpy(tp,ans[i-1][j]);
int len = strlen(tp);
for(int k = 0; k < 26; k++)
{
int t= dp[i-1][j];
if(ed[nex[j][k]] > 0)
t += ed[nex[j][k]];
tp[len] = 'a'+k;
tp[len+1] = 0;
if(t > dp[i][nex[j][k]] || (t == dp[i][nex[j][k]] && Compare(ans[i][nex[j][k]],tp) > 0))
{
strcpy(ans[i][nex[j][k]],tp);
dp[i][nex[j][k]] = t; }
if(t >tMax || (tMax == t && Compare(tan,tp) > 0))
{
tMax = t;
strcpy(tan,tp);
}
}
}
}
// printf("%d\n",tMax);
printf("%s\n",tan);
}
}; Tire ac;
char buf[105][12]; int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
ac.ini();
for(int i = 0; i < m; i++)
{
scanf("%s",buf[i]);
}
int x;
for(int i = 0; i < m; i++)
{
scanf("%d",&x);
ac.inser(buf[i],x);
}
ac.build();
ac.solve();
}
return 0;
}

  

hdu 2296 aC自动机+dp(得到价值最大的字符串)的更多相关文章

  1. hdu 2457&lpar;ac自动机&plus;dp&rpar;

    题意:容易理解... 分析:这是一道比较简单的ac自动机+dp的题了,直接上代码. 代码实现: #include<stdio.h> #include<string.h> #in ...

  2. Ring HDU - 2296 AC自动机&plus;简单DP和恶心的方案输出

    题意: 就是现在给出m个串,每个串都有一个权值,现在你要找到一个长度不超过n的字符串, 其中之前的m个串每出现一次就算一次那个字符串的权值, 求能找到的最大权值的字符串,如果存在多个解,输出最短的字典 ...

  3. HDU 2825 AC自动机&plus;DP

    题意:一个密码,长度为 n,然后有m个magic words,这个密码至少由k个magic words组成. 问这个密码可能出现的总数. 思路:首先构造AC自动机,由于m很小,才10 ,我们可以使用二 ...

  4. Lost&&num;39&semi;s revenge HDU - 3341 AC自动机&plus;DP&lpar;需要学会如何优雅的压缩状态&rpar;

    题意: 给你n个子串和一个母串,让你重排母串最多能得到多少个子串出现在重排后的母串中. 首先第一步肯定是获取母串中每个字母出现的次数,只有A T C G四种. 这个很容易想到一个dp状态dp[i][A ...

  5. DNA repair HDU - 2457 AC自动机&plus;DP

    题意: 给你N个模板串,并且给你一个文本串, 现在问你这个文本串最少需要改变几个字符才能使得它不包含任何模板串. (以上字符只由A,T,G,C构成) 题解: 刚开始做这一题的时候表示很懵逼,好像没有学 ...

  6. Ring - HDU 2296(自动机&plus;dp)

    题目大意:斯蒂文想送给他女盆友一个戒指,并且他想在戒指上刻一些字,他非常了解他女盆友喜欢什么单词,比如"love""forvevr"....并且他还把女盆友喜欢 ...

  7. HDU 2425 DNA repair (AC自动机&plus;DP)

    DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU 3341 Lost&&num;39&semi;s revenge AC自动机&plus;dp

    Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  9. hdu 2825 aC自动机&plus;状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. 好文分享&lowbar;java堆栈的区别

    Java Heap Memory Heap memory is used by java runtime to allocate memory to Objects and JRE classes. ...

  2. JS操作未跨域iframe里的DOM

    这里简单说明两个方法,都是未跨域情况下在index.html内操作b.html内的 DOM. 如:index.html内引入iframe,在index内如何用JS操作iframe内的DOM元素? 先贴 ...

  3. 全能直播王PC版-0707-full&lowbar;codecs

    全能直播王是一款高清流畅的全平台的电视直播应用,让您随时随地看高清电视直播. [全能特色] 1. 收录全国1400多个直播频道,包括湖南卫视.江苏卫视.浙江卫视等热门频道. 2. 频道多线路自动选择, ...

  4. 数组的foreach方法和jQuery中的each方法

    /* * 数组的forEach方法: * 1.返回给回调的参数先是值,然后是下标 * 2.回调函数执行时内部的this指向window * */ /*var arr = [1,2,3,4,5]; ar ...

  5. 调试WEB APP多设备浏览器&lpar;转&rpar;

      方法:adobe shadow  \ opera远程调试\ weinre adobe shadow: 我们经常使用Firefox的firebug或者Chrome的开发人员工具进行Web调试页面,J ...

  6. vmware产品

    https://my.vmware.com/cn/group/vmware/info?slug=datacenter_cloud_infrastructure/vmware_vsphere/5_5#o ...

  7. UI基础 - UILabel

    //根据文字获取size - (CGSize)getSizeWithstring:(NSString *)string { CGSize maxSize = CGSizeMake(320, 320); ...

  8. PAT乙级1001&period; 害死人不偿命的&lpar;3n&plus;1&rpar;猜想 &lpar;15&rpar;

    卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去,最后一定在某一步得到n=1.卡拉兹在1950年的世界数 ...

  9. Maven使用本地jar包(两种方式)

    有些项目会用到一些Maven库上没有的jar包,这就需要我们自己引入了 这种情况有两种办法: 第一种方式,在pom文件中引用时使用本地路径: 首先把jar包放到项目中: 然后在pom文件中引入: &l ...

  10. SQL 必知必会&&num;183&semi;笔记&lt&semi;14&gt&semi;更新和删除数据

    1. 更新数据 基本的UPDATE语句,由三部分组成: 要更新的表 列名和它们的新值 确定要更新那些行的过滤条件 更新单列示例: UPDATE Customers SET cust_email = ' ...