序列自动机

好像很高端的样子。。。

其实真的很简单。。。

序列自动机只有当数的类型比较少的时候才适用,一般出现字符串中,所以我们不妨设我们要处理的为字符串$A$

然后我们只需要记住第$i$个位置后字母$j$最早出现的位置即可。

就是这么简单

时间复杂度$O(26N)$

序列自动机的功能就是能遍历所有的子序列

好像非常冷门的样子。。。。。。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

inline int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
inline LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }

const int maxN=500000;
const int maxs=26;

struct sequence_automation
  {
      struct Tnode
        {
            int ch[maxs+1],fail;
            inline Tnode(){mmst(ch,0);fail=0;}
        }a[maxN+100];int MID;
      int last[maxs+1];
      inline int newnode(){a[++MID]=Tnode();return MID;}
      inline void clear(){MID=0;mmst(last,0);last[maxs]=newnode();}
      inline void add(int x)
        {
            int i,o=newnode();
            re(i,0,maxs)
              for(int p=last[i];p && !a[p].ch[x];p=a[p].fail)a[p].ch[x]=o;
            a[o].fail=last[x],last[x]=o;
        }
      inline void insert(char *s)
        {
            int i,len=strlen(s+1);
            clear();
            re(i,1,len)add(s[i]-'a');
        }
  };
View Code

 

posted @ 2015-09-04 14:16  maijing  阅读(1123)  评论(0编辑  收藏  举报