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.
InputThe 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.
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.
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'.
OutputFor 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 5Sample 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
题目大意:每个单词都有一个权重,求在给定长度范围内,总权重最大且字典序最小的串。
解题思路:利用end数组记录其权重,利用DP[i][j]记录其长度为i,节点个数为j状态的最大值;SS[i][j]记录其长度为i,节点个数为j状态的字符串。
Status | Accepted |
---|---|
Time | 78ms |
Memory | 9940kB |
Length | 5932 |
Lang | G++ |
Submitted | 2017-04-13 20:56:25 |
Shared | |
RemoteRunId | 20405967 |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 10007
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ul;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){
ll f=1;
while(q)
{
if(q&1) f=f*p%MOD;
p=p*p%MOD;
q>>=1;
}
return f;
}
const int N = 1010;
const int M = 26;
class Trie
{
public:
int next[N][M],fail[N],end[N];
int root,L;
int newnode()
{
for(int i = 0;i < M;i++)//每一个节点对应0-128中的任意一个。
next[L][i] = -1;
end[L++] = 0;//表示下面没有节点 初始化,如果是记录次数,就赋0 还可以赋任意的数,
return L-1;
}
void init()
{
L = 0;
memset(end,0,sizeof(end));
root = newnode();
}
void insert(char s[],int id)
{
int len = strlen(s);
int now = root;
for(int i = 0;i < len;i++)
{
if(next[now][s[i]-'a'] == -1)
next[now][s[i]-'a'] = newnode();
now=next[now][s[i]-'a'];//记录其对应的节点编号
}
end[now]=id;//记录当前匹配单词的节点
//end[now]++;也可以用匹配单词结束后来记录次数
}
void build()
{
queue<int>Q;
fail[root] = root;//根节点仍然是根节点
for(int i = 0;i < M;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 now = Q.front();//逐层拿出第一个
Q.pop();
end[now]+=end[fail[now]];//如果两个都能达到一个字符串,就就加上
//if(end[fail[now]]== 1) end[now] = 1;
for(int i = 0;i < M;i++)//对这一行
if(next[now][i] == -1)//如果下一行没有这个字符
next[now][i] = next[fail[now]][i];//他的下一个的这
else//如果有这个字符
{
fail[next[now][i]] = next[fail[now]][i];//他的下一个的
Q.push(next[now][i]);//下一行继续
}
}
}
/*bool used[N];//判断其是否被查找到
bool query(char buf[],int n,int id)
{
int len = strlen(buf);
int now = root;
memset(used,false,sizeof(used));//初始化used
bool flag = false;
for(int i = 0;i < len;i++)
{
now = next[now][buf[i]];
int temp = now;
while(temp != root)
{
if(end[temp] != -1)
{
used[end[temp]] = true;//记录被匹配的信息
flag = true;
}
temp = fail[temp];
}
}
}*/
int dp[110][N];
string ss[110][N];
string solve(int n)
{
int len = n;
for(int i=0;i<=len;i++)
{
for (int j = 0; j < L; ++j) {
dp[i][j] = -inf_int;
ss[i][j].clear();
}
}
dp[0][root] = 0;
for (int i = 0; i < len; ++i) {
for (int j = 0; j < L; ++j) {//代表走的步数
for (int k = 0; k < 26; ++k) {
int news = next[j][k];
int tmp = dp[i][j] +end[news];
if(dp[i+1][news]<=tmp)
{
if(dp[i+1][news]<tmp||ss[i+1][news]>ss[i][j]+char(k+'a'))//数值需要更新或者其字符串字典序可以进行更新
ss[i+1][news] = ss[i][j]+char(k+'a');
dp[i+1][news] = tmp;
}
}
}
}
string anss;
anss.clear();
int ans = 0;
for (int m = 0; m <= len; ++m)
{
for (int l = 0; l < L; ++l)
{
if(dp[m][l]>=ans)
{
if(dp[m][l]>ans)
{
ans = dp[m][l];
anss = ss[m][l];
}
else
{
if(anss.size()>ss[m][l].size())
{
anss = ss[m][l];
}
else if(anss.size()==ss[m][l].size())
{
if(anss>ss[m][l])
{
anss = ss[m][l];
}
}
}
}
}
}
if(ans==0)
{
cout <<""<<endl;
}
else
cout <<anss<<endl;
return anss;
}
};
Trie ac;
char buf[110][N];
int n,m;
int main()
{
int T,t;
scanf("%d",&T);
while(T--)
{
scanf("%d%d", &n,&m);
ac.init();
for(int i = 0; i < m; i++)
{
scanf("%s",buf[i]);
}
for(int i = 0; i < m; i++)
{
scanf("%d",&t);
ac.insert(buf[i],t);
}
ac.build();
ac.solve(n);
}
return 0;
}