好舍不得大家……心疼……
题目大意:把上一题的两个串改成多个串
对每一个串进行一次操作,记得把每个点的值传给自己的par
最后取个min就好了
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define N 200005
#define INF 1000000000
using namespace std;
int n,m,last = 1,tot = 1,p,q,np,nq,ans;
int son[N][26],par[N];
int P[N],num[N],mx[N],mv[N];
char s[N];
int new_node(int x)
{
mx[++ tot] = x;
return tot;
}
void add(int x)
{
p = last;
np = new_node(mx[p] + 1);
for (;p && !son[p][x];p = par[p]) son[p][x] = np;
if (!p) par[np] = 1;
else
{
q = son[p][x];
if (mx[q] == mx[p] + 1) par[np] = q;
else
{
nq = new_node(mx[p] + 1);
memcpy(son[nq],son[q],sizeof(son[nq]));
par[nq] = par[q],par[q] = par[np] = nq;
for (;son[p][x] == q;p = par[p]) son[p][x] = nq;
}
}
last = np;
}
int main()
{
scanf("%s",s + 1),n = strlen(s + 1);
for (int i = 1;i <= n;i ++) add(s[i] - 'a');
for (int i = 1;i <= tot;i ++) mv[mx[i]] ++;
for (int i = 1;i <= n;i ++) mv[i] += mv[i - 1];
for (int i = 1;i <= tot;i ++) P[mv[mx[i]] --] = i;
memset(mv,63,sizeof(mv));
while (~scanf("%s",s + 1))
{
int len = strlen(s + 1);
for (int i = 1,p = 1,l = 0;i <= len;i ++)
{
int x = s[i] - 'a';
if (son[p][x]) l ++,p = son[p][x];
else
{
for (;p && !son[p][x];p = par[p]);
if (!p) l = 0,p = 1;
else l = mx[p] + 1,p = son[p][x];
}
num[p] = max(num[p],l);
}
for (int i = tot;i > 1;i --)
{
int x = P[i];
mv[x] = min(mv[x],num[x]);
if (par[x] && num[x]) num[par[x]] = mx[par[x]];
num[x] = 0;
}
}
for (int i = 2;i <= tot;i ++) ans = max(ans,mv[i]);
cout << ans << endl;
return 0;
}