Spoj 8222 substrings (SAM)

Source: Internet
Author: User

indicate the source for reprint, thank you http://blog.csdn.net/ACM_cxlove? Viewmode = contents
by --- cxlove

Give a stringS, OrderF (x)IndicatesSIsXThe maximum number of occurrences of the substring. PleaseF (1) .. f (length (s )) (Thanks for CLJ's translation >_<)

Http://www.spoj.pl/problems/NSUBSTR/

When a Sam is created, the Len value of the node indicates the suffix length at that time. Although it is not necessarily a suffix, it can represent the length of a substring.

Starting from the current status, the number of paths that can reach the final state is the number of occurrences of the current length substring, and the maximum value is enough.

The method is to traverse the node topology on the Sam from the back to update the parent node from the child node (the pre on the Sam is not actually the parent node ).

The last case is that the sub-string is completely contained, that is, the long sub-string contains various short sub-strings.

The step of topology is to look at others, and the spoj time limit is too tight. Use length for a ing.

I still don't know much about Sam, but I can accept nodes with suffixes.

The simplest way is to traverse all the suffixes of a string.

# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define INF 100000005 # define M 40 # define n 510005 # define maxn 300005 # define EPS 1e-10 # define zero () FABS (a) <EPS # define min (A, B) (a) <(B )? (A) :( B) # define max (A, B) (a)> (B )? (A) :( B) # define Pb (a) push_back (a) # define MP (a, B) make_pair (a, B) # define MEM (A, B) memset (a, B, sizeof ()) # define ll unsigned long # define mod 1000000007 # define lson step <1 # define rson step <1 | 1 # define sqr (A) () * (a) # define key_value ch [CH [root] [1] [0] # define test puts ("OK"); # pragma comment (linker, "/Stack: 10241000000,1024000000") using namespace STD; struct Sam {Sam * Pre, * son [26]; int Len, G;} que [n ], * Root, * tail, * B [N]; int tot; void add (INT C, int L) {Sam * P = tail, * NP = & que [tot ++]; NP-> Len = L; tail = NP; while (P & P-> son [c] = NULL) p-> son [c] = NP, P = p-> pre; If (P = NULL) NP-> pre = root; else {Sam * q = p-> son [c]; If (p-> Len + 1 = Q-> Len) NP-> pre = Q; else {Sam * NQ = & que [tot ++]; * NQ = * q; NQ-> Len = p-> Len + 1; NP-> pre = Q-> pre = NQ; while (P & P-> son [c] = q) P-> son [c] = NQ, P = p-> pre; }}} char STR [n/2]; int DP [n/2]; int main () {While (SC ANF ("% s", STR )! = EOF) {int n = strlen (STR); Tot = 0; root = tail = & que [tot ++]; for (INT I = 0; I <N; I ++) add (STR [I]-'A', I + 1); int CNT [n/2]; MEM (CNT, 0 ); for (INT I = 0; I <tot; I ++) CNT [que [I]. len] ++; For (INT I = 1; I <= N; I ++) CNT [I] + = CNT [I-1]; for (INT I = 0; I <tot; I ++) B [-- CNT [que [I]. len] = & que [I]; for (INT I = 0; I <n; I ++) (Root = root-> son [STR [I]-'a'])-> G ++; MEM (DP, 0); For (INT I = tot-1; i> 0; I --) {DP [B [I]-> Len] = max (DP [B [I]-> Len], B [I]-> G); If (B [I]-> pre) B [I]-> pre-> G + = B [I]-> G ;} for (INT I = n-1; I> 0; I --) DP [I] = max (DP [I], DP [I + 1]); for (INT I = 1; I <= N; I ++) printf ("% d \ n", DP [I]);} return 0 ;}

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.