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

 
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
 
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\n",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 */

  

 
 

HDU 5635 ——LCP Array ——————【想法题】的更多相关文章

  1. hdu 5635 LCP Array(BC第一题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5635 LCP Array Time Limit: 4000/2000 MS (Java/Others) ...

  2. HDU 5632 Rikka with Array [想法题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5632 ------------------------------------------------ ...

  3. hdu 4655 Cut Pieces(想法题)

    Cut Pieces Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Tota ...

  4. HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...

  5. HDU 4972 Bisharp and Charizard 想法题

    Bisharp and Charizard Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching NBA. He ...

  6. LCP Array(思维)

    LCP Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  7. CodeForces 111B - Petya and Divisors 统计..想法题

    找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...

  8. hdu-5635 LCP Array

    LCP Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  9. HDU - 5969 最大的位或 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5969 (合肥)区域赛签到题...orz 题意:给你l,r,求x|y的max,x,y满足l<=x<=y ...

随机推荐

  1. Android学习笔记 Gallery图库组件的使用

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  2. 快速下载android源码

    众所周知的原因,android源码被墙了,还好国内有不少镜像,这里使用清华提供的镜像. 以下内容转自: https://wiki.tuna.tsinghua.edu.cn/MirrorUsage/an ...

  3. UIView 动画

    1.UIView 动画 核心动画 和 UIView 动画 的区别: 核心动画一切都是假象,并不会真实的改变图层的属性值,如果以后做动画的时候,不需要与用户交互,通常用核心动画(转场). UIView ...

  4. Codeforces Round #545 (Div. 2)D(KMP,最长公共前后缀,贪心)

    #include<bits/stdc++.h>using namespace std;const int N=1000007;char s1[N],s2[N];int len1,len2; ...

  5. 一道组合数问题--出自 曹钦翔_wc2012组合计数与动态规划

    一道组合数问题--出自 曹钦翔_wc2012组合计数与动态规划 [问题描述] 众所周知,xyc 是一个宇宙大犇,他最近在给他的学弟学妹们出模拟赛. 由于 xyc 实在是太巨了,他出了一套自认为很水的毒 ...

  6. 选课 树形背包dp

    题目描述 在大学里每个学生,为了达到一定的学分,必须从很多课程里选择一些课程来学习,在课程里有些课程必须在某些课程之前学习,如高等数学总是在其它课程之前学习.现在有N门功课,每门课有个学分,每门课有一 ...

  7. Qt 学习之路 2(74):线程和 QObject

    Home / Qt 学习之路 2 / Qt 学习之路 2(74):线程和 QObject Qt 学习之路 2(74):线程和 QObject  豆子  2013年12月3日  Qt 学习之路 2  2 ...

  8. C# .Net正则表达式去除HTML标记和空格

    C# .Net正则表达式去除HTML标记和空格 http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaoch ...

  9. O(1)乘法与快速乘O(log)

    //O(1)快速乘 inline LL quick_mul(LL x,LL y,LL MOD){ x=x%MOD,y=y%MOD; return ((x*y-(LL)(((long double)x* ...

  10. P3066 [USACO12DEC]逃跑的BarnRunning Away From (树上二分)

    题意 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 树上二分.这个做法还是基于树上差分的,也就是对于每一个点uu,我们要找到它向上跳LL的长度最高能够跳到的祖先.(当 ...