HDU - 2296 Ring ac自動機 + dp

一、內容

 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

二、思路

  • f[i][j] 表示 匹配好第i個字符後,在自動機上的節點位置爲j的最大價值。
  • 在我們更新價值的時候,我們只需要記錄這次增加的字母是那個,若價值相同的話就與之前的形成的字符串比較一下即可。
  • 最後輸出我們找到長度最小,價值最大,字典序最小的字符串。

三、代碼

#include <cstdio>
#include <cstring>
#include <queue>
#include <iostream>
#include <algorithm> 
using namespace std;
const int N = 55, M = 1005, INF = 0x3f3f3f3f;
int n, t, m, tr[M][26], len, ne[M], fail[M], w[105], f[N][M];
char s[15], c[M];
string path[N][M];
void add(int id) {
	int p = 0;
	for (int i = 0; i < s[i]; i++) {
		int j = s[i] - 'a';
		if (!tr[p][j]) tr[p][j] = ++len, c[len] = s[i]; //保存下這個節點的字符 
		p = tr[p][j];
	}
	fail[p] = id;
}
void build() {
	queue<int> q;
	for (int j = 0; j < 26; j++) {
		if (tr[0][j]) q.push(tr[0][j]);
	}
	while (!q.empty()) {
		int p = q.front(); q.pop();
		for (int j = 0; j < 26; j++) {
			int c = tr[p][j];
			if (!c) tr[p][j] = tr[ne[p]][j];
			else {
				q.push(c);
				ne[c] = tr[ne[p]][j];
				fail[c] |= fail[ne[c]];
			}
		}
	}
}
int main() {
	scanf("%d", &t);
	while (t--) {
		memset(fail, 0, sizeof(fail));
		memset(ne, 0, sizeof(ne));
		memset(tr, 0, sizeof(tr)); len = 0;
		memset(f, -0x3f, sizeof(f));
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= m; i++) {
			scanf("%s", s); add(i);
		}
		for (int i = 1; i <= m; i++) scanf("%d", &w[i]);
		build(); 
		f[0][0] = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= len; j++) path[i][j].clear();
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= len; j++) {
				if (f[i - 1][j] == -INF) continue;
				for (int k = 0; k < 26; k++) {
					int tj = tr[j][k];
					//統計下價值 
					int val = f[i - 1][j], t = tj;
					if (fail[t]) val += w[fail[t]];
					//看是否能從上一狀態轉移過來 
					if (val > f[i][tj]) {
						path[i][tj] = path[i - 1][j] + (char)('a' + k);
						f[i][tj] = val;
					} else if (val == f[i][tj]) {
						string str = path[i - 1][j] + (char)('a' + k);
						if (str < path[i][tj]) path[i][tj] = str;
					}
				}
			}
		} 
		//找尋字符最少 value最大的 
		int maxv = 0, mid = 0;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j <= len; j++) {
				if (f[i][j] > maxv) {
					maxv = f[i][j];
					mid = i; 
				}
			} 
		}
		//輸出字符 如果有多個長度相等的 就找字典序最小的 
		string ans; 
		for (int j = 0; j <= len; j++) {
			if (f[mid][j] != maxv) continue;
			if (ans.size() == 0 || path[mid][j] < ans) ans = path[mid][j];
		}                       
		cout << ans << endl;
	}	
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章