#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.

样例输入
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 = ; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
} static class Task2 {
BigInteger C(int n, int m) {
BigInteger ans = BigInteger.valueOf();
for (int i = ; i <= m; i++) {
ans = ans.multiply(BigInteger.valueOf(n - i + ));
}
for (int i = ; 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 + ];
int[] b = new int[n + ];
for (int i = ; i <= n; i++) {
a[i] = in.nextInt();
b[a[i]] = i;
}
ArrayList<Integer> arr = new ArrayList<>();
int now = ;
for (int i = ; i <= n; i++) {
if (a[i - ] != n && (a[i] == n || b[a[i - ] + ] > b[a[i] + ])) {
arr.add(now);
now = ;
} else {
now++;
}
}
arr.add(now);
if (arr.size() > ) {
out.println();
} else {
int sz = arr.size();
BigInteger[][] dp = new BigInteger[sz + ][];
for (int i = ; i <= sz; i++) {
for (int j = ; j <= ; j++) {
dp[i][j] = BigInteger.valueOf();
}
}
dp[][] = BigInteger.valueOf();
for (int i = ; i < sz; i++) {
for (int j = ; j < ; j++) {
for (int k = ; j + k <= ; k++) {
dp[i + ][j + k] = dp[i + ][j + k].add(dp[i][j].multiply(C(arr.get(i) + k - , k - )));
}
}
}
// out.println(dp[sz][26]);
BigInteger ans = BigInteger.valueOf();
for (int i = ; i <= ; 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), );
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//不会】的更多相关文章

  1. hihoCoder 1578 Visiting Peking University 【贪心】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1578 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for ...

  2. ACM-ICPC北京赛区(2017)网络赛1【模拟+枚举+数组操作】

    题目1 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for n ...

  3. hihoCoder 1582 Territorial Dispute 【凸包】(ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1582 : Territorial Dispute 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 In 2333, the C++ Empire and the Ja ...

  4. hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1584 : Bounce 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 For Argo, it is very interesting watching a cir ...

  5. hihoCoder 1586 Minimum 【线段树】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

    #1586 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2 ...

  6. ACM-ICPC北京赛区(2017)网络赛_Minimum

    题目9 : Minimum 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, -, a2^k ...

  7. ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 题目9 : Minimum

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a list of integers a0, a1, …, a2^k-1. You need t ...

  8. ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 i题 Minimum(线段树)

    描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. ...

  9. 【分类讨论】【计算几何】【凸包】hihocoder 1582 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 E. Territorial Dispute

    题意:平面上n个点,问你是否存在一种黑白染色方案,使得对于该方案,无法使用一条直线使得黑色点划分在直线一侧,白色点划分在另一侧.如果存在,输出一种方案. 如果n<=2,显然不存在. 如果所有点共 ...

  10. 【线段树】hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛 I. Minimum

    题意:给你一个序列(长度不超过2^17),支持两种操作:单点修改:询问区间中最小的ai*aj是多少(i可以等于j). 只需要线段树维护区间最小值和最大值,如果最小值大于等于0,那答案就是minv*mi ...

随机推荐

  1. 12.25模拟赛T3

    可以发现,答案O(根号)(因为链上答案最大,n/2,n/3...根号种) 每次求答案要二分 优秀的做法是: 对于小于根号n的暴力nlogn找,可能二分到同一个mid,记忆化一下最小的tot值 对于大于 ...

  2. 7月17号day9总结

    今天学习过程和小结 今天学习了如何使用idea操作hdfs. public class HDFSTest {    Configuration configuration;    FileSystem ...

  3. Idea IntelliJ远程调试教程

    总结 第一步:修改startup.sh 在倒第二行加上export JPDA_ADDRESS=8787 最后一行在start前面加上"   jpda   " 第二步:配置Idea, ...

  4. 【hdu1251-统计难题】Trie

    http://acm.hust.edu.cn/vjudge/problem/16379 题意:给定多个单词,多次询问符合某前缀的单词有多少个. 题解:tire.数组开了5*10^6才A,不然就RE. ...

  5. 【Mysql优化】聚簇索引与非聚簇索引概念

    必须为主键字段创建一个索引,这个索引就是所谓的"主索引".主索引与唯一索引的唯一区别是:前者在定义时使用的关键字是PRIMARY而不是UNIQUE.  首先明白两句话: innod ...

  6. certbot 免费httos证书申请

    https://keelii.com/2016/06/12/free-https-cert-lets-encrypt-apply-install/

  7. System and method for parallel execution of memory transactions using multiple memory models, including SSO, TSO, PSO and RMO

    A data processor supports the use of multiple memory models by computer programs. At a device extern ...

  8. linux 系统函数之 (dirname, basename)【转】

    转自:http://blog.csdn.net/peter_cloud/article/details/9308333 版权声明:本文为博主原创文章,未经博主允许不得转载. 除非你的原件考虑跨平台. ...

  9. C++中 相对路径与绝对路径 斜杠 '/' 与反斜杠 '\'的区别

    文件路径正斜杠和反斜杠 正斜杠,又称左斜杠,符号是"/":反斜杠,也称右斜杠,符号是"\".文件路径的表示可以分为绝对路径和相对路径: 1.绝对路径表示相对容易 ...

  10. gcc 簡單操作

    gcc -c test.c 產出 test.o object file gcc -c test.c -o XXX 產出 XXX object file gcc test.c -o aaa 產出 aaa ...