-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathseparate the number.cpp
85 lines (72 loc) · 1.29 KB
/
separate the number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdint>
#include <algorithm>
uint64_t ToInteger(const std::string& s)
{
std::istringstream istr(s);
uint64_t v;
istr >> v;
return v;
}
std::string ToString(const uint64_t& v)
{
std::ostringstream ostr;
ostr << v;
return ostr.str();
}
bool CanStartFrom(const std::string& s, const std::string& prefix)
{
if (prefix[0] == '0')
return false;
size_t sequenceLength = 1;
uint64_t v = ToInteger(prefix);
std::string t = prefix;
while (t.size() < s.size())
{
sequenceLength += 1;
v += 1;
t += ToString(v);
}
if (s != t)
return false;
if (sequenceLength < 2)
return false;
return true;
}
bool IsPossible(const std::string& s, uint64_t& startValue)
{
for (size_t prefixSize = 1; prefixSize <= 16; prefixSize++)
{
if (prefixSize > s.size())
break;
const std::string prefix = s.substr(0, prefixSize);
if (CanStartFrom(s, prefix))
{
startValue = ToInteger(prefix);
return true;
}
}
return false;
}
int main()
{
size_t t;
std::cin >> t;
for (size_t ti = 0; ti < t; ti++)
{
std::string s;
std::cin >> s;
uint64_t startValue;
if (IsPossible(s, startValue))
{
std::cout << "YES " << startValue << std::endl;
}
else
{
std::cout << "NO" << std::endl;
}
}
}