HDU 2296 Ring (AC自动机+DP)
Ring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1564 Accepted Submission(s): 487
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
//============================================================================
// Name : HDU.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std; int a[];
int dp[][];
char str[][][]; 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) < ;
} const int INF=0x3f3f3f3f;
struct Trie
{
int next[][],fail[],end[];
int root,L;
int newnode()
{
for(int i = ;i < ;i++)
next[L][i] = -;
end[L++] = -;
return L-;
}
void init()
{
L = ;
root = newnode();
}
void insert(char buf[],int id)
{
int len = strlen(buf);
int now = root;
for(int i = ;i < len;i++)
{
if(next[now][buf[i]-'a'] == -)
next[now][buf[i]-'a'] = newnode();
now = next[now][buf[i]-'a'];
}
end[now] = id;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ;i < ;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();
for(int i = ;i < ;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 solve(int n)
{
for(int i = ;i <= n;i++)
for(int j = ;j < L;j++)
dp[i][j] = -INF;
dp[][] = ;
strcpy(str[][],"");
char ans[];
strcpy(ans,"");
int Max = ;
char tmp[];
for(int i = ; i < n;i++)
for(int j = ;j < L;j++)
if(dp[i][j]>=)
{
strcpy(tmp,str[i][j]);
int len = strlen(tmp);
for(int k = ;k < ;k++)
{
int nxt=next[j][k];
tmp[len] = 'a'+k;
tmp[len+] = ;
int tt = dp[i][j];
if(end[nxt] != -)
tt+=a[end[nxt]]; if(dp[i+][nxt]<tt || (dp[i+][nxt]==tt && cmp(tmp,str[i+][nxt])))
{
dp[i+][nxt] = tt;
strcpy(str[i+][nxt],tmp);
if(tt > Max ||(tt==Max && cmp(tmp,ans)))
{
Max = tt;
strcpy(ans,tmp);
}
}
}
}
printf("%s\n",ans);
}
};
char buf[];
Trie ac;
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int T;
int n,m;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
ac.init();
for(int i = ;i < m;i++)
{
scanf("%s",buf);
ac.insert(buf,i);
}
for(int i = ;i < m;i++)
scanf("%d",&a[i]);
ac.build();
ac.solve(n);
}
return ;
}
HDU 2296 Ring (AC自动机+DP)的更多相关文章
- HDU 2296 Ring [AC自动机 DP 打印方案]
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...
- HDU 2296 Ring -----------AC自动机,其实我想说的是怎么快速打印字典序最小的路径
大冥神的代码,以后能贴的机会估计就更少了....所以本着有就贴的好习惯,= =....直接贴 #include <bits/stdc++.h> using LL = long long ; ...
- HDU2296 Ring —— AC自动机 + DP
题目链接:https://vjudge.net/problem/HDU-2296 Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- HDU 2296 Ring ( Trie图 && DP && DP状态记录)
题意 : 给出 m 个单词,每一个单词有一个权重,如果一个字符串包含了这些单词,那么意味着这个字符串拥有了其权重,问你构成长度为 n 且权重最大的字符串是什么 ( 若有权重相同的,则输出最短且字典序最 ...
- HDU-2296 Ring(AC自动机+DP)
题目大意:给出的m个字符串都有一个权值.用小写字母构造一个长度不超过n的字符串S,如果S包含子串s,则S获取s的权值.输出具有最大权值的最小字符串S. 题目分析:先建立AC自动机.定义状态dp(ste ...
- HDU2296 Ring(AC自动机 DP)
dp[i][j]表示行走i步到达j的最大值,dps[i][j]表示对应的串 状态转移方程如下: dp[i][chi[j][k]] = min(dp[i - 1][j] + sum[chi[j][k]] ...
- 对AC自动机+DP题的一些汇总与一丝总结 (2)
POJ 2778 DNA Sequence (1)题意 : 给出m个病毒串,问你由ATGC构成的长度为 n 且不包含这些病毒串的个数有多少个 关键字眼:不包含,个数,长度 DP[i][j] : 表示长 ...
- hdu 2296 aC自动机+dp(得到价值最大的字符串)
Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 2425 DNA repair (AC自动机+DP)
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 3341 Lost's revenge AC自动机+dp
Lost's revenge Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
随机推荐
- xmlsechema验证
//创建xmlDocument XmlDocument doc = new XmlDocument(); ...
- R语言字符串函数
字符串长度: nchar("hello world") #字符串连接:paste) <- value substr("abcdef", 2, 4)[1] ...
- Linux 下查看文件字符编码和转换编码
Linux 下查看文件字符编码和转换编码 如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而Linu ...
- 新浪实时股票数据接口http://hq.sinajs.cn/list=code
股票数据的获取目前有如下两种方法可以获取:1. http/javascript接口取数据2. web-service接口 1.http/javascript接口取数据1.1Sina股票数据接口以大秦铁 ...
- Qt 多线程学习
最近的项目上用到了关于多线程的知识,自己也比较感兴趣,所以就拿了那本<C++ GUI Qt4 编程>来学习. 这本书的第14章是关于多线程的知识,使用的Qt版本是Qt4.x.在下用的是最新 ...
- 学习:java设计模式—工厂模式
一.工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的. 工厂模式在<Java与模式>中分为三类: 1)简单工厂模式(Simple Facto ...
- H.264码流结构解析
from:http://wenku.baidu.com/link?url=hYQHJcAWUIS-8C7nSBbf-8lGagYGXKb5msVwQKWyXFAcPLU5gR4BKOVLrFOw4bX ...
- 最简单的视音频播放示例5:OpenGL播放RGB/YUV
本文记录OpenGL播放视频的技术.OpenGL是一个和Direct3D同一层面的技术.相比于Direct3D,OpenGL具有跨平台的优势.尽管在游戏领域,DirectX的影响力已渐渐超越OpenG ...
- Darwin Streaming server 的 Task 类
Darwin Streaming Server 是一个开放源代码的streaming server,对于streaming server的编程和软件结构有着一定的参考价值,它是使用C++写的,其中的并 ...
- HTML5全球普及加速:有望终结iOS与安卓界限【转】
HTML5或将成为未来移动互联网时代的颠覆者. 5年前,当第一台iPhone诞生时,乔布斯断然拒绝了FlashPlayer,而选择HTML5(新型网页标准)取代Flash的功能.他曾预言,“虽然现阶段 ...