hdu 2296 Ring AC自动机+DP

Ring

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


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

  每个串有个值,如果一个串中包含它就加上这个值,输出一个长度不超过N的串,使值最大。长度小的优先,长度一样时字典序小的优先。

  dp[i][u]表示长度为i在节点u的最大值,s[i][u]表示这个最大值对应的串。初始dp[0][0]为0,其它都是-1(因为不是任意的dp[i][u]都能达到)。更新的时候如果dp值相等再判断当前串是否更优,直接strcmp就行了,这么简单粗暴我还WA了好几次。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<queue>
using namespace std;

typedef unsigned long long ULL;

const int MAXN=13;
const int MAXM=110;
const int MAXL=55;
const int MAXNODE=MAXN*MAXM;
const int LOGMAXN=50;
const int INF=0x3f3f3f3f;
const int SIGMA_SIZE=26;
const int MOD=20090717;

int T,N,M;
int dp[MAXL][MAXNODE];
char s[MAXL][MAXNODE][MAXL];

struct AC{
    int ch[MAXNODE][SIGMA_SIZE];
    int val[MAXNODE];
    int f[MAXNODE];
    int sz;

    void clear(){
        memset(ch[0],0,sizeof(ch[0]));
        val[0]=0;
        sz=1;
    }
    int idx(char c){
        return c-'a';
    }
    void insert(char* s,int v){
        int u=0;
        for(int i=0;s[i];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]=v;
    }
    void get_fail(){
        queue<int> q;
        f[0]=0;
        for(int c=0;c<SIGMA_SIZE;c++){
            int u=ch[0][c];
            if(u){
                f[u]=0;
                q.push(u);
            }
        }
        while(!q.empty()){
            int r=q.front();
            q.pop();
            for(int c=0;c<SIGMA_SIZE;c++){
                int u=ch[r][c];
                if(!u){
                    ch[r][c]=ch[f[r]][c];
                    continue;
                }
                q.push(u);
                f[u]=ch[f[r]][c];
                val[u]+=val[f[u]];
            }
        }
    }
}ac;

void DP(){
    int MAX=0;
    char ans[MAXL],tmp[MAXL];
    memset(dp,-1,sizeof(dp));
    dp[0][0]=0;
    ans[0]=0;
    s[0][0][0]=0;
    for(int i=0;i<N;i++)
        for(int u=0;u<ac.sz;u++) if(dp[i][u]!=-1){
            strcpy(tmp,s[i][u]);
            int l=strlen(s[i][u]);
            for(int c=0;c<SIGMA_SIZE;c++){
                tmp[l]=c+'a';
                tmp[l+1]=0;
                if(dp[i][u]+ac.val[ac.ch[u][c]]>dp[i+1][ac.ch[u][c]]||dp[i][u]+ac.val[ac.ch[u][c]]==dp[i+1][ac.ch[u][c]]&&strcmp(tmp,s[i+1][ac.ch[u][c]])<0){
                    dp[i+1][ac.ch[u][c]]=dp[i][u]+ac.val[ac.ch[u][c]];
                    strcpy(s[i+1][ac.ch[u][c]],tmp);
                }
                if(dp[i+1][ac.ch[u][c]]>MAX||dp[i+1][ac.ch[u][c]]==MAX&&strlen(s[i+1][ac.ch[u][c]])<=strlen(ans)&&strcmp(s[i+1][ac.ch[u][c]],ans)<0){
                    MAX=dp[i+1][ac.ch[u][c]];
                    strcpy(ans,s[i+1][ac.ch[u][c]]);
                }
            }
        }
    printf("%s\n",ans);
}


char str[MAXM][MAXN];

int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        ac.clear();
        for(int i=0;i<M;i++) scanf("%s",str[i]);
        int cnt;
        for(int i=0;i<M;i++){
            scanf("%d",&cnt);
            ac.insert(str[i],cnt);
        }
        ac.get_fail();
        DP();
    }
    return 0;
}



你可能感兴趣的:(hdu 2296 Ring AC自动机+DP)