hdu 2296 Ring ( ac自动机(可重叠可覆盖重复匹配型)+dp+恶心输出

Ring

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


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
  
  
2 7 2 love ever 5 5 5 1 ab 5
 

Sample Output
  
  
lovever abab
Hint
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
 

Source
The 4th Baidu Cup final
题目大意:给出一些得分的串,构造出给定长度内得分最多的串,如果有多个,先取最短的,如果长度相等,取字典序最小的
题目分析:裸的ac自动机+线性dp就是输出太恶心,要注意当dp[i][j]不更新时,如果当前用来更新的值相等,需要在更新字符串,看是否得到同样的值能不能得到更小的串 
这个点上w了一次,这道题用stl处理起来还比较方便
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <cstring>
#define N 1300
#define kind 26

using namespace std;

char s[107][57];
int value[107];
int n,m;

int Next[N][kind],cnt[N],fail[N],pos;
int dp[57][N];
string res[57][N];

struct Pre
{
    int from;
    char ch;
}pre[57][N];

int newNode ( )
{
    for ( int i = 0 ; i < 26 ; i++ )
        Next[pos][i] = 0;
    fail[pos] = cnt[pos] = 0;
    return pos++;
}

void insert ( char * s , int x )
{
    int i , p = 0;
    for ( i = 0 ; s[i] ; i++ )
    {
        int k = s[i] -'a' , &x = Next[p][k];
        p = x?x : x = newNode();
    }
    cnt[p] += value[x];
}

void build ( )
{
    int i;
    queue<int> q;
    q.push(0);
    while ( !q.empty() )
    {
        int u = q.front();
        cnt[u] += cnt[fail[u]];
        q.pop ( );
        for ( int i = 0 ; i < 26 ; i++ )
        {
            int v = Next[u][i];
            if ( v == 0 ) Next[u][i] = Next[fail[u]][i];
            else q.push(v);
            if ( u && v ) 
                fail[v] = Next[fail[u]][i];
        }
    }
}

void print ( int i , int j )
{
    if ( i == 0 ) return;
    print ( i-1 , pre[i][j].from );
    printf ( "%c" , pre[i][j].ch );
}

int t;

int main ( )
{
    scanf ( "%d" , &t );
    while  ( t-- )
    {
        pos = 0 , newNode ();
        scanf ( "%d%d" , &n , &m );
        for ( int i = 1 ; i <= m ; i++ )
            scanf ( "%s" , s[i] );
            //insert ( s , i );
        for ( int i = 1 ; i <= m ; i++ )
            scanf ( "%d" , &value[i] );
        for ( int i = 1 ; i <= m ; i++ )
            insert ( s[i] , i );
        build ( );
        memset ( dp , -1 , sizeof ( dp ) );
        dp[0][0] = 0;
        res[0][0] = "";
        for ( int i = 1 ; i <= n ; i++ )
           for ( int j = 0 ; j < pos ; j++ )
           {
              if ( dp[i-1][j] == -1 ) continue;
              for ( int k = 0 ; k < kind ; k++ )
              {
                  int p = Next[j][k];
                  //dp[i][p] = max ( dp[i-1][j] + cnt[p] , dp[i][p] );
                  if ( dp[i][p] < dp[i-1][j] + cnt[p] )
                  {
                      dp[i][p] = dp[i-1][j] + cnt[p];
                     // pre[i][p].from = j;
                     // pre[i][p].ch = (char) (k+'a'); 
                      res[i][p] = res[i-1][j] + char ( k+'a' );
                  }
                  else if ( dp[i-1][j]+cnt[p] == dp[i][p] && res[i][p] > res[i-1][j] + char (k+'a'))
                     res[i][p] = res[i-1][j] + char (k+'a'); 
              }
           }
        int ans = 0 , x = -1;
        for ( int i = 1 ; i <= n ; i++ )
            for ( int j = 0 ; j < pos ; j++ )
                if ( ans < dp[i][j] )
                {
                    ans = dp[i][j];
                    x = i;
                }
        vector<string> v;
        if ( x != -1 )
            for ( int j = 0 ; j < pos ; j++ )
                if ( ans == dp[x][j] )
                    v.push_back ( res[x][j] );
        sort ( v.begin() , v.end() );
        if ( ans != 0 )
            cout << v[0];
        puts ( "" );
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值