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>
#define fast_cin ios_base::sync_with_stdio(0); cin.tie(NULL); cerr.tie(NULL);
#define endl "\n"
#define dbg(x) cerr << #x << ": " << x << endl;
#define DBG cerr << __LINE__ << ": I GOT STUCK\n";
using namespace std;
typedef long long ll;
typedef long double db;
const ll mxn = 1e5 + 10;
string s;
ll n, q;
pair<ll, ll>Q[mxn];
ll p[mxn];
bool good(ll x){
string cur = s;
for(ll i = 1;i<=x;i++){
// cover till the current state
cur[p[i]] = '*';
}
ll pre[n+5][26]{};
// to calculate how many letters are present
// pre calculate for array
for(ll i = 1;i<=n;i++){
for(ll j = 0;j<26;j++)
pre[i][j] = pre[i-1][j];
if(cur[i] == '*')continue;// if covered;
pre[i][cur[i] - 'a']++;// counting occurence
}
for(ll i = 1;i<=q;i++){
// check the count of occurence of each letter in each segment
for(ll j = 0;j<26;j++){
ll l = Q[i].first;
ll r = Q[i].second;
ll count = pre[r][j] - pre[l-1][j];
if(count > 1)return false;
}
}
return true;
}
void solve(){
cin >> s;
n = (ll)s.size();
s = "_" + s;
cin >> q;
for(ll i = 1;i<=q;i++){
ll a, b;cin >> a >> b;
Q[i] = {a, b};
}
for(ll i = 1;i<=n;i++)cin >> p[i];
ll l = 0, r= n;
ll ans = 0;
while(l<=r){
ll mid = l + (r - l)/2;
if(good(mid)){
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
cout << ans << endl;
}
// aaaaa
// 2
// 1 2
// 4 5
// 2 4 1 5 3
// abbabaab
// 3
// 1 3
// 4 7
// 3 5
// 6 3 5 1 4 2 7 8
int main() {
fast_cin;
int test;test = 1;
// int test;cin >>test;
while(test--){
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |