LCP Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 159 Accepted Submission(s): 47
Problem Description
Peter has a string
s=s
1
s
2
...s
n![]()
, let
suff
i
=s
i
s
i+1
...s
n![]()
be the suffix start with
i
-th character of
s
. Peter knows the lcp (longest common prefix) of each two adjacent suffixes which denotes as
a
i
=lcp(suff
i
,suff
i+1
)(1≤i<n
).
Given the lcp array, Peter wants to know how many strings containing lowercase English letters only will satisfy the lcp array. The answer may be too large, just print it modulo 10
9
+7
.
Given the lcp array, Peter wants to know how many strings containing lowercase English letters only will satisfy the lcp array. The answer may be too large, just print it modulo 10
Input
There are multiple test cases. The first line of input contains an integer
T
indicating the number of test cases. For each test case:
The first line contains an integer n
(
2≤n≤10
5
)
-- the length of the string. The second line contains
n−1
integers:
a
1
,a
2
,...,a
n−1![]()
(0≤a
i
≤n)
.
The sum of values of n
in all test cases doesn't exceed
10
6![]()
.
The first line contains an integer n
The sum of values of n
Output
For each test case output one integer denoting the answer. The answer must be printed modulo
10
9
+7
.
Sample Input
3 3 0 0 4 3 2 1 3 1 2
Sample Output
16250 26 0
Source
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[1000000];
int n;
void f()
{
int i,j;
long long ans=26;
for(i=1;i<n;i++)
{
if((a[i]-1)!=a[i+1]&&a[i]!=0)
{
printf("0\n");
return ;
}
}
int uu=0;
for(i=1;i<=n;i++)
{
if(a[i]==0)
{
if(uu>0)
{
ans*=25;
ans=ans%1000000007;
}
else
uu++;
}
}
printf("%I64d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int i,j;
for(i=1;i<n;i++)
{
scanf("%d",&a[i]);
}
a[n]=0;
f();
}
return 0;
}