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

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

传送门:POJ 1509: Glass Beads

题意:

The description of the necklace is a string A = a1a2 … am specifying sizes of the particular beads, where the last character am is considered to precede character a1 in circular fashion. The disjoint point i is said to be worse than the disjoint point j if and only if the string aiai+1 … ana1 … ai-1 is lexicografically smaller than the string ajaj+1 … ana1 … aj-1. String a1a2 … an is lexicografically smaller than the string b1b2 … bn if and only if there exists an integer i, i <= n, so that aj=bj, for each j, 1 <= j < i and ai < bi

这么多废话,真正有用的也就是上面两段话,题目求循环字符串中(首尾相接)字典序最小的位置,和《挑战》P381思路一致,在其后构造同样的字符串,输出长度小于n的位置即可。

只不过要注意可能有多个答案,需要选取最小的下标。比如对于aa,答案是1而不是2。此时构造:

a a a a z
1的情况: a a a a z
2的情况: a a a z

输出1而非2

代码如下:

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.Map;
import java.util.StringTokenizer;

public class Main{

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

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

    void read() {
        int t = ni();
        while (t --> 0) {
            String s = ns();
            int n = s.length();
            StringBuilder sb = new StringBuilder();
            sb.append(s);
            sb.append(s);
            sb.append((char)('z' + 1));
            SuffixArray sa = new SuffixArray(sb.toString().toCharArray());
            int ans = -1;
            for (int i = 0; i <= sb.length(); ++i) {
                if (sa.sa[i] < n) {
                    ans = sa.sa[i] + 1;
                    break;
                }
            }
            out.println(ans);
        }
    }

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

        SuffixArray(char[] cs){
            n = cs.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 ? cs[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;
                }
            }
        };
    }

    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);
        }
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Java编程

20个非常有用的Java程序片段

下面是20个非常有用的Java程序片段,希望能对你有用。

1.4K0
来自专栏行者悟空

我眼中的并发编程——Fork/Join模型

1475
来自专栏海天一树

Kaggle-Digit Recognizer kNN解决方案

一、题目 https://www.kaggle.com/c/digit-recognizer 二、kNN算法 请参考https://www.jianshu.co...

27710
来自专栏Java学习网

Java项目中使用最多的排名前100的类,你用的最多的是哪些

从事Java软件开发工作很大程度是要利用各种类库的api,有组织曾经从10000个开放源码的Java项目中,统计API类的使用频率;下面的列表显示了前100名。...

2806
来自专栏机器学习入门

挑战程序竞赛系列(95):3.6数值积分(1)

挑战程序竞赛系列(95):3.6数值积分(1) 传送门:AOJ 1313: Intersection of Two Prisms 题意: 有一个侧棱与Z轴平行...

18810
来自专栏蓝天

HBase Thrift2 CPU过高问题分析

外界连接9090端口均超时,但telnet端口总是成功。使用top命令观察,发现单个线程的CPU最高达99.99%,但并不总是99.9%,而是在波动。当迁走...

552
来自专栏函数式编程语言及工具

SDP(12): MongoDB-Engine - Streaming

   在akka-alpakka工具包里也提供了对MongoDB的stream-connector,能针对MongoDB数据库进行streaming操作。这个M...

41110
来自专栏JMCui

项目工具类

一、前言     在工作中,难免遇到各种各样的问题,每个人似乎都有一套自己的解决方案。而我,又不想每次解决完问题就把东西扔了,捡了芝麻,丢了西瓜,什么时候才能进...

3596
来自专栏互联网杂技

JavaScript 常用方法总结

经常使用的 JS 方法,今天记下,以便以后查询 /* 手机类型判断 */ var BrowserInfo = { userAgent: navigator.us...

3315
来自专栏码匠的流水账

聊聊jpa的动态查询

使用springside的DynamicSpecifications,再把mvc的参数映射为SearchFilter,也可以自己实现一套端到端的动态查询。

531

扫码关注云+社区