【HDU】2296 Ring AC自动机+DP

来源:互联网 发布:编程文件命名规则 编辑:程序博客网 时间:2024/04/30 03:01

Ring

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1989    Accepted Submission(s): 624


Problem Description
For the hope of a forever love, Steven is planning to send a ring to Jane with a romantic string engraved on. The string's length should not exceed N. The careful Steven knows Jane so deeply that he knows her favorite words, such as "love", "forever". Also, he knows the value of each word. The higher value a word has the more joy Jane will get when see it.
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.

 

Input
The input consists of several test cases. The first line of input consists of an integer T, indicating the number of test cases. Each test case starts with a line consisting of two integers: N, M, indicating the string's length and the number of Jane's favorite words. Each of the following M lines consists of a favorite word Si. The last line of each test case consists of M integers, while the i-th number indicates the value of Si.
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'.
 

Output
For each test case, output the string to engrave on a single line.
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.
 

Sample Input
27 2loveever5 55 1ab5
 

Sample Output
loveverabab
Hint
Sample 1: weight(love) = 5, weight(ever) = 5, so weight(lovever) = 5 + 5 = 10Sample 2: weight(ab) = 2 * 5 = 10, so weight(abab) = 10
 

Source
The 4th Baidu Cup final

传送门:【HDU】2296 Ring

题目大意:
你有m个单词,每个单词有一个价值,问你组成最长不超过n的字符串能得到的最大价值。保证单词无重复且允许单词重叠。

题目分析:
用AC自动机构造转移方程,然后DP。
dp[ i ][ j ]表示长度为 i,状态为 j 的字符串的最大价值。
则dp[ i ][ j ] = max{ dp[ i ][ pre ] + val ,pre是能到达 j 的所有状态,val是以正在枚举字符为结尾的单词的价值}。
然后如果只输出最大值,那么就是普通的DP,可是题目要求我们是字典序最小解,那么就需要多花一些功夫。
用path[ i ][ j ][ len ]表示长度为 i,状态为 j 的字符串最大价值时的长度为len,那么就在状态转移的时候需要比较的时候,新的字符串取长度较短且字典序最小的状态(长度较短是因为字符串必须是由所给单词构成,但是字典序最小解不一定是全部由单词构成)。
这样问题就解决了。

这次我发现自己的错误代码竟然能AC简直不可思议,然后又看了自动机部分,发现有处的语句应该写成如下形式:
if ( next[now][i] == -1 ) {next[now][i] = next[fail[now]][i] ;}else {fail[next[now][i]] = next[fail[now]][i] ;if ( end[fail[next[now][i]]] ) {end[next[now][i]] += end[fail[next[now][i]]] ;}Q[tail ++] = next[now][i] ;}

而不是:
if ( next[now][i] == -1 ) {next[now][i] = next[fail[now]][i] ;}else {fail[next[now][i]] = next[fail[now]][i] ;if ( end[fail[now]] ) {end[now] += end[fail[now]] ;}Q[tail ++] = next[now][i] ;}

其实是因为第二种的方法遗漏了极限情况

代码如下


#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define REP( I , N ) for ( int I = 0 ; I < N ; ++ I )#define clear( A , X ) memset ( A , X , sizeof A )typedef char type_buf ;//buf串类型const int maxN = 1200 ;//节点总数const int maxW = 26 ;//分支总数const int maxQ = 100000 ;//队列大小const int oo = 0x3f3f3f3f ;struct Trie {int next[maxN][maxW] ;int fail[maxN] , end[maxN] ;int dp[52][maxN] ;int Q[maxQ] ;int head , tail ;int P , root ;type_buf path[52][maxN][52] ;Trie () {}int New () {REP ( i , maxW )next[P][i] = -1 ;end[P] = 0 ;return P ++ ;}//Newvoid Init () {P = 0 ;root = New () ;}//Initint Get ( type_buf ch ) {return ch - 'a' ;}//Getvoid Insert ( type_buf buf[] , int val ) {int now = root ;for ( int i = 0 ; buf[i] ; ++ i ) {int x = Get ( buf[i] ) ;if ( next[now][x] == -1 ) next[now][x] = New () ;now = next[now][x] ;}end[now] += val ;}//Insertvoid Build () {head = tail = 0 ;fail[root] = root ;REP ( i , maxW ) {if ( next[root][i] == -1 ) {next[root][i] = root ;}else {fail[next[root][i]] = root ;Q[tail ++] = next[root][i] ;}}while ( head != tail ) {int now = Q[head ++] ;REP ( i , maxW ) {if ( next[now][i] == -1 ) {next[now][i] = next[fail[now]][i] ;}else {fail[next[now][i]] = next[fail[now]][i] ;if ( end[fail[next[now][i]]] ) {end[next[now][i]] += end[fail[next[now][i]]] ;}Q[tail ++] = next[now][i] ;}}}}//Buildvoid DP ( int n ) {type_buf tmp[52] ;clear ( dp , -1 ) ;clear ( tmp , 0 ) ;dp[0][0] = 0 ;REP ( i , n )REP ( j , P )if ( dp[i][j] != -1 ) {strcpy ( tmp , path[i][j] ) ;int len = strlen ( tmp ) ;REP ( k , maxW ) {int Next = next[j][k] ;tmp[len] = 'a' + k ;tmp[len + 1] = 0 ;if ( dp[i + 1][Next] < dp[i][j] + end[Next] ) {dp[i + 1][Next] = dp[i][j] + end[Next] ;strcpy ( path[i + 1][Next] , tmp ) ;}else if ( dp[i + 1][Next] == dp[i][j] + end[Next] ) {int len1 = strlen ( tmp ) ;int len2 = strlen ( path[i + 1][Next] ) ;if ( len1 < len2 || len1 == len2 && strcmp ( tmp , path[i + 1][Next] ) < 0 ) {strcpy ( path[i + 1][Next] , tmp ) ;}}}}int ans = 0 ;type_buf buf[52] ;REP ( i , n ) REP ( j , P ) {if ( ans < dp[i + 1][j] ) {ans = dp[i + 1][j] ;strcpy ( buf , path[i + 1][j] ) ;}else if ( ans == dp[i + 1][j] ) {int len1 = strlen ( buf ) ;int len2 = strlen ( path[i + 1][j] ) ;if ( len2 < len1 || len1 == len2 && strcmp ( path[i + 1][j] , buf ) < 0 ) {strcpy ( buf , path[i + 1][j] ) ;}}}if ( 0 == ans ) {printf ( "\n" ) ;}else {printf ( "%s\n" , buf ) ;}}//DP} ;Trie AC ;type_buf buf[100][52] ;void Work () {int n , m , val ;AC.Init () ;scanf ( "%d%d" , &n , &m ) ;REP ( i , m )scanf ( "%s" , buf[i] ) ;REP ( i , m ) {scanf ( "%d" , &val ) ;AC.Insert ( buf[i] , val ) ;}AC.Build () ;AC.DP ( n ) ;}//Workint main () {int T ;scanf ( "%d" , &T ) ;while ( T -- ) Work () ;return 0 ;}//main




0 0
原创粉丝点击