最长的子串至少出现两次

我正在尝试解决出现至少两次的最长子串。下面的代码给了我超时。怎么了如何改善?他们声明文本应包含大写拉丁字母,但这丝毫不影响它。

ABAAAABAB应该返回AAA。

#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;

class Node
{
public:
    char ch;
    unordered_map<char, Node*> children;
    vector<int> indexes;
    Node(char c) :ch(c) {}
};

int maxLen = 0;
string maxStr = "";

void insertInSuffixTree(Node* root, string str, int index, string originalSuffix, int level = 0)
{
    root->indexes.push_back(index);

    if (root->indexes.size() > 1 && maxLen < level)
    {
        maxLen = level;
        maxStr = originalSuffix.substr(0, level);
    }

    if (str.empty()) return;

    Node* child;
    if (root->children.count(str[0]) == 0) {
        child = new Node(str[0]);
        root->children[str[0]] = child;
    }
    else {
        child = root->children[str[0]];
    }

    insertInSuffixTree(child, str.substr(1), index, originalSuffix, level + 1);
}

int main()
{
    int num;
    string str;
    cin >> num;
    for (int i = 0; i < num; i++) {
        cin >> str;
        Node* root = new  Node('@');

        for (size_t i = 0; i < str.size(); i++) {
            string s = str.substr(i);
            insertInSuffixTree(root, s, i, s);
        }


    }
    cout << maxStr << endl;

    return 0;
}