1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| #include<bits/stdc++.h> using namespace std;
typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair <int,int> Pii; #define reg register #define mp make_pair #define pb push_back #define Mod1(x) ((x>=P)&&(x-=P)) #define Mod2(x) ((x<0)&&(x+=P)) #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i) template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); } template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); }
char IO; template <class T=int> T rd(){ T s=0; int f=0; while(!isdigit(IO=getchar())) f|=IO=='-'; do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return f?-s:s; }
const int N=3010,M=N*N/2,INF=1e9+10; bool Mbe;
int n; int nxt[N][26]; char A[N],B[N]; struct Edge{ int c,to,nxt; } e[M]; int head[M],cnt;
bool Med; int main(){ freopen("block.in","r",stdin),freopen("block.out","w",stdout); n=rd(),scanf("%s%s",A+1,B+1); drep(i,n,1) { rep(j,0,25) nxt[i][j]=nxt[i+1][j]; nxt[i][A[i]-'a']=i; } rep(i,1,n) { int u=0,p=0; rep(j,i,n) { int c=B[j]-'a'; if(!(p=nxt[p+1][c])) break; int v=-1; for(int k=head[u];k;k=e[k].nxt) if(e[k].c==c) { v=e[k].to; break; } if(~v) u=v; else { v=++cnt; e[v]=(Edge){c,v,head[u]}; head[u]=v,u=v; } } } printf("%d\n",cnt); }
|