Bzoj 1212 HNOI2004 L language ac automata (trie tree) + Dynamic programming

Source: Internet
Author: User

Topic: Given a Word table and M strings ask the longest prefix of each string, this prefix can be split into strings so that the strings appear in the Word table.

No longer dare to look at the wrong data range ... A problem clearly with trie tree can solve problems incredibly I wrote AC automata ...

Insert the words in the word list into the AC automaton the length of the word is recorded on the node where each word resides

Then for each string use F[i] to indicate whether the prefix of length I can be split into words in the Word table run AC automata

For each matching node from this node start to the fail path of the root all Len F[i]|=f[i-len]

Find the biggest f[i for 1] that's the answer.

Since the maximum word length is 10, you can use trie tree violence directly

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1050000using namespace Std;struct trie{int len; Trie *fail,*son[26];void* operator new (size_t size);} *root,*mempool,*c;int N,m;char s[m];void* Trie:: operator new (size_t size) {if (C==mempool) {c=new trie[1<<15]; mempool=c+ (1<<15); memset (c,0,sizeof (Trie) * (1<<15));} return C + +;} void Insert (Trie*&p,char *pos,int DPT) {if (!p) p=new trie;if (!*pos) {P->len=dpt;return;} Insert (p->son[(*pos)-' a '],pos+1,dpt+1);} void BFS () {static trie* q[1<<16];static unsigned short r,h;int i; Trie *temp;for (i=0;i<26;i++) if (Temp=root->son[i]) {temp->fail=root;q[++r]=temp;} while (r!=h) {Trie *p=q[++h];for (i=0;i<26;i++) if (P->son[i]) {temp=p->fail;while (temp!=root &&!) Temp->son[i]) temp=temp->fail;if (Temp->son[i]) temp=temp->son[i];p->son[i]->fail=temp;q[++r]= P->son[i];}}} int Aho_corasick_automaton () {int i,re=0; Trie *p=root,*temp;static bool F[m];f[0]=1;for (i=1;s[i];i++) {f[i]=0;while (p!=root &&!p->son[s[i]-' a ']) p=p->fail;if (p-> Son[s[i]-' A ']) {p=p->son[s[i]-' a '];for (temp=p;temp!=root;temp=temp->fail) if (Temp->len) {f[i]|=f[i-temp- >len];if (F[i]) break;}} if (F[i]) re=i;} return re;} int main () {int i;cin>>n>>m;for (i=1;i<=n;i++) scanf ("%s", s+1), Insert (root,s+1,0); BFS (); for (i=1;i<=m;i++) {scanf ("%s", s+1); Cout<<aho_corasick_automaton () <<endl;}}


Bzoj 1212 HNOI2004 L language ac automata (trie tree) + Dynamic programming

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.