后缀数组....
在两个串中唯一出现的最小公共子串
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;const int maxn=10100,INF=0x3f3f3f3f;int sa[maxn],rank[maxn],rank2[maxn],h[maxn],c[maxn],*x,*y,ans[maxn];
char str[maxn];bool cmp(int*r,int a,int b,int l,int n)
{if(r[a]==r[b]&&a+l<n&&b+l<n&&r[a+l]==r[b+l]) return true;return false;
}bool radix_sort(int n,int sz)
{for(int i=0;i<sz;i++) c[i]=0;for(int i=0;i<n;i++) c[x[y[i]]]++;for(int i=1;i<sz;i++) c[i]+=c[i-1];for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
}void get_sa(char c[],int n,int sz=128)
{x=rank,y=rank2;for(int i=0;i<n;i++) x[i]=c[i],y[i]=i;radix_sort(n,sz);for(int len=1;len<n;len*=2){int yid=0;for(int i=n-len;i<n;i++) y[yid++]=i;for(int i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len;radix_sort(n,sz);swap(x,y);x[sa[0]]=yid=0;for(int i=1;i<n;i++){x[sa[i]]=cmp(y,sa[i],sa[i-1],len,n)?yid:++yid;}sz=yid+1;if(sz>=n) break;}for(int i=0;i<n;i++) rank[i]=x[i];
}void get_h(char str[],int n)
{int k=0; h[0]=0;for(int i=0;i<n;i++){if(rank[i]==0) continue;k=max(k-1,0);int j=sa[rank[i]-1];while(i+k<n&&j+k<n&&str[i+k]==str[j+k]) k++;h[rank[i]]=k;}
}int main()
{cin>>str;int sg=strlen(str);str[sg]=127;cin>>str+sg+1;int n=strlen(str);get_sa(str,n);get_h(str,n);int ans=INF;int s1=0,s2=0,last=-1;for(int i=1;i<n;i++){if(sa[i-1]<sg&&sa[i]<sg) continue;if(sa[i-1]>sg&&sa[i]>sg) continue;int pre=h[i-1];int next=h[i+1];if(h[i]>max(pre,next)){ans=min(ans,max(pre,next)+1);}}if(ans==INF) ans=-1;printf("%d\n",ans);return 0;
}