Suffix array solves the problem of Online Multi-template matching

Source: Internet
Author: User

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

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.