- Prepare
- Algorithms
- Strings
- Separate the Numbers
- Discussions
Separate the Numbers
Separate the Numbers
+ 54 comments I have no faiths in Easy problems anymore :/
+ 55 comments Another Java solution, building up a test sequence string for each possible starting number and then comparing to the original.
public static void main(String[] args) { Scanner in = new Scanner(System.in); int q = in.nextInt(); for(int a0 = 0; a0 < q; a0++) { String s = in.next(); boolean valid = false; long firstx = -1; // Try each possible starting number for (int i=1; i<=s.length()/2; ++i) { long x = Long.parseLong(s.substring(0,i)); firstx = x; // Build up sequence starting with this number String test = Long.toString(x); while (test.length() < s.length()) { test += Long.toString(++x); } // Compare to original if (test.equals(s)) { valid = true; break; } } System.out.println(valid ? "YES " + firstx : "NO"); } }
+ 11 comments This challenge looks difficult (medium difficulty, as many pointed out) unless you know the right way to handle it, then it becomes almost trivial. I'm sharing this in hopes that you'll not waste hours of your time (like I did) on a 15-minutes problem.
Instead of performing laborious operations on each input string s, you should extract from its beginning a number a of length l (1 ≤ l ≤ s.size()/2), and by concatenating (appending) its subsequent numbers you build a new string t from scratch. Once it's done, compare t to s: if there's a match, print the result and move on to the next string, otherwise keep comparing strings built from numbers of different l until you either find a solution or finish checking all possible cases for s.
My C++ solution:
#include <bits/stdc++.h> using namespace std; int main(){ string s, t, a; cin.ignore(256, '\n'); while(cin >> s){ 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") << endl; } return 0; }
+ 5 comments c'mon guys, it is really easy! I will not post my solution, I will only hint, that instead of trying to violate in all dirty ways the given string, try to create a hypothetical beautiful string from the ground up since you already know the first symbol (= the first symbol of given string).
I mean, if the given string is "99100", start with "9" and create a hypothetical beautiful string like this "91011". It is not equal to "99100". Then take 2 digits "99" and try to create another hypothetical beautiful string - "99100" - wow! - this time the given string and our hypothetical beautiful string are equal! I hope you get the idea.
+ 2 comments This one is easy . Really ?
Sort 580 Discussions, By:
Please Login in order to post a comment