RT, WA 点 3 和其他某些点, 比着 SAM 的代码打的, 答案差的很小, 只有个位数
救救孩子吧
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define Wild_Donkey 0
using namespace std;
inline unsigned RD() {
unsigned intmp = 0;
char rdch(getchar());
while (rdch < '0' || rdch > '9') {
rdch = getchar();
}
while (rdch >= '0' && rdch <= '9') {
intmp = intmp * 10 + rdch - '0';
rdch = getchar();
}
return intmp;
}
inline int RDsg() {
int rdtp(0), rdsg(1);
char rdch(getchar());
while ((rdch < '0' || rdch > '9') && (rdch != '-')) {
rdch = getchar();
}
if (rdch == '-') {
rdsg = -1;
rdch = getchar();
}
while (rdch >= '0' && rdch <= '9') {
rdtp = rdtp * 10 + rdch - '0';
rdch = getchar();
}
return rdtp * rdsg;
}
unsigned a[10005], m, n, Cnt(0), t, Ans(0), Tmp(0), len(0), queueHead(0), queueTail(0);
bool b[10005];
char s[1000006];
struct Node {
Node *To[26], *Father, *Link;
char Visited, Character, toAgain[26];
unsigned Length;
}N[2000005], *now(N), *CntN(N), *Queue[1000005], *A, *C_c;
inline void Clr() {}
int main() {
// double Ti(clock()), Mti(0);
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
// t = RD();
// for (register unsigned T(1); T <= t; ++T){
// Clr();
n = RD();
N[0].Length = 0;
for (register unsigned i(1); i <= n; ++i) { // 读入 + 建 Trie
scanf("%s", s);
len = strlen(s);
now = N;
for (register unsigned j(0); j < len; ++j) {
s[j] -= 'a';
if(!(now->To[s[j]])) {
now->To[s[j]] = ++CntN;
CntN->Father = now;
CntN->Character = s[j];
CntN->Length = now->Length + 1;
}
now = now->To[s[j]];
}
}
Queue[++queueTail] = N; // 初始化队列, 准备 BFS
while (queueHead < queueTail) {
// printf("Hello\n"); // 简单的 BFS
now = Queue[++queueHead];
for (register char i(0); i < 26; ++i) {
// printf("%u\n", i);
if(now->To[i]) {
if(!(now->To[i]->Visited)) {
Queue[++queueTail] = now->To[i];
now->To[i]->Visited = 1;
}
}
}
}
// printf("done\n");
for (register unsigned i(2); i <= queueTail; ++i) { // BFS 留下的队列便是 BFS 序
now = Queue[i];
// printf("Test %u %u %u\n", now - N, i, now->Father - N);
A = now->Father;
while (A && !(A->toAgain[now->Character])) {
A->toAgain[now->Character] = 1;
A->To[now->Character] = now;
A = A->Link;
}
if(!A) {
now->Link = N;
continue;
}
if((A->Length + 1) ^ (A->To[now->Character]->Length)) {
// printf("Where\n");
C_c = A->To[now->Character];
A->To[now->Character] = ++CntN;
CntN->Length = A->Length + 1;
CntN->Link = C_c->Link;
memcpy(CntN->To, C_c->To, sizeof(C_c->To));
memcpy(CntN->toAgain, C_c->toAgain, sizeof(C_c->toAgain));
now->Link = CntN;
C_c->Link = CntN;
CntN->Character = C_c->Character;
while (A && A->To[C_c->Character] == C_c) {
A->To[C_c->Character] = CntN;
A = A->Link;
}
continue;
}
now->Link = A->To[now->Character];
}
for (register Node *i(N + 1); i <= CntN; ++i) {
// printf("%u Link %u Father %u\n", i - N, i->Link - N, i->Father - N);
Ans += i->Length - i->Link->Length;
// printf("Ans %u %u %u %u %u\n", Ans, i - N, i->Length, i->Link - N, i->Link->Length);
}
printf("%u\n", Ans);
// }
// Ti = clock() - Ti;
// printf("Time %lf MTime %lf\n", Ti, Mti);
// system("pause");
// fclose(stdin);
// fclose(stdout);
return Wild_Donkey;
}