PKU 3581 Sequence 解题报告
来源:互联网 发布:ubuntu windows10 编辑:程序博客网 时间:2024/05/18 02:59
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- const int MAX = 200001;
- struct Sfx{
- int idx;
- int key[2];
- bool operator<(const Sfx &other)const{
- return key[0] < other.key[0] ||
- key[0] == other.key[0] && key[1] < other.key[1];
- }
- };
- Sfx g_sfx[2][MAX], *sa = g_sfx[0], *temp_sa = g_sfx[1];
- int cnt[MAX];
- int rank[MAX];
- int A[MAX];
- int n;
- void cSort(Sfx *in, int key, Sfx *out){
- memset(cnt, 0, sizeof(cnt));;
- for(int i = 0; i < n; i++)
- cnt[in[i].key[key]]++;
- for(int i = 1; i < n; i++)
- cnt[i] += cnt[i-1];
- for(int i = n - 1; i >= 0; i--)
- out[ --cnt[in[i].key[key]] ] = in[i];
- }
- void build_sfx(){
- for(int i = n-1; i >= 0; i--){
- sa[i].idx = sa[i].key[1] = i;
- sa[i].key[0] = A[i];
- }
- sort(sa, sa+n);
- for(int i = 0; i < n; i++)sa[i].key[1] = 0;
-
- int wid = 1;
-
- while(wid < n){
- rank[sa[0].idx] = 0;
- for(int i = 1; i < n; i++){
- rank[sa[i].idx] = rank[sa[i-1].idx];
- if(sa[i-1] < sa[i])rank[sa[i].idx]++;
- }
- for(int i = 0; i < n; i++){
- sa[i].idx = i;
- sa[i].key[0] = rank[i];
- sa[i].key[1] = i + wid < n ? rank[i + wid] : 0;
- }
- cSort(sa, 1, temp_sa); cSort(temp_sa, 0, sa);
- wid *= 2;
- }
-
-
-
- }
- int main(){
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- while(scanf("%d", &n) != EOF){
- for(int i = n-1; i >= 0; i--)
- scanf("%d", &A[i]);
- build_sfx();
- int ans[4];
- ans[0] = n;
- for(int i = 0, j = 1; j <= 1; i++){
- if(sa[i].idx < ans[j-1])
- if(sa[i].idx >= 3 - j){
- ans[j] = sa[i].idx;
- for(int k = ans[j]; k < ans[j-1]; k++)
- printf("%d/n", A[k]);
- j++;
- }
- }
- n = ans[1];
- build_sfx();
- for(int i = 0, j = 2; j <= 2; i++){
- if(sa[i].idx < ans[j-1])
- if(sa[i].idx >= 3 - j){
- ans[j] = sa[i].idx;
- for(int k = ans[j]; k < ans[j-1]; k++)
- printf("%d/n", A[k]);
- j++;
- }
- }
- for(int k = 0; k < ans[2]; k++)printf("%d/n", A[k]);
- break;
-
- }
- return 0;
- }