题意:
给出sa[]数组,求原字符串,字符都是’a’~’z’所有小写字母。
做法:
qaq此题乱入。。。
其实是个贪心,涉及到了一点点的后缀数组。
考虑两个后缀 和 ,假设他们的大小关系是 ,现在需要确定 是否可以等于 ,则需要比较 和 的大小关系:
如果 ,那么 。否则 。
由于这题只有26个字母可以取,所以 的时候就让它等于,否则就新建一个字母。
最后如果字母数还超过26就是无解。
代码:
/*************************************************************
Problem: bzoj 4319 cerc2008 Suffix reconstruction
User: fengyuan
Language: C++
Result: Accepted
Time: 852 ms
Memory: 7148 kb
Submit_Time: 2018-01-23 22:18:16
*************************************************************/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cctype>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long ll;
const int N = 500010;
int n;
int sa[N], rk[N], a[N];
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i ++) {
scanf("%d", &sa[i]);
rk[sa[i]] = i;
}
a[sa[1]] = 0; int now = 0;
for(int i = 2; i <= n; i ++) {
int p1 = sa[i-1], p2 = sa[i];
if(rk[p1+1] < rk[p2+1]) a[sa[i]] = now;
else a[sa[i]] = ++ now;
}
if(now >= 26) { puts("-1"); return 0; }
for(int i = 1; i <= n; i ++) printf("%c", a[i]+'a');
return 0;
}