题意:
对于给出的字符串S, 长度不超过1000,
求其中本质不同的子串的数量, 这些子串满足在字符串S中出现了至少不重合的2次
题解:
将串放入后缀自动机中然后求出每一个节点对应的子串为后缀的子串出现的最早和最晚的位置
然后根据
//return len[last] - len[fail[last]];//多添加一个子串所产生不同子串的个数 然后根据这个改一下
if (R[i] - L[i] > len[fail[i]]) ans += min(len[i], R[i] - L[i]) - len[fail[i]];
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map> #define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a, b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define sfi(a) scanf("%d", &a)
#define sffi(a, b) scanf("%d %d", &a, &b)
#define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define sfL(a) scanf("%lld", &a)
#define sffL(a, b) scanf("%lld %lld", &a, &b)
#define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
#define sfs(a) scanf("%s", a)
#define sffs(a, b) scanf("%s %s", a, b)
#define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
#define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
#define FIN freopen("../in.txt","r",stdin)
#define gcd(a, b) __gcd(a,b)
#define lowbit(x) x&-x
#define IO iOS::sync_with_stdio(false) using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const ULL seed = ;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 1e4 + ;
const int maxm = 8e6 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ; struct Suffix_Automaton {
int last, tot, nxt[maxn << ][], fail[maxn << ];//last是未加入此字符前最长的前缀(整个串)所属的节点的编号
int len[maxn << ];// 最长子串的长度 (该节点子串数量 = len[x] - len[fa[x]])
LL num[maxn << ];// 该状态子串的数量
LL maxx[maxn << ];// 长度为x的子串出现次数最多的子串的数目
LL sum[maxn << ];// 该节点后面所形成的自字符串的总数
LL subnum, sublen;// subnum表示不同字符串数目,sublen表示不同字符串总长度
int X[maxn << ], Y[maxn << ]; // Y表示排名为x的节点,X表示该长度前面还有多少个
int L[maxn << ], R[maxn << ];//L表示对应节点代表的数组的最早出现位置,R表示最晚出现位置 void init() {
tot = last = ;
fail[] = len[] = ;
for (int i = ; i <= ; i++) nxt[][i] = ; } void extend(int c) {
int u = ++tot, v = last;
for (int i = ; i <= ; i++) nxt[u][i] = ;
fail[u] = ;
L[u] = R[u] = len[u] = len[v] + ;
num[u] = ;
for (; v && !nxt[v][c]; v = fail[v]) nxt[v][c] = u;
if (!v) fail[u] = ;
else if (len[nxt[v][c]] == len[v] + ) fail[u] = nxt[v][c];
else {
int now = ++tot, cur = nxt[v][c];
len[now] = len[v] + ;
memcpy(nxt[now], nxt[cur], sizeof(nxt[cur]));
fail[now] = fail[cur];
fail[cur] = fail[u] = now;
L[now] = L[cur], R[now] = R[cur];
for (; v && nxt[v][c] == cur; v = fail[v]) nxt[v][c] = now;
}
last = u;
//return len[last] - len[fail[last]];
} void get_sa() { // Y表示排名为x的节点,X表示该长度前面还有多少个
for (int i = ; i <= tot; i++) X[i] = ;
for (int i = ; i <= tot; i++) X[len[i]]++;
for (int i = ; i <= tot; i++) X[i] += X[i - ];
for (int i = ; i <= tot; i++) Y[X[len[i]]--] = i;
} int get_L_R() {
int ans = ;
for (int i = tot; i; i--) {
L[fail[Y[i]]] = min(L[fail[Y[i]]], L[Y[i]]);
R[fail[Y[i]]] = max(R[fail[Y[i]]], R[Y[i]]);
}
for (int i = ; i <= tot; i++)//不相交且出现次数至少2的子串个数
if (R[i] - L[i] > len[fail[i]]) ans += min(len[i], R[i] - L[i]) - len[fail[i]];
return ans;
}
} sam; char s[maxn]; int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
while (~sfs(s)) {
if (s[] == '#') break;
sam.init();
int len = strlen(s);
for (int i = ; i < len; ++i) sam.extend((s[i] - 'a'));
sam.get_sa();
printf("%d\n", sam.get_L_R());
}
#ifndef ONLINE_JUDGE
cout << "Totle Time : " << (double) clock() / CLOCKS_PER_SEC << "s" << endl;
#endif
return ;
}