AC自动机+DP+hdu2296

F.A.Q
Hand In Hand
Online Acmers
Forum | Discuss
Statistical Charts
Problem Archive
Realtime Judge Status
Authors Ranklist
 
      C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
Virtual Contests 
    DIY | Web-DIY beta
Recent Contests
BestCoder官方QQ群:385386683 欢迎加入~
寻人启事:2014级新生看过来!

Ring

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


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


这个题跟2457的DP差不多,就是当价值大或者长度小、字典序晓得时候更行状态,并且开一个字符串数组记录当前最优状态的字符串

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100;
const int maxm=1010;
const int INF=1000000000;
const int SIGMA_SIZE=26;
int N,M,value[maxn];
char word[60],str[105][1010][55];
int dp[105][1010];

bool cmp(char *s1,char *s2)
{
    int len1=strlen(s1);
    int len2=strlen(s2);
    if(len1!=len2)return len1<len2;
    return strcmp(s1,s2)<0;
}
struct AC
{
    int ch[maxm][26],val[maxm];
    int fail[maxm];
    int sz;
    void clear(){memset(ch[0],0,sizeof(ch[0]));sz=1;}
    int idx(char x){return x-'a';}
    void insert(char *s,int id)
    {
        int n=strlen(s);
        int u=0;
        for(int i=0;i<n;i++)
        {
            int c=idx(s[i]);
            if(!ch[u][c])
            {
                memset(ch[sz],0,sizeof(ch[sz]));
                val[sz]=0;
                ch[u][c]=sz++;
            }
            u=ch[u][c];
        }
        val[u]=id;
    }
    void getfail()
    {
        queue<int> q;
        fail[0]=0;
        int u=0;
        for(int i=0;i<SIGMA_SIZE;i++)
        {
            u=ch[0][i];
            if(u){fail[u]=0;q.push(u);}
        }
        while(!q.empty())
        {
            int r=q.front();q.pop();
            for(int c=0;c<SIGMA_SIZE;c++)
            {
                u=ch[r][c];
                if(!u){ch[r][c]=ch[fail[r]][c];continue;}
                q.push(u);
                int v=fail[r];
                while(v&&!ch[v][c])v=fail[v];
                fail[u]=ch[v][c];
            }
        }
    }
    void solve()
    {
        char ans[55],tmp[55];
        strcpy(ans,"");
        for(int i=0;i<=N;i++)
            for(int j=0;j<sz;j++)dp[i][j]=-INF;
        dp[0][0]=0;
        strcpy(str[0][0],"");
        int maxv=0;
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<sz;j++)
            {
                if(dp[i][j]<0)continue;
                strcpy(tmp,str[i][j]);
                int len=strlen(tmp);
                for(int k=0;k<SIGMA_SIZE;k++)
                {
                    int w=dp[i][j];
                    int next=ch[j][k];
                    if(val[next])w+=value[val[next]];
                    tmp[len]='a'+k;
                    tmp[len+1]=0;

                    if(dp[i+1][next]<w||(dp[i+1][next]==w&&cmp(tmp,str[i+1][next])))
                    {
                        dp[i+1][next]=w;
                        strcpy(str[i+1][next],tmp);
                        if(w>maxv||(w==maxv&&cmp(tmp,ans)))
                        {
                            maxv=w;
                            strcpy(ans,tmp);
                        }
                    }
                }
            }
        }
        printf("%s\n",ans);
    }
}ac;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&M);
        ac.clear();
        for(int i=1;i<=M;i++)
        {
            scanf("%s",word);
            ac.insert(word,i);
        }
        value[0]=0;
        for(int i=1;i<=M;i++)scanf("%d",&value[i]);
        ac.getfail();
        ac.solve();
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值