LCP Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 596 Accepted Submission(s): 161
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
真特喵的是一个很蛋疼的题目、其实如果题读懂了,结果也就很容易弄出来了、我们这边有三个人同时做这个题,最终因为每一个人的意见不同,导致结果不统一,更导致推论的不统一,更导致了没法实践、、、、、、一直都在Wa,终测之后的正确率也很容易证明,这是一个坑B的题目。。。
其实题干的意识是保证了最长前缀是一定从第一个字符开始匹配上的。我们一直在想的问题是强调了最长前缀应该是表示最长的部分,不用一定从第一个字符开始匹配,那么如果按照题目保证的来,那么a【】一定是递减的,而且递减一定是相差1的、如果不是相差1,那么是不可能的、除非当前a【】是0、
AC代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll long long int
const int mod = 1e9 + 7;
ll a[121212];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n-1;i++)
{
scanf("%I64d",&a[i]);
}
ll output=26;ll cur=0;
for(int i=n-2;i>=0;i--)
{
if(a[i]-cur==1)
{
cur=a[i];
}
else if(a[i]==0)
{
output=(output*25)%mod;
cur=0;
}
else
{
output=0;
break;
}
}
printf("%I64d\n",output);
}
}