This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i=a; i<=b; i++)
#define FORR(i, a, b) for(int i=a; i>=b; i--)
#define pb push_back
#define ppb pop_back
#define lc 2*u
#define rc 2*u+1
#define mid ((l+r)/2)
#define F first
#define S second
#define debug(x) cerr<<"$ "<<#x<<" : "<<x<<"$\n"
#define wall() cerr<<"\n----------------------\n"
typedef long long ll;
typedef pair<int, int> pii;
const ll MX=505, M=5e5+5, LG=19, inf=1e9+5, MOD=1e9+7, A=27;
int m, n, tr[M][A], N, dp[MX][MX];
string s;
void add(string& t){
int v=0;
FOR(i, 0, (int)t.size()-1){
if(!tr[v][t[i]-'a']) tr[v][t[i]-'a'] = ++N;
v = tr[v][t[i]-'a'];
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
FOR(i, 1, n){
string t;
cin>>t;
add(t);
}
cin>>s;
int L=s.size();
FORR(i, L-1, 0) FOR(j, i, L-1){
dp[i][j]=inf;
for(int v=tr[0][s[i]-'a'], h=1; v!=0 and i+h<=L; v=tr[v][s[i+h]-'a'], h++){
dp[i][j]=min(dp[i][j], 1+dp[i+h][j]);
}
}
FOR(q, 1, m){
int l, r;
cin>>l>>r;
r--;
//l--; r--;
cout<<(dp[l][r]==inf?-1:dp[l][r])<<'\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |