Ringtime limit:1000msmemory limit:32768kbthis problem'll be judged onHDU. Original id:2296
64-bit integer IO format: %i64d Java class name: Main 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.
InputThe 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 '.
OutputFor 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
2loveever5 1AB5
Sample Output
Loveverabab
HintSample 1:weight = 5, weight (ever) = 5, so weight (lovever) = 5 + 5 = Ten sample 2:weight (AB) = 2 * 5 = ten, so weight (abab) = 10SourceThe 4th Baidu Cup Final Problem solving: Trie diagram + dynamic planning. Since the dictionary order is required to be minimal, the DP is inverted from the back
1#include <bits/stdc++.h>2 using namespacestd;3 usingPII = pair<int,int>;4 Const intMAXN =510;5 6 structtrie{7 intch[maxn][ -],fail[maxn],cnt[maxn],tot;8 intDP[MAXN][MAXN];9 PII PATH[MAXN][MAXN];Ten intNewNode () { Onememset (Ch[tot],0,sizeofCh[tot]); AFail[tot] = Cnt[tot] =0; - returntot++; - } the voidinit () { -tot =0; - NewNode (); - } + voidInsertChar*STR,intValintRT =0){ - for(inti =0; Str[i]; ++i) { + int&x = ch[rt][str[i]-'a']; A if(!x) x =NewNode (); atRT =x; - } -CNT[RT] + =Val; - } - voidBuildintRT =0){ -queue<int>Q; in for(inti =0; I < -; ++i) - if(Ch[rt][i]) Q.push (Ch[rt][i]); to while(!Q.empty ()) { +RT =Q.front (); - Q.pop (); theCNT[RT] + =Cnt[fail[rt]]; * for(inti =0; I < -; ++i) { $ int&x = Ch[rt][i],y =Ch[fail[rt]][i];Panax Notoginseng if(x) { -FAIL[X] =y; the Q.push (x); +}Elsex =y; A } the } + } - voidSolveintN) { $Memset (DP,0,sizeofDP); $ for(inti = n; I --i) { - for(intj =0; J < tot; ++j) { - for(intK =0; K < -; ++k) { the intx = Ch[j][k],tmp = dp[i+1][X] +Cnt[x]; - if(tmp >Dp[i][j]) {WuyiDP[I][J] =tmp; thePATH[I][J] =PII (x,k); - } Wu } - } About } $ intPOS = n +1, ans =0, st =0; - for(inti = n +1; I --i) - if(dp[i][0] > ans) ans = Dp[pos = i][0]; - for(inti = pos; I <= N; ++i) { Aprintf"%c", Path[i][st].second +'a'); +St =Path[i][st].first; the } -Putchar ('\ n'); $ } the }ac; the CharSTR[MAXN][MAXN]; the intMain () { the intKase,n,m,val; -scanf"%d",&Kase); in while(kase--){ thescanf"%d%d",&n,&m); the for(inti =0; I < m; ++i) Aboutscanf"%s", Str[i]); the ac.init (); the for(inti =0; I < m; ++i) { thescanf"%d",&val); + Ac.insert (str[i],val); - } the ac.build ();Bayi ac.solve (n); the } the return 0; -}
View Code
HDU 2296 Ring