#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define deb(...)
#define DBP(...)
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x) for (auto& a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
void run();
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cout << fixed << setprecision(12);
run();
cout << flush; _Exit(0);
}
constexpr int MOD = 998'244'353;
constexpr int ALPHA = 7; // end marker + a,b,c,d,e,f
struct State {
ll any[ALPHA] = {}; // c -> #{paths ending with c}
ll tight[ALPHA][ALPHA] = {}; // (d,c) -> #{tight paths ending with c, which don't pass over {c,d} when extended}
State() {
any[0] = 1;
rep(i, 0, ALPHA) tight[i][0] = 1;
}
ll get() const {
ll ans = 0;
rep(i, 0, ALPHA) ans += any[i] - tight[0][i];
ans %= MOD;
if (ans < 0) ans += MOD;
return ans;
}
};
enum CellType : uint8_t {
KEEP = 0, ZERO = 1, SET = 2
};
struct Transition {
ll any[ALPHA][ALPHA] = {}; // c -> direct coefficients
CellType cells[ALPHA][ALPHA] = {}; // (d,c) -> tight cell state
ll tight[ALPHA][ALPHA] = {}; // c -> coefficients for refs
bool refMasks[ALPHA][ALPHA] = {}; // index -> column mask in state
int refRows[ALPHA] = {}; // index -> row in state
int numRefs = 0;
Transition() {
rep(i, 0, ALPHA) any[i][i] = 1;
}
void push(int c) {
rep(i, 0, ALPHA) if (i != c) rep(j, 0, ALPHA) any[c][j] += any[i][j];
rep(j, 0, ALPHA) any[c][j] %= MOD;
ll vec[ALPHA] = {};
bool newRef = 0;
rep(i, 0, ALPHA) {
if (cells[c][i] == KEEP) {
refMasks[numRefs][i] = 1;
newRef = 1;
} else if (cells[c][i] == SET) {
rep(j, 0, numRefs) vec[j] += tight[i][j];
}
}
rep(i, 0, numRefs) {
vec[i] %= MOD;
}
if (newRef) {
vec[numRefs] = 1;
refRows[numRefs++] = c;
}
memcpy(tight[c], vec, sizeof(vec));
rep(i, 0, ALPHA) cells[c][i] = ZERO;
rep(i, 0, ALPHA) cells[i][c] = SET;
}
void apply(State& s) const {
ll newAny[ALPHA];
rep(i, 0, ALPHA) {
newAny[i] = 0;
rep(j, 0, ALPHA) newAny[i] += any[i][j] * s.any[j];
newAny[i] %= MOD;
}
memcpy(s.any, newAny, sizeof(newAny));
ll refVals[ALPHA];
rep(i, 0, numRefs) {
refVals[i] = 0;
rep(j, 0, ALPHA) if (refMasks[i][j]) refVals[i] += s.tight[refRows[i]][j];
refVals[i] %= MOD;
}
rep(i, 0, ALPHA) {
ll val = 0;
rep(j, 0, numRefs) val += tight[i][j] * refVals[j];
val %= MOD;
rep(j, 0, ALPHA) {
if (cells[j][i] == SET) {
s.tight[j][i] = val;
} else if (cells[j][i] == ZERO) {
s.tight[j][i] = 0;
}
}
}
}
};
constexpr int BLOCK = 300;
string txt;
vector<State> states;
vector<Transition> transitions;
void rebuild(int i) {
int begin = i*BLOCK, end = min(begin+BLOCK, sz(txt));
transitions[i] = {};
rep(j, begin, end) transitions[i].push(txt[j]-'a'+1);
}
ll updateFrom(int i) {
State state = states[i];
transitions[i].apply(state);
while (++i < sz(states)) {
states[i] = state;
transitions[i].apply(state);
}
return state.get();
}
void run() {
int n, q, p;
cin >> n >> q >> txt;
int numBlocks = (n+BLOCK-1) / BLOCK;
states.resize(numBlocks);
transitions.resize(numBlocks);
rep(i, 0, sz(states)) rebuild(i);
cout << updateFrom(0) << '\n';
while (q--) {
cin >> p;
p--;
cin >> txt[p];
rebuild(p/BLOCK);
cout << updateFrom(p/BLOCK) << '\n';
}
}
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define deb(...) #define DBP(...) using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); run(); cout << flush; _Exit(0); } constexpr int MOD = 998'244'353; constexpr int ALPHA = 7; // end marker + a,b,c,d,e,f struct State { ll any[ALPHA] = {}; // c -> #{paths ending with c} ll tight[ALPHA][ALPHA] = {}; // (d,c) -> #{tight paths ending with c, which don't pass over {c,d} when extended} State() { any[0] = 1; rep(i, 0, ALPHA) tight[i][0] = 1; } ll get() const { ll ans = 0; rep(i, 0, ALPHA) ans += any[i] - tight[0][i]; ans %= MOD; if (ans < 0) ans += MOD; return ans; } }; enum CellType : uint8_t { KEEP = 0, ZERO = 1, SET = 2 }; struct Transition { ll any[ALPHA][ALPHA] = {}; // c -> direct coefficients CellType cells[ALPHA][ALPHA] = {}; // (d,c) -> tight cell state ll tight[ALPHA][ALPHA] = {}; // c -> coefficients for refs bool refMasks[ALPHA][ALPHA] = {}; // index -> column mask in state int refRows[ALPHA] = {}; // index -> row in state int numRefs = 0; Transition() { rep(i, 0, ALPHA) any[i][i] = 1; } void push(int c) { rep(i, 0, ALPHA) if (i != c) rep(j, 0, ALPHA) any[c][j] += any[i][j]; rep(j, 0, ALPHA) any[c][j] %= MOD; ll vec[ALPHA] = {}; bool newRef = 0; rep(i, 0, ALPHA) { if (cells[c][i] == KEEP) { refMasks[numRefs][i] = 1; newRef = 1; } else if (cells[c][i] == SET) { rep(j, 0, numRefs) vec[j] += tight[i][j]; } } rep(i, 0, numRefs) { vec[i] %= MOD; } if (newRef) { vec[numRefs] = 1; refRows[numRefs++] = c; } memcpy(tight[c], vec, sizeof(vec)); rep(i, 0, ALPHA) cells[c][i] = ZERO; rep(i, 0, ALPHA) cells[i][c] = SET; } void apply(State& s) const { ll newAny[ALPHA]; rep(i, 0, ALPHA) { newAny[i] = 0; rep(j, 0, ALPHA) newAny[i] += any[i][j] * s.any[j]; newAny[i] %= MOD; } memcpy(s.any, newAny, sizeof(newAny)); ll refVals[ALPHA]; rep(i, 0, numRefs) { refVals[i] = 0; rep(j, 0, ALPHA) if (refMasks[i][j]) refVals[i] += s.tight[refRows[i]][j]; refVals[i] %= MOD; } rep(i, 0, ALPHA) { ll val = 0; rep(j, 0, numRefs) val += tight[i][j] * refVals[j]; val %= MOD; rep(j, 0, ALPHA) { if (cells[j][i] == SET) { s.tight[j][i] = val; } else if (cells[j][i] == ZERO) { s.tight[j][i] = 0; } } } } }; constexpr int BLOCK = 300; string txt; vector<State> states; vector<Transition> transitions; void rebuild(int i) { int begin = i*BLOCK, end = min(begin+BLOCK, sz(txt)); transitions[i] = {}; rep(j, begin, end) transitions[i].push(txt[j]-'a'+1); } ll updateFrom(int i) { State state = states[i]; transitions[i].apply(state); while (++i < sz(states)) { states[i] = state; transitions[i].apply(state); } return state.get(); } void run() { int n, q, p; cin >> n >> q >> txt; int numBlocks = (n+BLOCK-1) / BLOCK; states.resize(numBlocks); transitions.resize(numBlocks); rep(i, 0, sz(states)) rebuild(i); cout << updateFrom(0) << '\n'; while (q--) { cin >> p; p--; cin >> txt[p]; rebuild(p/BLOCK); cout << updateFrom(p/BLOCK) << '\n'; } } |
English