Suffix array kattis suffix sorting (not completed

Source: Internet
Author: User

Suffix Sortinginput

The input consists of less than 10<? Xml:namespace PREFIX = "[Default] http://www.w3.org/1998/Math/MathML" NS = "Http://www.w3.org/1998/Math/MathML"/> Ten test cases. Each test case begins with a line containing a non-empty string s, of the length at most 100000100000. Then follows a line containing an integer nn followed by nn integers Q1,..., qnq1,..., qn, where 0≤qi<length (s) 0≤qi<length ? (s), each indicating a query.

Output

For each test case, output a line containing nn integers p1,..., pnp1,..., pn, where Pipi is the starting position of the QiQi ' t h smallest suffix of s.

Sample Input 1
Sample Output 1

POPUP5 0 1 2 3 4POPUP5 0 1 2 3 4Suffixes is jolly fun, eh old chap?7 35 3 18 33 26 6 2
1 4 0 2 30 1 4 2 317 18 19 20 21 22 23

Oneself also did not understand, structure to see is to understand but algorithm on the net there is no ready-made introduction, by the way also looked at the suffix tree, indeed are good things ah, but the essence should be in the production algorithm, but the algorithm is not ... God TM Paper charges are simply academic monopoly ... Let's go ahead and wait for the time to parse the template and algorithm ...

In addition to spit groove on this web-based template ah ... Who wrote that, can put the character length with char variable declaration I also served Ah, the result of more than the ASCII code table must explode, but I measured the small data of course not have 128 characters or more ... I really am ... In the middle of the night for this bug changed half a day, I hope online to put irregular template people are exploding good ...

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstring>
using namespace Std;

const int MAXN = 100005;
int RANK_C[MAXN], WB[MAXN], WV[MAXN], WSS[MAXN];
int n;

BOOL CMP (int *r, int A, int b, int l)
{
return R[a] = = R[b] && r[a + l] = = r[b + L];
}

void Da (char *r, int *sa, int n, int m)
{
int I, J, p, *x = rank_c, *y = WB, *t;
for (i = 0; i<m; i++) wss[i] = 0;
for (i = 0; i<n; i++) wss[x[i] = r[i]]++;
for (i = 1; i<m; i++) wss[i] + + wss[i-1];
for (i = n-1; I >= 0; i--) sa[--wss[x[i]] = i;
for (j = 1, p = 1; p<n; J *= 2, m = p)
{
for (p = 0, i = n-j; i<n; i++) y[p++] = i;
for (i = 0; i<n; i++) if (Sa[i] >= j) y[p++] = sa[i]-J;
for (i = 0; i<n; i++) wv[i] = x[y[i]];
for (i = 0; i<m; i++) wss[i] = 0;
for (i = 0; i<n; i++) wss[wv[i]]++;
for (i = 1; i<m; i++) wss[i] + + wss[i-1];
for (i = n-1; I >= 0; i--) sa[--wss[wv[i]] = y[i];
for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i<n; i++)
X[sa[i]] = cmp (y, sa[i-1], Sa[i], j)? P-1: p++;
}
Return
}

int main ()
{
Char S[MAXN];
int R[MAXN], sa[maxn], i,l;
while (Fgets (S,maxn,stdin))
{
L = strlen (s);
Da (S, SA, L, 130);
int num;
CIN >> Num;
int flag = 0;
for (i = 0; i < num; i++)//sa[i]: Who is in the first row?
{
int target;
scanf ("%d", &target);
if (flag)
printf ("%d", Sa[target + 1]);
Else
{
flag = 1;
printf ("%d", Sa[target + 1]);
}
}
printf ("\ n");
GetChar ();
}
return 0;
}

Suffix array kattis suffix sorting (not completed

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.