Bzoj 1031: [JSOI2007] character encryption cipher

Source: Internet
Author: User

This is a string of questions, see, the first reaction is to get them all out after sorting, but looked at the following data range, not to mention the time, space may not open. Instead of thinking about data structures. Processing strings with trie, suffix arrays, and hash-based LCP, a little bit, hash-based LCP can be string matching, but the size of it! I don't know how to do it, trie can be used as a dictionary, but to compare size ... (Will be very troublesome, I am not very clear ah!) )。 But the suffix array has only been seen once, and not the General Assembly, take this opportunity to learn! It's pretty good after all. It is said that the suffix array has two ways of writing, I will only learn to multiply well, last night spent some time to understand the first half, but the latter part is not quite understand. Write it as a black box code! If a great God knows the principle of the latter part of the operation, ask for advice.

This is the code for Postfix array multiplication (copied from LRJ petition).

1 #defineRep (i,j,k) for (int i = j; I <= K; i++)2 #defineDown (i,j,k) for (int i = j; I >= K; i--)3 4 CharS[MAXN];5 intSA[MAXN], T[MAXN], T2[MAXN], C[MAXN], N;6 7 voidBuild_sa (intm)8 {9     intI, *x = t, *y =T2;TenRep (I,0, M-1) C[i] =0; OneRep (I,0, N-1) C[x[i] = s[i]]++; ARep (I,1, M-1) C[i] + = c[i-1]; -Down (i,n-1,1) Sa[--c[x[i]] =i; -      for(intK =1; K <= N; K <<=1){ the         intp =0; Rep (i,n-k,n-1) y[p++] =i; -Rep (I,0, N-1)if(Sa[i] >= k) y[p++] = Sa[i]-K; -Rep (I,0, M-1) C[i] =0; -Rep (I,0, N-1) c[x[y[i]]]++; +Rep (I,1, M-1) C[i] + = c[i-1]; -Down (i,n-1,0) sa[--c[x[y[i] []] =Y[i]; + swap (x, y); Ap =1, x[sa[0]] =0; atRep (I,1, N-1) -X[sa[i]] = y[sa[i]] = = y[sa[i-1]] && y[sa[i-1]+k] = = Y[sa[i]+k]? P-1: p++; -         if(P >= N) Break; -m =p; -     } -}

Sa[i] is the position of the first character of the suffix that is ranked I.

The next is the code (it's not hard to know the suffix)

1#include <cstdio>2#include <iostream>3#include <cstring>4 #defineRep (i,j,k) for (int i = j; I <= K; i++)5 #defineDown (i,j,k) for (int i = j; I >= K; i--)6 #defineMAXN 2001007 using namespacestd;8 9 CharS[MAXN];Ten intSA[MAXN], T[MAXN], T2[MAXN], C[MAXN], N; One  A voidBuild_sa (intm) - { -     intI, *x = t, *y =T2; theRep (I,0, M-1) C[i] =0; -Rep (I,0, N-1) C[x[i] = s[i]]++; -Rep (I,1, M-1) C[i] + = c[i-1]; -Down (i,n-1,1) Sa[--c[x[i]] =i; +      for(intK =1; K <= N; K <<=1){ -         intp =0; Rep (i,n-k,n-1) y[p++] =i; +Rep (I,0, N-1)if(Sa[i] >= k) y[p++] = Sa[i]-K; ARep (I,0, M-1) C[i] =0; atRep (I,0, N-1) c[x[y[i]]]++; -Rep (I,1, M-1) C[i] + = c[i-1]; -Down (i,n-1,0) sa[--c[x[y[i] []] =Y[i]; - swap (x, y); -p =1, x[sa[0]] =0; -Rep (I,1, N-1) inX[sa[i]] = y[sa[i]] = = y[sa[i-1]] && y[sa[i-1]+k] = = Y[sa[i]+k]? P-1: p++; -         if(P >= N) Break; tom =p; +     } - } the  * intMain () $ {Panax Notoginsengscanf"%s", s); -n =strlen (s); theRep (I,0, N-1){ +S[i+n] =S[i]; A     } theN *=2; +Build_sa ( -); -     intSize = N/2; $Rep (I,0, N-1){ $         if(Sa[i] <size) { -printf"%c", s[sa[i]+size-1] ); -         } the     } -cout<<Endl;Wuyi     return 0; the}

Bzoj 1031: [JSOI2007] character encryption cipher

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.