题目描述
给定一个只包含小写字母的字符串 S。
请你求出 S 的所有出现次数不为 11 的子串的出现次数乘上该子串长度的最大值。
输入格式
一行一个仅包含小写字母的字符串 S。
输出格式
一个整数,为所求答案。
题解:这里就不讲后缀自动机的模板相关问题了,网有很多关于后缀自动机的详解(推荐)。我在看完了后缀自动机之后做这题,卡住我的不是后缀自动机,而是以下操作
//Part 2 按len从大到小排序(和SA好像啊)后计算答案for(int i=1;i<=node;i++) t[len[i]]++;for(int i=1;i<=node;i++) t[i]+=t[i-1];for(int i=1;i<=node;i++) A[t[len[i]]--]=i;for(int i=node;i>=1;i--){//从小到大枚举,实际上在模拟parent树的DFSint now=A[i];siz[fa[now]]+=siz[now];if(siz[now]>1) ans=max(ans,1ll*siz[now]*len[now]);}
这段代码的要做的就是把节点 按节点代表最大字符串长度 排序,但我一直看不懂这里的代码逻辑,于是转换方式,用容易看懂的逻辑写代码。
方式一
struct edge
{int pos, len;bool operator<(edge e1){return len > e1.len;}
}e[N];//存节点及其代表的最大长度for (int i = 1; i <= tot; i++)
{e[i] = { i,nd[i].len };
}sort(e + 1, e + tot + 1);//按长度从大到小排序
//因为节点所代表的字符长度越长,这个节点就越接近于叶子节点
//所以排好序后,从小到大遍历也就是从叶子节点到根节点的遍历,把endpos的个数往上累加for (int i = 1; i <= tot; i++)
{siz[nd[e[i].pos].fa] += siz[e[i].pos];
}for (int i = 1; i <= tot; i++)
{if (siz[i] > 1)ans = max(ans, (long long)(siz[i] * nd[i].len));
}
方式二
int deg[N << 2];
queue<int> q;
void solve()
{while (!q.empty()) q.pop();memset(deg, 0, sizeof deg);for (int i = 1; i <= num; i++){++deg[a[i].fa];siz[i] = 0;}for (int i = 1; i <= num; i++){if (!deg[i]) q.push(i);//不是任何节点的父节点,即叶子节点入队}while (!q.empty()){int x = q.front();q.pop();if (pre[x])++siz[x];if (a[x].fa == 1) continue;siz[a[x].fa] += siz[x];if ((--deg[a[x].fa]) == 0)//该节点的子节点计算过一次,减一,当减为0后,即子节点全部计 //算完成,视为叶子节点入队{q.push(a[x].fa);}}long long ans = 0;for (int i = 1; i <= num; i++){if (siz[i] > 1){ans = max(ans, (long long)(siz[i] * a[i].len));}}cout << ans;
}
现在贴上这三种方式的完整代码
一,
#include<iostream>
#include<map>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
const long long N = 2e6 + 115;
struct node
{int fa, len;int son[26];long long size;node(){fa = len = 0;size = 0;memset(son, 0, sizeof son);}
}d[N];int las, tot,n;void add(int now)
{int p = las;int np = ++tot;las = np;d[np].size = 1;//作为实节点,原始endpos=1d[np].len = d[p].len + 1;for (; p && !d[p].son[now]; p = d[p].fa) d[p].son[now] = np;if (!p) d[np].fa = 1;else{int q = d[p].son[now];if (d[q].len == d[p].len + 1) d[np].fa = q;else{int nq = ++tot;d[nq] = d[q];d[nq].size = 0;//作为虚节点,原始endpos数=0d[nq].len = d[p].len + 1;d[q].fa = nq;d[np].fa = nq;for (; p && d[p].son[now] == q; p = d[p].fa) d[p].son[now] = nq;}}
}int tmp[N], tp[N];void so()
{for (int i = 0; i <= n; i++)tmp[i] = 0;for (int i = 1; i <= tot; i++)tmp[d[i].len]++;for (int i = 1; i <=n ; i++)tmp[i] += tmp[i - 1];for (int i = 1; i <=tot; i++)tp[tmp[d[i].len]--] = i;for (int i = tot; i >= 1; i--){int now = tp[i];d[d[now].fa].size += d[now].size;}
}void getans()
{long long ans = 0;for (int i = 1; i <= tot;i++){if (d[i].size > 1)ans = max(ans, d[i].size * d[i].len);}cout << ans;
}int main()
{string s;cin >> s;n = s.size();las = tot = 1;for (int i = 0; i < n; i++){add(s[i] - 'a');}so();getans();
}
二,
#include<iostream>
#include<map>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<vector>
using namespace std;
const long long N = 2e6 + 115;int siz[N];struct node
{int fa, len;int ch[26];node() { memset(ch, 0, sizeof(ch)); len = 0; }
}nd[N ];
int las = 1, tot = 1;
//las最近的一个实节点,tot节点总数
struct edge
{int pos, len;bool operator<(edge e1){return len > e1.len;}
}e[N];
void add(int c)
{int p = las, np;las = np = ++tot;siz[tot] = 1;nd[np].len = nd[p].len + 1;for (; p && !nd[p].ch[c]; p = nd[p].fa) nd[p].ch[c] = np;if (!p) nd[np].fa = 1;else{int q = nd[p].ch[c];if (nd[p].len + 1 == nd[q].len){nd[np].fa = q;}else{int nq = ++tot;nd[nq] = nd[q];nd[nq].len = nd[p].len + 1;nd[q].fa = nd[np].fa = nq;for (; p && nd[p].ch[c] == q; p = nd[p].fa) nd[p].ch[c] = nq;}}
}long long ans = 0;int main()
{string s;cin >> s;int le = s.size();for (int i = 0; i < le; i++){add(s[i] - 'a');}for (int i = 1; i <= tot; i++){e[i] = { i,nd[i].len };}sort(e + 1, e + tot + 1);for (int i = 1; i <= tot; i++){siz[nd[e[i].pos].fa] += siz[e[i].pos];}for (int i = 1; i <= tot; i++){if (siz[i] > 1)ans = max(ans, (long long)(siz[i] * nd[i].len));}cout << ans;}
三,
#include<iostream>
#include<map>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
const long long N = 1e6 + 115;int siz[N << 2];struct node
{int fa, len;int ch[26];node() { memset(ch, 0, sizeof(ch)); len = 0; }
}a[N << 2];int cnt[N << 2];int num = 1, np = 1;
bool pre[N << 2];
void add(int c)
{int p = np; np = ++num;pre[np] = true;//标记实节点a[np].len = a[p].len + 1;for (; p && !a[p].ch[c]; p = a[p].fa) a[p].ch[c] = np;if (!p) a[np].fa = 1;else{int q = a[p].ch[c];if (a[q].len == a[p].len + 1) a[np].fa = q;else{int nq = ++num;a[nq] = a[q];a[nq].len = a[p].len + 1;a[q].fa = a[np].fa = nq;for (; p && a[p].ch[c] == q; p = a[p].fa) a[p].ch[c] = nq;}}
}int deg[N << 2];
queue<int> q;
void solve()
{while (!q.empty()) q.pop();memset(deg, 0, sizeof deg);for (int i = 1; i <= num; i++){++deg[a[i].fa];siz[i] = 0;}for (int i = 1; i <= num; i++){if (!deg[i]) q.push(i);//不是任何节点的父节点,即叶子节点入队}while (!q.empty()){int x = q.front();q.pop();if (pre[x])++siz[x];if (a[x].fa == 1) continue;siz[a[x].fa] += siz[x];if ((--deg[a[x].fa]) == 0)//该节点的子节点计算过一次,减一,当减为0后,即子节点全部计 //算完成,视为叶子节点入队{q.push(a[x].fa);}}long long ans = 0;for (int i = 1; i <= num; i++){if (siz[i] > 1){ans = max(ans, (long long)(siz[i] * a[i].len));}}cout << ans;
}int main()
{string s;cin >> s;int le = s.size();for (int i = 0; i < le; i++){add(s[i] - 'a');}solve();}
特别注意:节点里的数组是整数数组,而不是字符数组,int ch[26] 而不是char ch[26] ,你猜我为什么着重提这个.....