結果

問題 No.2761 Substitute and Search
ユーザー umimel
提出日時 2024-05-17 22:32:45
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3,717 ms / 4,000 ms
コード長 3,359 bytes
コンパイル時間 2,154 ms
コンパイル使用メモリ 185,560 KB
実行使用メモリ 53,504 KB
最終ジャッジ日時 2024-12-20 14:33:01
合計ジャッジ時間 20,341 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[2] = {999727999, 1070777777};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 1;
long long pow_mod(long long x, long long n, long long mod){
long long ret = 1;
while(n > 0){
if(n & 1) ret = (ret*x)%mod;
x = x*x%mod;
n >>= 1;
}
return ret;
}
int l;
//1-indexed
ll sum(vector<ll> &bit, ll i, ll mod){
ll s = 0;
while(i > 0){
s += bit[i];
s %= mod;
i -= i & -i;
}
return s;
}
void add(vector<ll> &bit, ll i, ll x, ll mod){
while(i <= l){
bit[i] += x;
bit[i] %= mod;
i += i & -i;
}
}
void solve(){
int n, q; cin >> n >> l >> q;
vector<vector<char>> s(n, vector<char>(l+1));
for(int i=0; i<n; i++) for(int j=1; j<=l; j++) cin >> s[i][j];
vector<ll> B(2);
for(ll i=0; i<2; i++) B[i] = rng()%MOD[i];
vector<vector<ll>> pow(2, vector<ll>(l+1, 1LL));
vector<vector<ll>> invpow(2, vector<ll>(l+1, 1LL));
for(ll i=0; i<2; i++){
for(ll j=1; j<=l; j++){
pow[i][j] = pow[i][j-1]*B[i]%MOD[i];
invpow[i][j] = pow_mod(pow[i][j], MOD[i]-2, MOD[i]);
}
}
vector<vector<ll>> R(2, vector<ll>(26, 0LL));
for(ll i=0; i<2; i++){
for(ll j=0; j<26; j++) R[i][j] = rng()%MOD[i];
}
//
vector<vector<vector<ll>>> bit(n, vector<vector<ll>>(2, vector<ll>(l+1, 0LL)));
for(int i=0; i<n; i++){
for(int j=0; j<2; j++){
for(int k=1; k<=l; k++){
add(bit[i][j], k, R[j][s[i][k]-'a']*pow[j][k-1]%MOD[j], MOD[j]);
}
}
}
while(q--){
int type; cin >> type;
if(type == 1){
int k; cin >> k;
char c, d; cin >> c >> d;
for(int i=0; i<n; i++){
if(s[i][k] == c){
for(int j=0; j<2; j++){
ll pre = R[j][c-'a']*pow[j][k-1]%MOD[j];
ll post = R[j][d-'a']*pow[j][k-1]%MOD[j];
add(bit[i][j], k, (-pre+post+MOD[j])%MOD[j], MOD[j]);
}
s[i][k] = d;
}
}
}
if(type == 2){
string t; cin >> t;
int siz = (int)t.size();
vector<ll> hash(2, 0LL);
for(int j=0; j<2; j++){
for(int k=0; k<siz; k++){
hash[j] = (hash[j] + R[j][t[k]-'a']*pow[j][k]%MOD[j])%MOD[j];
}
}
int ans = 0;
for(int i=0; i<n; i++){
bool flg = true;
for(int j=0; j<2; j++){
if(sum(bit[i][j], siz, MOD[j]) != hash[j]) flg = false;
}
ans += flg;
}
cout << ans << '\n';
}
}
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int T=1;
//cin >> T;
while(T--) solve();
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0