ACM-ICPC北京赛区(2017)网络赛2【后缀数组+Java//不会】
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ACM-ICPC北京赛区(2017)网络赛2【后缀数组+Java//不会】,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4046字,纯文字阅读大概需要6分钟。
内容图文
![ACM-ICPC北京赛区(2017)网络赛2【后缀数组+Java//不会】](/upload/InfoBanner/zyjiaocheng/1318/6b955f87c24748f8968fdf0bb318ed64.jpg)
#1579 : Reverse Suffix Array
描述
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.
- 样例输入
-
1 5 4 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(); } staticclass 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; } publicvoid solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] a = newint[n + 1]; int[] b = newint[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); } } } staticclass 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) { thrownew RuntimeException(e); } } return tokenizer.nextToken(); } publicint nextInt() { return Integer.parseInt(next()); } } }
原文:http://www.cnblogs.com/Roni-i/p/7586498.html
内容总结
以上是互联网集市为您收集整理的ACM-ICPC北京赛区(2017)网络赛2【后缀数组+Java//不会】全部内容,希望文章能够帮你解决ACM-ICPC北京赛区(2017)网络赛2【后缀数组+Java//不会】所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。