您的位置:首页 > 其它

HDU 2296 Ring (AC自动机+DP,5级)

2013-09-07 12:43 302 查看
I - RingCrawling in process...Crawling failedTime Limit:1000MSMemory Limit:32768KB 64bit IO Format:%I64d & %I64uSubmitStatusAppoint description:System Crawler (2013-05-30)DescriptionFor 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, suchas "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 andthe 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 Specification1. T ≤ 152. 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
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
思路:找出AC转移态,dp[x][y]x个字符,y节点的最优值。
注意点:失败指针的所有子状态也得加上,虽然本题不加也能A
1
49 3
loveufoever
ever
al
12 7 3

答案应该是

aloveufoeveraloveufoeveraloveufoeveraloveufoever

最大的权值为88
[/code]
#include<iostream>#include<cstdio>#include<queue>#include<cstring>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))#define ll(x) (1<<x)using namespace std;const int msize=5100;const int INF=1;const int sig=26;const int MOD=20090717;class AC_Machine{public:int val[msize],ch[msize][sig],f[msize],sz,last[msize];void clear(){sz=1;clr(ch[0],0);val[0]=0;}int idx(char z){return z-'a';}void insert(char*s,int v){int u=0,c;for(int i=0;s[i];++i){c=idx(s[i]);if(!ch[u][c]){clr(ch[sz],0);val[sz]=0;ch[u][c]=sz++;}u=ch[u][c];}val[u]+=v;}void getFail(){int u,v,r;queue<int>Q;FOR(c,0,sig-1){u=ch[0][c];if(u){Q.push(u);f[u]=0;last[u]=0;}}while(!Q.empty()){r=Q.front();Q.pop();val[r]+=val[ f[r] ];//其包含的子孙也合并FOR(c,0,sig-1){u=ch[r][c];if(!u){ch[r][c]=ch[ f[r] ][c];continue;}Q.push(u);v=f[r];// while(v&&!ch[v][c])v=f[v];f[u]=ch[v][c];last[u]=val[ f[u] ]?f[u]:last[ f[u] ];}}}int print(int u){ int ret=0;while(u){ret+=val[u];u=last[u];}return ret;}void find(char*s){ int ret=0;int u=0,c;for(int i=0;s[i];++i){c=idx(s[i]);u=ch[u][c];if(val[u])ret+=print(u);else if(last[u])ret+=print(last[u]);}printf("asn=%d\n",ret);}};AC_Machine ac;char s[110][14];int dp[55][msize],N,M,K;//前xchar str[55][msize][55];bool cmp(char*s,char*t){int ls=strlen(s),lt=strlen(t);if(ls^lt)return ls<lt;else return strcmp(s,t)<0;}void getans(){ int u=0;clr(dp,-1);dp[0][0]=0;strcpy(str[0][0],"");char ans[55],tmp[55];int Max=0;strcpy(ans,"");FOR(i,1,N){FOR(j,0,ac.sz-1)if(dp[i-1][j]>=0){ strcpy(tmp,str[i-1][j]);int len=strlen(tmp);FOR(k,0,25){u=ac.ch[j][k];tmp[len]='a'+k;tmp[len+1]=0;int ret=dp[i-1][j];ret+=ac.val[u];if(dp[i][u]<ret||(dp[i][u]==ret&&cmp(tmp,str[i][u]))){dp[i][u]=ret;strcpy(str[i][u],tmp);if(ret>Max||(ret==Max&&cmp(tmp,ans))){Max=ret;strcpy(ans,tmp);}}}}}printf("%s\n",ans);}int main(){int cas,x;while(~scanf("%d",&cas)){while(cas--){ac.clear();scanf("%d%d",&N,&M);FOR(i,1,M){scanf("%s",s[i]);}FOR(i,1,M){scanf("%d",&x);ac.insert(s[i],x);}ac.getFail();getans();}}}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: