题目链接:https://vjudge.net/problem/HDU-2296
Ring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4429 Accepted Submission(s): 1474
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.
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'.
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.
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
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
Source
题意:
给出m个单词以及每个单词的价值,问长度不超过n的字符串价值最大是多少?求出这个字符串。
如果价值不同,取价值最大的;
如果价值相同,则取长度最短的;
如果价值相同,且长度相同,取字典序最小的。
题解:
1.把m个单词插入AC自动机中。
2.设dp[i][j]为:长度为i,且到达的状态为j(自动机上的状态)的最大价值。设path[i][j](string类型)为:长度为i,且到达的状态为j的最大价值的路径。
3.状态转移详情请看代码及注释。
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <vector> 6 #include <cmath> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const double EPS = 1e-6; 15 const int INF = 2e9; 16 const LL LNF = 2e18; 17 const int MOD = 1e9+7; 18 const int MAXN = 5e3+10; 19 20 int dp[55][MAXN]; 21 string path[55][MAXN]; 22 23 struct Trie 24 { 25 int sz, base; 26 int next[MAXN][26], fail[MAXN], end[MAXN]; 27 char ch[MAXN]; //用于记录结点(状态)上的字符 28 int root, L; 29 int newnode() 30 { 31 for(int i = 0; i<sz; i++) 32 next[L][i] = -1; 33 end[L++] = 0; 34 return L-1; 35 } 36 void init(int _sz, int _base) 37 { 38 sz = _sz; 39 base = _base; 40 L = 0; 41 root = newnode(); 42 } 43 void insert(char buf[], int val) 44 { 45 int len = strlen(buf); 46 int now = root; 47 for(int i = 0; i<len; i++) 48 { 49 if(next[now][buf[i]-base] == -1) next[now][buf[i]-base] = newnode(); 50 now = next[now][buf[i]-base]; 51 ch[now] = buf[i]; 52 } 53 end[now] += val; // end用于记录值 54 } 55 void build() 56 { 57 queue<int>Q; 58 fail[root] = root; 59 for(int i = 0; i<sz; i++) 60 { 61 if(next[root][i] == -1) next[root][i] = root; 62 else fail[next[root][i]] = root, Q.push(next[root][i]); 63 } 64 while(!Q.empty()) 65 { 66 int now = Q.front(); 67 Q.pop(); 68 // end[now] += end[fail[now]]; //不用累加,因为在DP的时候会累加 69 for(int i = 0; i<sz; i++) 70 { 71 if(next[now][i] == -1) next[now][i] = next[fail[now]][i]; 72 else fail[next[now][i]] = next[fail[now]][i], Q.push(next[now][i]); 73 } 74 } 75 } 76 77 void query(int n) 78 { 79 for(int i = 0; i<=n; i++) 80 for(int j = 0; j<L; j++) 81 dp[i][j] = -INF; 82 83 dp[0][root] = 0; path[0][root] = ""; 84 for(int i = 0; i<=n; i++) 85 for(int j = 0; j<L; j++) 86 for(int k = 0; k<sz; k++) 87 { 88 int newi = i+1; 89 int newj = next[j][k]; 90 /* 可更新的两种情况: 91 情况1:新串价值<旧串价值 92 情况2:新串价值=旧串价值 且 新串字典序<旧串字典序 93 注:因为长度都是i+1,所以不用考虑长度的情况 94 */ 95 if(dp[newi][newj]<dp[i][j]+end[newj] || 96 (dp[newi][newj]==dp[i][j]+end[newj]&& path[newi][newj]>path[i][j]+ch[newj])) 97 { 98 dp[newi][newj] = dp[i][j]+end[newj]; 99 path[newi][newj] = path[i][j]+ch[newj]; 100 } 101 } 102 103 int posi = 0, posj = 0, maxx = -1; 104 for(int i = 0; i<=n; i++) 105 for(int j = 0; j<L; j++) 106 if( dp[i][j]>maxx || 107 (dp[i][j]==maxx&&path[i][j].size()<path[posi][posj].size())|| 108 (dp[i][j]==maxx&&i==posi&&path[i][j]<path[posi][posj])) 109 { 110 /* 可更新的三种情况: 111 情况1:新串价值<旧串价值 112 情况2:新串价值=旧串价值 且 新串长度<旧串长度 113 情况2:新串价值=旧串价值 且 新串长度=旧串长度 且 新串字典序<旧串字典序 114 */ 115 maxx = dp[i][j]; 116 posi = i; 117 posj = j; 118 } 119 cout<<path[posi][posj]<<endl; 120 } 121 }; 122 123 Trie ac; 124 char buf[110][110]; 125 int H[110]; 126 int main() 127 { 128 int T, n, m; 129 scanf("%d", &T); 130 while(T--) 131 { 132 ac.init(26, 'a'); 133 scanf("%d%d", &n,&m); 134 for(int i = 1; i<=m; i++) scanf("%s", buf[i]); 135 for(int i = 1; i<=m; i++) scanf("%d", &H[i]); 136 for(int i = 1; i<=m; i++) ac.insert(buf[i], H[i]); 137 138 ac.build(); 139 ac.query(n); 140 } 141 return 0; 142 }