[CSP-S Simulation Test]: simple sequence (DP)

Title Description

Once there bracketed sequence $ s $, satisfies $ | s | = m $. You need to count the number of $ (p, q) $ bracketed sequence.
Where $ (p, q) $ satisfies $ | p | + | s | + | q | = n $, and $ p + s + q $ is a legitimate bracket sequence.


Input Format

Read data from files $ bracket.in $ in. The first line of two positive integers $ n, m $.
A second line length $ m $ sequence parentheses, represents $ s $.


Output Format

Output to a file in $ bracket.out $.
A line output integer representing eligible $ (p, q) $ modulus number takes a value of $ 10 to $ ^ 9 + 7.


Sample

Sample input 1:

4 1 (

Sample output 1:

4

Sample input 2:

4 4 (())

Sample Output 2:

1

Sample input 3:

4 3 (((

Sample output 3:

0


Data range and tips

For $ 10 \% $ data, $ n \ leqslant 20 $;
for $ 25 \% $ data, $ n \ leqslant 200 $;
for data additional $ 5 \% $ a, $ n = m $;
for $ 55 \% $ data, $ n-m \ leqslant 200 $;
for $ 100 \% $ data, $ 1 \ leqslant m \ leqslant n \ leqslant 10 ^ 5, n-m \ leqslant 2,000 $.


answer

First, it might set the left parenthesis as $ 1 + $, $ -1 $ is a right parenthesis, so as to meet the last and $ 0 $, and all prefixes of not less than $ 0 $.

The minimum original prefix string is less than $ 0 $ process, it must be valid so that the left bracket before the original string.

Consider $ the DP $, set $ dp [i] [j] $ denotes the length of $ I $, the sum of the number of brackets sequence $ J $, then $ p $ program number $ dp [i] [j] $, $ Q $ program number $ dp [nmi] [ja] $ ($ a $ string is the sum of the original).

The sum of the product of their answers.

Time complexity: $ \ Theta ((nm) ^ 2) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


代码时刻

#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
int n,m;
char ch[100001];
int res,minn;
long long dp[2001][2001],ans;
int main()
{
	scanf("%d%d%s",&n,&m,ch+1);
	for(int i=1;i<=m;i++)
	{
		if(ch[i]=='(')res++;
		else res--;
		minn=min(minn,res);
	}
	dp[0][0]=1;
	for(int i=1;i<=n-m;i++)
		for(int j=0;j<=i;j++)
		{
			dp[i][j]=dp[i-1][j+1];
			if(j)dp[i][j]=(dp[i][j]+dp[i-1][j-1])%mod;
		}
	for(int i=0;i<=n-m;i++)
		for(int j=0;j<=i;j++)
			if(j+res<=n-m&&j+minn>=0)
				ans=(ans+dp[i][j]*dp[n-m-i][res+j]%mod)%mod;
	printf("%lld",ans);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11670426.html