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

BestCoder Round #74 (div.2)

题目大意:

解题思路:由于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

*/

  

时间: 2024-08-11 05:29:13

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

hdu 2158 不错的想法题

思路 : 两个变量i    j  对每一个i满足查找的都用j来维护  是的这个区间的长度是对i的最小满足要求的区间   这样就不用跑O^n的时间复杂度了 #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int num[100010],mark[100010]; int main() { int n,m,Q,i,j; while(~scanf("%d%d&q

HDU - 5818 Joint Stacks 想法题 关键

http://codeforces.com/problemset/problem/669/D 题意:n个数1~N围成一个圈.q个操作包括操作1:输入x, 所有数右移x.操作2:1,2位置上的数(swap(a[1], a[2])交换:3,4交换.... 题解:观察,发现所有奇数行为都是一样的,偶数同理,(对于操作1 两者相同,对于操作2 奇数位++,偶数位上--: 模拟1,2,即可,然后依次输出其它数字即可.(看清 ac代码: #define _CRT_SECURE_NO_WARNINGS #in

HDU 2410 Barbara Bennett&#39;s Wild Numbers (想法题)

题目链接:HDU 2410 Barbara Bennett's Wild Numbers 题意:给出两串数w,s(长度相同),第一串中有"?",问"?"取的值使w对应的数大于s对应的数 的最大方案数. 思路:W,S一一对应比较: 遇到第一个'?'之前比较情况 1.w[i]<s[i] 方案数0种:break: 2.w[i]>s[i] break.之后有n个''?' 方案数就有10的n次方种. 3.w[i]=s[i] 继续比较,重复1.2两个条件. 遇到'?

HDU 2410 Barbara Bennett&amp;#39;s Wild Numbers (想法题)

题目链接:HDU 2410 Barbara Bennett's Wild Numbers 题意:给出两串数w,s(长度同样),第一串中有"?",问"?"取的值使w相应的数大于s相应的数 的最慷慨案数. 思路:W,S一一相应比較: 遇到第一个'?'之前比較情况 1.w[i]<s[i] 方案数0种:break: 2.w[i]>s[i] break. 之后有n个''?' 方案数就有10的n次方种. 3.w[i]=s[i] 继续比較.反复1.2两个条件. 遇到'

HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出,选择跳过的题,简称想法题. 首先考虑区间的更新方法:区间左端l不动,右端r滑动, 滑到有k个数>=m时,此区间符合条件,并且发现右端点再往右滑到底,此条件一直符合(因为若加入的数小于"第K大的数",则毫无影响.若不然,加入该数会产生一个新的第k大数,保证>="第K大

HDU 4972 Bisharp and Charizard 想法题

Bisharp and Charizard Time Limit: 1 Sec  Memory Limit: 256 MB Description Dragon is watching NBA. He loves James and Miami Heat. Here's an introduction of basketball game:http://en.wikipedia.org/wiki/Basketball. However the game in Dragon's version i

HDU 4893 线段树裸题

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2512    Accepted Submission(s): 751 Problem Description Recently, Doge got a funny birthday present from his new friend, Pro

hdu-5635 LCP Array

LCP Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 358    Accepted Submission(s): 102 Problem Description Peter has a string s=s1s2...sn, let suffi=sisi+1...sn be the suffix start with 

HDU 3854 Glorious Array(树状数组)

题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前距离小于K的结点有多少对,K是一个定值. 思路:先求最初时候小于k的结点有多少对,然后每次改变颜色的时候,统计该点左侧和右侧各有多少同色和异色的结点(这一步使用树状数组),分别处理就行.另外需要预处理离某个结点最近的两个距离小于K的结点的位置. 代码写的略乱. #include<cstdio> #