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 
#include 
#include 
#include 
#include  
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;
}

你可能感兴趣的:(ac自动机,DP,HDU)