Ring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1859 Accepted Submission(s): 577
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.
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‘.
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.
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 ababHintSample 1: weight(love) = 5, weight(ever) = 5, so weight(lovever) = 5 + 5 = 10 Sample 2: weight(ab) = 2 * 5 = 10, so weight(abab) = 10
ac自动机+dp,要求把路径输出来,多开一维数组,记录每一个状态的路径,然后更新答案。
代码:
/* *********************************************** Author :xianxingwuguan Created Time :2014-2-5 13:02:53 File Name :3.cpp ************************************************ */ #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #include <string.h> #include <limits.h> #include <string> #include <time.h> #include <math.h> #include <queue> #include <stack> #include <set> #include <map> using namespace std; #define INF 0x3f3f3f3f #define eps 1e-8 #define pi acos(-1.0) typedef long long ll; int cmp(char *str1,char *str2){ int len1=strlen(str1); int len2=strlen(str2); if(len1!=len2) return len1<len2; return strcmp(str1,str2)<0; } char str[55][1100][55]; int dp[55][1100]; struct Trie{ int next[1200][26],fail[1200],end[1200]; int root,L; int newnode(){ for(int i=0;i<26;i++) next[L][i]=-1; end[L++]=-1; return L-1; } void init(){ L=0; root=newnode(); } void insert(char *str,int id){ int len=strlen(str); int now=root; for(int i=0;i<len;i++){ int p=str[i]-‘a‘; if(next[now][p]==-1) next[now][p]=newnode(); now=next[now][p]; } end[now]=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 now=q.front(); q.pop(); for(int i=0;i<26;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]); } } } void solve(int n){ for(int i=0;i<=n;i++) for(int j=0;j<=L;j++) dp[i][j]=-INF; dp[0][0]=0; char ans[110];strcpy(ans,""); int Max=0; strcpy(str[0][0],""); for(int i=0;i<n;i++) for(int j=0;j<L;j++) if(dp[i][j]!=-INF){ int len=strlen(str[i][j]); for(int k=0;k<26;k++){ char tmp[100]; strcpy(tmp,str[i][j]); tmp[len]=‘a‘+k;tmp[len+1]=‘\0‘; int tt=dp[i][j]; int pp=next[j][k]; if(end[pp]!=-1)tt+=end[pp]; if(tt>dp[i+1][pp]||(tt==dp[i+1][pp]&&cmp(tmp,str[i+1][pp]))){ dp[i+1][pp]=tt; strcpy(str[i+1][pp],tmp); if(dp[i+1][pp]>Max||(dp[i+1][pp]==Max&&cmp(str[i+1][pp],ans))){ strcpy(ans,str[i+1][pp]); Max=dp[i+1][pp]; } } } } printf("%s\n",ans); } }ac; char s[110][20]; int st[110]; int main() { //freopen("data.in","r",stdin); //freopen("data.out","w",stdout); int i,j,k,m,n,T; scanf("%d",&T); while(T--){ ac.init(); scanf("%d%d",&n,&m); for(i=0;i<m;i++)scanf("%s",s[i]); for(i=0;i<m;i++)scanf("%d",&st[i]); for(i=0;i<m;i++) ac.insert(s[i],st[i]); ac.build(); ac.solve(n); } return 0; }