♥HDOJ 5635-LCP Array

来源:互联网 发布:wpf 管理系统源码购 编辑:程序博客网 时间:2024/06/05 21:08

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=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 asai=lcp(suffi,suffi+1)(1i<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 modulo109+7.
 

Input
There are multiple test cases. The first line of input contains an integerT indicating the number of test cases. For each test case:

The first line contains an integer n (2n105) -- the length of the string. The second line contains n1 integers: a1,a2,...,an1(0ain).

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 modulo109+7.
 

Sample Input
330 043 2 131 2
 

Sample Output
16250260
 

Source
BestCoder Round #74 (div.2)

#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;}elseuu++;}}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;} 


 
1 0
原创粉丝点击