【hdu 2296】 Ring AC-自动机+DP

Ring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3083 Accepted Submission(s): 985

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

题意:ac自动机中,每个单词带一个权值,求一个N长度以内权值最大的串;
思路:建树后,在字符串树上DP 
dp[i][j]: i表示第i长度树上第j(j<=tot)点最大权值,值一样比较串长度与字典序;
和上道题相似:poj 3691DNA repair
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

#include
#include
#include
#include
#include
using namespace std;
int T;
int n,m; 
queue<int> q; 
int sum[105];
char s[105][55];
int flag[55*11];
int nex[55*11];
char p[55][11][55];
struct node{
    int ch[26];
    void init()
    {
        for(int i=0;i<26;i++)
        ch[i]=0;
    }
}t[55*11]; 
string pa[55][55*11];
int dp[55][55*11];
int tot;
inline void insert(char s[],int x)
{
//  cout<
    int now=0; 
    for(int i=0;s[i];i++)
    {
        int tt=s[i]-'a';
        if(!t[now].ch[tt])
        {
            t[now].ch[tt]=++tot;
            t[tot].init();
            flag[tot]=0;
            nex[tot]=0; 
        } 
        now=t[now].ch[tt];
    }
    flag[now]=x;
}
inline void get_nex()
{
    for(int i=0;i<26;i++)
    if(t[0].ch[i])
    q.push(t[0].ch[i]);


    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        int next=nex[now];
        for(int i=0;i<26;i++)
        {
            if(t[now].ch[i])
            {
                nex[t[now].ch[i]]=t[next].ch[i];
                flag[t[now].ch[i]]+=flag[t[next].ch[i]];
                q.push(t[now].ch[i]);
            }
            else t[now].ch[i]=t[next].ch[i];
        } 
    }
}
inline void DP()
{
    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;


    for(int i=0;ifor(int j=0;j<=tot;j++)
        if(dp[i][j]!=-1)
        for(int k=0;k<26;k++)
        if(t[j].ch[k])
        {
            int nex=t[j].ch[k];
            if(dp[i+1][nex]==-1||dp[i+1][nex]1][nex]=flag[nex]+dp[i][j];
                pa[i+1][nex]=pa[i][j]+char(k+'a');
            } 
            else if(dp[i+1][nex]==dp[i][j]+flag[nex]&&pa[i][j]+(char)(k+'a')1][nex])  
            {   
                pa[i+1][nex]=pa[i][j]+(char)(k+'a');
            }
        }   
    }
    int ans=0;
    for(int i=1;i<=n;i++) 
    for(int j=0;j<=tot;j++) ans=max(ans,dp[i][j]);  
    if(ans==0) 
    {
        puts("");
        return;
    }  
    string str=" ";  
    for(int i=1;i<=n;i++) 
    for(int j=0;j<=tot;j++) 
    if(dp[i][j]==ans&&(str==" "||(pa[i][j].size()cout<int main()
{
    scanf("%d",&T);
    while(T--)
    {
        tot=0;
        t[0].init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)   scanf("%s",s[i]);
        for(int i=1;i<=m;i++) 
        {
            scanf("%d",&sum[i]);
        }
        for(int i=1;i<=m;i++) insert(s[i],sum[i]);
        get_nex();
        DP();
    } 
} 

你可能感兴趣的:(ac-自动机,dp,字符串)