Separate the Numbers

A numeric string, , is beautiful if it can be split into a sequence of two or more positive integers, , satisfying the following conditions:

  1.  for any  (i.e., each element in the sequence is  more than the previous element).
  2. No  contains a leading zero. For example, we can split  into the sequence , but it is not beautiful because  and  have leading zeroes.
  3. 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 ta;

    for (int l = 1l <= s.size() / 2 && s != tl++)
    {
        a = t = s.substr(0l);
        for (int i = 1t.size() < s.size(); i++)
            t += to_string(stoll(a) + i);
    }
    cout << (s == t ? "YES " + a : "NO"<< "\n";
}

int main()
{
    int q = 7;
    vector<stringarr = {"1234",
                          "91011",
                          "99100",
                          "101103",
                          "010203",
                          "13",
                          "1"};

    for (int i = 0i < qi++)
    {
        string s = arr[i];
        separateNumbers(s);
    }

    return 0;
}


No comments:

Post a Comment