zoukankan      html  css  js  c++  java
  • POJ 2774 最长公共子串

    一定好好学SAM。。。模板在此:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<queue>
     6 #include<cstring>
     7 #define PAU putchar(' ')
     8 #define ENT putchar('
    ')
     9 using namespace std;
    10 const int maxn=250000+10,sig=26;
    11 struct SAM{
    12     struct node{
    13         int len;node*p,*tx[sig];
    14     }sam[maxn<<1],*nodecnt,*root,*x;
    15     void init(){nodecnt=sam;return;}
    16     node*newnode(int len=0){node*t=nodecnt++;t->len=len;return t;}
    17     void extend(int pos,char ch){
    18         int c=ch-'a';node*p=x;x=newnode(pos+1);
    19         for(;p&&!p->tx[c];p=p->p) p->tx[c]=x;
    20         if(!p) x->p=root;
    21         else{ node*q=p->tx[c];
    22             if(q->len==p->len+1) x->p=q;
    23             else{ node*r=newnode();
    24                 r[0]=q[0];r->len=p->len+1;q->p=x->p=r;
    25                 for(;p&&p->tx[c]==q;p=p->p) p->tx[c]=r;
    26             }
    27         } return;
    28     }
    29     void build(char*s){x=root=newnode();for(int i=0;s[i];i++)extend(i,s[i]);return;}
    30     int query(char*t){
    31         int len=strlen(t),ans=0;node*p=root;
    32         for(int i=0,L=0;i<len;i++){
    33             int c=t[i]-'a';
    34             if(p->tx[c])L++,p=p->tx[c];
    35             else{
    36                 for(;p&&!p->tx[c];p=p->p);
    37                 if(p) L=p->len+1,p=p->tx[c];
    38                 else p=root,L=0;
    39             } ans=max(L,ans);
    40         } return ans;
    41     }
    42 }sol;
    43 inline int read(){
    44     int x=0,sig=1;char ch=getchar();
    45     while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();}
    46     while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
    47     return x*=sig;
    48 }
    49 inline void write(int x){
    50     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
    51     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
    52     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
    53 }
    54 char s[maxn],t[maxn];
    55 void init(){
    56     scanf("%s%s",s,t);
    57     sol.init();sol.build(s);write(sol.query(t));
    58     return;
    59 }
    60 void work(){
    61     return;
    62 }
    63 void print(){
    64     return;
    65 }
    66 int main(){init();work();print();return 0;}

    注意一个copy的写法:r[0]=q[0];似乎就不用写一个copy函数了?(雾

  • 相关阅读:
    Linux中的文件类型
    Verilog定义变量类型为signed的几种情况
    verilog中>>>和>>的区别
    Linux中的快捷键
    Linux中的常用命令
    CVS版本控制
    [GitHub] fatal: unable to access 'https://github.com/': Failed to connect to github.com port 443: Operation timed out
    如何在手机(安卓)中搜索照片
    JS+CSS+HTML 前端开发(二)
    JS+CSS+HTML 前端开发(一)
  • 原文地址:https://www.cnblogs.com/chxer/p/4559600.html
Copyright © 2011-2022 走看看