Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add palindromic tree #97

Draft
wants to merge 4 commits into
base: gmat
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions content/strings/PalindromicTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Author: Unknown
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acá te podes poner a vos

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Por ahora que no tengo idea ni de qué hace, lo dejo así

* Date: 2024-11-05
* License: CC0
* Source: notebook el vasito
* Status: untested
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Desciprción

*/

#pragma once

struct PalindromicTree {
static const ll alpha = 26;
struct Node {
ll len, link = 0, cnt = 1;
array<ll, alpha> to{};
};
vector<Node> ns;
ll last;
PalindromicTree() : last(0) {ns.pb({-1}); ns.pb({0});}
void add(ll i, string &s) {
ll p = last, c = s[i] - 'a';
while (s[i - ns[p].len - 1] != s[i]) p = ns[p].link;
if (ns[p].to[c]) last = ns[p].to[c], ns[last].cnt++;
else {
ll q = ns[p].link;
while (s[i - ns[q].len - 1] != s[i]) q = ns[q].link;
last = ns[p].to[c] = SZ(ns);
ns.pb({ns[p].len + 2, max(1ll, ns[q].to[c]), 1});
}
}
};
Loading