大冥神的代码,以后能贴的机会估计就更少了。。。。所以本着有就贴的好习惯,= =。。。。直接贴

#include <bits/stdc++.h>
using LL = long long ;
#define ALL(v) (v).begin(),(v).end()
#define showtime fprintf(stderr,"time = %.15f\n",clock() / (double)CLOCKS_PER_SEC) char str[100][11];
int n,m;
int weight[100]; const int N = 10 * 100 + 5;
int go[N][26],val[N],fail[N],tot; int get_node() {
memset(go[tot],-1,sizeof(go[tot]));
val[tot] = 0;
return tot ++;
} int dp[50 + 1][N],pre[50 + 1][N],pc[50 + 1][N];
std::vector<int> order[50 + 1]; void insert(char *s,int w) {
int u = 0;
for ( ; *s; ++ s) {
int c = *s - 'a';
if (go[u][c] == -1)
go[u][c] = get_node();
u = go[u][c];
}
val[u] += w;
} std::string work() {
tot = 0;
get_node();
for (int i = 0; i < m; ++ i) {
insert(str[i],weight[i]);
}
std::vector<int> vec{0};
fail[0] = -1;
for (int I = 0; I < (int)vec.size(); ++ I) {
int u = vec[I];
for (int c = 0; c < 26; ++ c) {
if (go[u][c] == -1) continue;
int v = go[u][c];
int f = fail[u];
while (f != -1 && go[f][c] == -1) f = fail[f];
fail[v] = f == -1 ? 0 : go[f][c];
vec.push_back(v);
}
}
for (int I = (int)vec.size() - 1; I >= 0; -- I) {
int u = vec[I];
for (int c = 0; c < 26; ++ c) {
int f = u;
while (f != -1 && go[f][c] == -1) f = fail[f];
go[u][c] = f == -1 ? -1 : go[f][c];
}
for (int f = fail[u]; f != -1; f = fail[f]) {
val[u] += val[f];
}
}
memset(dp,-1,sizeof(dp));
memset(pre,-1,sizeof(pre));
dp[0][0] = 0;
for (int i = 0; i <= n; ++ i) {
order[i].clear();
}
order[0].push_back(0);
for (int i = 0; i < n; ++ i) {
for (int u : order[i]) {
for (int c = 0; c < 26; ++ c) {
if (go[u][c] == -1) continue;
int v = go[u][c];
dp[i + 1][v] = std::max(dp[i + 1][v],dp[i][u] + val[v]);
}
}
for (int u : order[i]) {
for (int c = 0; c < 26; ++ c) {
if (go[u][c] == -1) continue;
int v = go[u][c];
if (dp[i + 1][v] == dp[i][u] + val[v] && pre[i + 1][v] == -1) {
pre[i + 1][v] = u;
pc[i + 1][v] = c;
order[i + 1].push_back(v);
}
}
}
} int ai = 0,au = 0;
for (int i = 0; i <= n; ++ i) {
for (int u : order[i]) {
if (dp[i][u] > dp[ai][au]) {
ai = i;
au = u;
}
}
}
std::string ret;
while (ai) {
ret.push_back(pc[ai][au] + 'a');
au = pre[ai][au];
-- ai;
}
std::reverse(ALL(ret));
return ret;
} int main() {
int cas;
scanf("%d",&cas);
while (cas--) {
scanf("%d%d",&n,&m);
for (int i = 0; i < m; ++ i) {
scanf("%s",str[i]);
}
for (int i = 0; i < m; ++ i) {
scanf("%d",weight + i);
}
printf("%s\n",work().c_str());
}
}

  

HDU 2296 Ring -----------AC自动机,其实我想说的是怎么快速打印字典序最小的路径的更多相关文章

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

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

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

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

  3. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

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

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

  5. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  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. HDU2296 Ring —— AC自动机 + DP

    题目链接:https://vjudge.net/problem/HDU-2296 Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  8. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

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

    题目大意:给出的m个字符串都有一个权值.用小写字母构造一个长度不超过n的字符串S,如果S包含子串s,则S获取s的权值.输出具有最大权值的最小字符串S. 题目分析:先建立AC自动机.定义状态dp(ste ...

随机推荐

  1. webform开发基础

    ASP.NET WebForm C/S(Client/Server):客户端服务器 B/S(Browser/Server):浏览器服务器 C/S和B/S的区别: 首先必须强调的是C/S和B/S并没有本 ...

  2. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  3. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  4. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  5. [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  6. C#进阶系列——WebApi 异常处理解决方案

    前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定不陌生,记得在介绍 AOP 的时候 ...

  7. Java反射机制

    Java反射机制 一:什么事反射机制 简单地说,就是程序运行时能够通过反射的到类的所有信息,只需要获得类名,方法名,属性名. 二:为什么要用反射:     静态编译:在编译时确定类型,绑定对象,即通过 ...

  8. PHP中多站点的设置

    当初配置多站点的问题,整了几个小时没整好,第二天一看才发现是单词拼错了(无奈..),所以能复制的一定复制!!! 1,首先进行Apache的配置(先复制一份进行备份,以免丢失). 进入Apache文件下 ...

  9. AngularJS指令

    1. AngularJS指令的特点: AngularJS通过被称为指令的新属性来扩展HTML,指令的前缀为ng-. AngularJS通过内置的指令来为应用添加功能. AngularJS允许你自定义指 ...

  10. FragmentTabHost简单保存状态的方法

    private View rootView;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container ...