#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. 【bzoj4724】[POI2017]Podzielno 二分

    题目描述 B进制数,每个数字i(i=0,1,...,B-1)有a[i]个.你要用这些数字组成一个最大的B进制数X(不能有前导零,不需要用完所有数字),使得X是B-1的倍数.q次询问,每次询问X在B进制 ...

  2. 【bzoj1458】士兵占领 有上下界最小流

    题目描述 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放置了Li个士兵 ...

  3. BZOJ4570 SCOI2016妖怪(三分)

    strength=atk*(1+b/a)+dnf*(1+a/b).设a/b=x,可以发现这是一个关于x的对勾函数.开口向上的一堆凸函数取max还是凸函数,三分即可. 然而无良出题人既卡精度又卡时间.众 ...

  4. 【题解】HAOI2012高速公路

    一节政治课的结果……推式子+推式子+推式子…… 首先注意到一个区间里面,选择(x, y)和(y, x)的费用是一样的.所以我们把这两种情况合为一种,那么现在询问的区间为(l, r),则一共的情况就有 ...

  5. python实现关联规则

    代码中Ci表示候选频繁i项集,Li表示符合条件的频繁i项集 # coding=utf-8 def createC1(dataSet): # 构建所有1项候选项集的集合 C1 = [] for tran ...

  6. 接到新数据库时,分析业务常用的SQL语句

    USE DataBaseName--清空当前GridView显示,释放内存: SELECT GETDATE() --数据库关系图 SELECT * FROM sysdiagrams --列出所有表 S ...

  7. 设计一个JavaScript框架需要编写哪些模块

    在这个js框架随处乱跑的时代,你是否考虑过写一个自己的框架?下面的内容也许会有点帮助. 一个框架应该包含哪些内容? 1. 语言扩展 大部分现有的框架都提供了这部分内容,语言扩展应当是以ECMAScri ...

  8. angular的一些问题

    引入第三方类库 1.安装依赖 npm install jquey --save 2.引入项目 在angular-cli.json "scripts": [ "../nod ...

  9. codeforces803D. Magazine Ad

    D. Magazine Adtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutput ...

  10. oracle获取主机服务器IP

    --要获取服务器端的IP :: SYS@XXX> select utl_inaddr.get_host_address from dual; GET_HOST_ADDRESS --------- ...