这道题主要对给定限制条件的利用,然后利用排列组合的知识进行求解,我们通过枚举最后一个填满的点来进行计数,并通过排列组合中的乘法原则简化计数次数
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define MOD 1000000007
#define MAX 107
using namespace std;
int n;
int mark[MAX];
long long dp[MAX][MAX];
long long c[MAX][MAX];
void init ( )
{
c[0][0] = 1;
for ( int i = 1 ; i <= 100 ; i++ )
for ( int j = 0 ; j <= i ; j++ )
if ( i==j || j == 0 ) c[i][j] = 1;
else c[i][j] = ( c[i-1][j-1] + c[i-1][j] )%MOD;
}
void DP ( int left = 1 , int right= n )
{
if ( left == right )
{
dp[left][right]=1;
return;
}
if ( !dp[left+1][right] ) DP ( left +1 , right );
if ( !dp[left][right-1] ) DP ( left , right-1 );
dp[left][right]=(dp[left+1][right]+dp[left][right-1])%MOD;
for ( int t = left + 1 ; t < right ; t++ )
if ( mark[t-1] == mark[t+1] )
{
if ( !dp[left][t-1] ) DP ( left , t-1 );
if ( !dp[t+1][right]) DP ( t+1 , right);
dp[left][right] = ( dp[left][right]
+((dp[left][t-1]*dp[t+1][right])%MOD * c[right-left][t-left])%MOD) %MOD;
}
}
int main ( )
{
while ( ~scanf ( "%d" , &n ) )
{
for ( int i = 1 ; i <= n ; i++ )
scanf ( "%d" , &mark[i] );
memset ( dp , 0 , sizeof ( dp ) );
init ( );
DP ( );
cout << dp[1][n] << endl;
}
}