bzoj4319: cerc2008 Suffix reconstruction

15 篇文章 0 订阅
11 篇文章 0 订阅

题面在这里

题意:

给出sa[]数组,求原字符串,字符都是’a’~’z’所有小写字母。

做法:

qaq此题乱入。。。
其实是个贪心,涉及到了一点点的后缀数组。

考虑两个后缀 suf(p1) s u f ( p 1 ) suf(p2) s u f ( p 2 ) ,假设他们的大小关系是 suf(p1)<suf(p2) s u f ( p 1 ) < s u f ( p 2 ) ,现在需要确定 s[p1] s [ p 1 ] 是否可以等于 s[p2] s [ p 2 ] ,则需要比较 suf(p1+1) s u f ( p 1 + 1 ) suf(p2+1) s u f ( p 2 + 1 ) 的大小关系:
如果 suf(p1+1)<suf(p2+1) s u f ( p 1 + 1 ) < s u f ( p 2 + 1 ) ,那么 s[p1]s[p2] s [ p 1 ] 可 以 等 于 s [ p 2 ] 。否则 s[p1]<s[p2] s [ p 1 ] < s [ p 2 ]
由于这题只有26个字母可以取,所以 s[p1]s[p2] s [ p 1 ] 可 以 等 于 s [ p 2 ] 的时候就让它等于,否则就新建一个字母。
最后如果字母数还超过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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值