前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >4.7后缀数组

4.7后缀数组

作者头像
用户1147447
发布2019-05-26 00:45:20
1.1K0
发布2019-05-26 00:45:20
举报
文章被收录于专栏:机器学习入门机器学习入门

挑战程序竞赛系列(69):4.7后缀数组(1)


题意:

给定N个数字组成的序列A1,A2,....,AnA_1, A_2, ...., A_n。其中A1A_1比其他数字都大。现在要把这个序列分成三段,并将每段分别反转,求能得到的字典序最小的序列是什么?要求分得的每段都不为空。

第一次接触后缀数组,采用《挑战》P378的后缀算法,时间复杂度为O(nlog2n)O(n\log^2n),基本思想如下:

alt text
alt text
alt text
alt text

思想很简单,假设长度为l的后缀排名已知,我们可以直接根据长度为l的后缀排名算出长度为2l的后缀排名,总共两种决策,如果在长度为l的两个后缀排名不同,则即使在长度为2l中,这两后缀排名相对顺序不发生变化。当且仅当两个后缀在长度为l的排名相同时,还需要额外的比较一次,比较的信息隐藏于长度l中,具体看《挑战》表格中的对应变化关系。

此题利用后缀数组计算出第一段的最小后缀,但在计算后面两段的字典序最小时,需要将两个原序列拼接得到新的序列中的某个子串反转后得到的序列。

alt text
alt text

举个简单的例子:

10 1 2 2 3 4

拼接: 2 2 3 4 2 2 3 4
反转: 4 3 2 2 4 3 2 2
输出: x o o o x x x x
x表示不可能输出的位置,o表示候选位置,观察发现在候选位置中,最小后缀为2 2 4 3
即为答案。

代码如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class Main{

    String INPUT = "./data/judge/201709/P3581.txt";

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    void read() {
        int n = ni();
        int[] a = new int[n];
        for (int i = 0; i < n; ++i) a[i] = ni();
        int[] rev = reverse(a, 0, n - 1);
        SuffixArray sa = new SuffixArray(rev);
        int p1 = 0;
        for (int i = 0; i < n; ++i) {
            p1 = n - sa.sa[i];
            if (p1 >= 1 && n - p1 >= 2) break;
        }

        int m = n - p1;
        rev = reverse(merge(a, p1, n - 1), 0, 2 * m - 1);
        sa = new SuffixArray(rev);
        int p2 = 0;
        for (int i = 0; i <= 2 * m; ++i) {
            p2 = p1 + m - sa.sa[i];
            if (p2 - p1 >= 1 && n - p2 >= 1) break;
        }

        int[] rev1 = reverse(a, 0, p1 - 1);
        int[] rev2 = reverse(a, p1, p2 - 1);
        int[] rev3 = reverse(a, p2, n - 1);
        for (int i = 0; i < n; ++i) {
            if (i >= 0 && i <= p1 - 1) out.println(rev1[i]);
            else if (i >= p1 && i <= p2 - 1) out.println(rev2[i]);
            else out.println(rev3[i]);
        }
    }

    class SuffixArray{
        int k = 1;
        int n;
        Integer[] sa;
        int[] rank, tmp;

        SuffixArray(int[] arra){
            n    = arra.length;
            sa   = new Integer[n + 1];
            rank = new int[n + 1];
            tmp  = new int[n + 1];

            for (int i = 0; i <= n; ++i) {
                sa[i] = i;
                rank[i] = i < n ? arra[i] : -1;
            }

            for (k = 1; k <= n; k *= 2) {
                Arrays.sort(sa, cmp);
                tmp[sa[0]] = 0;
                for (int i = 1; i <= n; ++i) {
                    tmp[sa[i]] = tmp[sa[i - 1]] + (cmp.compare(sa[i - 1], sa[i]) < 0 ? 1 : 0);
                }

                for (int i = 0; i <= n; ++i) {
                    rank[i] = tmp[i];
                }
            }
        }

        private Comparator<Integer> cmp = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                int i = o1;
                int j = o2;
                if (rank[i] != rank[j]) return rank[i] - rank[j];
                else {
                    int ri = i + k <= n ? rank[i + k] : -1;
                    int rj = j + k <= n ? rank[j + k] : -1;
                    return ri - rj;
                }
            }
        }; 
    }


    void swap(int[] arra, int i, int j) {
        int tmp = arra[i];
        arra[i] = arra[j];
        arra[j] = tmp;
    }

    int[] reverse(int[] a, int i, int j) {
        int n = a.length;
        int[] rev = new int[n];
        for (int k = 0; k < n; ++k) rev[k] = a[k];
        while (i < j) {
            swap(rev, i++, j--);
        }
        return rev;
    }

    int[] merge(int[] a, int i, int j) {
        int n = j - i + 1;
        n += n;
        int[] merge = new int[n];
        for (int k = 0; k < n / 2; ++k) {
            merge[k] = merge[k + n / 2] = a[i + k];
        }
        return merge;
    }


    FastScanner in;
    PrintWriter out;

    void run() throws IOException {
        boolean oj;
        try {
            oj = ! System.getProperty("user.dir").equals("F:\\java_workspace\\leetcode");
        } catch (Exception e) {
            oj = System.getProperty("ONLINE_JUDGE") != null;
        }

        InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
        in = new FastScanner(is);
        out = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        read();
        out.flush();
        if (!oj){
            System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
        }
    }

    public boolean more(){
        return in.hasNext();
    }

    public int ni(){
        return in.nextInt();
    }

    public long nl(){
        return in.nextLong();
    }

    public double nd(){
        return in.nextDouble();
    }

    public String ns(){
        return in.nextString();
    }

    public char nc(){
        return in.nextChar();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;
        boolean hasNext;

        public FastScanner(InputStream is) throws IOException {
            br = new BufferedReader(new InputStreamReader(is));
            hasNext = true;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    hasNext = false;
                    return "##";
                }
            }
            return st.nextToken();
        }

        String next = null;
        public boolean hasNext(){
            next = nextToken();
            return hasNext;
        }

        public int nextInt() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Integer.parseInt(more);
        }

        public long nextLong() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Long.parseLong(more);
        }

        public double nextDouble() {
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return Double.parseDouble(more);
        }

        public String nextString(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more;
        }

        public char nextChar(){
            if (next == null){
                hasNext();
            }
            String more = next;
            next = null;
            return more.charAt(0);
        }
    }
}
alt text
alt text
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年09月22日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 挑战程序竞赛系列(69):4.7后缀数组(1)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档