gym 100958 b 题解

gym 100958 b

若我们按照第一个字符将字符串分类,可以发现所有的字符串形如:

a...

a...

b...

b...

b...

.

.

z...

z...

由于开头是a的一定比开头是b的小,所以这两类就可以分开考虑了,而同一类就只需要比一下后\(len-1\)个字符,这样就转换成了几个子问题。

我们可以设dp状态为\(dp_{i,j,l,r}\)表示\(s_l,s_{l+1}...s_{r-1},s_r\)的第i位都\(\geq j\)的方案数。

转移的时候枚举\(i\)的字符和第一类的长度就可以了。

rng_58

using namespace std;
 
#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define snuke(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
 
typedef long long ll;
#define MOD 1000000007ll
 
int N;
string s[60];
ll dp[30][30][60][60];
 
ll func(int pos, int next, int L, int R){
	int i;
	
	if(next == 27) return dp[pos][next][L][R] = 0;
	if(pos == 20) return dp[pos][next][L][R] = ((R-L == 1) ? 1 : 0);
	if(dp[pos][next][L][R] != -1) return dp[pos][next][L][R];
	
	ll ans = func(pos, next+1, L, R);
	for(int M=L+1;M<=R;M++){
		if(s[M-1][pos] == '?' && next == 0) break;
		if(s[M-1][pos] != '?' && next != s[M-1][pos] - 'a' + 1) break;
		ll tmp = func(pos+1, 0, L, M);
		if(M < R) tmp = tmp * func(pos, next+1, M, R) % MOD;
		ans = (ans + tmp) % MOD;
	}
	
	return dp[pos][next][L][R] = ans;
}
 
int main(void){
	int i,j,k,l;
	
	cin >> N;
	REP(i,N) cin >> s[i];
	
	char small = 'a';
	small--;
	REP(i,N) while(s[i].length() < 20) s[i] += small;
	
	REP(i,30) REP(j,30) REP(k,60) REP(l,60) dp[i][j][k][l] = -1;
	
	ll ans = func(0, 0, 0, N);
	cout << ans << endl;
	
	return 0;
}
posted @ 2020-12-03 12:54  WWW~~~  阅读(87)  评论(0编辑  收藏  举报