Finally, I learned how to multiply. First, I would like to apply an array of suffixes with the most watery and watery.
# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 1e6; char Buf [maxn]; int STR [maxn], Len, sa [maxn]; inline int idx (char c) {return C-'A';} int stra [maxn], strb [maxn], strcnt [maxn]; void build_sa (int * STR, int * Sa, int N, int m) {int I, j, * x = stra, * Y = strb, * CNT = strcnt, chcnt; // for (I = 0; I <m; I ++) CNT [I] = 0; for (I = 0; I <n; I ++) CNT [x [I] = STR [I] ++; for (I = 1; I <m; I ++) CNT [I] + = CNT [I-1]; for (I = n-1; I> = 0; I --) SA [-- CNT [x [I] = I; // sort by doubling length for (chcnt = 1, j = 1; chcnt <n; j <= 1, m = chcnt) {// sort by the second keyword, for (I = N-J, chcnt = 0; I <n; I ++) y [chcnt ++] = I; for (I = 0; I <n; I ++) if (SA [I]> = J) y [chcnt ++] = sa [I]-J; // sort for (I = 0; I <m; I ++) CNT [I] = 0; for (I = 0; I <N; I ++) CNT [x [Y [I] ++; (I = 1; I <m; I ++) CNT [I] + = CNT [I-1]; for (I = n-1; I> = 0; I --) SA [-- CNT [x [Y [I] = Y [I]; // recalculates the ranking array swap (x, y) based on the SA value ); for (chcnt = 1, x [SA [0] = 0, I = 1; I <n; I ++) {bool eql = Y [SA [I] = Y [SA [I-1] & Y [SA [I] + J] = Y [SA [I -1] + J]; X [SA [I] = eql? Chcnt-1: chcnt ++ ;}} char dict [maxn]; void query (INT Len) {int L = 0, r = Len, ansstr = 0, clen; clen = strlen (dict); While (L <= r) {int mid = (L + r)> 1; bool OK =! Strncmp (dict, BUF + SA [Mid], clen); If (OK) {ansstr = mid; r = mid-1;} else l = Mid + 1 ;} for (INT I = ansstr ;! Strncmp (dict, BUF + SA [I], clen); I ++) {printf ("find % s at POS % d \ n", dict, sa [I]) ;}} int main () {While (scanf ("% s", Buf )! = EOF) {Len = 0; while (BUF [Len]! = 0) {STR [Len] = idx (BUF [Len]); Len ++;} STR [Len] = 0; build_sa (STR, SA, Len + 1, 27); int N; scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% s ", dict); query (LEN + 1) ;}} return 0 ;}
Suffix array solves the problem of Online Multi-template matching