题意:
给出一个字符串,问有几个不同子串
思路:
问不同子串的个数,如果用 trie 树来理解的话就是,问 trie 树的节点个数。算法复杂度在O(n^2)
代码:( trie 树的建立使用了 Kuangbin 大大的 AC自动机模板)
由于 AC 自动机大部分时间损耗在初始化 每个节点上,故经过“测试”,本题字符在 ' ! ' 到 ' Z ' 之间,优化至 70 ms。(若直接用 128 的数组,160ms已超时)
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
int ans;char buf[2000];
struct Trie
{
int next[500010][60];
int root,L;
int newnode()
{
for(int i = 0; i < 60; i++)
next[L][i] = -1;
L++;
ans++;
return L-1;
}
void init()
{
L = 0;
root = newnode();
}
void insert(int k,int len)
{
int now = root;