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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include<bits/stdc++.h> using namespace std;
void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f; }
void print(int x) { if(x<0) putchar('-'),x=-x; if(!x) return ;print(x/10),putchar(x%10+48); } void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double #define ll long long
#define pii pair<int,int > #define vec vector<int >
#define pb push_back #define mp make_pair #define fr first #define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 3e5+10; const int inf = 1e9; const lf eps = 1e-8; const int mod = 1e9+7;
char s[maxn],t[maxn]; int n,m,tr[maxn][26],fail[maxn],cnt,vis[maxn],fa[maxn],dep[maxn],f[maxn],g[maxn],h[maxn];
void ins() { int now=0,len=strlen(t+1); for(int i=1;i<=len;i++) { int r=t[i]-'a'; if(!tr[now][r]) tr[now][r]=++cnt,fa[cnt]=now,dep[cnt]=dep[now]+1; now=tr[now][r]; }vis[now]=1; }
void build_fail() { queue<int > q; for(int i=0;i<26;i++) if(tr[0][i]) q.push(tr[0][i]); while(!q.empty()) { int x=q.front();q.pop(); if(vis[x]) h[x]=dep[x]; else h[x]=h[fail[x]]; for(int i=0;i<26;i++) if(tr[x][i]) { fail[tr[x][i]]=tr[fail[x]][i]; q.push(tr[x][i]); } else tr[x][i]=tr[fail[x]][i]; } }
void get_suf() { int x=0; for(int i=1;i<=n;i++) x=tr[x][s[i]-'a'],f[i]=h[x]; }
#define ls p<<1 #define rs p<<1|1 #define mid ((l+r)>>1)
struct segment_tree { int t[maxn<<2];
void modify(int p,int l,int r,int x,int v) { if(l==r) return t[p]=v,void(); if(x<=mid) modify(ls,l,mid,x,v); else modify(rs,mid+1,r,x,v); t[p]=min(t[ls],t[rs]); }
int query(int p,int l,int r,int x,int y) { if(x<=l&&r<=y) return t[p]; int ans=1e9; if(x<=mid) ans=min(ans,query(ls,l,mid,x,y)); if(y>mid) ans=min(ans,query(rs,mid+1,r,x,y)); return ans; } }T;
int main() { read(m);scanf("%s",s+1);n=strlen(s+1); for(int i=1;i<=m;i++) scanf("%s",t+1),ins(); build_fail(); get_suf(); for(int i=1;i<=n;i++) { if(f[i]==i) g[i]=1; else if(f[i]) g[i]=T.query(1,1,n,i-f[i],i-1)+1; else g[i]=1e9; T.modify(1,1,n,i,g[i]>1e9?1e9:g[i]); }write(g[n]>=1e9?-1:g[n]); return 0; }
|