spoj1812 longest Common Substring II (suffix automaton)

Source: Internet
Author: User

Put a code ...

---------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 200009;Const int cn = +;struct Node {Node *fa, *CH[CN];int len, Min, Max;void Upd () {min = min (min, Max);}} POOL[MAXN], *pt, *root, *last;void Upd (node* &t, int &v) {T->max = Max (T->max, v);}node* newNode (int v) {pt->fa = NULL;Pt->len = v;memset (pt->ch, 0, sizeof pt->ch);return pt++;}void Sam_init () {pt = pool;root = last = NewNode (0);}void Extend (int c) {Node *p = last, *NP = NewNode (P->len + 1);For (; p &&!p->ch[c]; p = p->fa)P->ch[c] = NP;if (!p)np->fa = root;else {node* q = p->ch[c];if (p->len + 1 = = Q->len)NP->FA = q;else {node* NQ = NewNode (P->len + 1);memcpy (nq->ch, q->ch, sizeof q->ch);NQ->FA = q->fa;Q->fa = Np->fa = NQ;For (; p && p->ch[c] = = q; p = p->fa)P->ch[c] = NQ;}}Last = NP;} Char S[MAXN];void Sam_build () {scanf ("%s", s);for (int i = 0, n = strlen (s); i < n; i++)Extend (S[i]-' a ');}bool Work () {if (scanf ("%s", s) = =-1)return false;For (node* t = root; t! = pt; t++)T->max = 0;int n = strlen (s);node* t = root;for (int i = 0, lth = 0; i < n; i++) {int c = s[i]-' a ';if (T->ch[c])upd (t = t->ch[c], ++lth);else {for (; t &&!t->ch[c]; t = t->fa);if (!t)t = root, lth = 0;Elselth = T->len + 1, upd (t = t->ch[c], lth);}}For (node* o = pt;--o! = pool;)upd (O->FA, O->max);For (node* o = pt;--o;) {o->upd ();if (o = = Pool) break;}return true;} int main () {sam_init ();sam_build ();For (node* t = root; t! = pt; t++)t->min = t->len;while (work ());int ans = 0;For (node* t = root; t! = pt; t++)ans = max (ans, t->min);printf ("%d\n", ans);return 0;}

---------------------------------------------------------------------

spoj1812 longest Common Substring II (suffix automaton)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.