很明显的DP状态了,设dp[i][j],设当前在状态点i,经过j步能得到的最大分值。也是从root直接扩展就可以了。

至于字符串,实在有点困难,开始想着记录路径,但后来发现路径从后往前回溯不一定是字典序最小,夭折。。。看别人的,发现直接就把字符串存下来,跪了,也对,毕竟才50个。

直接存字符串,比较,选最小,即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string.h>
#include <queue>
#include <cmath>
#include <map>
#include <vector>
#define LL __int64
using namespace std; const int Maxn=1110;
const int dictsize=26;
const int root=0;
const int inf=(1<<30);
int fail[Maxn],trie[Maxn][dictsize];
int hv[Maxn];
int tag[Maxn];
int head,tail,tot;
int que[Maxn];
char str[Maxn];
int n,m,anshv;
int dp[Maxn][55];
char dpc[Maxn][55][55];
char ans[55]; void Insert_trie(int s){
int p=0,i=0,index;
while(str[i]){
index=str[i]-'a';
if(trie[p][index]==-1) trie[p][index]=++tot;
p=trie[p][index];
i++;
}
tag[p]=s;
} void build_ac(){
head=tail=0;
que[tail++]=root;
while(head!=tail){
int tmp=que[head++];
int p=-1;
for(int i=0;i<dictsize;i++){
if(trie[tmp][i]!=-1){
if(tmp==root) fail[trie[tmp][i]]=root;
else{
p=fail[tmp];
while(p!=-1){
if(trie[p][i]!=-1){
fail[trie[tmp][i]]=trie[p][i];
break;
}
p=fail[p];
}
if(p==-1) fail[trie[tmp][i]]=root;
}
if(tag[fail[trie[tmp][i]]]>=0) tag[trie[tmp][i]]=tag[fail[trie[tmp][i]]];
que[tail++]=trie[tmp][i];
}
else{ //trie[tmp][i]==-1
if(tmp==root) trie[tmp][i]=root;
else{
p=fail[tmp];
while(p!=-1){
if(trie[p][i]!=-1){
trie[tmp][i]=trie[p][i];
break;
}
p=fail[p];
}
if(p==-1) trie[tmp][i]=root;
}
}
}
}
} 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;
} int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
head=tail=tot=0;
memset(fail,-1,sizeof(fail));
memset(trie,-1,sizeof(trie));
memset(tag,-1,sizeof(tag));
for(int i=0;i<m;i++){
scanf("%s",str);
Insert_trie(i);
}
for(int i=0;i<m;i++)
scanf("%d",&hv[i]);
build_ac();
// cout<<"YES"<<endl;
for(int i=0;i<=tot;i++){
for(int j=0;j<=n;j++){
strcpy(dpc[i][j],"");
dp[i][j]=-1;
}
}
dp[0][0]=0;
strcpy(ans,"");
anshv=0;
for(int j=0;j<n;j++){
for(int i=0;i<=tot;i++){
if(dp[i][j]>=0){
strcpy(str,dpc[i][j]);
int len=strlen(str);
for(int k=0;k<dictsize;k++){
int son=trie[i][k];
str[len]='a'+k;
str[len+1]='\0';
int tt=dp[i][j];
if(tag[son]>=0)
tt+=hv[tag[son]];
if(dp[son][j+1]<tt||(tt==dp[son][j+1]&&cmp(str,dpc[son][j+1]))){
dp[son][j+1]=tt;
strcpy(dpc[son][j+1],str);
}
if(dp[son][j+1]>anshv||dp[son][j+1]==anshv&&cmp(dpc[son][j+1],ans)){
anshv=dp[son][j+1];
strcpy(ans,dpc[son][j+1]);
}
}
}
}
}
puts(ans);
}
return 0;
}

  

HDU 2296的更多相关文章

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

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. HDU 2296 Ring (AC自动机+DP)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. HDU 2296 Ring ( Trie图 && DP && DP状态记录)

    题意 : 给出 m 个单词,每一个单词有一个权重,如果一个字符串包含了这些单词,那么意味着这个字符串拥有了其权重,问你构成长度为 n 且权重最大的字符串是什么 ( 若有权重相同的,则输出最短且字典序最 ...

  4. HDU 2296 Ring -----------AC自动机,其实我想说的是怎么快速打印字典序最小的路径

    大冥神的代码,以后能贴的机会估计就更少了....所以本着有就贴的好习惯,= =....直接贴 #include <bits/stdc++.h> using LL = long long ; ...

  5. Ring - HDU 2296(自动机+dp)

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

  6. HDU 2296:Ring

    Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a ...

  7. HDU 2296 Ring [AC自动机 DP 打印方案]

    Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

  8. Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出

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

  9. 【原创】AC自动机小结

    有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配.           AC自动机 其实 就是创建了一个状态的转移图,思想很 ...

随机推荐

  1. web认证方案

    web构建在http之上,而它又是无状态协议,如何控制用户访问服务器上的受限资源呢? 最原始你想法通过http基本认证,每次发请求时都向后台传递用户名密码信息,服务器每次收到请求后都先验证用户是否合法 ...

  2. 简述RTMPDump与编译移植

    RTMPDump主页 ,RTMPDump库主要包含三部分: 1.一个基本的客户端程序 2.两个服务器程序(rtmpsrv.rtmpsuck) 3.一个支持rtmp协议的库—librtmp 下载RTMP ...

  3. 曼哈顿距离 C++

    template <class T1, class T2>double ManhattanDistance(std::vector<T1> &inst1, std::v ...

  4. C# Socket通讯 本机多网卡,指定网卡通讯

    IPAddress ip = IPAddress.Parse("192.168.0.188"); IPAddress IPLocal = IPAddress.Parse(" ...

  5. phpmyadmin搭建

    phpadmin配置: 一.phpadmin安装及配置 1.解压phpadmin压缩包,并复制到 /usr/local/apache2/htdocs目录,重命名为dataManage 2.进入data ...

  6. 用js制作一个计算器

    使用js制作计算器 <!doctype html> <html lang="en"> <head> <meta charset=" ...

  7. jQuery中容易让人困惑的东西

    前言:jqueryt很灵活,太灵活了,可以说是他一个优点,也是他一个缺点,达到一种效果,十个人也许会用十种不同的方法来实现这个过程,结果一样,过程不一样,这到底是好,还是坏呢. 一,什么是jquery ...

  8. 针对Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解决方案

    背景:本项目使用JDK1.8 编译maven工程的时候出现如下错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-pl ...

  9. HDU_3999_二叉排序树

    The order of a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  10. apicloud常用分享方法

    app中经常会有分享的功能,不管是分享商品还是文字还是图片或者是发送给微信好友扣扣好友,一下做一总结. 分享的样式(分享所有的样式模块):MNActionButton. 在apicloud 中添加这个 ...