HDU2296-Ring

Ring

                                                                      Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                                                              Total Submission(s): 4116    Accepted Submission(s): 1360


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. 

 

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
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
 

Recommend
lcy
 

题意:给你m个模板串,第i个模板串每出现一次会产生vi的价值,要求构造一个长度小于等于n的串使得价值最大,有多种方案的时候先选长度最小的,再选字典序最小的

解题思路:ac自动机,dp[i][j]表示长度问i的字符串到达j状态的最大值,a[i][j]表示对应的串


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cctype>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

char ch[15], a[55][1200][55], ans[55], temp[55];
int n, m, ma;
int dp[55][1200], x[120];

bool cmp(char s1[], char s2[])
{
	int len1 = strlen(s1);
	int len2 = strlen(s2);
	if (len1 != len2)return len1 < len2;
	else return strcmp(s1, s2) < 0;
}

struct Trie
{
	int next[1200][26], fail[1200], flag[1200];
	int root, tot;
	int newnode()
	{
		for (int i = 0; i < 26; i++) next[tot][i] = -1;
		flag[tot++] = 0;
		return tot - 1;
	}
	void init()
	{
		tot = 0;
		root = newnode();
	}
	void insert(char ch[], int id)
	{
		int k = root;
		for (int i = 0; ch[i]; i++)
		{
			if (next[k][ch[i] - 'a'] == -1) next[k][ch[i] - 'a'] = newnode();
			k = next[k][ch[i] - 'a'];
		}
		flag[k] = id;
	}
	void build()
	{
		queue<int>q;
		fail[root] = root;
		for (int i = 0; i < 26; i++)
		{
			if (next[root][i] == -1) next[root][i] = root;
			else
			{
				fail[next[root][i]] = root;
				q.push(next[root][i]);
			}
		}
		while (!q.empty())
		{
			int pre = q.front();
			q.pop();
			for (int i = 0; i < 26; i++)
			{
				if (next[pre][i] == -1) next[pre][i] = next[fail[pre]][i];
				else
				{
					fail[next[pre][i]] = next[fail[pre]][i];
					q.push(next[pre][i]);
				}
			}
		}
	}
	void solve()
	{
		for (int i = 0; i <= n; i++)
			for (int j = 0; j < tot; j++)
				dp[i][j] = -INF;
		dp[0][0] = 0;
		strcpy(a[0][0], "");
		ma = 0;
		strcpy(ans, "");
		for (int i = 0; i < n; i++)
			for (int j = 0; j < tot; j++)
				if (dp[i][j] >= 0)
				{
					strcpy(temp, a[i][j]);
					int len = strlen(temp);
					for (int k = 0; k < 26; k++)
					{
						temp[len] = 'a' + k;
						temp[len + 1] = 0;
						int p = dp[i][j] + x[flag[next[j][k]]];
						if (p > dp[i + 1][next[j][k]] || (dp[i + 1][next[j][k]] == p&&cmp(temp, a[i + 1][next[j][k]])))
						{
							dp[i + 1][next[j][k]] = p;
							strcpy(a[i + 1][next[j][k]], temp);
							if (ma < p || (ma == p&&cmp(temp, ans)))
							{
								strcpy(ans, temp);
								ma = p;
							}
						}
					}
				}
		printf("%s\n", ans);
	}
	void debug()
	{
		for (int i = 0; i < tot; i++)
		{
			printf("id = %3d,fail = %3d,end = %3d,chi = [", i, fail[i], flag[i]);
			for (int j = 0; j < 26; j++) printf("%2d", next[i][j]);
			printf("]\n");
		}
	}
}ac;

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d %d", &n, &m);
		ac.init();
		for (int i = 1; i <= m; i++)
		{
			scanf("%s", ch);
			ac.insert(ch, i);
		}
		for (int i = 1; i <= m; i++) scanf("%d", &x[i]);
		ac.build();
		ac.solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值