Codeforces 696 D. Legen ...

Source: Internet
Author: User

Description

Each string has some value, asking how much value it can generate for a string of length \ (l\), the total number of characters not exceeding \ (200\), \ (L\leqslant 10^{14}\).

Sol

AC automata + multiply Floyd.

Use the AC automaton to count the weights that are obtained at each node.

Then in the AC automata from the root node to find a longest road, the Floyd multiplied on it ...

Find oneself AC automatic machine do fail tree when write wrong, changed for a long time did not change out, finally found can't directly throw the root into the queue ... All child nodes of the root should be thrown in.

Then you can do it like a quick power. Write the matrix when you need to determine whether to arrive, I use \ ( -1\) to indicate whether to arrive.

Transfer is \ (c[i][j]=max\{c[i][j],a[i][k]+b[k][j]\},a[i][k]!=-1,b[k][j]!=-1\).

Code
#include <bits/stdc++.h>using namespace Std;const int M = 205;const int N = 20050;typedef long Long ll;typedef vecto r< LL > Vec;typedef vector< Vec > Mat;void F (Mat &a,int v=-1) {int n=a.size (); for (int. i=0;i<n;i++) for ( int j=0;j<n;j++) A[i][j]=v;} MAT operator * (const MAT &a,const mat &b) {int n=a.size (); Mat C (N,vec (n)); F (C); for (int k=0;k<n;k++) for (int. i=0;i<n;i++) for (int j=0;j<n;j++) if (a[i][k]!=-1 && b[k][j]!=-1) c[i ][j]=max (C[i][j],a[i][k]+b[k][j]); return C;} Mat operator ^ (Mat a,ll b) {int n=a.size (); Mat R (N,vec (n)); F (R); for (int i=0;i<n;i++) r[i][i]=0;for (; b;b>>=1,a=a*a) if (b&1) R=r*a;return R;} void Print (const Mat &a) {int n=a.size (); for (int. i=0;i<n;i++) {for (int j=0;j<n;j++) cout<<a[i][j]< < ""; Cout<<endl;} cout<< "---------------------" &LT;&LT;ENDL;} struct Ac_auto {int rt,cnt;int f[n],ch[n][26],v[n];int NewNode () {++cnt;v[cnt]=f[cnt]=0,memset (ch[cnt],0,sizeof (ch[ CNT]); return CNT; }void init () {Cnt=rt=f[rt]=v[rt]=0;memset (ch[rt],0,sizeof (Ch[rt]));} void Insert (char *s,int val) {int L=strlen (s), o=rt;for (int i=0;i<l;i++) {if (!ch[o][s[i]-' a ']) ch[o][s[i]-' a ']= NewNode (); o=ch[o][s[i]-' A '];} V[o]+=val;} void Getfail () {queue< int > q;for (int i=0;i<26;i++) if (Ch[rt][i]) Q.push (Ch[rt][i]); for (;! Q.empty ();) {int X=q.front (); Q.pop (); for (int i=0;i<26;i++) {if (!ch[x][i]) Ch[x][i]=ch[f[x]][i];else Q.push (Ch[x][i]), f[ch[x] [I]] =ch[f[x]][i],v[ch[x][i]]+=v[f[ch[x][i]];}} for (int i=0;i<=cnt;i++) cout<<i<< "--" &LT;&LT;F[I]&LT;&LT;ENDL;} void make (Mat &a) {a.resize (Cnt+1,vec (cnt+1)); int n=a.size (); F (A); for (int. i=0;i<n;i++) for (int j=0;j<26;j++) a[i][ch[i][j]]=v[ch[i][j]],a[i][ch[rt][j]]=v[ch[rt][j];}} Ac LL N,l,ans; LL A[n];char S[n];int Main () {//freopen ("in.in", "R", stdin); Ios::sync_with_stdio (false); Ac.init ();cin>>n> >l;for (int i=1;i<=n;i++) cin>>a[i];for (int i=1;i<=n;i++) Cin>>s,ac.insert (S,a[i]); Ac.getFail (); Mat R;ac. Make (R);//print (R); r=r^l;for (int i=0;i< (int) r.size (); i++) Ans=max (Ans,r[0][i]);cout<<ans<<endl; return 0;}

  

Codeforces 696 D. Legen ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.