首页 > 搜索 > BFS搜索 > hdu 2296 Ring -动态规划-[解题报告]C++
2014
01-05

hdu 2296 Ring -动态规划-[解题报告]C++

Ring

问题描述 :

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.

输入:

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’.

输出:

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’.

样例输入:

2
7 2
love
ever
5 5
5 1
ab
5

样例输出:

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2296

题目大意:求长度不大于n的字符串,满足价值最大,价值计算为所包含的模板串的价值总和,并且要求字符串为最小字典序。

题目思路:ac自动机+dp,只是记录路径比较麻烦,当然用string记录的话会比较方便,但觉得比较慢,就想到将模式串反转,倒着dp,开始以为对于当前状态只要找到使当前达到最优值且前趋尽量小就可以了,可是会出现前趋的不同结点字符相同的情况,这样就不能简单地判断哪个最优了,所以要比较两条路径上的所有字符,直到可以判断大小为止。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<queue>
#include<algorithm>
#include<vector>
#include<stack>
#include<list>
#include<iostream>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
#define Max 110
int max(int a,int b)
{
	return a>b?a:b;
}
int min(int a,int b)
{
	return a<b?a:b;
}
int q[12*110],cnt;
char str[110][12];
int dp[60][1100];
int pre[60][1100];
char  c[1100];
struct node
{
    int cnt,fail,al;
    int next[26];
    void init()
    {
        cnt=fail=0;
        memset(next,0,sizeof(next));
    }
}tri[12*110];
int t,n,m;
inline bool cmp(int i,int x,int y)
{
    for(int k=i;k>=1;k--)
    {
        if(c[x]!=c[y])
            return c[x]<c[y];
        x=pre[k][x];
        y=pre[k][y];
    }
    return false;
}
void insert(char *s,int val)
{
    int i,p,x;
    p=0;
    for(i=0;s[i];i++)
    {
        x=s[i]-'a';
        if(!tri[p].next[x])
        {
            tri[++cnt].init();
            tri[cnt].al=s[i];
            tri[p].next[x]=cnt;
            c[cnt]=s[i];
        }
        p=tri[p].next[x];
    }
    tri[p].cnt=val;
}
void bfs()
{
    int i,p=0,suf,head=0,tail=0;
    for(i=0;i<26;i++)
    {
        if(tri[0].next[i])
        {
            q[tail++]=tri[0].next[i];
            tri[q[tail-1]].fail=0;
        }
    }
    while(head<tail)
    {
        p=q[head++];suf=tri[p].fail;
        tri[p].cnt+=tri[suf].cnt;
        for(i=0;i<26;i++)
        {
            if(tri[p].next[i])
            {
                q[tail++]=tri[p].next[i];
                tri[q[tail-1]].fail=tri[suf].next[i];
            }
            else
                tri[p].next[i]=tri[suf].next[i];
        }
    }
}
void solve()
{
    int ans=0,len=0,pos=0,i,j,k,tmp;
    for(i=0;i<=n;i++)
        for(j=0;j<=cnt;j++)
            dp[i][j]=-1;
    dp[0][0]=0;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=cnt;j++)
        {
            if((i&&(j==0))||dp[i][j]==-1)
                continue;
            if(i&&(ans<dp[i][j]))
            {
                ans=dp[i][j];
                len=i;
                pos=j;
            }
            else if(i&&(ans==dp[i][j]))
            {
                if(len==i&&cmp(len,j,pos))
                    pos=j;
            }
            if(i==n)
                continue;
            for(k=0;k<26;k++)
            {
                tmp=tri[j].next[k];
                if(tmp==0)
                    continue;
                if(dp[i+1][tmp]<dp[i][j]+tri[tmp].cnt)
                {
                    dp[i+1][tmp]=dp[i][j]+tri[tmp].cnt;
                    pre[i+1][tmp]=j;
                }
                else if(dp[i+1][tmp]==dp[i][j]+tri[tmp].cnt)
                {
                        if(cmp(i,j,pre[i+1][tmp]))
                            pre[i+1][tmp]=j;
                }
            }
        }
    }
    if(ans==0)
    {
        puts("");
        return ;
    }
    for(i=len;i>0;i--)
    {
        printf("%c",c[pos]);
       pos=pre[i][pos];
    }
    puts("");
}
int main()
{
   //freopen("D:/ring.txt","w",stdout);
    int v,i,len;
    char s[20];
    scanf("%d",&t);
    while(t--)
    {
        cnt=0;tri[0].init();
        scanf("%d%d",&n,&m);
        for(i=0;i<m;i++)
            scanf("%s",str[i]);
        for(i=0;i<m;i++)
        {
            scanf("%d",&v);
            len=strlen(str[i]);
            for(int j=len-1;j>=0;j--)
                s[len-1-j]=str[i][j];
            s[len]=0;
            insert(s,v);
        }
        bfs();
        solve();
    }
}

 

解题转自:http://blog.csdn.net/wings_of_liberty/article/details/7605534