Coj 1287 to find the number of occurrences of a matching string in a pattern string

Source: Internet
Author: User

This is to maintain a total number of strings that appear from the current location in the node of the suffix automaton

1#include <cstdio>2#include <cstring>3#include <iostream>4 5 using namespacestd;6 #defineN 2000107 #defineM 268 9 structsamnode{TenSamnode *son[ -] , *F; One     intL, SC; A     voidinit () { -f =NULL; -          for(intI=0; i< -; i++) Son[i] =NULL; theL = SC =0; -     } -}*root, *Last , Sam[n]; -  + intCNT, N, M, K, ret; - CharS[n]; +  A voidinit () { atsam[0].init (); -root = last = &sam[cnt=0]; -RET =0; - } -  - voidAddintx) in { -Samnode *p = &sam[++cnt], *jp=Last ; toP->init (); +P->l = jp->l+1; -P->sc=1; theLast =p; *      for(; jp&&!jp->son[x]; jp=jp->f) jp->son[x]=p; $     if(!JP) P->f =Root;Panax Notoginseng     Else{ -         if(jp->l+1= = jp->son[x]->l) P->f = jp->Son[x]; the         Else{ +Samnode *r = &sam[++cnt], *q = jp->Son[x]; AR->init (); the*r = *Q; +R->l = jp->l+1; -Q->f = P->f =R; $              for(; JP && jp->son[x]==q; jp=jp->f) jp->son[x]=R; $         } -     } -p =Last ; the      while(p->f) { -p = p->F;WuyiP->sc++; the     } - } Wu Charstr[ -]; - intSolve () About { $Samnode *cur =Root; -     intLen =strlen (str); -      for(intI=0; I<len; i++){ -         intv = str[i]-'a'; A         if(cur->Son[v]) { +Cur = cur->Son[v]; the         } -         Else{ $             return 0; the         } the     } the     returnCur->SC; the } -  in  the intMain () the { About    //freopen ("Data_in.txt", "R", stdin); the     intN, m, op; the      while(~SCANF ("%d%d", &n, &m)) the     { +scanf"%s", s); -  the init ();Bayi          for(intI=0; I<n; i++) Add (s[i]-'a'); the          while(m--){ the            //cout<<m<<endl; -scanf"%d", &op); -             if(op==2){ the               //cout<< "in" <<endl; thescanf"%s", str); theprintf"%d\n", Solve ()); the  -             } the             Else{ thescanf"%s", str); theAdd (str[0]-'a');94             } the         } the     } the     return 0;98}

Coj 1287 to find the number of occurrences of a matching string in a pattern string

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.