USACO · 1540

COW Splits

This C++ solution uses string processing for USACO 1540 COW Splits. Read the reasoning, inspect the code, or try your own test case below.

1540StringsString processingC++61 lines
Solution065of 248
Open official problem ↗ Download C++ file ↓ Search the library → Open full Code Lab ↗ Report an issue ↗

Approach

String processing

COW Splits has strings made from COW rotations and asks to label square-subsequence deletion operations; the code implements the one/two-operation construction and odd-N impossibility.

Strings

Problem and code

Useful links.

Written by benbenyaojifen. Try the problem first, then compare your approach with the code.

Open official problem ↗View exact source file ↗
Implementation

Problem_2_COW_Splits.cpp

C++

    #include <bits/stdc++.h>
    using namespace std;
    void solve() {
        int n; string s; cin >> n >> s;
        //notice that if it is possilbe we can always do it in 1 or 2 steps
        if (n & 1) {
            cout << -1 << '\n';
            return;
        }
        // partition in half
        if (s.substr(0, 3 * n / 2) == s.substr(3 * n / 2)) {
            cout << 1 << '\n';
            for (int i = 0; i < 3 * n; i++) {
                cout << 1 << " \n"[i == 3 * n - 1];
            }
            return;
        }
        vector<int> ans(3 * n, 2);
        for (int i = 0; i < n / 2; i++) {
            int l = 3 * i, r = 3 * n / 2 + 3 * i;
            string a = s.substr(l, 3), b = s.substr(r, 3);
            if (a == b) {
                for (int j = 0; j < 3; j++) {
                    ans[l + j] = 1;
                    ans[r + j] = 1;
                }
            } else {
                bool done = false;
                for (int j = 0; j < 3 && !done; j++) {
                    for (int k = j + 1; k < 3 && !done; k++) {
                        string t = "";
                        t += a[j]; t += a[k];
                        int p = 0;
                        for (char c : b) {
                            if (p < 2 && c == t[p]) p++;
                        }
                        if (p == 2) {
                            ans[l + j] = ans[l + k] = 1;
                            for (int x = 0; x < 3; x++) {
                                if (b[x] == t[0] || b[x] == t[1]) {
                                    ans[r + x] = 1;
                                }
                            }
                            done = 1;
                        }
                    }
                }
            }
        }
        cout << 2 << '\n';
        for (int i = 0; i < ans.size(); i++) {
            cout << ans[i] <<  " \n"[i == ans.size() - 1];
        }
    }
    int main() {
        ios::sync_with_stdio(0); cin.tie(0);
        int t, k; cin >> t >> k;
        while (t--) {
            solve();
        }
    }
        

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗Keep studying →

Test this problem

Run your code here.

Paste your code, run a test case, compare the output, or trace selected values.

Full trace, comparison & stress testing ↗
StatusReady
Output
No run yet.
Diagnostics
No diagnostics yet.

Each run is isolated and has strict limits. Passing one test does not guarantee the judge will accept the solution.