您的位置:首页 > 其它

LCP Array(思维)

2016-03-05 21:59 429 查看

LCP Array

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 90 Accepted Submission(s): 26

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case output one integer denoting the answer. The answer must be printed modulo 109+7.

[align=left]Sample Input[/align]

3
3
0 0
4
3 2 1
3
1 2

[align=left]Sample Output[/align]

16250
26
0

题解:给出最大相邻后缀的最大前缀让找小写字母组成的串的种数

可以找到规律,当出现数组不等于0的时候,必然是递减的;然后找里面0和连续递减串的个数;

就是26*25^m

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define mem(x,y) memset(x,y,sizeof(x))
typedef long long LL;
const int MOD=1e9+7;
const int MAXN=1e5+100;
int a[MAXN];
LL solve(int N){
int cnt=N;
a
=0;
for(int i=2;i<=N;i++){
if(a[i-1]!=0&&a[i]!=a[i-1]-1)return 0;
if(a[i-1])cnt--;
}
LL ans=26;
for(int i=1;i<cnt;i++){
ans=ans*25%MOD;
}
return ans;
}
int main(){
int T,N;
SI(T);
while(T--){
SI(N);
int cnt;
for(int i=1;i<N;i++){
SI(a[i]);
}
printf("%I64d\n",solve(N));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: