题目链接:点击打开链接
LCP Array
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1527 Accepted Submission(s): 465
Problem Description
Peter has a string
s=s1s2...sn
, let
suffi=sisi+1...sn
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
ai=lcp(suffi,suffi+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 109+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 109+7 .
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≤105) -- the length of the string. The second line contains n−1 integers: a1,a2,...,an−1 (0≤ai≤n) .
The sum of values of n in all test cases doesn't exceed 106 .
The first line contains an integer n ( 2≤n≤105) -- the length of the string. The second line contains n−1 integers: a1,a2,...,an−1 (0≤ai≤n) .
The sum of values of n in all test cases doesn't exceed 106 .
Output
For each test case output one integer denoting the answer. The answer must be printed modulo
109+7
.
Sample Input
3 3 0 0 4 3 2 1 3 1 2
Sample Output
16250 26 0
大意:
问题描述
Peter有一个字符串s=s1s2...sn, 令suffi=sisi+1...sn是s第i字符开头的后缀. Peter知道任意两个相邻的后缀的最长公共前缀ai=lcp(suffi,suffi+1)(1≤i<n). 现在给你数组a, Peter有多少个仅包含小写字母的字符串满足这个数组. 答案也许会很大, 你只要输出对109+7取模的结果即可.
输入描述
输入包含多组数据. 第一行有一个整数T, 表示测试数据的组数. 对于每组数据: 第一行包含一个整数n (2≤n≤105)表示字符串的长度. 第二行包含n−1个整数: a1,a2,...,an−1 (0≤ai≤n). 所有数据中n的和不超过106.
输出描述
对于每组数据, 输出答案对109+7取模的结果.
思路:
观察 a[i] 可以发现,a[i] 是逐一递减的而且每次递减值为 1,直至递减为 0,再开始又一次的递减。
发现是排列组合问题等价于排列 26 种颜色,两两相邻不相同放同一种颜色,第一次肯定是放 26 种颜色啦,然后碰见一次 0 放一种颜色
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const LL mod=1e9+7;
int n;
int a[100010];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
bool flag=1;
LL ans=26;
for(int i=1;i<n;i++)
{
scanf("%d",a+i);
if(a[i]>=n-i+1)
flag=0;
if(a[i-1]!=0&&a[i]!=a[i-1]-1)
{
flag=0;
}
if(a[i]==0)
ans=ans*25%mod;
}
if(flag)
printf("%lld\n",ans);
else
puts("0");
}
return 0;
}