A numeric string, , is beautiful if it can be split into a sequence of two or more positive integers, , satisfying the following conditions:
- for any (i.e., each element in the sequence is more than the previous element).
- No contains a leading zero. For example, we can split into the sequence , but it is not beautiful because and have leading zeroes.
- The contents of the sequence cannot be rearranged. For example, we can split into the sequence , but it is not beautiful because it breaks our first constraint (i.e., ).
Perform queries where each query consists of some integer string . For each query, print whether or not the string is beautiful on a new line. If it is beautiful, print YES x
, where is the first number of the increasing sequence. If there are multiple such values of , choose the smallest. Otherwise, print NO
.
Example:
Input: q = 7, arr = {"1234", "91011", "99100", "101103", "010203", "13", "1"}
Output:
YES 1 YES 9 YES 99 NO NO NO NO
Approach:
C++
#include <bits/stdc++.h>using namespace std;void separateNumbers(string s){string t, a;for (int l = 1; l <= s.size() / 2 && s != t; l++){a = t = s.substr(0, l);for (int i = 1; t.size() < s.size(); i++)t += to_string(stoll(a) + i);}cout << (s == t ? "YES " + a : "NO") << "\n";}int main(){int q = 7;vector<string> arr = {"1234","91011","99100","101103","010203","13","1"};for (int i = 0; i < q; i++){string s = arr[i];separateNumbers(s);}return 0;}
No comments:
Post a Comment