1686D - Linguistics - CodeForces Solution


constructive algorithms greedy sortings strings two pointers *2000

Please click on ads to support us..

C++ Code:

#include <string>
#include <iomanip>
#include <iostream>
#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <numeric>
#include <cstring>
#include <complex>
#include <cassert>
#include <functional>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef vector<pii> vii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef complex<double> cd;
 
#define x first
#define y second
#define mp make_pair 
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(), v.end()

const int INF = 1e9 + 1;
const ll INF_LL = (ll)1e18;
// const ll MOD = 1e9 + 7;
const int MOD = 998244353;
const int ALPHA = 26;
const int MAXN = 2e5 + 200;
const int LOG = 18;
const int SIEVE = (int)1e6 + 10;
const ld EPS = 1e-9;
const ld SCALE = 1e+6;

void solve() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    string s; cin >> s;
    int n = (int) s.size();
    int as = a + c + d;
    int cnt = 0;
    for (char ch : s)
        cnt += (ch == 'A');

    if (as != cnt) {
        cout << "NO" << '\n';
        return;
    }

    int any = 0;
    int total = 0;
    vi cs, ds;
    int beg = 0;
    while (beg < n) {
        int end = beg;
        while (end + 1 < n && s[end + 1] != s[end]) {
            end += 1;
        }
        int cc = end - beg;
        if (cc > 0) {
            total += cc / 2;
            if (cc % 2 == 0) {
                any += cc / 2;
            } else {
                if (s[beg] == 'A') {
                    cs.push_back(cc / 2);
                } else {
                    ds.push_back(cc / 2);
                }
            }
        }
        beg = end + 1;
    }

    sort(all(cs)), sort(all(ds));
    int rem_c = c;
    int rem_d = d;
    for (int x : cs) {
        if (rem_c >= x + 1) {
            rem_c -= x + 1;
            total += 1;
        }
    }
    for (int x : ds) {
        if (rem_d >= x + 1) {
            rem_d -= x + 1;
            total += 1;
        }
    }
    cout << (total >= c + d ? "YES" : "NO") << '\n';
}    

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

    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}  


Comments

Submit
0 Comments
More Questions

150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle