結果

問題 No.2626 Similar But Different Name
ユーザー Nzt3
提出日時 2024-02-09 23:44:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,568 bytes
コンパイル時間 2,151 ms
コンパイル使用メモリ 196,580 KB
最終ジャッジ日時 2025-02-19 04:17:00
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 TLE * 1 -- * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;

constexpr array<ll,2> primes={1029268439,1236788731};
constexpr int base=340522599;
constexpr int MAX_N=500000;

void ch_hash_add(array<ll,2>& a,char c){
  a[0]=(a[0]*base+c)%primes[0];
  a[1]=(a[1]*base+c)%primes[1];
}
void ch_hash_rem(array<ll,2>& a,char c,const array<ll,2>& inv){
  a[0]=((a[0]-inv[0]*c)%primes[0]+primes[0])%primes[0];
  a[1]=((a[1]-inv[1]*c)%primes[1]+primes[1])%primes[1];
}
namespace Lib{
  ll modpow(ll a,int n,ll mod){
    long long ret=1,t=a;
    while(n>0){
      if(n&1)ret=ret*t%mod;
      t=t*t%mod;
      n/=2;
    }
    return ret;
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N,M,K;
  cin>>N>>M>>K;
  string S,T;
  cin>>S>>T;
  array<ll,2>T_hash={0,0},inv={Lib::modpow(base,M,primes[0]),Lib::modpow(base,M,primes[1])};
  for(int i=0;i<M;i++){
    char c=T[i];
    if(c>='a'){
      c^=32;
    }
    ch_hash_add(T_hash,c);
  }
  array<ll,2>S_hash={0,0};
  for(int i=0;i<M;i++){
    char c=S[i];
    if(c>='a'){
      c^=32;
    }
    ch_hash_add(S_hash,c);
  }
  S+='/';
  bitset<MAX_N>Sb,Tb,Ab;
  for(int i=0;i<N;i++){
    if(S[i]>='a')Sb[i]=1;
  }
  for(int i=0;i<M;i++){
    if(T[i]>='a')Tb[i]=1;
    Ab[i]=1;
  }
  int ans=0;
  for(int i=M;i<=N;i++){
    if(T_hash==S_hash){
      int s=(((Sb>>(i-M))&Ab)^Tb).count();
      if(s>0&&s<=K)ans+=1;
    }
    char c=S[i];
    if(c>='a'){
      c^=32;
    }
    ch_hash_add(S_hash,c);
    c=S[i-M];
    if(c>='a'){
      c^=32;
    }
    ch_hash_rem(S_hash,c,inv);
  }
  cout<<ans<<'\n';
}
0