B. Beautiful String
Problem Statement
You are given a binary string of length .
Your task is to find any subsequence of such that:
- The subsequence is non-decreasing. That is, each character in is not greater than the next one.
- Let denote the string obtained by removing all characters of from , while preserving the order of the remaining characters. Then must be a palindrome.
You only need to output any valid subsequence that satisfies both conditions. If no such subsequence exists, output -1.
Note that an empty string is both non-decreasing and a palindrome.
A binary string is a string that consists of characters ‘0’ and ‘1’. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. A palindrome is a string that reads the same forwards and backward.
Solution
The problem asks us to find a non-decreasing subsequence from a binary string , such that the remaining string is a palindrome.
A non-decreasing subsequence of a binary string can only be composed of all ‘0’s, all ‘1’s, or some ‘0’s followed by some ‘1’s.
Let’s consider the simplest cases for the subsequence :
- consists of all ‘0’s in . The remaining string would consist of all ‘1’s from . A string of all ‘1’s is always a palindrome.
- consists of all ‘1’s in . The remaining string would consist of all ‘0’s from . A string of all ‘0’s is always a palindrome.
Both of these choices for satisfy the conditions. The subsequence of all ‘0’s is non-decreasing, and the remaining string of all ‘1’s is a palindrome. Similarly, the subsequence of all ‘1’s is non-decreasing, and the remaining string of all ‘0’s is a palindrome.
Therefore, we can simply choose to remove all ‘1’s as our subsequence . The remaining characters (all ‘0’s) will form a palindrome. The code implements this by collecting the indices of all ‘1’s to form the subsequence . Since the problem guarantees a solution always exists, this approach is valid.
The provided code iterates through the string , and if it finds a character ‘1’, it adds its 1-based index to a vector. This vector of indices represents the subsequence consisting of all ‘1’s. Finally, it prints the size of this subsequence and the indices themselves.
View Code
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <set>
#include <tuple>
#include <map>
#include <vector>
using namespace std;
#define int long long
// #define mod 1000000007
#define N 500005
void solve() {
int n;
cin >> n;
string s;
cin >> s;
vector<int> ans;
for (int i = 0; i < n; i++) {
if(s[i] == '1') {
ans.push_back(i + 1);
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cout << endl;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef DEBUG
freopen("./input.txt", "r", stdin);
#endif
int t;
cin >> t;
while (t--) {
#ifdef DEBUG
static int test_num = 1;
cout << "test case: " << test_num++ << endl;
#endif
solve();
}
}