ACM-ICPC Beijing Division (2017) online race 2 "suffix array +java//not"

Source: Internet
Author: User

#1579: Reverse Suffix Arraytime limit: 1000mssingle point time limit: 1000msmemory limit: 256MBDescription

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

Let S=s1s2...sn is 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 being an array of integers providing the starting positions of suffixes of s in Lex Icographical order. This means, an entry a[i] are 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 be there whose suffix array is the same as the G Iven suffix Array.

input

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 SU Ffix array A.

1≤T≤10, 1≤n≤100,000

1≤a[i]≤n (i = 1...N)

Output

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

Sample input
154 3 2) 5 1
Sample output
98280
"Test Instructions": Known suffix array, the number of possible cases of the original string.
"Analysis": Java,26^3 dp, http://blog.csdn.net/skywalkert/article/details/51731556 ( on Cdoj also has a similar
"Code":
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 classMain { Public Static voidMain (string[] args) {InputStream InputStream= System.inch; OutputStream OutputStream= System. out; Inputreaderinch=NewInputreader (InputStream); PrintWriter out=NewPrintWriter (OutputStream); Task2 Solver=NewTask2 (); intTestcount = Integer.parseint (inch. Next ());  for(inti =1; I <= Testcount; i++) Solver.solve (i,inch, out);  out. Close (); }    Static classTask2 {BigInteger C (intNintm) {BigInteger ans= Biginteger.valueof (1);  for(inti =1; I <= m; i++) {ans= Ans.multiply (biginteger.valueof (n-i +1)); }             for(inti =2; I <= m; i++) {ans=Ans.divide (biginteger.valueof (i)); }            returnans; }         Public voidSolveintTestnumber, Inputreaderinch, PrintWriter out) {            intn =inch. Nextint (); int[] A =New int[n +1]; int[] B =New int[n +1];  for(inti =1; I <= N; i++) {A[i]=inch. Nextint (); B[a[i]]=i; } ArrayList<Integer> arr =NewArraylist<>(); intnow =1;  for(inti =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 () > -) {                 out. println (0); } Else {                intSZ =arr.size (); Biginteger[][] DP=NewBiginteger[sz +1][ -];  for(inti =0; I <= sz; i++) {                     for(intj =0; J <= -; J + +) {Dp[i][j]= Biginteger.valueof (0); }} dp[0][0] = biginteger.valueof (1);  for(inti =0; I < sz; i++) {                     for(intj =0; J < -; J + +) {                         for(intK =1; J + K <= -; k++) {Dp[i+1][j + K] = Dp[i +1][j + k].add (dp[i][j].multiply (C (arr.Get(i) + K-2K1))); }                    }                }                //out.println (dp[sz][26]);BigInteger ans = biginteger.valueof (0);  for(inti =1; I <= -; i++) {ans=Ans.add (Dp[sz][i]); }                 out. println (ANS); }        }    }    Static classInputreader { PublicBufferedReader Reader;  PublicStringTokenizer Tokenizer;  PublicInputreader (InputStream stream) {Reader=NewBufferedReader (NewInputStreamReader (Stream),32768); Tokenizer=NULL; }         PublicString Next () { while(Tokenizer = =NULL|| !Tokenizer.hasmoretokens ()) {                Try{Tokenizer=NewStringTokenizer (Reader.readline ()); } Catch(IOException e) {Throw NewRuntimeException (e); }            }            returnTokenizer.nexttoken (); }         Public intNextint () {returnInteger.parseint (Next ()); }    }}
809ms

ACM-ICPC Beijing Division (2017) online race 2 "suffix array +java//not"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.