zoukankan      html  css  js  c++  java
  • 【SPOJ】LCS

    题面

    https://vjudge.net/problem/SPOJ-LCS

    题解

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #define ri register int
    #define N 100050
    using namespace std;
    char s1[N],s2[N];
    struct SAM{
      int ff[N<<1],len[N<<1];
      int ch[N<<1][26];
      int last,tot;
      inline void clear() {
        last=tot=1;
        memset(ch,0,sizeof(ch));
      }
      inline void extend(int c) {
        int p,np,q,nq;
        p=last;np=++tot;last=tot;len[np]=len[p]+1;
        while(p && !ch[p][c]) ch[p][c]=np,p=ff[p];
        if (!p) ff[np]=1;
        else {
          q=ch[p][c];
          if (len[q]==len[p]+1) {
            ff[np]=q;
          }
          else {
            nq=++tot;
            len[nq]=len[p]+1;
            for (ri i=0;i<26;i++) ch[nq][i]=ch[q][i]; ff[nq]=ff[q];
            ff[np]=ff[q]=nq;
            while (p && ch[p][c]==q) ch[p][c]=nq,p=ff[p];
          }
        }
      }
    
      inline int match() {
        int ret=0;
        int cnt=0,now=1;
        for (ri i=1,l=strlen(s2+1);i<=l;i++) {
          if (ch[now][s2[i]-'a']) {
            cnt++;
            now=ch[now][s2[i]-'a'];
            ret=max(ret,cnt);
          }
          else {
            while (now && !ch[now][s2[i]-'a']) now=ff[now];
            if (!now) {
              cnt=0;
              now=1;
              continue;
            }
            else {
              cnt=len[now]+1;
              ret=max(ret,cnt);
              now=ch[now][s2[i]-'a'];
            }
          }
        }
        return ret;
      }
    } sam;
    
    int main(){
      scanf("%s%s",s1+1,s2+1);
      sam.clear();
      for (ri i=1,l=strlen(s1+1);i<=l;i++) sam.extend(s1[i]-'a');
      printf("%d
    ",sam.match());
      return 0;
    }
  • 相关阅读:
    android Textview动态设置大小
    小米1plus MIUI RadioButton的问题
    快读
    高精集合
    清北学堂part2
    清北学堂part1
    OTZ%%%子谦。大佬
    筛质数大优化
    回文日期
    高精度加法
  • 原文地址:https://www.cnblogs.com/shxnb666/p/11279167.html
Copyright © 2011-2022 走看看