QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#845552 | #1942. Another Substring Query Problem | Shunpower | TL | 2426ms | 371912kb | C++20 | 3.2kb | 2025-01-06 17:06:13 | 2025-01-06 17:06:14 |
Judging History
answer
//Author:KIT / Shunpower
//Cloud Island & Rain Temperature
//May the force be with you and me.
#include <bits/stdc++.h>
#pragma GCC Optimize("Ofast")
#define ET return 0
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define ll long long
#define ull unsigned long long
#define inf INT_MAX
#define uinf INT_MIN
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr1(i,a,b) for(int i=a;i<=b;i++)
#define fr2(i,a,b) for(int i=a;i>=b;i--)
#define ld long double
#define il inline
//Quickly power: ll d=qpow(b,p>>1,k);
//Segment Tree: Memory Limit Excceed
//Segment Tree: Modify()->Pushdown()
//Mod: +M, %M, define int ll
//Mod: Don't use 998244353 instead of 1e9+7 and so on
//Don't solve a problem for too long time.
using namespace std;
const int N=2e5+10;
namespace Shun{
int lowbit(int x){
return x&-x;
}
template <typename T>
inline void read(T &x){
T s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-'){
w=-1;
}
ch=getchar();
}
while(ch>='0'&&ch<='9'){
s=s*10+ch-'0';
ch=getchar();
}
x=s*w;
}
template <typename T>
inline void write(T x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9){
write(x/10);
}
putchar(x%10+'0');
}
}
using namespace Shun;
string s;
int n,q;
const ll base=103,base2=107;
const int lim=450;
const ll mod=1e9+7,mod2=998244353;
// const ll mod2=20000003;
ll powb[N],powb2[N],hsh[N],hsh2[N];
ll gethsh(int l,int r){
return (hsh[r]+mod-hsh[l-1]*powb[r-l+1]%mod)%mod;
}
ll gethsh2(int l,int r){
return (hsh2[r]+mod2-hsh2[l-1]*powb2[r-l+1]%mod2)%mod2;
}
string tt[N];
int kk[N];
ll hht[N],hht2[N];
map <pii,vector<int>> p;
map <pii,int> ext;
int main(){
#ifdef Ltp
freopen("hack.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin>>s;
s='@'+s,n=s.length()-1;
powb[0]=1,powb2[0]=1;
fr1(i,1,n) powb[i]=powb[i-1]*base%mod;
fr1(i,1,n) powb2[i]=powb2[i-1]*base2%mod2;
fr1(i,1,n) hsh[i]=(hsh[i-1]*base%mod+s[i])%mod;
fr1(i,1,n) hsh2[i]=(hsh2[i-1]*base2%mod2+s[i])%mod2;
cin>>q;
// if(q!=4&&q!=631){
// cout<<clock()<<'\n';
// ET;
// }
fr1(i,1,q){
cin>>tt[i]>>kk[i];
fr1(j,0,tt[i].length()-1) hht[i]=(hht[i]*base%mod+tt[i][j])%mod;
fr1(j,0,tt[i].length()-1) hht2[i]=(hht2[i]*base2%mod2+tt[i][j])%mod2;
ext[{hht[i],hht2[i]}]=1;
}
fr1(i,1,lim){
fr1(j,1,n-i+1){
ll h=gethsh(j,j+i-1),h2=gethsh2(j,j+i-1);
// cout<<j<<" "<<i<<" "<<h<<","<<h2<<endl;
if(ext.count({h,h2})) p[{h,h2}].pb(j);
}
}
fr1(i,1,q){
string t;
int k,m;
t=tt[i],k=kk[i];
t='@'+t,m=t.length()-1;
ll ht=hht[i],ht2=hht2[i];
// cout<<ht<<" "<<ht2<<endl;
if(t.length()<=lim){
// cout<<"!"<<p[ht].size()<<endl;
if(!p.count({ht,ht2})) cout<<"-1\n";
else if(p[{ht,ht2}].size()<k) cout<<"-1\n";
else cout<<p[{ht,ht2}][k-1]<<'\n';
}
else{//n^2/lim
fr1(i,1,n-m+1){
if(gethsh(i,i+m-1)==ht&&gethsh2(i,i+m-1)==ht2){
k--;
if(!k){
cout<<i<<'\n';
goto label;
}
}
}
cout<<"-1\n";
label:
continue;
}
}
ET;
}
//ALL FOR Zhang Junhao.
//祝你 Happy Happy Happy New Year
//唱着歌 再对着镜头比个耶
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2426ms
memory: 371912kb
input:
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh...
output:
50997 112451 97534 13355 150562 5772 146250 10904 4343 194777 71395 96501 6809 18571 166901 114789 123587 176339 29660 82650 63509 80101 47907 66143 199971 108032 140518 49221 46212 28127 61461 149025 142818 33646 17132 196161 36671 28656 16860 141951 139429 72866 162807 21343 194803 16263 118701 20...
result:
ok 631 lines
Test #2:
score: -100
Time Limit Exceeded
input:
gnzlzbsarybaxilacuopvdkhjfiynrgfdhsrecyxedcrriztcxboczhfaseqglcwshaewaepasxqpaxxbroxndbxqabrvtrrshrnnlyfodtxyltjkbghfkjcqrqsuwdsgypcaroaojqbfrjqbdrimglxcqtfmqaeogqunaegfpfzcmyjwfgbwaigwitcyspiedbisjuedeyseneuzjdteifzvdfzhenapmmreqpvwsmtrxowjnmmrthzhojgxsoppfgfgohoghuwvqclzsmeydusfndrdyqokpqwupixyoxr...