【hdu 5635】LCP Array 中文题意&题解&代码(C++)

LCP Array

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

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).

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.

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


中文题意:
有一个数组a,a[i]表示一个字符串以第i位开头的后缀与以第i+1位为开头的后缀的最长公共前缀的长度,现在给你数组a, 问有多少个仅包含小写字母的字符串满足这个数组的性质. 答案也许会很大, 你只要输出对1000000007取模的结果.


题解:
将字符串用线段表示,画个图后很容易会发现假如a[i]不等于0则这个字符串从第i位开始一直到第i+a[i]位都必须是同一个字符,而且当a[i]不为零时a[i+1]一定等于a[i]-1,因此利用这个性质我们可以判断出是否存在满足数组a的字符串,那么该怎么统计个数呢,还是自己在本子上写写画画会发现只有当a[i]等于0的时候字符串的第i位与第i+1是不同的字符,然后就简单了,直接一遍扫过去,然而代码能力太差wa了好多次,最后发现脑残的在输入数据的判断时加了break。。。。


#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int flag,tot,T,n;
long long  ans;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        ans=26;tot=0;flag=0;
        int mmod=1000000007;
        int now=0,pre=0;
        scanf("%d",&n);
        for (int i=1;i<n;i++)
        {
            scanf("%d",&now);
            if ( now!=pre-1 && pre>0 )  flag=1;
            else if ( now==0 )  tot++;
        pre=now;
        }
        if (now>1)  flag=1;
     //最后一位的最长公共前缀一定是0或1,不可能超过n-i
     //至于为什么不用在中间判断a[i]是否大于n-i,那是因为如果最后一位满足了上面的条件,而中间有一位超过了n-i,那么一定存在now!=pre-1
        for (int i=1;i<=tot;i++)
        ans=(ans*25)%mmod;
        if (flag) printf("0\n");
        else printf("%I64d\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值