Ural 1297 suffix array: Find the longest response substring

Source: Internet
Author: User

Train of Thought: This afternoon I started WAF and went through discuss. There is an array in it: abcdefdcba. This output is ABCD, so it is wrong.

Then you will know that the suffix array you have written has a bug with this echo string, and you will not know how to change it.

Then, let's look at the problem solution. rmq is used to pre-process the longest public prefix of any two suffixes first. Because I don't know this, I read it for another afternoon ......

Then, when I understood the combination of rmq and the suffix, I found that rmq is not required here. I just need to handle the above example, haha ...... Wit ......

However, in the papers of the National Training Team, rmq was used as the Final Solution and rmq was used together. It was also used in other topics.

This does not require rmq:

# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <map> # include <queue> # include <set> # include <cmath> # include <bitset> # define MEM (, b) memset (a, B, sizeof (A) # define lson I <1, L, mid # define rson I <1 | 1, Mid + 1, R # define llson j <1, L, mid # define rrson j <1 | 1, Mid + 1, R # define INF 0x7fffffff # define maxn 4010 using namespace STD; typedef long ll; typedef unsigned long ull; void Rad IX (int * STR, int * a, int * B, int N, int m) {static int count [maxn]; MEM (count, 0 ); for (INT I = 0; I <n; I ++) ++ count [STR [A [I]; for (INT I = 1; I <= m; I ++) Count [I] + = count [I-1]; for (INT I = n-1; I> = 0; I --) B [-- count [STR [A [I] = A [I];} void suffix (int * STR, int * Sa, int N, int m) {static int rank [maxn], a [maxn], B [maxn]; for (INT I = 0; I <n; I ++) rank [I] = I; radix (STR, rank, SA, n, m); rank [SA [0] = 0; For (INT I = 1; I <n; I ++) Rank [SA [I] = rank [SA [I-1] + (STR [SA [I]! = STR [SA [I-1]); For (INT I = 0; 1 <I <n; I ++) {for (Int J = 0; j <N; j ++) {A [J] = rank [J] + 1; B [J] = J + (1 <I)> = n? 0: rank [J + (1 <I)] + 1; SA [J] = J;} Radix (B, SA, rank, N, N ); radix (A, rank, SA, n, n); rank [SA [0] = 0; For (Int J = 1; j <n; j ++) rank [SA [J] = rank [SA [J-1] + (A [SA [J-1]! = A [SA [J] | B [SA [J-1]! = B [SA [J]) ;}} void calcheight (int * STR, int * Sa, int * H, int N) {static int rank [maxn]; int K = 0; H [0] = 0; For (INT I = 0; I <n; I ++) rank [SA [I] = I; for (INT I = 0; I <n; I ++) {k = 0? 0: K-1; If (rank [I]) while (STR [I + k] = STR [SA [rank [I]-1] + k]) K ++; else K = 0; H [rank [I] = K ;}} int A [maxn], sa [maxn], height [maxn]; int main () {string s; while (CIN> S) {string STR; For (INT I = s. size ()-1; I> = 0; I --) STR + = s [I]; STR = S + "#" + STR; copy (Str. begin (), str. end (), a); int n = Str. size (); suffix (A, SA, N, N + 256); calcheight (A, SA, height, n); int Len = 0, Pos = 1; for (INT I = 1; I <n; I ++) if (SA [I] + SA [I-1] = N-height [I] & ((SA [I] <S. Size ())! = (SA [I-1] <S. size () {If (height [I]> Len) {Len = height [I]; Pos = min (SA [I], sa [I-1]);} else if (height [I] = Len) Pos = min (Pos, min (SA [I], sa [I-1]);} If (LEN> 1) cout <Str. substr (Pos, Len) <Endl; else cout <s [0] <Endl;} return 0 ;} /* abcddcbaefdcbaabcdefdcba // if there is no SA [I] + SA [I-1] = N-height [I] This example will output abcdabcddbcaaaaaaaaa */


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.