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. form表单以get方式提交时action中?后面的参数部分不生效

    form表单的提交方式是get方式,action="?sss=test",问号后面参数是接受不到的,谨记!

  2. 「TJOI2013」循环格

    题目链接 戳我 \(Solution\) 我们观察发现循环格要满足每个点的入度都为\(1\) 证明: 我们假设每个点的入读不一定为\(1\),那么必定有一个或多个点的入度为0,那么则不满足循环格的定义 ...

  3. (二)RabbitMQ使用笔记

    1.RabbitMQ简介 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. 官网:http://www.rabbitmq.c ...

  4. django修改数据库连接

    settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'domain', 'USER ...

  5. Spring Boot的每个模块包详解

    Spring Boot的每个模块包详解,具体如下: 1.spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. 2.spring-boot-s ...

  6. python连接postgreSQL

    利用python(我用的是python2.7版本)连接postgresql数据库,这里使用psycopg2这个插件 官网下载psycopg2-2.5.1.tar.gz:http://initd.org ...

  7. 190225Redis

    一.Redis的简单使用 Redis操作模式 # Author:Li Dongfei import redis r = redis.Redis(host='192.168.56.7', port=63 ...

  8. C++ #include " " 与 <>有什么区别?

    #include <> 和 #include "" 都会在实现定义的位置查找文件,并将其包含. 区别是若 #include "" 查找成功,则遮蔽 ...

  9. VS2010 由于缺少调试目标"xx.exe"

    有两种可能会造成这种现像.A.配制属性出了问题. 一种方法:右击“解决方案”->“属性”,在弹出的“属性页”框中,选择左边的“配置属性”,在右边,将应用程序的生成那个框框勾上,二可能是这里的属性 ...

  10. centos 安装搜狗

    原文:https://www.cnblogs.com/blueherb/p/9521827.html 1.通过centos的搜狐到百度下载linux fo sougou 注:找到下载路径(定义为A) ...