HDU2296 Ring —— AC自动机 + DP
题目链接:https://vjudge.net/problem/HDU-2296
Ring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4429 Accepted Submission(s): 1474
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.
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'.
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.
7 2
love
ever
5 5
5 1
ab
5
abab
Sample 1: weight(love) = 5, weight(ever) = 5, so weight(lovever) = 5 + 5 = 10
Sample 2: weight(ab) = 2 * 5 = 10, so weight(abab) = 10
题意:
给出m个单词以及每个单词的价值,问长度不超过n的字符串价值最大是多少?求出这个字符串。
如果价值不同,取价值最大的;
如果价值相同,则取长度最短的;
如果价值相同,且长度相同,取字典序最小的。
题解:
1.把m个单词插入AC自动机中。
2.设dp[i][j]为:长度为i,且到达的状态为j(自动机上的状态)的最大价值。设path[i][j](string类型)为:长度为i,且到达的状态为j的最大价值的路径。
3.状态转移详情请看代码及注释。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MOD = 1e9+;
const int MAXN = 5e3+; int dp[][MAXN];
string path[][MAXN]; struct Trie
{
int sz, base;
int next[MAXN][], fail[MAXN], end[MAXN];
char ch[MAXN]; //用于记录结点(状态)上的字符
int root, L;
int newnode()
{
for(int i = ; i<sz; i++)
next[L][i] = -;
end[L++] = ;
return L-;
}
void init(int _sz, int _base)
{
sz = _sz;
base = _base;
L = ;
root = newnode();
}
void insert(char buf[], int val)
{
int len = strlen(buf);
int now = root;
for(int i = ; i<len; i++)
{
if(next[now][buf[i]-base] == -) next[now][buf[i]-base] = newnode();
now = next[now][buf[i]-base];
ch[now] = buf[i];
}
end[now] += val; // end用于记录值
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ; i<sz; i++)
{
if(next[root][i] == -) next[root][i] = root;
else fail[next[root][i]] = root, Q.push(next[root][i]);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
// end[now] += end[fail[now]]; //不用累加,因为在DP的时候会累加
for(int i = ; i<sz; i++)
{
if(next[now][i] == -) next[now][i] = next[fail[now]][i];
else fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]);
}
}
} void query(int n)
{
for(int i = ; i<=n; i++)
for(int j = ; j<L; j++)
dp[i][j] = -INF; dp[][root] = ; path[][root] = "";
for(int i = ; i<=n; i++)
for(int j = ; j<L; j++)
for(int k = ; k<sz; k++)
{
int newi = i+;
int newj = next[j][k];
/* 可更新的两种情况:
情况1:新串价值<旧串价值
情况2:新串价值=旧串价值 且 新串字典序<旧串字典序
注:因为长度都是i+1,所以不用考虑长度的情况
*/
if(dp[newi][newj]<dp[i][j]+end[newj] ||
(dp[newi][newj]==dp[i][j]+end[newj]&& path[newi][newj]>path[i][j]+ch[newj]))
{
dp[newi][newj] = dp[i][j]+end[newj];
path[newi][newj] = path[i][j]+ch[newj];
}
} int posi = , posj = , maxx = -;
for(int i = ; i<=n; i++)
for(int j = ; j<L; j++)
if( dp[i][j]>maxx ||
(dp[i][j]==maxx&&path[i][j].size()<path[posi][posj].size())||
(dp[i][j]==maxx&&i==posi&&path[i][j]<path[posi][posj]))
{
/* 可更新的三种情况:
情况1:新串价值<旧串价值
情况2:新串价值=旧串价值 且 新串长度<旧串长度
情况2:新串价值=旧串价值 且 新串长度=旧串长度 且 新串字典序<旧串字典序
*/
maxx = dp[i][j];
posi = i;
posj = j;
}
cout<<path[posi][posj]<<endl;
}
}; Trie ac;
char buf[][];
int H[];
int main()
{
int T, n, m;
scanf("%d", &T);
while(T--)
{
ac.init(, 'a');
scanf("%d%d", &n,&m);
for(int i = ; i<=m; i++) scanf("%s", buf[i]);
for(int i = ; i<=m; i++) scanf("%d", &H[i]);
for(int i = ; i<=m; i++) ac.insert(buf[i], H[i]); ac.build();
ac.query(n);
}
return ;
}
最新文章
- [转]Oracle存在则更新,不存在则插入
- subversion(SVN)常规使用
- Java内存管理:深入Java内存区域
- Head First HTML与CSS — 布局与定位
- day01:study HTTP协议
- python笔记:#007#变量
- dataTables 插件学习整理
- window.open post
- dede织梦后台页面及功能修改精简操作方法
- 2018-01-11 Antlr4实现数学四则运算
- js数组,字符串,json互相转换函数有哪些
- beta6
- 2018.08.28 集合堆栈机(模拟+STL)
- 实验一 《网络对抗技术》逆向及Bof技术
- N!(N的阶乘)最末位非0的求解方法
- 简明python教程 --C++程序员的视角(五):面向对象的编程
- HDU 2825 Wireless Password【AC自动机+DP】
- [BZOJ4872][六省联考2017]分手是祝愿(期望DP)
- adore-ng笔记和Linux普通用户提权
- java---springMVC与strutsMVC的区别