首页 > ACM题库 > HDU-杭电 > HDU 2970-Suffix reconstruction[解题报告]HOJ
2014
02-24

HDU 2970-Suffix reconstruction[解题报告]HOJ

Suffix reconstruction

问题描述 :

Given a text s[1..n] of length n, we create its suffix array by taking all its suffixes: s[1..n], s[2..n],…., s[n..n] and sorting them lexicographically. As a result we get a sorted list of suffixes: s[p(1)..n], s[p(2)..n],…, s[p(n)..n] and call the sequence p(1),p(2),…,p(n) the suffix array of s[1..n].

For example, if s = abbaabab, the sorted list of all suffixes becomes: aabab, ab, abab, abbaabab, b, baabab, bab, bbaabab and the suffix array is 4, 7, 5, 1, 8, 3,6, 2.

It turns out that it is possible to construct this array in a linear time. Your task will be completely different, though: given p(1), p(2), p(3),… , p(n) you should check if there exist at least one text consisting of lowercase letters of the English alphabet for which this sequence is the suffix array. If so, output any such text. Otherwise output -1.

输入:

The input contains several descriptions of suffix arrays. The first line contains the number of descriptions t (t <= 100). Each description begins with a line containing the length of both the text and the array n (1 <= n <= 500000). Next line contains integers p(1), p(2), … ,p(n). You may assume that 1 <= p(i) <= n and no value of p(i) occurs twice. Total size of the input will not exceed 50MB.

输出:

The input contains several descriptions of suffix arrays. The first line contains the number of descriptions t (t <= 100). Each description begins with a line containing the length of both the text and the array n (1 <= n <= 500000). Next line contains integers p(1), p(2), … ,p(n). You may assume that 1 <= p(i) <= n and no value of p(i) occurs twice. Total size of the input will not exceed 50MB.

样例输入:

6
2
1 2
2
2 1
3
2 3 1
6
3 4 5 1 2 6
14
3 10 2 12 14 5 13 4 1 8 6 11 7 9
7
5 1 7 4 3 2 6

样例输出:

ab
aa
bab
bcaaad
ebadcfgehagbdc
bcccadc

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int MAXN = 500000 + 10;
int num[MAXN], n1[MAXN];

int main()
{
 int t, i, cut, n;
 char c, res[MAXN];
 scanf("%d", &t);
 while(t--)
 {
 scanf("%d", &n);
 for( i=1; i<=n; i++ )
 {
 res[i] = 0;
 scanf("%d", &num[i]);
 n1[num[i]] = i;
 }
 bool flag = true;
 n1[n+1] = -1;
 c = 'a';
 res[num[1]] = c;
 cut = n1[num[1] + 1];
 for( i=2; i<=n; i++ )
 {
 if(n1[(num[i] + 1)] > cut)
 {
 res[num[i]] = c;
 cut = n1[num[i] + 1];
 }
 else
 {
 c = c + 1;
 if(c > 'z')
 {
 flag = false;
 break;
 }
 res[num[i]] = c;
 cut = n1[num[i] + 1];
 //printf("cut : %d\n", cut);
 }
 }
 if(!flag)
 {
 printf("-1\n");
 continue;
 }
 for( i=1; i<=n; i++ ) printf("%c", res[i]);
 putchar('\n');
 }
 return 0;
}

  1. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])