• [HDU2296]Ring


    vjudge

    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
    

    简单翻译一下:给出m个模式串(m≤100),每个模式串有一个权值。先要求构造一个长度不大于n(n≤50)的串使其包含的子模式串的权值和最大。若存在多解,则要求输出长度最小的,若仍存在多解则要求输出字典序最小的。

    sol

    DP出最大权值应该不难吧
    要求字典序最小其实也很好办。开一个string记录路径就行了(string自带比较字典序)。

    code

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int N = 1500;
    int T,n,m,tot,ch[26][N],fail[N],cnt[N],node[101],dp[55][N];
    string path[55][N];
    char s[N];
    queue<int>Q;
    void Insert(int k)
    {
    	cin>>s;int l=strlen(s),x=0;
    	for (int i=0;i<l;i++)
    	{
    		if (!ch[s[i]-'a'][x]) ch[s[i]-'a'][x]=++tot;
    		x=ch[s[i]-'a'][x];
    	}
    	node[k]=x;
    }
    void Get_Fail()
    {
    	for (int i=0;i<26;i++) if (ch[i][0]) Q.push(ch[i][0]);
    	while (!Q.empty())
    	{
    		int u=Q.front();Q.pop();
    		for (int i=0;i<26;i++)
    			if (ch[i][u]) fail[ch[i][u]]=ch[i][fail[u]],Q.push(ch[i][u]);
    			else ch[i][u]=ch[i][fail[u]];
    		cnt[u]+=cnt[fail[u]];
    	}
    }
    void DP()
    {
    	memset(dp,-1,sizeof(dp));
    	dp[0][0]=0;path[0][0]="";
    	for (int i=0;i<n;i++)
    		for (int j=0;j<=tot;j++)
    			if (dp[i][j]!=-1)
    				for (int k=0;k<26;k++)
    				{
    					int u=ch[k][j];
    					if (dp[i][j]+cnt[u]>dp[i+1][u])
    					{
    						dp[i+1][u]=dp[i][j]+cnt[u];
    						path[i+1][u]=path[i][j]+(char)(k+'a');
    					}
    					else if (dp[i][j]+cnt[u]==dp[i+1][u])
    					{
    						string str=path[i][j]+(char)(k+'a');
    						if (str<path[i+1][u]) path[i+1][u]=str;
    					}
    				}
    }
    int main()
    {
    	cin>>T;
    	while (T--)
    	{
    		cin>>n>>m;
    		for (int i=0;i<=tot;i++)
    		{
    			fail[i]=cnt[i]=0;
    			for (int j=0;j<26;j++) ch[j][i]=0;
    		}
    		tot=0;
    		for (int i=1;i<=m;i++) Insert(i);
    		for (int i=1,v;i<=m;i++) cin>>v,cnt[node[i]]+=v;
    		Get_Fail();
    		DP();
    		string str="";int ans=0,maxx=0;
    		for (int i=1;i<=n;i++)
    			for (int j=0;j<=tot;j++)
    				maxx=max(maxx,dp[i][j]);
    		for (int i=1;i<=n;i++)
    		{
    			for (int j=0;j<=tot;j++)
    				if (dp[i][j]>ans||(dp[i][j]==ans&&path[i][j]<str))
    					ans=dp[i][j],str=path[i][j];
    			if (ans==maxx) break;
    		}
    		cout<<str<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    [Scala] akka actor编程(一)
    随便说说
    [Java] Java执行Shell命令
    [Scala] Scala基础知识
    [Linux] 账户管理命令(二)
    [Linux] 账户管理命令(一)
    [Kerberos] Java client访问kerberos-secured cluster
    沟通与影响技术培训
    Python
    ML 基础知识
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8358979.html
Copyright © 2020-2023  润新知