HDU 2296
很明显的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的更多相关文章
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 2296 Ring (AC自动机+DP)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 2296 Ring ( Trie图 && DP && DP状态记录)
题意 : 给出 m 个单词,每一个单词有一个权重,如果一个字符串包含了这些单词,那么意味着这个字符串拥有了其权重,问你构成长度为 n 且权重最大的字符串是什么 ( 若有权重相同的,则输出最短且字典序最 ...
- HDU 2296 Ring -----------AC自动机,其实我想说的是怎么快速打印字典序最小的路径
大冥神的代码,以后能贴的机会估计就更少了....所以本着有就贴的好习惯,= =....直接贴 #include <bits/stdc++.h> using LL = long long ; ...
- Ring - HDU 2296(自动机+dp)
题目大意:斯蒂文想送给他女盆友一个戒指,并且他想在戒指上刻一些字,他非常了解他女盆友喜欢什么单词,比如"love""forvevr"....并且他还把女盆友喜欢 ...
- HDU 2296:Ring
Problem Description For the hope of a forever love, Steven is planning to send a ring to Jane with a ...
- HDU 2296 Ring [AC自动机 DP 打印方案]
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...
- Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出
题意: 就是现在给出m个串,每个串都有一个权值,现在你要找到一个长度不超过n的字符串, 其中之前的m个串每出现一次就算一次那个字符串的权值, 求能找到的最大权值的字符串,如果存在多个解,输出最短的字典 ...
- 【原创】AC自动机小结
有了KMP和Trie的基础,就可以学习神奇的AC自动机了.AC自动机其实就是在Trie树上实现KMP,可以完成多模式串的匹配. AC自动机 其实 就是创建了一个状态的转移图,思想很 ...
随机推荐
- Spring Boot、微服务架构和大数据
一文读懂 Spring Boot.微服务架构和大数据治理三者之间的故事 https://www.cnblogs.com/ityouknow/p/9034377.html 微服务架构 微服务的诞生并非偶 ...
- bzoj 1093 [ ZJOI 2007 ] 最大半连通子图 —— 拓扑+DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093 先缩点,然后就是找最长链,DP一下即可: 注意缩点后的重边!会导致重复计算答案. 代码 ...
- go的常量与变量
一.常量 1.1 定义 常量使用关键字const 定义,用于存储不会变化的数据 定义方法 const identifier [type] = value package main // 常量定义 co ...
- Blender之Property
目标 [x] 总结Blender插件之属性bpy.props 总结 bpy.props bpy.props模块用来扩展Blender内置数据的属性. 这些函数的结果用于为用Blender注册的类分配属 ...
- POJ 1111(数字很吉利嘛) 简单BFS
Image Perimeters Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8594 Accepted: 5145 Desc ...
- C#利用ICSharpCode将远程文件打包并下载
应用于ASP.NET MVC中 方法主体代码: public void GetFilesByOrder(string Order_ID, string IntNumber) { MemoryStrea ...
- 利用windbg获取dump的dll文件
根据堆栈对应的地址查找其对应的Module ID,然后将对应的Module保存. !IP2MD 命令从托管函数中获取 MethodDesc 结构地址. !dumpmodule 1caa50 下面的命令 ...
- [转]Oracle 存储过程语法
转自:http://www.cnblogs.com/chuncn/archive/2009/04/29/1381282.html 存储过程 1 CREATE OR REPLACE PROCEDURE ...
- MySql-Connector for NET 连接驱动选择
尝试在Visual Studio2010, 2012环境下链接Mysql, 为啥不直接在App.config里面写字符串, 当然是可以,但是当你想用EF 的时候,必须要有个数据源, 首先在[服务资源管 ...
- vue-cli脚手架安装过程(精简版)
1:打开node的控制台,并找到对应的文件夹 2:检查node的版本 node -v 3:检查npm的版本 npm -v 4:检查cnpm的版本 cnpm -v 5:安装全局及脚手架 cnpm ins ...