#include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> using namespace std; typedef long long LL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) int M[100][100][2]; char S[100], S2[100]; int n; int go(int a, int b, int c) { if (M[a][b][c] != -1) return M[a][b][c]; if (a == b) { return (S[a] == 'C') == c; } int res = 0; FOR(s, a, b) { if (go(a, s, (c + 1) % 2) && go(s+1,b, (c + 1) %2)) res = 1; } return M[a][b][c] = res; } int go() { int res = 0; int k = 0; FOR(i,0,n) { k += S[i] == 'N'; } if (k == 0) { MINUS(M); return go(0, n - 1, 0) + go(0, n - 1, 1); } else { FOR(i,0,n) S2[i] = S[i]; FOR(m,0,1<<k) { int k = 0; FOR(i,0,n) { if (S2[i] == 'N') { if ((m>>k)&1) S[i] = 'C'; else S[i] = 'Z'; ++k; } else S[i] = S2[i]; } MINUS(M); res += go(0, n - 1, 0) + go(0, n - 1, 1); } FOR(i,0,n) S[i] = S2[i]; return res; } } int main() { int q; scanf("%d%d", &n, &q); scanf("%s", S); printf("%d\n", go()); FOR(i,0,q) { int idx; char c; scanf("%d %c", &idx, &c); S[idx - 1] = c; MINUS(M); printf("%d\n", go()); } }
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <cstdio> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <sstream> #include <iostream> #include <map> #include <cmath> #include <set> #include <queue> #include <numeric> #include <ctime> using namespace std; typedef long long LL; #define INF 1000000000 #define SZ(v) ((int)(v).size()) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define FORE(i,a,b) for(int i=(a);i<=(b);i++) #define FS first #define SD second #define MP make_pair #define ZERO(t) memset(t, 0, sizeof(t)) #define MINUS(t) memset(t, -1, sizeof(t)) #define MOD 1000000007 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) #define MAX(_a,_b) ((_a)>(_b)?(_a):(_b)) int M[100][100][2]; char S[100], S2[100]; int n; int go(int a, int b, int c) { if (M[a][b][c] != -1) return M[a][b][c]; if (a == b) { return (S[a] == 'C') == c; } int res = 0; FOR(s, a, b) { if (go(a, s, (c + 1) % 2) && go(s+1,b, (c + 1) %2)) res = 1; } return M[a][b][c] = res; } int go() { int res = 0; int k = 0; FOR(i,0,n) { k += S[i] == 'N'; } if (k == 0) { MINUS(M); return go(0, n - 1, 0) + go(0, n - 1, 1); } else { FOR(i,0,n) S2[i] = S[i]; FOR(m,0,1<<k) { int k = 0; FOR(i,0,n) { if (S2[i] == 'N') { if ((m>>k)&1) S[i] = 'C'; else S[i] = 'Z'; ++k; } else S[i] = S2[i]; } MINUS(M); res += go(0, n - 1, 0) + go(0, n - 1, 1); } FOR(i,0,n) S[i] = S2[i]; return res; } } int main() { int q; scanf("%d%d", &n, &q); scanf("%s", S); printf("%d\n", go()); FOR(i,0,q) { int idx; char c; scanf("%d %c", &idx, &c); S[idx - 1] = c; MINUS(M); printf("%d\n", go()); } } |