[coci2012] cover string AC automata

Source: Internet
Author: User

Given a lowercase string of length n, now Mirko has m several lengths of Li Strings. Now Mirko is going to overwrite the given string with this m string. When covered, you must ensure that:
1.Mirko string cannot be disassembled, rotated;
A string of 2.Mirko must be exactly the same as the given string to be overwritten,
3. Several overrides can be partially overlapped
The 4.Mirko string can be used indefinitely.
The number of letters in the string to be given cannot be overwritten.

Children, as an elder, I think it is necessary for me to impart some life experience to you.

A bunch of functions of string, use caution with prudence;

I only because not careful, the strlen (s), strlen (CH) and other functions written in the loop, resulting in infinite tle tragedy;

The figure is as follows:

Two strlen in the inside:

#01:Accepted(15MS, 322696KB)
#02:Accepted(15MS, 322696KB)
#03:Accepted(187ms, 322696KB)
#04:Accepted(3234ms, 322696KB)
#05:Accepted(3453MS, 322624KB)
#06:Time Limit exceeded(?, 322624KB)
#07:Time Limit exceeded(?, 322624KB)
#08:Time Limit exceeded(?, 322624KB)
#09:Time Limit exceeded(?, 322624KB)
#10:Time Limit exceeded(?, 322624KB)
#11:Time Limit exceeded(?, 322624KB)
#12:wrong Answer(5000ms, 322624KB)
#13:wrong Answer(5000ms, 322624KB)
#14:Time Limit exceeded(?, 322624KB)
#15:Time Limit exceeded(?, 322624KB)
#16:Time Limit exceeded(?, 322624KB)
#17:wrong Answer(5000ms, 322624KB)
#18:wrong Answer(5000ms, 322624KB)
#19:Time Limit exceeded(?, 322624KB)

Do not know will think me violent enumeration;

A strlen in the loop:

#01:Accepted(15MS, 345056KB)
#02:Accepted(0MS, 345056KB)
#03:Accepted(0MS, 345056KB)
#04:Accepted(218ms, 345056KB)
#05:Accepted(78ms, 345056KB)
#06:Accepted(93MS, 345056KB)
#07:Accepted(46ms, 345056KB)
#08:Accepted(1656ms, 345056KB)
#09:Time Limit exceeded(?, 345056KB)
#10:Accepted(93MS, 344984KB)
#11:Accepted(125MS, 344984KB)
#12:Accepted(1562ms, 344984KB)
#13:Time Limit exceeded(?, 344984KB)
#14:Accepted(171ms, 344984KB)
#15:Accepted(500ms, 344984KB)
#16:Accepted(1640ms, 344984KB)
#17:wrong Answer(5000ms, 344984KB)
#18:Accepted(250ms, 344984KB)
#19:Accepted(734ms, 344984KB)

There are still three groups of super;

When there is no strlen in the inside:

#01:Accepted(0MS, 357712KB)
#02:Accepted(0MS, 357712KB)
#03:Accepted(15MS, 357712KB)
#04:Accepted(78ms, 357712KB)
#05:Accepted(93MS, 357712KB)
#06:Accepted(46ms, 357712KB)
#07:Accepted(46ms, 357712KB)
#08:Accepted(375MS, 357712KB)
#09:Accepted(187ms, 357712KB)
#10:Accepted(93MS, 357712KB)
#11:Accepted(31ms, 357712KB)
#12:Accepted(359ms, 357712KB)
#13:Accepted(328ms, 357712KB)
#14:Accepted(187ms, 357712KB)
#15:Accepted(125MS, 357712KB)
#16:Accepted(359ms, 357712KB)
#17:Accepted(468ms, 357712KB)
#18:Accepted(281ms, 357712KB)
#19:Accepted(140ms, 357712KB)

After knocking the question, I have tears in my eyes.

At the same time, I knocked through the problem several times the AC automaton template;

Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <cstdlib>6#include <ctime>7#include <vector>8#include <algorithm>9#include <queue>Ten#include <map> One using namespacestd; A #defineLL Long Long - Const intmaxn=2500010; - intn,m; the Chars[303000],ch[5050]; - BOOLvis[ -][ -][ -][ -][ -]; - intlinkk[1000000][ -],flag[maxn],f[maxn],q[maxn],g[maxn],k[maxn],tail=0, head=1, len=0, NEXT[MAXN]; - voidInsert () { +     intnow=0, p=strlen (CH); -      for(intI=0; i<p;i++){ +         if(!linkk[now][ch[i]]) linkk[now][ch[i]]=++len,g[len]=Ch[i]; Anow=Linkk[now][ch[i]]; at         if(i==p-1) flag[now]=p; -     } - } - voidinit () { -     //printf ("first:%d\n", Clock ()); -scanf"%d%s%d",&n,s,&m); in      for(intI=0; i<n;i++) s[i]=s[i]-'a'+1; -      for(intI=4; i<n;i++) vis[s[i-4]][s[i-3]][s[i-2]][s[i-1]][s[i]]=1; to     //printf ("second:%d\n", Clock ()); +      for(intI=1; i<=m;i++){ -scanf"%s", ch); the         BOOLp=0; *         intu=strlen (CH); $          for(intj=0; j<u;j++) ch[j]=ch[j]-'a'+1;Panax Notoginseng          for(intj=4; j<u;j++) -             if(!vis[ch[j-4]][ch[j-3]][ch[j-2]][ch[j-1]][CH[J]]) {p=1; Break;} the         if(!p) Insert (); +     } A     //printf ("init:%d\n", Clock ()); the } + voidBFs () { -Head=0, tail=0; q[++tail]=0;intx=0, now=0, temp; $      while(++head<=tail) { $x=Q[head]; -          for(intI=1; i<= -; i++){ -             if(Linkk[x][i]) { thenow=linkk[x][i],temp=F[x]; -                 if(x) {Wuyi                      while(Temp&&!linkk[temp][i]) temp=F[temp]; thef[now]=Linkk[temp][i]; -                     if(Flag[f[now]]) next[now]=F[now]; Wu                     Elsenext[now]=Next[f[now]]; -                 } Aboutq[++tail]=Now ; $             } -         } -     } - } A voidWork () { + BFS (); the     //printf ("bfs:%d\n", Clock ()); -     intnow=0; $      for(intI=0; i<n;i++){ the         if(!Linkk[now][s[i]]) { the             inttemp=F[now]; the              while(!linkk[temp][s[i]]&&temp) temp=F[temp]; thenow=Linkk[temp][s[i]]; -         } in         Elsenow=Linkk[now][s[i]]; the         inttemp=Now ; the          while(temp) { About             if(Flag[temp]) k[i-flag[temp]+1]=Flag[temp]; thetemp=Next[temp]; the         } the     } +     //printf ("work:%d\n", Clock ()); -     intlast=-1, sum=0; the      for(intI=0; i<n;i++){Bayi         if(K[i]) Last=max (last,k[i]+i-1); the         if(i>last) sum++; the     } -cout<<sum<<Endl; -     //cout<<clock () <<endl; the } the intMain () { theFreopen ("1.in","R", stdin); theFreopen ("1.out","W", stdout); - init (); the Work (); the}
View Code

[coci2012] cover string AC automata

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.