-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC.cpp
65 lines (59 loc) · 1.68 KB
/
C.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 150+5;
const int ha = 51123987;
int n;
char str[MAXN];
inline void add(int &x,int y){
x += y-ha;x += x>>31&ha;
}
int f[2][MAXN][MAXN][MAXN];
// i,j,a,b
int nxt[MAXN][3];
int main(){
scanf("%d%s",&n,str+1);
int now = 0;f[0][0][0][0] = 1;
nxt[n+1][0] = nxt[n+1][1] = nxt[n+1][2] = n+1;
ROF(i,n,0){
FOR(j,0,2) nxt[i][j] = nxt[i+1][j];
if(i)nxt[i][str[i]-'a'] = i;
}
FOR(i,1,n){
CLR(f[now^1],0);
FOR(j,0,n){
FOR(a,0,i-1){
FOR(b,0,i-1-a){
if(!f[now][j][a][b]) continue;
int nj = nxt[j][0];
if(nj <= n) add(f[now^1][nj][a+1][b],f[now][j][a][b]);
nj = nxt[j][1];
if(nj <= n) add(f[now^1][nj][a][b+1],f[now][j][a][b]);
nj = nxt[j][2];
if(nj <= n) add(f[now^1][nj][a][b],f[now][j][a][b]);
}
}
}
now ^= 1;
}
int ans = 0;
FOR(a,0,n){
FOR(b,0,n-a){
int c = n-a-b;
if(std::abs(std::max({a,b,c})-std::min({a,b,c})) <= 1) FOR(k,1,n) add(ans,f[now][k][a][b]);
}
}
printf("%d\n",ans);
return 0;
}