BestCoder Round #74 (div.2)【1排列组合】

来源:互联网 发布:崇洋媚外的中国知乎 编辑:程序博客网 时间:2024/05/16 01:01

题目链接:点击打开链接

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)(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 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 (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 modulo 109+7.
 

Sample Input
330 043 2 131 2
 

Sample Output
16250260

大意:

问题描述
Peter有一个字符串s=s_{1}s_{2}...s_{n}s=s1s2...sn, 令\text{suff}_i =s_{i}s_{i+1}...s_{n}suffi=sisi+1...snssii字符开头的后缀. Peter知道任意两个相邻的后缀的最长公共前缀a_i = \text{lcp}(\text{suff}_i, \text{suff}_{i+1}) \quad (1 \le i < nai=lcp(suffi,suffi+1)(1i<n).现在给你数组aa, Peter有多少个仅包含小写字母的字符串满足这个数组. 答案也许会很大, 你只要输出对10^9 + 7109+7取模的结果即可.
输入描述
输入包含多组数据. 第一行有一个整数TT, 表示测试数据的组数. 对于每组数据:第一行包含一个整数nn (2 \le n \le 10^5)2n105)表示字符串的长度. 第二行包含n - 1n1个整数: a_1,a_2,...,a_{n-1}a1,a2,...,an1 (0 \le a_i \le n)(0ain).所有数据中nn的和不超过10^6106.
输出描述
对于每组数据, 输出答案对10^9+7109+7取模的结果.
思路:

观察 a[i] 可以发现,a[i] 是逐一递减的而且每次递减值为 1,直至递减为 0,再开始又一次的递减。

发现是排列组合问题等价于排列 26 种颜色,两两相邻不相同放同一种颜色,第一次肯定是放 26 种颜色啦,然后碰见一次 0 放一种颜色

#include<cstdio>#include<cstring>#include<algorithm>#define LL long longusing 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);elseputs("0");}return 0;}





0 0
原创粉丝点击