Reprinted please indicate the source, thank youHttp://blog.csdn.net/acm_cxlove/article/details/7854526
By --- cxlove
Question: Give a string a, give several strings B, and ask how many different substrings in string a are not
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4416
Previously I used SA once, http://blog.csdn.net/acm_cxlove/article/details/8013942
I just learned Sam, but it took me a long time, sad
Create a Sam for string a, run all string B over LCS, record the maximum length of each position in string a, and use Len to remove the matching length.
However, note the following:
The first is the different substrings in A. This should be de-duplicated. First, the matching length should be initialized to the PRE Len, that is, the repeated substrings should be removed in the initial situation.
The next step is to run the string B every time and update the string to the PRE. The first step is to run the topology once.
Update every time, similar to spoj's lcs2, decisive t
After reading the zz1215 practice, I instantly understood that after a node is matched, the pre-represented string is the suffix of the current string and must have appeared, therefore, set the pre matching length to Len so that each node only needs to be updated once.
Make it easier to remember and be decisive.
However, the efficiency of SA and Sam is almost the same.
# Include <iostream> # include <cstdio> # include <map> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <set> # include <string> # include <queue> # define INF 1600005 # define M 40 # define n 210001 # define maxn 2000005 # define EPS 1e-7 # 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 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"); # define PI ACOs (-1.0) # define lowbit (X) (x) & (-(X) # pragma comment (linker, "/Stack: 1024000000,1024000000") # define VI vecto R <int> using namespace STD; struct Sam {Sam * Pre, * son [26]; int Len, ML;} * root, * tail, que [N], * B [N]; int tot; char STR [n/2]; void add (INT C, int L) {Sam * NP = & que [tot ++], * P = tail; 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 ;}} bool vis [N]; void Update (SAM * P) {If (P = NULL | Vis [p-que]) return; P-> ML = p-> Len; vis [p-que] = true; Update (p-> pre);} int main () {// freopen ("input.txt", "r", stdin ); int T, CAS = 0, N; scanf ("% d", & T); While (t --) {scanf ("% d % s", & N, str); Tot = 0; MEM (VIS, false); root = tail = & que [tot ++]; for (INT I = 0; STR [I]; I ++) add (STR [I]-'A', I + 1); For (INT I = 0; I <tot; I ++) if (que [I]. PRE) que [I]. ML = que [I]. pre-> Len; while (n --) {scanf ("% s", STR); Sam * P = root; int Len = 0; For (INT I = 0; STR [I]; I ++) {int c = STR [I]-'A'; If (p-> son [c]! = NULL) Len ++, P = p-> son [c], update (p-> pre ); else {While (P & P-> son [c] = NULL) P = p-> pre; If (P = NULL) Len = 0, P = root; else Len = p-> Len + 1, P = p-> son [c], update (p-> pre);} p-> ML = max (Len, p-> ML) ;}} ll ans = 0; For (INT I = 0; I <tot; I ++) {// printf ("% d \ n", que [I]. len, que [I]. ML); ans + = que [I]. len-que [I]. ML;} printf ("case % d: % i64d \ n", ++ cas, ANS); For (INT I = 0; I <tot; I ++) {que [I]. ML = 0; que [I]. pre = NULL; MEM (que [I]. son, null) ;}} return 0 ;}