#include #include #include using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 4000000000000000001 unsigned long xor128() { static unsigned long x=123456789, y=362436069, z=521288629, w=88675123; unsigned long t=(x^(x<<11)); x=y; y=z; z=w; return (w=(w^(w>>19))^(t^(t>>8))); } struct rolling_hash{ long long t_hash; static inline vector power; static const long long MOD = (1LL<<61)-1; static const long long b = 123456; int sz; rolling_hash(){ sz = 0; t_hash = 0; } rolling_hash(char c){ sz = 1; t_hash = b*c; } long long mul(__int128 x,__int128 y){ __int128 t = x*y; t = (t>>61) + (t&MOD); if(t>=MOD)t -= MOD; return t; } long long get_pow(int sz){ if(power.size()>sz)return power[sz]; while(power.size()<=sz){ if(power.size()==0)power.push_back(1); else power.push_back(mul(power.back(),b)); } return power.back(); } rolling_hash &operator+=(const rolling_hash &another){ (*this).t_hash = mul((*this).t_hash,get_pow(another.sz)); (*this).t_hash += another.t_hash; if((*this).t_hash>=MOD)(*this).t_hash -= MOD; (*this).sz += another.sz; return (*this); } rolling_hash operator+(const rolling_hash &another)const{ return (rolling_hash(*this)+=another); } rolling_hash &operator-=(const rolling_hash &another){ (*this).t_hash += MOD - mul(another.t_hash,get_pow((*this).sz-another.sz)); if((*this).t_hash>=MOD)(*this).t_hash -= MOD; (*this).sz -= another.sz; return (*this); } rolling_hash operator-(const rolling_hash &another)const{ return (rolling_hash(*this)-=another); } bool operator<(const rolling_hash &another)const{ if((*this).t_hash!=another.t_hash)return (*this).t_hash h(26); mint get(char c){ if(c=='?')return 0; return h[c-'a']; } void solve(){ string s,t; cin>>s>>t; reverse(t.begin(),t.end()); vector f(s.size()+t.size()+1); { vector x(s.size()),y(t.size()); rep(i,x.size()){ mint v = get(s[i]); x[i] = v*v*v; } rep(i,y.size()){ mint v = get(t[i]); y[i]= v; } x = convolution(x,y); rep(i,x.size())f[i] += x[i]; } { vector x(s.size()),y(t.size()); rep(i,x.size()){ mint v = get(s[i]); x[i] = v; } rep(i,y.size()){ mint v = get(t[i]); y[i]= v*v*v; } x = convolution(x,y); rep(i,x.size())f[i] += x[i]; } { vector x(s.size()),y(t.size()); rep(i,x.size()){ mint v = get(s[i]); x[i] = -2*v*v; } rep(i,y.size()){ mint v = get(t[i]); y[i]= v*v; } x = convolution(x,y); rep(i,x.size())f[i] += x[i]; } reverse(t.begin(),t.end()); string ss = s; rep(i,s.size()){ if(ss[i]=='?')ss[i] = 'a'; } vector sR(s.size()+1); rep(i,ss.size()){ sR[i+1] = sR[i] + rolling_hash(ss[i]); } vector tR(t.size()+1); rep(i,t.size()){ tR[i+1] = tR[i] + rolling_hash(t[i]); } int ans = -1; rep(i,s.size() - t.size() + 1){ if(f[i+t.size()-1]!=0)continue; if(ans==-1){ ans = i; continue; } int ok = 0,ng = ss.size()+1; while(ng-ok>1){ int mid = (ok+ng)/2; rolling_hash X,Y; { int rem = mid; X += sR[min(ans,rem)]; rem -= min(ans,rem); X += tR[min((int)t.size(),rem)]; rem -= min((int)t.size(),rem); if(rem>0){ X += sR[rem + t.size() + ans] - sR[t.size() + ans]; } } { int rem = mid; Y += sR[min(i,rem)]; rem -= min(i,rem); Y += tR[min((int)t.size(),rem)]; rem -= min((int)t.size(),rem); if(rem>0){ Y += sR[rem + t.size() + i] - sR[t.size() + i]; } } if(X==Y)ok = mid; else ng = mid; } if(ok==ss.size())continue; char x,y; { int rem = ok; if(remy)ans = i; } if(ans==-1){ cout<<-1<>_t; rep(_,_t){ int n,m; cin>>n>>m; solve(); } return 0; }