1845C - Strong Password - CodeForces Solution


binary search dp greedy strings

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <iostream>
#include <vector>
#include <cassert>
#include <map>


using namespace std;
using ll = long long;


bool solve() {
  string s;
  int m;
  string l,r;
  cin>>s>>m;
  cin>>l>>r;
  int n = s.length();
  map<char,vector<int>> ind;
  for (int i=0; i<n; i++) {
    ind[s[i]].push_back(i);
  }

  // from [l[i],r[i]], greedily pick c that is farthest
  // right. this minimizes the chance of completing
  // the subsequence. this is the best we can do
  // at any index i.
  
  int at = -1;
  for (int i=0; i<m; i++) {
    int to = -1;
    for (char c=l[i]; c<=r[i]; c++) {
      auto it = upper_bound(ind[c].begin(), ind[c].end(), at);
      if (it == ind[c].end()) return true;
      to = max(to, *it);
    }
    at = to;
  }
  return false;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

  int t;
  cin>>t;
  while (t--) {
    cout<<(solve() ? "YES" : "NO")<<"\n";
  }
  
  return 0;
}


Comments

Submit
0 Comments
More Questions

151C - Win or Freeze
1585A - Life of a Flower
1662A - Organizing SWERC
466C - Number of Ways
1146A - Love "A"
1618D - Array and Operations
1255A - Changing Volume
1710C - XOR Triangle
415C - Mashmokh and Numbers
8A - Train and Peter
591A - Wizards' Duel
1703G - Good Key Bad Key
1705A - Mark the Photographer
1707A - Doremy's IQ
1706B - Making Towers
1325B - CopyCopyCopyCopyCopy
1649C - Weird Sum
1324B - Yet Another Palindrome Problem
525A - Vitaliy and Pie
879A - Borya's Diagnosis
1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail