-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path320A - Magic Numbers.cpp
61 lines (42 loc) · 1.1 KB
/
320A - Magic Numbers.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
/*
A magic number is a number formed by concatenation of numbers 1, 14 and 144. We can use each of these numbers any number of times. Therefore 14144, 141414 and 1411 are magic numbers but 1444, 514 and 414 are not.
You're given a number. Determine if it is a magic number or not.
Input
The first line of input contains an integer n, (1?=?n?=?109). This number doesn't contain leading zeros.
Output
Print "YES" if n is a magic number or print "NO" if it's not.
Examples
inputCopy
114114
outputCopy
YES
inputCopy
1111
outputCopy
YES
inputCopy
441231
outputCopy
NO
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
int n = s.length();
string tmp = "";
bool res = false;
for (int i = 0; i < n; i++) {
res = false;
tmp += s[i];
if (tmp.length() >= 3 && s.substr(i -2, 3) == "144") res = true;
if (tmp.length() >= 2 && s.substr(i - 1, 2) == "14") res = true;
if (tmp.length() >= 1 && s.substr(i, 1) == "1") res = true;
if (!res) break;
}
res ? cout << "YES\n" : cout << "NO\n";
return 0;
}