• 【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;
    }
  • 相关阅读:
    循环
    list和tuple
    Python字符串和编码注意点
    【转载】国内安卓推送的希望
    【转载】Android属性动画和视图动画的区别
    【转载】Android 属性动画详解
    【转载】 android 属性动画详解
    java8 新特性学习详细篇2
    java8 新特性详细篇
    JAVA8十大新特性
  • 原文地址:https://www.cnblogs.com/shxnb666/p/11279167.html
Copyright © 2020-2023  润新知