首页 > 代码库 > 后缀数组 kattis suffix sorting (未完成
后缀数组 kattis suffix sorting (未完成
Suffix Sorting
Input
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" />10 test cases. Each test case begins with a line containing a non-empty string s, of 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’th smallest suffix of s.
Sample Input 1
Sample Output 1
popup 5 0 1 2 3 4 Popup 5 0 1 2 3 4 Suffixes are jolly fun, eh old chap? 7 35 3 18 33 26 6 2
1 4 0 2 3 0 1 4 2 3 17 18 19 20 21 22 23
自己也没搞太懂,结构看是看懂了但是算法网上根本没有现成的介绍,顺便也看了一下后缀树,的确都是好东西啊,不过精华应该都在制作算法上,但是算法偏偏没有……神tm论文收费简直是学术垄断…先放着吧,等有时间就把模板和算法给解析一下……
另外吐槽一下这个网上找的模板啊……谁写的啊,能把字符长度用char变量声明我也是服了啊,结果导致超过ascii码表必定爆炸,但是我测的小数据当然不可能有128个字符以上……我真的是……半夜为了这个bug改了半天,希望网上放不正规模板的人都爆炸好么……
#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] : 排在第i个的是谁
{
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;
}
后缀数组 kattis suffix sorting (未完成