HDU 5635 ——LCP Array ——————【想法题】

LCP Array

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


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
3
3
0 0
 
4
3 2 1
 
 
3
1 2
 
Sample Output
16250
26
0
 
Source
 
题目大意:
 
 
解题思路:由于ai是相邻后缀的最长公共前缀LCP,那么我们手写几个样例对应的字符串看看是不是有什么规律,我们发现当a[i-1]!=0&&a[i] >= a[i-1]是不可能存在对应的字符串的如 1 2 0或 1 1 0,就算是递减的,也应该是依次减1的;同时a[i] <= n-i,即suff[i]与suff[i+1]的最长公共前缀应该小于n-i的,存在限制关系。还有就是出现0的个数就是应该结果乘以25的个数,如果所给数据是存在非0解的,那么结果最小解应该是26,所以初始化为26。
 
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<math.h>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1e6;
const LL mod = 1000000007;
int a[maxn];
int main(){
    int cas ;
    scanf("%d",&cas);
    while(cas--){
        int n, flag = 0, c = 0;
        scanf("%d",&n);
        for(int i = 1; i < n; i++){
            scanf("%d",&a[i]);
            if(!a[i]) c++;
            if(a[i] > n-i){ //限制条件
                flag = 1;
            }
            if(a[i-1] != 0 && a[i-1] - a[i] != 1){  //减1递减
                flag = 1;
            }
        }
        if(flag){
            puts("0"); continue;
        }
        LL ans = 26;
        for(int i = 1; i <= c; i++){
            ans = (ans * 25) % mod;
        }
        printf("%lld
",ans);
    }
    return 0;
}
/*
55
5
0 1 0 4
5
0 1 0 2
4
3 2 1
5
0 1 2 0
7
0 0 3 2 1 0

*/

  

 
 
原文地址:https://www.cnblogs.com/chengsheng/p/5248081.html