【HDU5635 BestCoder Round 74 (div2)A】【贪心 暴力】LCP Array 相邻后缀最大相同前缀 输出字符串方案数

LCP Array

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


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
BestCoder Round #74 (div.2)
 

Recommend
wange2014
 
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n;
void datamaker()
{
	freopen("c://test//input.in", "w", stdout);
	casenum = 10000;
	printf("%d\n", casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		n = rand() % 10 + 2; printf("%d\n", n);
		for (int i = 1; i < n; ++i)printf("%d ", rand() % (n+2));
		puts("");
	}
}

int b[N];
int observe()
{
	for (int i = 1; i < n; ++i)scanf("%d", &b[i]);
	if (b[n - 1] != 0 && b[n - 1] != 1)return 0;

	LL ans = b[1] == 0 ? 26 * 25 : 26;
	for (int i = 2; i < n; ++i)
	{
		if (b[i - 1] != 0 && b[i] != b[i - 1] - 1)return 0;
		if (b[i] == 0)ans = ans * 25 % Z;
	}
	return ans;
}
int solve()
{
	int x;
	LL ans = 26;
	int stop = 0;//stop表示a[stop]!=a[stop+1]
	for (int i = 1; i < n; ++i)
	{
		scanf("%d", &x);
		if (stop < i) stop = i + x;
		else if (stop != i + x)ans = 0;
		if (x == 0)ans = ans * 25 % Z;
	}
	if (stop > n)ans = 0;
	return ans;
}
int main()
{
	//datamaker(); return 0;
	//fre();
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d", &n);
		//printf("%d\n", observe()); continue;
		printf("%d\n", solve());
	}
	return 0;
}
/*
【trick&&吐槽】
1,多组数据一定要把单组的数据处理完
2,边界的乘法要注意别搞错

【题意】
给你一个字符串,其完全由小写字母组成,其长度为n(2<=n<=1e5)
我们知道任意两个相邻后缀的最长公共前缀,让你求出这个字符串可能的方案数。

【类型】
思维

【分析】
=======================================
first thought

因为题目告诉你了每两个相邻后缀的关系。
所以我们根本不需要考虑两个相邻后缀的公共前缀的长度是多少,
只需要考虑是否为为0,
如果为0,那么意味着下一个字符串有26-1种可能
如果不为0,那么意味着下一个字符串有唯一种可能

========================================
于是我收获了WA.

思考了下,发现,因为可能会出现前后矛盾。
我们假设读到的a[i]与a[i+1]的最长前缀的数组为b[]
那么b[]肯定是呈现如此风貌的数组——

x,x-1,x-2,...,2,1,0,0,...,0,0,y,y-1,y-2,...,2,1,0,...,1or0(最后一定是1或0)
我们只要检测是否b[]是这样的数列即可

【时间复杂度&&优化】
O(n)

*/


你可能感兴趣的:(贪心,暴力,题库-HDU)