点击跳转
这里可以学习用多项式卷积匹配的思想,每种字母分开来看
大写字母单独搞
每种小写字母,如果这个位置有这个小写字母,就是1,否则就是0
然后用哈希判断一下是否相同
字符集大小假设是 Ω \Omega Ω
那么时间复杂度就是 O ( Ω ( n + m ) ) O(\Omega(n+m)) O(Ω(n+m))
#include
#include
#include
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
struct Easymath
{
ll prime[maxn], phi[maxn], mu[maxn];
bool mark[maxn];
ll fastpow(ll a, ll b, ll c)
{
ll t(a%c), ans(1ll);
for(;b;b>>=1,t=t*t%c)if(b&1)ans=ans*t%c;
return ans;
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
if(!b){x=1,y=0;return;}
ll xx, yy;
exgcd(b,a%b,xx,yy);
x=yy, y=xx-a/b*yy;
}
ll inv(ll x, ll p) //p是素数
{return fastpow(x%p,p-2,p);}
ll inv2(ll a, ll p)
{
ll x, y;
exgcd(a,p,x,y);
return (x+p)%p;
}
void shai(ll N)
{
ll i, j;
for(i=2;i<=N;i++)mark[i]=false;
*prime=0;
phi[1]=mu[1]=1;
for(i=2;i<=N;i++)
{
if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
for(j=1;j<=*prime and i*prime[j]<=N;j++)
{
mark[i*prime[j]]=true;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
mu[i*prime[j]]=-mu[i];
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
}em;
#define mod 998244353ll
#define Base 4894651ll
ll inv[maxn], PB[maxn];
struct Hash
{
ll M, base; //m should be a prime number, and base should be less than m.
vector<int> h;
Hash(ll M, ll base)
{
this->M=M;
this->base=base;
}
void build(char* s, ll len)
{
ll i;
h.resize(len+1);
rep(i,1,len)
{
if(s[i])h[i]=(h[i-1]+s[i]*PB[i-1])%M;
else h[i]=h[i-1];
}
}
ll substr(ll l, ll r) //返回非负哈希值
{
return (((ll)h[r]-h[l-1])*inv[l-1]%M+M)%M;
}
};
ll n;
char s[maxn], t[maxn];
char ss[27][maxn], tt[27][maxn];
int main()
{
ll i;
inv[0]=1;
inv[1]=em.inv(Base,mod);
rep(i,2,maxn-1)inv[i]=inv[i-1]*inv[1]%mod;
PB[0]=1;
rep(i,1,maxn-1)PB[i]=PB[i-1]*Base%mod;
ll T=read();
while(T--)
{
scanf("%s%s",s+1,t+1);
ll n=strlen(s+1), m=strlen(t+1), i, j, ans=0;
cl(ss), cl(tt);
rep(i,1,n)if(s[i]>'z' or s[i]<'a')ss[0][i]=s[i];
rep(j,1,26)rep(i,1,n)ss[j][i]=(s[i]=='a'+j-1);
rep(i,1,n)if(t[i]>'z' or t[i]<'a')tt[0][i]=t[i];
rep(j,1,26)rep(i,1,n)tt[j][i]=(t[i]=='a'+j-1);
Hash *hs[27], *ht[27];
rep(i,0,26)
{
hs[i] = new Hash(998244353,4894651);
ht[i] = new Hash(998244353,4894651);
hs[i]->build(ss[i],n);
ht[i]->build(tt[i],m);
}
ll f[27];
memset(f,-1,sizeof(f));
drep(i,m,1)
{
if('a'<=t[i] and t[i]<='z')f[t[i]-'a'+1]=i;
}
rep(i,1,n-m+1)
{
bool ok = true;
if(hs[0]->substr(i,i+m-1)!=ht[0]->substr(1,m))ok=false;
rep(j,1,26)
{
if(!ok)break;
if(f[j]==-1)continue;
ll c=s[i+f[j]-1]-'a'+1;
if(hs[c]->substr(i,i+m-1)!=ht[j]->substr(1,m))ok=false;
}
ans+=ok;
}
printf("%lld\n",ans);
rep(i,0,26)
{
delete hs[i];
delete ht[i];
}
}
return 0;
}