HDU 5008 Xi'an Network Competition B Question: suffix array find the K kiddie string

Source: Internet
Author: User

Idea: Nima, this question has been around for a day. During the competition, I used the N ^ 2 method, absolutely T. Then I learned it today after reading other people's code for a day. It seems quite easy to follow, that is, I did not expect that spoj 694 705 has been used to find different substrings. I know how to find the number of different substrings, however, this skill was left behind during the competition, and then it won't happen.

But today I used my own template with two suffixes (multiply and DC3), and I really wanted to jump to the building !!

Up to now, I don't know what went wrong. The processing method and standard practice are the same, but it is wa. Then I used another template and used my own processing method, I doubt if I have made any mistakes in the two templates, but I have already used these two templates to solve countless problems. I really don't know where there are errors, fuck ...... Speechless ......

The numbers of different substrings produced by each suffix are: N-sa [I]-height [I], which has been solved in spoj 694 705.

Then the smallest k must be from the height [I] array, because the suffix has been sorted in the Lexicographic Order, and then you can find the K.

The template found now is also very fast, and the code volume is really short, fast, very good ...... This code is ranked in rank1, 328 Ms.

In fact, this question is random data. If it is not random data, my code will be T, because the last and ninth sentences are written in the while loop, imagine if the data is 10 ^ 5 A, the processing method will traverse to the end every time, which is the complexity of N ^ 2, so ...... The correct posture should be rmq + two points. The official question is rmq + two points. I used rmq + for Binary when I first started using my own template, but it may be because of the template wa. Then I am too lazy to write rmq + in this template, because the data in the online competition is too watery, for example, in the third question of the online competition, there is a code on the acdream group, and the data water reaches this code, and the sample code is not even, I really don't know what the data looks like. Because this is all random data, so yy.

# Include <stdio. h> # include <string. h >#include <algorithm> # include <iostream> using namespace STD; typedef long ll; const int n = 111000; char s [N]; // n> 256int N, sa [N], height [N], _ rank [N], TMP [N], top [N]; void Makesa () // O (N * logn) {int I, j, Len, NA; NA = (n <256? 256: N); memset (top, 0, na * sizeof (INT); for (I = 0; I <n; I ++) top [_ rank [I] = s [I] & 0xff] ++; for (I = 1; I <Na; I ++) top [I] + = top [I-1]; for (I = 0; I <n; I ++) sa [-- top [_ rank [I] = I; for (LEN = 1; Len <n; Len <= 1) {for (I = 0; I <n; I ++) {J = sa [I]-len; If (j <0) J + = N; TMP [top [_ rank [J] ++] = J;} SA [TMP [top [0] = 0] = J = 0; for (I = 1; I <n; I ++) {If (_ ran K [TMP [I]! = _ Rank [TMP [I-1] | _ rank [TMP [I] + Len]! = _ Rank [TMP [I-1] + Len]) Top [+ + J] = I; SA [TMP [I] = J;} memcpy (_ rank, SA, N * sizeof (INT); memcpy (SA, TMP, N * sizeof (INT); If (j> = n-1) break;} void LCP () // o (4 * n) {int I, j, k; For (j = _ rank [height [I = k = 0] = 0]; I <n-1; I ++, K ++) while (k> = 0 & S [I]! = S [SA [J-1] + k]) height [J] = (k --), j = _ rank [SA [J] + 1];} ll f [N]; int main () {While (scanf ("% s", S )! = EOF) {n = strlen (s) + 1; int Len = n-1; Makesa (); LCP (); int Q; For (INT I = 1; I <= Len; I ++) f [I] = f [I-1] + len-sa [I]-height [I]; ll K, V, L = 0, r = 0; scanf ("% d", & Q); While (Q --) {scanf ("% i64d", & V ); k = (V ^ L ^ r) + 1; if (F [Len] <k) {L = r = 0; puts ("0 0"); continue ;} int Pos = lower_bound (F + 1, F + n, k)-f; L = sa [POS]; // int len1; // If (POS) len1 = k-f [pos-1] + height [POS]; // else len1 = K; int len1 = len-(F [POS]-k)-L; while (++ POS <n & height [POS]> = len1) // it is best to use rmq + binary here, avoid the case of 100,000 a data if (L> SA [POS]) L = sa [POS]; l ++; r = L + len1-1; printf ("% i64d % i64d \ n", l, R) ;}} return 0 ;}


HDU 5008 Xi'an Network Competition B Question: suffix array find the K kiddie string

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.