ACM-ICPC北京賽區(2017)網路賽2【尾碼數組+Java//不會】

來源:互聯網
上載者:User

標籤:pos   span   img   http   也有   else   nta   efi   problem   

#1579 : Reverse Suffix Array時間限制:1000ms單點時限:1000ms記憶體限制:256MB描述

There is a strong data structure called "Suffix Array" which can effectively solve string problems.

Let S=s1s2...sn be a string and let S[i,j] denote the substring of S ranging from i to j. The suffix array A of S is now defined to be an array of integers providing the starting positions of suffixes of S in lexicographical order. This means, an entry A[i] is the starting position of the i-th smallest suffix in S and thus for all 1 < i ≤ n:  S[A[i-1], n] < S[A[i], n].

For example: the suffix array of “banana” is [6, 4, 2, 1, 5, 3].

Here comes another problem called "Reverse Suffix Array".

Given a suffix array, you need to figure out how many lower case strings are there whose suffix array is the same as the given suffix array.

輸入

First line contains a positive number T which means the number of test cases.

For each test cases, first line contains a positive number N, the second line contains N integer(s) which indicates the suffix array A.

1 ≤ T ≤ 10, 1 ≤ N ≤ 100,000

1 ≤ A[i] ≤ N (i = 1...N)

輸出

For each test case, output one line contains the answer. If no qualified string exists, output 0.

範例輸入
154 3 2 5 1
範例輸出
98280
【題意】:已知尾碼數組,求原串的可能情況數。
【分析】:java,26^3 dp,http://blog.csdn.net/skywalkert/article/details/51731556(在 cdoj 上也有一個類似
【代碼】:
import java.io.OutputStream;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.util.StringTokenizer;import java.math.BigInteger;import java.io.IOException;import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.ArrayList;import java.io.InputStream;/** * Built using CHelper plug-in * Actual solution is at the top */public class Main {    public static void main(String[] args) {        InputStream inputStream = System.in;        OutputStream outputStream = System.out;        InputReader in = new InputReader(inputStream);        PrintWriter out = new PrintWriter(outputStream);        Task2 solver = new Task2();        int testCount = Integer.parseInt(in.next());        for (int i = 1; i <= testCount; i++)            solver.solve(i, in, out);        out.close();    }    static class Task2 {        BigInteger C(int n, int m) {            BigInteger ans = BigInteger.valueOf(1);            for (int i = 1; i <= m; i++) {                ans = ans.multiply(BigInteger.valueOf(n - i + 1));            }            for (int i = 2; i <= m; i++) {                ans = ans.divide(BigInteger.valueOf(i));            }            return ans;        }        public void solve(int testNumber, InputReader in, PrintWriter out) {            int n = in.nextInt();            int[] a = new int[n + 1];            int[] b = new int[n + 1];            for (int i = 1; i <= n; i++) {                a[i] = in.nextInt();                b[a[i]] = i;            }            ArrayList<Integer> arr = new ArrayList<>();            int now = 1;            for (int i = 2; i <= n; i++) {                if (a[i - 1] != n && (a[i] == n || b[a[i - 1] + 1] > b[a[i] + 1])) {                    arr.add(now);                    now = 1;                } else {                    now++;                }            }            arr.add(now);            if (arr.size() > 26) {                out.println(0);            } else {                int sz = arr.size();                BigInteger[][] dp = new BigInteger[sz + 1][27];                for (int i = 0; i <= sz; i++) {                    for (int j = 0; j <= 26; j++) {                        dp[i][j] = BigInteger.valueOf(0);                    }                }                dp[0][0] = BigInteger.valueOf(1);                for (int i = 0; i < sz; i++) {                    for (int j = 0; j < 26; j++) {                        for (int k = 1; j + k <= 26; k++) {                            dp[i + 1][j + k] = dp[i + 1][j + k].add(dp[i][j].multiply(C(arr.get(i) + k - 2, k - 1)));                        }                    }                }                // out.println(dp[sz][26]);                BigInteger ans = BigInteger.valueOf(0);                for (int i = 1; i <= 26; i++) {                    ans = ans.add(dp[sz][i]);                }                out.println(ans);            }        }    }    static class InputReader {        public BufferedReader reader;        public StringTokenizer tokenizer;        public InputReader(InputStream stream) {            reader = new BufferedReader(new InputStreamReader(stream), 32768);            tokenizer = null;        }        public String next() {            while (tokenizer == null || !tokenizer.hasMoreTokens()) {                try {                    tokenizer = new StringTokenizer(reader.readLine());                } catch (IOException e) {                    throw new RuntimeException(e);                }            }            return tokenizer.nextToken();        }        public int nextInt() {            return Integer.parseInt(next());        }    }}
809ms

 

ACM-ICPC北京賽區(2017)網路賽2【尾碼數組+Java//不會】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.