#include <array>
#include <bitset>
#include <cstring>
#include <cstdio>
#include <map>
#include <set>
#include <string>
const int LITER = 6;
const int MASEK = 1 << LITER;
const unsigned MAX = 998244353;
struct Modulo
{
unsigned value;
Modulo(unsigned v = 0) : value(v) { }
void operator+=(Modulo m) {
value += m.value;
if (value >= MAX) {
value -= MAX;
}
}
void operator++() {
if (++value == MAX) {
value = 0;
}
}
bool operator==(Modulo m) const {
return value == m.value;
}
};
struct Stan
{
std::array<Modulo, LITER> c; // c[x] to różne kończące się na x
// z[x][maska] zlicza te unikalne podciągi, które
// * kończą się na a (x=0), b (x=1) lub c (x=2)
std::array<std::array<Modulo, MASEK>, LITER> z; // unikalne
// maska opisuje, czy po podciągu występuje a lub b lub c
std::array<bool, LITER> wystepowalo;
void reset() {
c.fill(0);
for (int i=0; i<LITER; ++i) {
z[i].fill(0);
}
wystepowalo.fill(false);
}
unsigned count_dup() const {
return (MAX + count_all() - count_uni()) % MAX;
}
unsigned count_all() const {
Modulo result;
for (int i=0; i<LITER; ++i) {
result += c[i];
}
return result.value;
}
unsigned count_uni() const {
Modulo result;
for (int i=0; i<LITER; ++i) {
for (unsigned m=0; m<MASEK; ++m) {
result += z[i][m];
}
}
return result.value;
}
bool operator==(const Stan& s) const {
return c == s.c && z == s.z && wystepowalo == s.wystepowalo;
}
};
void update(const Stan& old, Stan& niu, char c) {
const int x = c - 'a';
niu.reset();
niu.wystepowalo = old.wystepowalo;
Modulo suma;
for (int y=0; y<LITER; ++y) {
niu.c[y] = old.c[y];
suma += old.c[y];
}
niu.c[x] = suma;
++niu.c[x];
const unsigned mx = (1 << x); // a:1, b:2, c:4
// ostatnią wybraną literą jest x (a/b/c)
for (int y=0; y<LITER; ++y) {
// kończące się na inne litery możemy wykorzystać
if (y != x) {
for (unsigned m=0; m<MASEK; ++m) {
// o ile nie występowało po nich x
if (!(m & mx)) {
// dopisując do nich x, wtedy kończą się na x
// i wtedy po nich nic nie wystepuje
niu.z[x][0] += old.z[y][m];
}
}
// albo biorąc tak jak jest
for (unsigned m=0; m<MASEK; ++m) {
niu.z[y][m|mx] += old.z[y][m];
// ale wtedy będzie po nich występowało x
}
}
}
// kończącym się na x możemy dopisać x
// i wtedy po nich nic nie wystepuje
for (unsigned m=0; m<MASEK; ++m) {
niu.z[x][0] += old.z[x][m];
}
// jeśli x jeszcze nie występowało
if (!old.wystepowalo[x]) {
// dodajemy pojedynczą literę
++niu.z[x][0];
}
niu.wystepowalo[x] = true;
}
Stan stany[50000];
char slowo[50001];
int main() {
int N, Q; scanf("%d %d\n", &N, &Q);
fread(slowo, N, 1, stdin);
slowo[N] = 0;
//if (argc < 2) return 1;
//const char* slowo = argv[1];
//int N = strlen(slowo);
//printf("Odczytano słowo: “%s”\n", slowo);
Stan temp; temp.reset();
update(temp, stany[0], slowo[0]);
for (int i=1; i<N; ++i) {
update(stany[i-1], stany[i], slowo[i]);
}
printf("%u\n", stany[N-1].count_dup());
while (Q-- > 0) {
int p; char c;
scanf("%d %c\n", &p, &c); --p;
if (slowo[p] != c) {
slowo[p] = c;
while (p < N) {
update(stany[p-1], temp, slowo[p]);
if (!(p % 10) && temp == stany[p]) {
break;
}
stany[p] = temp;
++p;
}
}
printf("%u\n", stany[N-1].count_dup());
}
}
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 166 167 168 169 170 171 | #include <array> #include <bitset> #include <cstring> #include <cstdio> #include <map> #include <set> #include <string> const int LITER = 6; const int MASEK = 1 << LITER; const unsigned MAX = 998244353; struct Modulo { unsigned value; Modulo(unsigned v = 0) : value(v) { } void operator+=(Modulo m) { value += m.value; if (value >= MAX) { value -= MAX; } } void operator++() { if (++value == MAX) { value = 0; } } bool operator==(Modulo m) const { return value == m.value; } }; struct Stan { std::array<Modulo, LITER> c; // c[x] to różne kończące się na x // z[x][maska] zlicza te unikalne podciągi, które // * kończą się na a (x=0), b (x=1) lub c (x=2) std::array<std::array<Modulo, MASEK>, LITER> z; // unikalne // maska opisuje, czy po podciągu występuje a lub b lub c std::array<bool, LITER> wystepowalo; void reset() { c.fill(0); for (int i=0; i<LITER; ++i) { z[i].fill(0); } wystepowalo.fill(false); } unsigned count_dup() const { return (MAX + count_all() - count_uni()) % MAX; } unsigned count_all() const { Modulo result; for (int i=0; i<LITER; ++i) { result += c[i]; } return result.value; } unsigned count_uni() const { Modulo result; for (int i=0; i<LITER; ++i) { for (unsigned m=0; m<MASEK; ++m) { result += z[i][m]; } } return result.value; } bool operator==(const Stan& s) const { return c == s.c && z == s.z && wystepowalo == s.wystepowalo; } }; void update(const Stan& old, Stan& niu, char c) { const int x = c - 'a'; niu.reset(); niu.wystepowalo = old.wystepowalo; Modulo suma; for (int y=0; y<LITER; ++y) { niu.c[y] = old.c[y]; suma += old.c[y]; } niu.c[x] = suma; ++niu.c[x]; const unsigned mx = (1 << x); // a:1, b:2, c:4 // ostatnią wybraną literą jest x (a/b/c) for (int y=0; y<LITER; ++y) { // kończące się na inne litery możemy wykorzystać if (y != x) { for (unsigned m=0; m<MASEK; ++m) { // o ile nie występowało po nich x if (!(m & mx)) { // dopisując do nich x, wtedy kończą się na x // i wtedy po nich nic nie wystepuje niu.z[x][0] += old.z[y][m]; } } // albo biorąc tak jak jest for (unsigned m=0; m<MASEK; ++m) { niu.z[y][m|mx] += old.z[y][m]; // ale wtedy będzie po nich występowało x } } } // kończącym się na x możemy dopisać x // i wtedy po nich nic nie wystepuje for (unsigned m=0; m<MASEK; ++m) { niu.z[x][0] += old.z[x][m]; } // jeśli x jeszcze nie występowało if (!old.wystepowalo[x]) { // dodajemy pojedynczą literę ++niu.z[x][0]; } niu.wystepowalo[x] = true; } Stan stany[50000]; char slowo[50001]; int main() { int N, Q; scanf("%d %d\n", &N, &Q); fread(slowo, N, 1, stdin); slowo[N] = 0; //if (argc < 2) return 1; //const char* slowo = argv[1]; //int N = strlen(slowo); //printf("Odczytano słowo: “%s”\n", slowo); Stan temp; temp.reset(); update(temp, stany[0], slowo[0]); for (int i=1; i<N; ++i) { update(stany[i-1], stany[i], slowo[i]); } printf("%u\n", stany[N-1].count_dup()); while (Q-- > 0) { int p; char c; scanf("%d %c\n", &p, &c); --p; if (slowo[p] != c) { slowo[p] = c; while (p < N) { update(stany[p-1], temp, slowo[p]); if (!(p % 10) && temp == stany[p]) { break; } stany[p] = temp; ++p; } } printf("%u\n", stany[N-1].count_dup()); } } |
English