/* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; const LL M=998244353; set<string> subwords; set<string> subwords2; void recurse(string &s, LL pos, string sub) { string z = s[pos]+sub; if (subwords.count(z)>0) subwords2.insert(z); else subwords.insert(z); if (pos!=0) { recurse(s, pos-1, z); recurse(s, pos-1, sub); } } LL brut(string s) { subwords.clear(); subwords2.clear(); recurse(s, s.size()-1, ""); FOREACH(p,subwords2) cout << *p << "\n"; cout << "Subwords: " << subwords.size() << "\n"; cout << "Subwords repeated: " << subwords2.size() << "\n"; cout << "Singletons: " << (subwords.size()-subwords2.size()) << "\n"; return subwords2.size(); } LL dp[1000]; LL last[1000]; LL calc(string s) { LL len=s.size(); FOR(i,0,999) last[i]=-1; FOR(i,0,len-1) { cout << "i=" << i << " s[i]=" << s[i] << " last[s[i]]=" << last[s[i]] << "\n"; if (i==0) dp[i]=2; else dp[i]=2*dp[i-1]; if (last[s[i]]>0) { dp[i] -= dp[last[s[i]]-1]; cout << " odejmuje: " << dp[last[s[i]]-1] << "\n"; } last[s[i]]=i; } return dp[len-1] - 1; } int countSub(string str) { // Create an array to store index // of last vector<int> last(256, -1); // Length of input string int n = str.length(); // dp[i] is going to store count of distinct // subsequences of length i. int dp[n + 1]; int db[n + 1]; // Empty substring has only one subsequence dp[0] = 1; db[0] = 0; // Traverse through all lengths from 1 to n. for (int i = 1; i <= n; i++) { // Number of subsequences with substring // str[0..i-1] dp[i] = 2 * dp[i - 1]; db[i] = 2 * db[i - 1]; cout << "character=" << str[i-1] << " last=" << last[str[i-1]] << "\n"; // If current character has appeared // before, then remove all subsequences // ending with previous occurrence. if (last[str[i - 1]] != -1) { dp[i] = dp[i] - dp[last[str[i - 1]]]; cout << "odjete: " << dp[last[str[i - 1]]] << "\n"; db[i] += dp[last[str[i - 1]]] - 2*db[last[str[i-1]]+1]; } // Mark occurrence of current character last[str[i - 1]] = (i - 1); cout << "dp[" << i << "]=" << dp[i] << "\n"; } //return dp[n]; return db[n]; } LL g[60000]; LL subs[60000]; class Node { public: LL pocz, kon; LL value; Node* parent; Node* child1; Node* child2; Node() { parent=NULL; child1=NULL; child2=NULL; value=0; } void addValue(LL delta) { value=(value+delta)%M; if (value<M) value+=M; if (parent!=NULL) parent->addValue(delta); } void setValue(LL newValue) { LL delta=newValue-value; if (delta<0) delta+=M; this->addValue(delta); } LL getSum(LL p, LL k) { if (p<=pocz&&k>=kon) return value; if (p>kon||k<pocz) return 0; return (child1->getSum(p,k)+child2->getSum(p,k))%M; } }; Node* nodes[600000]; Node* root; Node* createTree(LL pocz, LL kon, Node* parent) { //cout << "Creating node: " << pocz << ".." << kon << "\n"; Node* node=new Node(); node->parent=parent; node->pocz=pocz; node->kon=kon; node->value=0; if (pocz==kon) nodes[pocz]=node; else { LL mySize=kon-pocz+1; LL half = mySize/2; node->child1 = createTree(pocz, pocz+half-1, node); node->child2 = createTree(pocz+half, kon, node); } return node; } LL antiSingletons(string &s) { LL n = s.size()-1; //s = '#'+s; LL last[256]; FOR(i,0,255) last[i]=-1; g[0]=1; // jeden singleton dlugosci 0 (?) subs[0]=1; LL sum=1; FOR(i,1,n) { char t = s[i]; //cout << "-- pos: " << i << " char: " << t << "\n"; subs[i]=(2*subs[i-1])%M; LL lastPos = last[t]; if (lastPos!=-1) { //cout << " lastPos=" << lastPos << " g[lastPos]=" << g[lastPos] << "\n"; //LL sum2=0; //FOR(j,lastPos,i-1) sum2+=g[j]; LL sum2=root->getSum(lastPos,i-1); sum2=sum2%M; sum -= g[lastPos]; // jesli byly jakies zakonczone na ostatnim t, to wypadaja if (sum<0) sum+=M; g[lastPos]=0; nodes[lastPos]->setValue(0); g[i]=sum2; nodes[i]->setValue(sum2); sum=(sum+g[i])%M; subs[i] -= subs[lastPos-1]; if (subs[i]<0) subs[i]+=M; } else { //cout << " first timer\n"; g[i] = sum; nodes[i]->setValue(sum); sum = (sum * 2)%M; } last[t]=i; //cout << " g[" << i << "]=" << g[i] << " sum=" << sum << "\n"; } LL pp=subs[n]-sum; if (pp<0) pp+=M; //cout << "Subs=" << subs[n] << "\n"; return pp; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); LL n; LL q; LL pos; char ch; cin >> n; cin >> q; LL ts=2; while (ts<n) ts*=2; //cout << "Tree size: " << ts << "\n"; cout.flush(); root=createTree(1,ts,NULL); string s; cin >> s; s = '#'+s; cout << antiSingletons(s) << "\n"; FOR(i,1,q) { cin >> pos; cin >> ch; s[pos]=ch; cout << antiSingletons(s) << "\n"; } return 0; }
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | /* ----------------------- Autor: Tomasz Boguslawski -------------------------- */ #include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<iomanip> #include<string> #include<sstream> #include<cstring> #include<map> #include<vector> #include<set> #include<queue> #include<algorithm> #include <fstream> #include<math.h> #define LL long long #define FOR(x, b, e) for(LL x = b; x <= (e); x++) #define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s) #define FORD(x, b, e) for(LL x = b; x >= (e); x--) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define DEBUG if (debug) #define MIN(a,b) ((a>b)?b:a) #define MAX(a,b) ((a>b)?a:b) using namespace std; const LL M=998244353; set<string> subwords; set<string> subwords2; void recurse(string &s, LL pos, string sub) { string z = s[pos]+sub; if (subwords.count(z)>0) subwords2.insert(z); else subwords.insert(z); if (pos!=0) { recurse(s, pos-1, z); recurse(s, pos-1, sub); } } LL brut(string s) { subwords.clear(); subwords2.clear(); recurse(s, s.size()-1, ""); FOREACH(p,subwords2) cout << *p << "\n"; cout << "Subwords: " << subwords.size() << "\n"; cout << "Subwords repeated: " << subwords2.size() << "\n"; cout << "Singletons: " << (subwords.size()-subwords2.size()) << "\n"; return subwords2.size(); } LL dp[1000]; LL last[1000]; LL calc(string s) { LL len=s.size(); FOR(i,0,999) last[i]=-1; FOR(i,0,len-1) { cout << "i=" << i << " s[i]=" << s[i] << " last[s[i]]=" << last[s[i]] << "\n"; if (i==0) dp[i]=2; else dp[i]=2*dp[i-1]; if (last[s[i]]>0) { dp[i] -= dp[last[s[i]]-1]; cout << " odejmuje: " << dp[last[s[i]]-1] << "\n"; } last[s[i]]=i; } return dp[len-1] - 1; } int countSub(string str) { // Create an array to store index // of last vector<int> last(256, -1); // Length of input string int n = str.length(); // dp[i] is going to store count of distinct // subsequences of length i. int dp[n + 1]; int db[n + 1]; // Empty substring has only one subsequence dp[0] = 1; db[0] = 0; // Traverse through all lengths from 1 to n. for (int i = 1; i <= n; i++) { // Number of subsequences with substring // str[0..i-1] dp[i] = 2 * dp[i - 1]; db[i] = 2 * db[i - 1]; cout << "character=" << str[i-1] << " last=" << last[str[i-1]] << "\n"; // If current character has appeared // before, then remove all subsequences // ending with previous occurrence. if (last[str[i - 1]] != -1) { dp[i] = dp[i] - dp[last[str[i - 1]]]; cout << "odjete: " << dp[last[str[i - 1]]] << "\n"; db[i] += dp[last[str[i - 1]]] - 2*db[last[str[i-1]]+1]; } // Mark occurrence of current character last[str[i - 1]] = (i - 1); cout << "dp[" << i << "]=" << dp[i] << "\n"; } //return dp[n]; return db[n]; } LL g[60000]; LL subs[60000]; class Node { public: LL pocz, kon; LL value; Node* parent; Node* child1; Node* child2; Node() { parent=NULL; child1=NULL; child2=NULL; value=0; } void addValue(LL delta) { value=(value+delta)%M; if (value<M) value+=M; if (parent!=NULL) parent->addValue(delta); } void setValue(LL newValue) { LL delta=newValue-value; if (delta<0) delta+=M; this->addValue(delta); } LL getSum(LL p, LL k) { if (p<=pocz&&k>=kon) return value; if (p>kon||k<pocz) return 0; return (child1->getSum(p,k)+child2->getSum(p,k))%M; } }; Node* nodes[600000]; Node* root; Node* createTree(LL pocz, LL kon, Node* parent) { //cout << "Creating node: " << pocz << ".." << kon << "\n"; Node* node=new Node(); node->parent=parent; node->pocz=pocz; node->kon=kon; node->value=0; if (pocz==kon) nodes[pocz]=node; else { LL mySize=kon-pocz+1; LL half = mySize/2; node->child1 = createTree(pocz, pocz+half-1, node); node->child2 = createTree(pocz+half, kon, node); } return node; } LL antiSingletons(string &s) { LL n = s.size()-1; //s = '#'+s; LL last[256]; FOR(i,0,255) last[i]=-1; g[0]=1; // jeden singleton dlugosci 0 (?) subs[0]=1; LL sum=1; FOR(i,1,n) { char t = s[i]; //cout << "-- pos: " << i << " char: " << t << "\n"; subs[i]=(2*subs[i-1])%M; LL lastPos = last[t]; if (lastPos!=-1) { //cout << " lastPos=" << lastPos << " g[lastPos]=" << g[lastPos] << "\n"; //LL sum2=0; //FOR(j,lastPos,i-1) sum2+=g[j]; LL sum2=root->getSum(lastPos,i-1); sum2=sum2%M; sum -= g[lastPos]; // jesli byly jakies zakonczone na ostatnim t, to wypadaja if (sum<0) sum+=M; g[lastPos]=0; nodes[lastPos]->setValue(0); g[i]=sum2; nodes[i]->setValue(sum2); sum=(sum+g[i])%M; subs[i] -= subs[lastPos-1]; if (subs[i]<0) subs[i]+=M; } else { //cout << " first timer\n"; g[i] = sum; nodes[i]->setValue(sum); sum = (sum * 2)%M; } last[t]=i; //cout << " g[" << i << "]=" << g[i] << " sum=" << sum << "\n"; } LL pp=subs[n]-sum; if (pp<0) pp+=M; //cout << "Subs=" << subs[n] << "\n"; return pp; } /// MAIN int main(int argc, char* argv[]) { // magic formula, which makes streams work faster: ios_base::sync_with_stdio(0); LL n; LL q; LL pos; char ch; cin >> n; cin >> q; LL ts=2; while (ts<n) ts*=2; //cout << "Tree size: " << ts << "\n"; cout.flush(); root=createTree(1,ts,NULL); string s; cin >> s; s = '#'+s; cout << antiSingletons(s) << "\n"; FOR(i,1,q) { cin >> pos; cin >> ch; s[pos]=ch; cout << antiSingletons(s) << "\n"; } return 0; } |