zoukankan      html  css  js  c++  java
  • [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;
    }
    
  • 相关阅读:
    python接口自动化之发送post(四)
    python接口自动化之发送get(三)
    python接口自动化之fiddler使用(二)
    python读取yaml配置文件
    python接口自动化测试之http协议(一)
    python接口自动化测试之根据excel中的期望结果是否存在于请求返回的响应值中来判断用例是否执行成功
    python3读取、写入、追加写入excel文件
    python UI自动化之处理多窗口
    python UI自动化之js操作
    python UI自动化之切换iframe
  • 原文地址:https://www.cnblogs.com/zhoushuyu/p/8358979.html
Copyright © 2011-2022 走看看