I - Ring

Crawling in process...

Crawling failed

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

​Submit​​​ ​​​Status​

System Crawler (2013-05-30)

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

`27 2loveever5 55 1ab5`

Sample Output

`loveverabab`

Hint

`Sample 1: weight(love) = 5, weight(ever) = 5, so weight(lovever) = 5 + 5 = 10Sample 2: weight(ab) = 2 * 5 = 10, so weight(abab) = 10`
`思路：找出AC转移态，dp[x][y]x个字符，y节点的最优值。`
`注意点：失败指针的所有子状态也得加上，虽然本题不加也能A`
`149 3loveufoevereveral12 7 3答案应该是aloveufoeveraloveufoeveraloveufoeveraloveufoever最大的权值为88`
`#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);val=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[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;int dp[msize],N,M,K;//前xchar str[msize];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;  strcpy(str,"");  char ans,tmp;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();    }  }}`