HDU 2296 Ring AC automatic machine +DP

Source: Internet
Author: User

Description

For the hope of a forever love, Steven was 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 she favorite words, such as "Love", "Forever". Also, he knows the value of each word. The higher value a word has the more joy Jane would get when see it.
The weight of a word is defined as it appeared times in the romantic string multiply by it value, while the weight of th E romantic string is defined as the sum of all words ' weight. You should output of 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 the test cases. Each test case starts with a line consisting of 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 all 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 was less than and bigger than 0.
4.1≤hi≤100.
5. All the words in the input is different.
6. All the words just consist of ' a '-' Z '.

Output

For each test case, the output of the string to engrave on a.
If there ' s more than one possible answer, first output the shortest one. If There is still multiple solutions, output the smallest in lexicographically order.
The answer is an empty string.

Sample Input

Sample Output

Hint

Compared to the normal AC automata +DP, similar to the previous type problem, the problem is that the string must be recorded along the way, and the string is the smallest dictionary order.

can use the STL in the string record, the speed feeling is OK, not very slow.

Test instructions and ideas here will not say, relatively simple, and the front are the same, you can refer to my previous blog.

#include <cstdio>#include<cstring>#include<queue>#include<algorithm>#include<string>#include<iostream>using namespacestd;intch[102* A][ -],end[102* A],cur,fail[102* A],last[102* A],h[102];Charstr[ -],str0[102][ A];voidGet_fail () {intNow,tmpfail,next; Queue<int>Q;  for(intj=0; j< -; J + +)    {        if(ch[0][j]) {Q.push (ch[0][j]); fail[ch[0][J]] =0; last[ch[0][J]] =0; }    }     while(!Q.empty ()) { Now=Q.front ();        Q.pop ();  for(intj=0; j< -; J + +)        {            if(!ch[now][j])Continue; Next=Ch[now][j];            Q.push (Next); Tmpfail=Fail[now];  while(Tmpfail&&!ch[tmpfail][j]) Tmpfail =Fail[tmpfail]; Fail[next]=Ch[tmpfail][j]; Last[next]= End[fail[next]]?Fail[next]:last[fail[next]]; }    }}stringans_str[102* A][ -];intdp[102* A][ -];intMain () {intN,m,now,t,kase=1; scanf ("%d",&T);  while(t--) {scanf ("%d%d",&n,&m); memset (CH,0,sizeof(CH)); memset (End,0,sizeof(End)); Memset (Last,0,sizeof(last)); Cur=1; intLen;  for(intI=1; i<=m; i++) {scanf ("%s", Str0[i]); Len=strlen (Str0[i]); now=0;  for(intj=0; j<len; J + +) {Str0[i][j]-='a'; if(ch[now][str0[i][j]]==0) Ch[now][str0[i][j]] = cur++; now=Ch[now][str0[i][j]]; STR0[I][J]+='a'; } End[now]=i;        } get_fail ();  for(intI=1; i<=m; i++) scanf ("%d",&H[i]); Memset (DP,-1,sizeof(DP)); dp[0][0]=0; ans_str[0][0]=string(""); intans=-1, Ans_now,ans_len;  for(intI=0; i<n; i++)        {             for(inttnow=0; tnow<cur; tnow++)            {                if(dp[tnow][i]!=-1)                 for(intC=0; c< -; C++)                {                    intnow = tnow,tsum=0;  while(Now&&!ch[now][c]) now =Fail[now]; now=Ch[now][c]; if(End[now]) tsum+=H[end[now]]; intTMP =Now ;  while(Last[tmp]) {Tsum+=H[end[last[tmp]]; TMP=Last[tmp]; }                    stringtmp_s =string("a"); tmp_s[0]=c+'a'; if(dp[tnow][i]+tsum>dp[now][i+1]||(Dp[tnow][i]+tsum==dp[now][i+1] && ans_str[tnow][i]+tmp_s < ans_str[now][i+1]) {ans_str[now][i+1] = ans_str[tnow][i]+tmp_s; Dp[now][i+1] = dp[tnow][i]+Tsum; }                    if(dp[now][i+1]>ans | |(Dp[now][i+1]==ans && ans_len==i+1&& (Ans_str[ans_now][ans_len] > ans_str[now][i+1])) {ans= dp[now][i+1]; Ans_now=Now ; Ans_len= i+1; }                }            }        }        if(ans>0) cout<<Ans_str[ans_now][ans_len]; Puts (""); }}

HDU 2296 Ring AC automatic machine +DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.