[CERC 2008] Suffix reconstruction

[题目链接]

         https://www.lydsy.com/JudgeOnline/problem.php?id=4319

[算法]

        首先 , 我们可以求出这个字符串的rank数组

        按照SA逐位枚举 , 贪心构造 , 即可

        时间复杂度 : O(N)

[代码]

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 5e6 + 10;

int n;
int sa[N] , rk[N];
char s[N];

template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}
int main()
{
        
        read(n);
        for (int i = 1; i <= n; ++i)
        {
                read(sa[i]);
                rk[sa[i]] = i;        
        }
        s[sa[1]] = 'a';
        char now = 'a';
        for (int i = 2; i <= n; ++i)
        {
                if (rk[sa[i - 1] + 1] < rk[sa[i] + 1]) 
                        s[sa[i]] = now;
                else
                {
                        ++now;
                        if (now > 'z')
                        {
                                puts("-1");
                                return 0;        
                        }        
                        s[sa[i]] = now;
                }        
        }
        for (int i = 1; i <= n; ++i) putchar(s[i]);
        puts("");
        
        return 0;
    
}

猜你喜欢

转载自www.cnblogs.com/evenbao/p/10778128.html