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

I - Ring
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description:

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

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


#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;//前x
char 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();
    }
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值