output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> template <class T> inline T mod(T n, T m) { return (n % m + m) % m; } template <class T> inline void checkmin(T &a, const T b) { if (b < a) a = b; } template <class T> inline void checkmax(T &a, const T b) { if (b > a) a = b; } template <class T, class C> inline void checkmin(T &a, const T b, C c) { if (c(b, a)) a = b; } template <class T, class C> inline void checkmax(T &a, const T b, C c) { if (c(a, b)) a = b; } template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } template <class T> inline T gcd(T a, T b) { if (a < 0) return gcd(-a, b); if (b < 0) return gcd(a, -b); return (b == 0) ? a : gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { if (a < 0) return lcm(-a, b); if (b < 0) return lcm(a, -b); return a * (b / gcd(a, b)); } template <class T> T Abs(T x) { return x > 0 ? x : -x; } int bigmod(long long B, long long P, long long M) { long long R = 1; while (P > 0) { if (P % 2 == 1) { R = (R * B) % M; } P /= 2; B = (B * B) % M; } return (int)R; } long long mulmod(long long a, long long b, long long c) { long long x = 0, y = a % c; while (b > 0) { if (b % 2 == 1) { x = (x + y) % c; } y = (y * 2) % c; b /= 2; } return x % c; } template <class T> inline T pow(T a, long long b) { T c(1); while (b) { if (b & 1) c *= a; a *= a, b >>= 1; } return c; } template <class T> inline bool scan(T &ret) { char c; int sgn; T bit = 0.1; if (c = getchar(), c == EOF) return 0; while (c != '-' && c != '.' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); if (c == ' ' || c == '\n') { ret *= sgn; return 1; } while (c = getchar(), c >= '0' && c <= '9') ret += (c - '0') * bit, bit /= 10; ret *= sgn; return 1; } template <class T> inline bool isprime(T n) { if (n <= 1) return false; for (T i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } bool isvowel(char ch) { ch = tolower(ch); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return true; return false; } bool isupper(char c) { return c >= 'A' && c <= 'Z'; } bool islower(char c) { return c >= 'a' && c <= 'z'; } bool isletter(char c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); } bool isdigit(char c) { return c >= '0' && c <= '9'; } int main() { std::string s; std::cin >> s; for (int i = 0; i < (int)s.size(); i++) { if (s[i] != '4' && s[i] != '1') { puts("NO"); return 0; } if (s[i] == '4' && s[i - 1] == '4' && s[i + 1] == '4') { puts("NO"); return 0; } if (s[i] == '4' && s[i - 1] != '1' && s[i - 2] != '1') { puts("NO"); return 0; } } puts("YES"); return 0; }
### Prompt Please formulate a cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> template <class T> inline T mod(T n, T m) { return (n % m + m) % m; } template <class T> inline void checkmin(T &a, const T b) { if (b < a) a = b; } template <class T> inline void checkmax(T &a, const T b) { if (b > a) a = b; } template <class T, class C> inline void checkmin(T &a, const T b, C c) { if (c(b, a)) a = b; } template <class T, class C> inline void checkmax(T &a, const T b, C c) { if (c(a, b)) a = b; } template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } template <class T> inline T gcd(T a, T b) { if (a < 0) return gcd(-a, b); if (b < 0) return gcd(a, -b); return (b == 0) ? a : gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { if (a < 0) return lcm(-a, b); if (b < 0) return lcm(a, -b); return a * (b / gcd(a, b)); } template <class T> T Abs(T x) { return x > 0 ? x : -x; } int bigmod(long long B, long long P, long long M) { long long R = 1; while (P > 0) { if (P % 2 == 1) { R = (R * B) % M; } P /= 2; B = (B * B) % M; } return (int)R; } long long mulmod(long long a, long long b, long long c) { long long x = 0, y = a % c; while (b > 0) { if (b % 2 == 1) { x = (x + y) % c; } y = (y * 2) % c; b /= 2; } return x % c; } template <class T> inline T pow(T a, long long b) { T c(1); while (b) { if (b & 1) c *= a; a *= a, b >>= 1; } return c; } template <class T> inline bool scan(T &ret) { char c; int sgn; T bit = 0.1; if (c = getchar(), c == EOF) return 0; while (c != '-' && c != '.' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); if (c == ' ' || c == '\n') { ret *= sgn; return 1; } while (c = getchar(), c >= '0' && c <= '9') ret += (c - '0') * bit, bit /= 10; ret *= sgn; return 1; } template <class T> inline bool isprime(T n) { if (n <= 1) return false; for (T i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } bool isvowel(char ch) { ch = tolower(ch); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return true; return false; } bool isupper(char c) { return c >= 'A' && c <= 'Z'; } bool islower(char c) { return c >= 'a' && c <= 'z'; } bool isletter(char c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); } bool isdigit(char c) { return c >= '0' && c <= '9'; } int main() { std::string s; std::cin >> s; for (int i = 0; i < (int)s.size(); i++) { if (s[i] != '4' && s[i] != '1') { puts("NO"); return 0; } if (s[i] == '4' && s[i - 1] == '4' && s[i + 1] == '4') { puts("NO"); return 0; } if (s[i] == '4' && s[i - 1] != '1' && s[i - 2] != '1') { puts("NO"); return 0; } } puts("YES"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s = to_string(n); for (int i = 0; i < s.length(); i++) { if (s[i] == '1' and s[i + 1] == '4' and s[i + 2] == '4') { s[i] = 'a'; s[i + 1] = 'a'; s[i + 2] = 'a'; i += 2; } else if (s[i] == '1' and s[i + 1] == '4') { s[i] = 'a'; s[i + 1] = 'a'; i += 1; } else if (s[i] == '1') { s[i] = 'a'; } } for (int i = 0; i < s.length(); i++) { if (s[i] != 'a') { cout << "NO"; return 0; } } cout << "YES"; }
### Prompt Please formulate a cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s = to_string(n); for (int i = 0; i < s.length(); i++) { if (s[i] == '1' and s[i + 1] == '4' and s[i + 2] == '4') { s[i] = 'a'; s[i + 1] = 'a'; s[i + 2] = 'a'; i += 2; } else if (s[i] == '1' and s[i + 1] == '4') { s[i] = 'a'; s[i + 1] = 'a'; i += 1; } else if (s[i] == '1') { s[i] = 'a'; } } for (int i = 0; i < s.length(); i++) { if (s[i] != 'a') { cout << "NO"; return 0; } } cout << "YES"; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename P> P dectobin(P a) { if (a == 0) return 0; else return (a % 2 + 10 * dectobin(a / 2)); } template <typename Y> Y bintodec(Y a) { long long ans = 0, b = 1, t = a; while (t) { long long ld = t % 10; t /= 10; ans += ld * b; b = b * 2; } return ans; } template <typename H> H modExp(H x, H n) { long long r = 1; while (n > 0) { if (n % 2 == 1) { r = (r * x) % 1000000007; } x = (x * x) % 1000000007; n /= 2; } return r; } template <typename T> T isPowerOfTwo(T x) { return x && (!(x & (x - 1))); } template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ostream_iterator<long long> output(cout, " "); string second; cin >> second; long long d; long long n = second.length(); if (n == 1) { if ((long long)(second[0] - '0') == 1) cout << "YES"; else cout << "NO"; } else { for (long long i = 0; i <= n - 3; i++) { long long a = (long long)(second[i] - '0'); long long b = (long long)(second[i + 1] - '0'); long long c = (long long)(second[i + 2] - '0'); if (i >= 1) d = (long long)(second[i - 1] - '0'); if (a == 4 && b == 4 && c == 4) { cout << "NO"; return 0; } else if (a == 4 && b == 1 && c == 4 && i == 0) { cout << "NO"; return 0; } else if (a == 4 && b == 4 && c == 1 && d != 1) { cout << "NO"; return 0; } else if (a == 4 && b == 1 && c == 1 && i == 0) { cout << "NO"; return 0; } } for (long long(i) = (0); i < (n); i++) { long long a = (long long)(second[i] - '0'); if (a != 4 && a != 1) { cout << "NO"; return 0; } } cout << "YES"; } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename P> P dectobin(P a) { if (a == 0) return 0; else return (a % 2 + 10 * dectobin(a / 2)); } template <typename Y> Y bintodec(Y a) { long long ans = 0, b = 1, t = a; while (t) { long long ld = t % 10; t /= 10; ans += ld * b; b = b * 2; } return ans; } template <typename H> H modExp(H x, H n) { long long r = 1; while (n > 0) { if (n % 2 == 1) { r = (r * x) % 1000000007; } x = (x * x) % 1000000007; n /= 2; } return r; } template <typename T> T isPowerOfTwo(T x) { return x && (!(x & (x - 1))); } template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ostream_iterator<long long> output(cout, " "); string second; cin >> second; long long d; long long n = second.length(); if (n == 1) { if ((long long)(second[0] - '0') == 1) cout << "YES"; else cout << "NO"; } else { for (long long i = 0; i <= n - 3; i++) { long long a = (long long)(second[i] - '0'); long long b = (long long)(second[i + 1] - '0'); long long c = (long long)(second[i + 2] - '0'); if (i >= 1) d = (long long)(second[i - 1] - '0'); if (a == 4 && b == 4 && c == 4) { cout << "NO"; return 0; } else if (a == 4 && b == 1 && c == 4 && i == 0) { cout << "NO"; return 0; } else if (a == 4 && b == 4 && c == 1 && d != 1) { cout << "NO"; return 0; } else if (a == 4 && b == 1 && c == 1 && i == 0) { cout << "NO"; return 0; } } for (long long(i) = (0); i < (n); i++) { long long a = (long long)(second[i] - '0'); if (a != 4 && a != 1) { cout << "NO"; return 0; } } cout << "YES"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; int i, c, p; while (cin >> s) { c = 0; p = 0; if (s[0] != '1') { cout << "NO" << endl; continue; } for (i = 0; i < s.size() - 1; i++) { if (s[i] == '1' && (s[i + 1] == '1' || s[i + 1] == '4')) { if (s[i + 1] == '4') c++; continue; } else if (s[i] == '1' && (s[i + 1] != '1' || s[i + 1] != '4')) { p = 1; break; } if (s[i] == '4' && s[i + 1] == '4') c++; else { if (c > 2) { p = 1; break; } c = 0; } if (s[i] == '4' && s[i + 1] == '1') continue; else if (s[i] == '4' && (s[i + 1] != '1' && s[i + 1] != '4')) { p = 1; break; } } if (c > 2) p = 1; if (p == 0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
### Prompt Your task is to create a CPP solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; int i, c, p; while (cin >> s) { c = 0; p = 0; if (s[0] != '1') { cout << "NO" << endl; continue; } for (i = 0; i < s.size() - 1; i++) { if (s[i] == '1' && (s[i + 1] == '1' || s[i + 1] == '4')) { if (s[i + 1] == '4') c++; continue; } else if (s[i] == '1' && (s[i + 1] != '1' || s[i + 1] != '4')) { p = 1; break; } if (s[i] == '4' && s[i + 1] == '4') c++; else { if (c > 2) { p = 1; break; } c = 0; } if (s[i] == '4' && s[i + 1] == '1') continue; else if (s[i] == '4' && (s[i + 1] != '1' && s[i + 1] != '4')) { p = 1; break; } } if (c > 2) p = 1; if (p == 0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main(void) { string s; cin >> s; int i = 0; while (true) { if (s.length() <= i) break; if (s[i] == '1') { i++; if (s[i] == '4') { i++; if (s[i] == '4') { i++; } } } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
### Prompt Generate a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main(void) { string s; cin >> s; int i = 0; while (true) { if (s.length() <= i) break; if (s[i] == '1') { i++; if (s[i] == '4') { i++; if (s[i] == '4') { i++; } } } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int a; int main() { string a; cin >> a; if (a[0] != '1') { cout << "NO"; return 0; } for (int i = 0; i < a.size(); i++) { if (a[i] == '1') { if (a[i + 1] == '4') { i += 1; if (a[i + 1] == '4') { i += 1; } } } else { cout << "NO"; return 0; } } cout << "YES"; }
### Prompt Construct a CPP code solution to the problem outlined: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a; int main() { string a; cin >> a; if (a[0] != '1') { cout << "NO"; return 0; } for (int i = 0; i < a.size(); i++) { if (a[i] == '1') { if (a[i + 1] == '4') { i += 1; if (a[i + 1] == '4') { i += 1; } } } else { cout << "NO"; return 0; } } cout << "YES"; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e2 + 7; char s[N]; bool solve() { int n = strlen(s); if (s[0] != '1') return false; for (int i = (0); i < (n - 2); ++i) if (s[i] == '4' && s[i + 1] == '4' && s[i + 2] == '4') return false; for (int i = (0); i < (n); ++i) if (s[i] != '1' && s[i] != '4') return false; return true; } int main() { scanf("%s", s); puts(solve() ? "YES" : "NO"); return 0; }
### Prompt Generate a cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e2 + 7; char s[N]; bool solve() { int n = strlen(s); if (s[0] != '1') return false; for (int i = (0); i < (n - 2); ++i) if (s[i] == '4' && s[i + 1] == '4' && s[i + 2] == '4') return false; for (int i = (0); i < (n); ++i) if (s[i] != '1' && s[i] != '4') return false; return true; } int main() { scanf("%s", s); puts(solve() ? "YES" : "NO"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string m; cin >> m; set<int> magic; set<int>::iterator it; for (int i = 0; i < m.size(); i++) { magic.insert(m[i] - '0'); } if (m[0] == '4') { cout << "NO"; return 0; } else if (magic.size() > 2) { cout << "NO"; return 0; } for (it = magic.begin(); it != magic.end(); it++) { if (*it != 1 && *it != 4) { cout << "NO"; return 0; } } for (int i = 0; i < m.size(); i++) { if (m[i] == '4' && m[i + 1] == '4' && m[i + 2] == '4') { cout << "NO"; return 0; } } cout << "YES"; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string m; cin >> m; set<int> magic; set<int>::iterator it; for (int i = 0; i < m.size(); i++) { magic.insert(m[i] - '0'); } if (m[0] == '4') { cout << "NO"; return 0; } else if (magic.size() > 2) { cout << "NO"; return 0; } for (it = magic.begin(); it != magic.end(); it++) { if (*it != 1 && *it != 4) { cout << "NO"; return 0; } } for (int i = 0; i < m.size(); i++) { if (m[i] == '4' && m[i + 1] == '4' && m[i + 2] == '4') { cout << "NO"; return 0; } } cout << "YES"; return 0; } ```
#include <bits/stdc++.h> using namespace std; using namespace std; int main() { int n; cin >> n; while (n > 0) { if (n % 1000 == 144) { n /= 1000; } else if (n % 100 == 14) { n /= 100; } else if (n % 10 == 1) { n /= 10; } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; using namespace std; int main() { int n; cin >> n; while (n > 0) { if (n % 1000 == 144) { n /= 1000; } else if (n % 100 == 14) { n /= 100; } else if (n % 10 == 1) { n /= 10; } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n; int main() { cin >> n; int p = 0; int x; while (n) { x = n % 10; if (x != 4 && x != 1) return (cout << "NO", 0); if (x == 4) p++; if (p > 2) return (cout << "NO", 0); if (x == 1) p = 0; n /= 10; } if (p == 0) cout << "YES"; else cout << "NO"; return (0); }
### Prompt Please formulate a CPP solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; int main() { cin >> n; int p = 0; int x; while (n) { x = n % 10; if (x != 4 && x != 1) return (cout << "NO", 0); if (x == 4) p++; if (p > 2) return (cout << "NO", 0); if (x == 1) p = 0; n /= 10; } if (p == 0) cout << "YES"; else cout << "NO"; return (0); } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; if (s[0] == '4') { cout << "NO" << endl; return 0; } if (s.length() >= 3) { for (int i = 0; i < s.length(); i++) { if (s[i] != '4' && s[i] != '1') { cout << "NO" << endl; return 0; } if (i < s.length() - 2 && s[i] == '4' && s[i + 1] == '4' && s[i + 2] == '4') { cout << "NO" << endl; return 0; } } cout << "YES" << endl; } if (s.length() < 3) { if (s != "1" && s != "11" && s != "14") { cout << "NO" << endl; } else { cout << "YES" << endl; } } }
### Prompt Generate a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; if (s[0] == '4') { cout << "NO" << endl; return 0; } if (s.length() >= 3) { for (int i = 0; i < s.length(); i++) { if (s[i] != '4' && s[i] != '1') { cout << "NO" << endl; return 0; } if (i < s.length() - 2 && s[i] == '4' && s[i + 1] == '4' && s[i + 2] == '4') { cout << "NO" << endl; return 0; } } cout << "YES" << endl; } if (s.length() < 3) { if (s != "1" && s != "11" && s != "14") { cout << "NO" << endl; } else { cout << "YES" << endl; } } } ```
#include <bits/stdc++.h> using namespace std; int n, a[111], kol; int main() { cin >> n; while (n) { a[kol++] = n % 10; if (a[0] == 1) { kol = 0; for (int i = 0; i < 111; i++) a[i] = 0; } if (a[0] == 4 && a[1] == 4 && a[2] == 1) { kol = 0; for (int i = 0; i < 111; i++) a[i] = 0; } if (a[0] == 4 && a[1] == 1) { kol = 0; for (int i = 0; i < 111; i++) a[i] = 0; } n = n / 10; } if (kol) cout << "NO" << endl; else cout << "YES" << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, a[111], kol; int main() { cin >> n; while (n) { a[kol++] = n % 10; if (a[0] == 1) { kol = 0; for (int i = 0; i < 111; i++) a[i] = 0; } if (a[0] == 4 && a[1] == 4 && a[2] == 1) { kol = 0; for (int i = 0; i < 111; i++) a[i] = 0; } if (a[0] == 4 && a[1] == 1) { kol = 0; for (int i = 0; i < 111; i++) a[i] = 0; } n = n / 10; } if (kol) cout << "NO" << endl; else cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int i, j, c, a[12], b, n; while (cin >> b) { i = 1; while (b != 0) { a[i++] = b % 10; b = b / 10; } i = i - 1; c = 0; if (a[i] == 4) { cout << "NO" << endl; n = 0; } else { for (j = i; j > 0; j--) { if (a[j] == 1 || a[j] == 4) { n = 1; } else { cout << "NO" << endl; n = 0; break; } if (a[j] == 4) { c++; if (c > 2) { cout << "NO" << endl; n = 0; break; } } else c = 0; } } if (n == 1) cout << "YES" << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int i, j, c, a[12], b, n; while (cin >> b) { i = 1; while (b != 0) { a[i++] = b % 10; b = b / 10; } i = i - 1; c = 0; if (a[i] == 4) { cout << "NO" << endl; n = 0; } else { for (j = i; j > 0; j--) { if (a[j] == 1 || a[j] == 4) { n = 1; } else { cout << "NO" << endl; n = 0; break; } if (a[j] == 4) { c++; if (c > 2) { cout << "NO" << endl; n = 0; break; } } else c = 0; } } if (n == 1) cout << "YES" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long int; using pii = pair<int, int>; void _print() { cout << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { cout << t; if (sizeof...(v)) cout << ", "; _print(v...); } const int mod = 1000000007; void io_set() {} void solve() { string s; cin >> s; bool flag = true; for (int i = 0; i < s.size(); i++) { if (i + 2 < s.size() && s[i] == '1' && s[i + 1] == '4' && s[i + 2] == '4') { i += 2; } else if (i + 1 < s.size() && s[i] == '1' && s[i + 1] == '4') { i += 1; } else if (s[i] == '1') ; else { flag = false; } } cout << (flag ? "YES" : "NO") << '\n'; } signed main(int argc, char **argv) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int tt = 1; while (tt--) { solve(); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long int; using pii = pair<int, int>; void _print() { cout << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { cout << t; if (sizeof...(v)) cout << ", "; _print(v...); } const int mod = 1000000007; void io_set() {} void solve() { string s; cin >> s; bool flag = true; for (int i = 0; i < s.size(); i++) { if (i + 2 < s.size() && s[i] == '1' && s[i + 1] == '4' && s[i + 2] == '4') { i += 2; } else if (i + 1 < s.size() && s[i] == '1' && s[i + 1] == '4') { i += 1; } else if (s[i] == '1') ; else { flag = false; } } cout << (flag ? "YES" : "NO") << '\n'; } signed main(int argc, char **argv) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int tt = 1; while (tt--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n > 0) { if (n % 1000 == 144) { n /= 1000; } else if (n % 100 == 14) { n /= 100; } else if (n % 10 == 1) { n /= 10; } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; }
### Prompt Your task is to create a CPP solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n > 0) { if (n % 1000 == 144) { n /= 1000; } else if (n % 100 == 14) { n /= 100; } else if (n % 10 == 1) { n /= 10; } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; if (str[0] != '1') { puts("NO"); return 0; } int cnt = 0; for (int i = 0; i < str.length(); i++) { if (str[i] != '1' && str[i] != '4') { puts("NO"); return 0; } if (str[i] == '1') cnt = 0; else if (str[i] == '4') cnt++; if (cnt == 3) { puts("NO"); return 0; } } puts("YES"); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; if (str[0] != '1') { puts("NO"); return 0; } int cnt = 0; for (int i = 0; i < str.length(); i++) { if (str[i] != '1' && str[i] != '4') { puts("NO"); return 0; } if (str[i] == '1') cnt = 0; else if (str[i] == '4') cnt++; if (cnt == 3) { puts("NO"); return 0; } } puts("YES"); return 0; } ```
#include <bits/stdc++.h> using namespace std; double tick() { static clock_t oldt, newt = clock(); double diff = 1.0 * (newt - oldt) / CLOCKS_PER_SEC; oldt = newt; return diff; } char s[12]; bool check(const char *s) { if (*s == 0) return true; if (*s != '1') return false; if (check(++s)) return true; if (*s != '4') return false; if (check(++s)) return true; if (*s != '4') return false; if (check(++s)) return true; return false; } int main() { cin >> s; cout << (check(s) ? "YES" : "NO") << endl; cerr << "execution time is: " << tick() << "\n"; }
### Prompt Generate a cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; double tick() { static clock_t oldt, newt = clock(); double diff = 1.0 * (newt - oldt) / CLOCKS_PER_SEC; oldt = newt; return diff; } char s[12]; bool check(const char *s) { if (*s == 0) return true; if (*s != '1') return false; if (check(++s)) return true; if (*s != '4') return false; if (check(++s)) return true; if (*s != '4') return false; if (check(++s)) return true; return false; } int main() { cin >> s; cout << (check(s) ? "YES" : "NO") << endl; cerr << "execution time is: " << tick() << "\n"; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int f = 2; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') f = 0; else if (s[i] == '4' && f < 2) f++; else f = -1; if (f == -1) break; } if (f == -1) printf("NO\n"); else printf("YES\n"); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int f = 2; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') f = 0; else if (s[i] == '4' && f < 2) f++; else f = -1; if (f == -1) break; } if (f == -1) printf("NO\n"); else printf("YES\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int f = 1, l = s.length(); for (int i = 0; i < l; i++) { if (s[i] == '1') continue; else if (s[i] == '4' && i - 1 >= 0) { if (s[i - 1] == '1') continue; else if (i - 2 >= 0 && s[i - 1] == '4' && s[i - 2] == '1') continue; else { f = 0; break; } } else { f = 0; break; } } if (f) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
### Prompt Create a solution in CPP for the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int f = 1, l = s.length(); for (int i = 0; i < l; i++) { if (s[i] == '1') continue; else if (s[i] == '4' && i - 1 >= 0) { if (s[i - 1] == '1') continue; else if (i - 2 >= 0 && s[i - 1] == '4' && s[i - 2] == '1') continue; else { f = 0; break; } } else { f = 0; break; } } if (f) cout << "YES" << endl; else cout << "NO" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 10; const long long int INF = 2e18 + 10; const int Mod = 1e9 + 7; const int MAXN = 2e5 + 10; const int N = 1e5 + 10; string s; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] != '1' && s[i] != '4') { cout << "NO" << endl; return 0; } if (s[i] == '1') continue; if (s[i] == '4') { if (i - 1 >= 0) { if (s[i - 1] == '1') continue; else { if ((i - 2) >= 0) { if (s[i - 2] == '1') continue; else { cout << "NO" << endl; return 0; } } else { cout << "NO" << endl; return 0; } } } else { cout << "NO" << endl; return 0; } } } cout << "YES" << endl; return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 10; const long long int INF = 2e18 + 10; const int Mod = 1e9 + 7; const int MAXN = 2e5 + 10; const int N = 1e5 + 10; string s; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] != '1' && s[i] != '4') { cout << "NO" << endl; return 0; } if (s[i] == '1') continue; if (s[i] == '4') { if (i - 1 >= 0) { if (s[i - 1] == '1') continue; else { if ((i - 2) >= 0) { if (s[i - 2] == '1') continue; else { cout << "NO" << endl; return 0; } } else { cout << "NO" << endl; return 0; } } } else { cout << "NO" << endl; return 0; } } } cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; getline(cin, s); int i = 0, len; len = s.length(); if (s[0] != '1') cout << "NO"; else { for (i = 1; i < len; i++) { if (s[i] != '4' && s[i] != '1') { cout << "NO"; break; } if (s[i] == '4') { if (s[i - 1] == '1' || s[i - 2] == '1') continue; else cout << "NO"; break; } } } if (i == len) cout << "YES"; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; getline(cin, s); int i = 0, len; len = s.length(); if (s[0] != '1') cout << "NO"; else { for (i = 1; i < len; i++) { if (s[i] != '4' && s[i] != '1') { cout << "NO"; break; } if (s[i] == '4') { if (s[i - 1] == '1' || s[i - 2] == '1') continue; else cout << "NO"; break; } } } if (i == len) cout << "YES"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { char num[18]; int n, i, j; int flag = 1, k = 0; scanf("%s", &num); n = strlen(num); if (num[0] != '1') flag = 0; if (flag) { for (i = 1; i < n; i++) { if (num[i] != '1' && num[i] != '4') { flag = 0; break; } } } if (flag) { for (i = 0; i < n; i++) { if (num[i] == '4') { k = k + 1; if (k >= 3) break; } if (num[i] != '4') k = 0; } if (k >= 3) flag = 0; } if (flag) printf("YES\n"); else printf("NO\n"); return 0; }
### Prompt Develop a solution in CPP to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { char num[18]; int n, i, j; int flag = 1, k = 0; scanf("%s", &num); n = strlen(num); if (num[0] != '1') flag = 0; if (flag) { for (i = 1; i < n; i++) { if (num[i] != '1' && num[i] != '4') { flag = 0; break; } } } if (flag) { for (i = 0; i < n; i++) { if (num[i] == '4') { k = k + 1; if (k >= 3) break; } if (num[i] != '4') k = 0; } if (k >= 3) flag = 0; } if (flag) printf("YES\n"); else printf("NO\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int L, i, j; string S; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> S; L = S.length(); for (i = 0; i < L; i++) { if (S[i] != '1') { if (S[i] == '4') { if (i == 0) { cout << "NO\n"; return 0; } if (S[i - 1] == '4' && S[i - 2] == '4') { cout << "NO\n"; return 0; } } else { cout << "NO\n"; return 0; } } } cout << "YES\n"; }
### Prompt Please create a solution in cpp to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int L, i, j; string S; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> S; L = S.length(); for (i = 0; i < L; i++) { if (S[i] != '1') { if (S[i] == '4') { if (i == 0) { cout << "NO\n"; return 0; } if (S[i - 1] == '4' && S[i - 2] == '4') { cout << "NO\n"; return 0; } } else { cout << "NO\n"; return 0; } } } cout << "YES\n"; } ```
#include <bits/stdc++.h> using namespace std; long long arr[100005], arrP[10005]; int main() { string s; cin >> s; for (int i = 0; i < s.size(); i++) { if (s.substr(i, 3) == "144") i += 2; else if (s.substr(i, 2) == "14") i++; else if (s.substr(i, 1) == "1") { } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; }
### Prompt In Cpp, your task is to solve the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long arr[100005], arrP[10005]; int main() { string s; cin >> s; for (int i = 0; i < s.size(); i++) { if (s.substr(i, 3) == "144") i += 2; else if (s.substr(i, 2) == "14") i++; else if (s.substr(i, 1) == "1") { } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; } ```
#include <bits/stdc++.h> using namespace std; string s; bool bad; int cnt = 0; int main() { cin >> s; bad = (s[0] != '1'); for (char c : s) { if (c != '1' && c != '4') bad = true; if (c == '1') cnt = 0; else cnt++; if (cnt > 2) bad = true; } cout << (bad ? "NO" : "YES"); return 0; }
### Prompt Develop a solution in CPP to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s; bool bad; int cnt = 0; int main() { cin >> s; bad = (s[0] != '1'); for (char c : s) { if (c != '1' && c != '4') bad = true; if (c == '1') cnt = 0; else cnt++; if (cnt > 2) bad = true; } cout << (bad ? "NO" : "YES"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string a; cin >> a; bool f = true; long long int i; for (i = 0; i < a.length();) { if (a[i] == '1' && a[i + 1] == '4' && a[i + 2] == '4') { i += 3; } else if (a[i] == '1' && a[i + 1] == '4') { i += 2; } else if (a[i] == '1') { i++; } else { f = false; break; } } if (f) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
### Prompt Create a solution in Cpp for the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string a; cin >> a; bool f = true; long long int i; for (i = 0; i < a.length();) { if (a[i] == '1' && a[i + 1] == '4' && a[i + 2] == '4') { i += 3; } else if (a[i] == '1' && a[i + 1] == '4') { i += 2; } else if (a[i] == '1') { i++; } else { f = false; break; } } if (f) cout << "YES" << endl; else cout << "NO" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int i; string x; cin >> x; bool f = true; for (i = 0; i < x.size(); i++) { if (x[i] == '1' || x[i] == '4') continue; else { f = false; break; } } int c = 0; if (x[0] == '4') f = false; if (f) { for (i = 0; i < x.size() - 1; i++) { if (x[i] == '4' && x[i + 1] == '4') c++; else c = 1; if (c > 2) { f = false; break; } } } if (f) cout << "YES" << endl; else cout << "NO" << endl; }
### Prompt Develop a solution in CPP to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int i; string x; cin >> x; bool f = true; for (i = 0; i < x.size(); i++) { if (x[i] == '1' || x[i] == '4') continue; else { f = false; break; } } int c = 0; if (x[0] == '4') f = false; if (f) { for (i = 0; i < x.size() - 1; i++) { if (x[i] == '4' && x[i + 1] == '4') c++; else c = 1; if (c > 2) { f = false; break; } } } if (f) cout << "YES" << endl; else cout << "NO" << endl; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; cout << (regex_match(s, regex("(1|14|144)*")) ? "YES" : "NO"); }
### Prompt Please formulate a cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; cout << (regex_match(s, regex("(1|14|144)*")) ? "YES" : "NO"); } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int len = s.length(); for (int i = 0; i < len;) { if (s[i] != '1') { cout << "NO" << endl; return 0; } if (i + 3 <= len) { if (s[i + 1] == '4' and s[i + 2] == '4') { i += 3; } else if (s[i + 1] == '4') { i += 2; } else ++i; } else if (i + 2 <= len) { if (s[i + 1] == '4') { i += 2; } else ++i; } else ++i; } cout << "YES" << endl; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int len = s.length(); for (int i = 0; i < len;) { if (s[i] != '1') { cout << "NO" << endl; return 0; } if (i + 3 <= len) { if (s[i + 1] == '4' and s[i + 2] == '4') { i += 3; } else if (s[i + 1] == '4') { i += 2; } else ++i; } else if (i + 2 <= len) { if (s[i + 1] == '4') { i += 2; } else ++i; } else ++i; } cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long k; string s, ss[100], s1; void xuly() { cin >> s; if (s[0] != '1') { cout << "NO"; return; } ss[1] = "1"; ss[2] = "14"; ss[3] = "144"; s1 = '.'; for (int i = 3; i >= 1; i--) { while (s.find(ss[i]) != -1) { k = s.find(ss[i]); s.erase(k, i); s.insert(k, s1); } } for (int i = 0; i <= s.length() - 1; i++) { if (s[i] >= '0' && s[i] <= '9') { cout << "NO"; return; } } cout << "YES"; } int main() { xuly(); }
### Prompt In CPP, your task is to solve the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long k; string s, ss[100], s1; void xuly() { cin >> s; if (s[0] != '1') { cout << "NO"; return; } ss[1] = "1"; ss[2] = "14"; ss[3] = "144"; s1 = '.'; for (int i = 3; i >= 1; i--) { while (s.find(ss[i]) != -1) { k = s.find(ss[i]); s.erase(k, i); s.insert(k, s1); } } for (int i = 0; i <= s.length() - 1; i++) { if (s[i] >= '0' && s[i] <= '9') { cout << "NO"; return; } } cout << "YES"; } int main() { xuly(); } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); char c; cin >> c; while (1) { if (c == '1') { if (!(cin >> c)) break; if (c == '4') { if (!(cin >> c)) break; if (c == '4') { if (!(cin >> c)) break; } } } else { cout << "NO"; return 0; } } cout << "YES"; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); char c; cin >> c; while (1) { if (c == '1') { if (!(cin >> c)) break; if (c == '4') { if (!(cin >> c)) break; if (c == '4') { if (!(cin >> c)) break; } } } else { cout << "NO"; return 0; } } cout << "YES"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s.length() >= 3 and s[i] == '1' and s[i + 2] == '4' and s[i + 1] == '4') { s.erase(i, 3); i--; } else if (s.length() >= 2 and s[i + 1] == '4' and s[i] == '1') { s.erase(i, 2); i--; } else if (s[i] == '1') { s.erase(i, 1); i--; } } if (s.length() == 0) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s.length() >= 3 and s[i] == '1' and s[i + 2] == '4' and s[i + 1] == '4') { s.erase(i, 3); i--; } else if (s.length() >= 2 and s[i + 1] == '4' and s[i] == '1') { s.erase(i, 2); i--; } else if (s[i] == '1') { s.erase(i, 1); i--; } } if (s.length() == 0) cout << "YES" << endl; else cout << "NO" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); if (s[0] == '4') { cout << "NO" << endl; return 0; } int F = s.find("1444"); if (F > -1) { cout << "NO" << endl; return 0; } bool f = false; for (int i = 0; i < n; i++) { if (s[i] == '0') { f = true; } else if (s[i] == '2') { f = true; } else if (s[i] == '3') { f = true; } else if (s[i] == '5') { f = true; } else if (s[i] == '6') { f = true; } else if (s[i] == '7') { f = true; } else if (s[i] == '8') { f = true; } else if (s[i] == '9') { f = true; } } if (f == true) cout << "NO" << endl; else cout << "YES" << endl; return 0; }
### Prompt Please formulate a cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); if (s[0] == '4') { cout << "NO" << endl; return 0; } int F = s.find("1444"); if (F > -1) { cout << "NO" << endl; return 0; } bool f = false; for (int i = 0; i < n; i++) { if (s[i] == '0') { f = true; } else if (s[i] == '2') { f = true; } else if (s[i] == '3') { f = true; } else if (s[i] == '5') { f = true; } else if (s[i] == '6') { f = true; } else if (s[i] == '7') { f = true; } else if (s[i] == '8') { f = true; } else if (s[i] == '9') { f = true; } } if (f == true) cout << "NO" << endl; else cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string number; cin >> number; bool possible = true; for (int i = 0; i < number.size();) { if (number[i] == '1' && number[i + 1] == '4' && number[i + 2] == '4') { i += 3; continue; } else if (number[i] == '1' && number[i + 1] == '4') { i += 2; continue; } else if (number[i] == '1') { i++; continue; } else { possible = false; break; } } if (possible) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
### Prompt Construct a Cpp code solution to the problem outlined: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string number; cin >> number; bool possible = true; for (int i = 0; i < number.size();) { if (number[i] == '1' && number[i + 1] == '4' && number[i + 2] == '4') { i += 3; continue; } else if (number[i] == '1' && number[i + 1] == '4') { i += 2; continue; } else if (number[i] == '1') { i++; continue; } else { possible = false; break; } } if (possible) { cout << "YES" << endl; } else { cout << "NO" << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { string a; cin >> a; int n = a.size(); for (int i = 0; i < n; i++) { if ((a[i] == '4' && (a[i - 1] != '1' && a[i - 2] != '1')) || (a[i] != '1' && a[i] != '4')) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string a; cin >> a; int n = a.size(); for (int i = 0; i < n; i++) { if ((a[i] == '4' && (a[i - 1] != '1' && a[i - 2] != '1')) || (a[i] != '1' && a[i] != '4')) { cout << "NO"; return 0; } } cout << "YES"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int z = 0; if (s[0] != '1') cout << "NO" << endl; else { for (int i = 1; i < s.size(); i++) { if ((s[i] == '1') || (s[i] == '4' && s[i - 1] == '1') || (s[i] == '4' && s[i - 1] == '4' && s[i - 2] == '1')) continue; else { cout << "NO" << endl; z = 1; break; } } if (z == 0) cout << "YES" << endl; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int z = 0; if (s[0] != '1') cout << "NO" << endl; else { for (int i = 1; i < s.size(); i++) { if ((s[i] == '1') || (s[i] == '4' && s[i - 1] == '1') || (s[i] == '4' && s[i - 1] == '4' && s[i - 2] == '1')) continue; else { cout << "NO" << endl; z = 1; break; } } if (z == 0) cout << "YES" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; int i, p = 0; for (i = 0; i < s.size(); i++) { if (s[i] != '1' && s[i] != '4' || s[0] == '4') p++; if (s[i] == '4') { if (s[i + 1] == '4' && s[i + 2] == '4') p++; } } if (p == 0) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
### Prompt In Cpp, your task is to solve the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; int i, p = 0; for (i = 0; i < s.size(); i++) { if (s[i] != '1' && s[i] != '4' || s[0] == '4') p++; if (s[i] == '4') { if (s[i + 1] == '4' && s[i + 2] == '4') p++; } } if (p == 0) cout << "YES" << endl; else cout << "NO" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; void exec(int T) { string s; cin >> s; for (int i = 0; i < s.size();) { if ((i + 2) < s.size() && s.substr(i, 3) == "144") { i += 3; continue; } if ((i + 1) < s.size() && s.substr(i, 2) == "14") { i += 2; continue; } if (i < s.size() && s[i] == '1') { i++; continue; } cout << "NO\n"; return; } cout << "YES\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; for (int T = 1; T <= t; T++) { exec(T); } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; void exec(int T) { string s; cin >> s; for (int i = 0; i < s.size();) { if ((i + 2) < s.size() && s.substr(i, 3) == "144") { i += 3; continue; } if ((i + 1) < s.size() && s.substr(i, 2) == "14") { i += 2; continue; } if (i < s.size() && s[i] == '1') { i++; continue; } cout << "NO\n"; return; } cout << "YES\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; for (int T = 1; T <= t; T++) { exec(T); } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); cout.flush(); string s; cin >> s; if (s[0] != '1') { cout << "NO" << endl; return 0; } int cnt = 0; for (int i = 1; i < s.size(); i++) { if (s[i] == '4') cnt++; else cnt = 0; if (cnt == 3 || (s[i] != '1' && s[i] != '4')) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
### Prompt Please formulate a CPP solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); cout.flush(); string s; cin >> s; if (s[0] != '1') { cout << "NO" << endl; return 0; } int cnt = 0; for (int i = 1; i < s.size(); i++) { if (s[i] == '4') cnt++; else cnt = 0; if (cnt == 3 || (s[i] != '1' && s[i] != '4')) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; if (s[0] != '1') { cout << "NO" << endl; return 0; } int cntr = 0; for (int i = 0; i < s.size() - 1; i++) { if (s[i] == '1') { if (s[i + 1] != '4' && s[i + 1] != '1') { cout << "NO" << endl; return 0; } cntr = 0; } else if (s[i] == '4') { if ((s[i - 1] != '1' && s[i - 1] != '4') || (s[i + 1] != '1' && s[i + 1] != '4')) { cout << "NO" << endl; return 0; } if (cntr > 2) { cout << "NO" << endl; return 0; } cntr++; } } if (s[s.size() - 1] == '4') { cntr++; } if (cntr > 2) cout << "NO" << endl; else cout << "YES" << endl; return 0; }
### Prompt Generate a CPP solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; if (s[0] != '1') { cout << "NO" << endl; return 0; } int cntr = 0; for (int i = 0; i < s.size() - 1; i++) { if (s[i] == '1') { if (s[i + 1] != '4' && s[i + 1] != '1') { cout << "NO" << endl; return 0; } cntr = 0; } else if (s[i] == '4') { if ((s[i - 1] != '1' && s[i - 1] != '4') || (s[i + 1] != '1' && s[i + 1] != '4')) { cout << "NO" << endl; return 0; } if (cntr > 2) { cout << "NO" << endl; return 0; } cntr++; } } if (s[s.size() - 1] == '4') { cntr++; } if (cntr > 2) cout << "NO" << endl; else cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string t; cin >> t; long long int i = 0; long long int c = 1; while (1) { if (i >= t.length()) break; if (t[i] == '1') { i++; if (i < t.length()) if (t[i] == '4') { i++; if (i < t.length()) { if (t[i] == '4') { i++; } } } } else { c = 0; break; } } if (c) cout << "YES"; else cout << "NO"; }
### Prompt Construct a Cpp code solution to the problem outlined: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string t; cin >> t; long long int i = 0; long long int c = 1; while (1) { if (i >= t.length()) break; if (t[i] == '1') { i++; if (i < t.length()) if (t[i] == '4') { i++; if (i < t.length()) { if (t[i] == '4') { i++; } } } } else { c = 0; break; } } if (c) cout << "YES"; else cout << "NO"; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int f = 0; if (s[0] != '1' && s[0] != '4') { cout << "NO"; return 0; } if (s[0] == '4') { cout << "NO"; return 0; } for (int i = (1); i <= (s.length() - 1); i++) { if (s[i] != '1' && s[i] != '4') { f = 1; break; } if (s[i] == '4') { if (s[i - 1] != '1' && s[i - 2] != '1') { f = 1; break; } } } if (f) cout << "NO"; else cout << "YES"; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int f = 0; if (s[0] != '1' && s[0] != '4') { cout << "NO"; return 0; } if (s[0] == '4') { cout << "NO"; return 0; } for (int i = (1); i <= (s.length() - 1); i++) { if (s[i] != '1' && s[i] != '4') { f = 1; break; } if (s[i] == '4') { if (s[i - 1] != '1' && s[i - 2] != '1') { f = 1; break; } } } if (f) cout << "NO"; else cout << "YES"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int cnt = 0; bool flag = true; for (int i = 0; i < s.length(); i++) { if (s[i] != '1' && s[i] != '4') { flag = false; break; } if (s[0] == '4') { flag = false; break; } if (s.find("444") != s.npos) { flag = false; break; } } if (flag) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int cnt = 0; bool flag = true; for (int i = 0; i < s.length(); i++) { if (s[i] != '1' && s[i] != '4') { flag = false; break; } if (s[0] == '4') { flag = false; break; } if (s.find("444") != s.npos) { flag = false; break; } } if (flag) cout << "YES" << endl; else cout << "NO" << endl; return 0; } ```
#include <bits/stdc++.h> long long dx[4] = {-1, 0, 1, 0}; long long dy[4] = {0, -1, 0, 1}; long long mod = 1e9 + 7; long long inf = INT_MAX; using namespace std; bool isPrime(long long n) { long long j; for (j = 2; j <= sqrt(n); j++) { if (n % j == 0) { break; } } if (j > sqrt(n)) { return 1; } else { return 0; } } long long modexpo(long long a, long long b) { long long ans = 1; a = a % mod; while (b > 0) { if ((b & 1) == 1) { ans = ((ans % mod) * (a % mod)) % mod; } b = b >> 1; a = ((a % mod) * (a % mod)) % mod; } return ans; } long long invmod(long long n) { return modexpo(n, mod - 2); } long long comb(long long n, long long r) { if (r == 0) { return 1; } long long fact[n + 1]; fact[0] = 1; for (long long i = 1; i <= n; i++) { fact[i] = (fact[i - 1] * i) % mod; } return (fact[n] * invmod(fact[r]) % mod * invmod(fact[n - r]) % mod) % mod; } long long gcd(long long a, long long b) { if (a == 0) { return b; } return gcd(b % a, a); } long long lcm(long long a, long long b) { return ((a / gcd(a, b)) * (b / gcd(a, b))) * gcd(a, b); } bool sortbysecdesc(const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second > b.second; } bool prime[1000005]; void sieve(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } vector<long long> adj[100005]; bool visited[100005]; void dfs(long long i) { visited[i] = 1; for (auto j : adj[i]) { if (visited[j] == 0) { dfs(j); } } } void solve(long long k) { string n; cin >> n; map<string, long long> m; queue<string> q; q.push("1"); q.push("14"); q.push("144"); while (q.size() != 0) { string a = q.front(); q.pop(); m[a] = 1; a += '1'; if (a.size() <= 9) { q.push(a); } a += '4'; if (a.size() <= 9) { q.push(a); } a += '4'; if (a.size() <= 9) { q.push(a); } } if (m[n]) { cout << "YES" << "\n"; return; } cout << "NO" << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; long long k = 1; while (k <= t) { solve(k); k++; } return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> long long dx[4] = {-1, 0, 1, 0}; long long dy[4] = {0, -1, 0, 1}; long long mod = 1e9 + 7; long long inf = INT_MAX; using namespace std; bool isPrime(long long n) { long long j; for (j = 2; j <= sqrt(n); j++) { if (n % j == 0) { break; } } if (j > sqrt(n)) { return 1; } else { return 0; } } long long modexpo(long long a, long long b) { long long ans = 1; a = a % mod; while (b > 0) { if ((b & 1) == 1) { ans = ((ans % mod) * (a % mod)) % mod; } b = b >> 1; a = ((a % mod) * (a % mod)) % mod; } return ans; } long long invmod(long long n) { return modexpo(n, mod - 2); } long long comb(long long n, long long r) { if (r == 0) { return 1; } long long fact[n + 1]; fact[0] = 1; for (long long i = 1; i <= n; i++) { fact[i] = (fact[i - 1] * i) % mod; } return (fact[n] * invmod(fact[r]) % mod * invmod(fact[n - r]) % mod) % mod; } long long gcd(long long a, long long b) { if (a == 0) { return b; } return gcd(b % a, a); } long long lcm(long long a, long long b) { return ((a / gcd(a, b)) * (b / gcd(a, b))) * gcd(a, b); } bool sortbysecdesc(const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second > b.second; } bool prime[1000005]; void sieve(int n) { memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } vector<long long> adj[100005]; bool visited[100005]; void dfs(long long i) { visited[i] = 1; for (auto j : adj[i]) { if (visited[j] == 0) { dfs(j); } } } void solve(long long k) { string n; cin >> n; map<string, long long> m; queue<string> q; q.push("1"); q.push("14"); q.push("144"); while (q.size() != 0) { string a = q.front(); q.pop(); m[a] = 1; a += '1'; if (a.size() <= 9) { q.push(a); } a += '4'; if (a.size() <= 9) { q.push(a); } a += '4'; if (a.size() <= 9) { q.push(a); } } if (m[n]) { cout << "YES" << "\n"; return; } cout << "NO" << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; long long k = 1; while (k <= t) { solve(k); k++; } return 0; } ```
#include <bits/stdc++.h> int main() { char a[15]; int n, m, i, j, sum, count, l, z, x; while (~scanf("%s", a)) { z = 0; x = 0; count = 0; l = strlen(a); for (i = 0; i < l; i++) { if (a[i] == '1') { z++; } else if (a[i] == '4') { x++; } } if (x > 2 * z || (z + x) != l) { printf("NO\n"); continue; } else { for (i = 0; i < l; i++) { if (a[i] == '1' && a[i + 1] == '4' && a[i + 2] == '4' && a[i + 3] == '4' || a[0] == '4') { count++; break; } } if (count) { printf("NO\n"); } else printf("YES\n"); } } }
### Prompt Your challenge is to write a CPP solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> int main() { char a[15]; int n, m, i, j, sum, count, l, z, x; while (~scanf("%s", a)) { z = 0; x = 0; count = 0; l = strlen(a); for (i = 0; i < l; i++) { if (a[i] == '1') { z++; } else if (a[i] == '4') { x++; } } if (x > 2 * z || (z + x) != l) { printf("NO\n"); continue; } else { for (i = 0; i < l; i++) { if (a[i] == '1' && a[i + 1] == '4' && a[i + 2] == '4' && a[i + 3] == '4' || a[0] == '4') { count++; break; } } if (count) { printf("NO\n"); } else printf("YES\n"); } } } ```
#include <bits/stdc++.h> using namespace std; bool chk(string num) { for (int i = 0; i < (int)num.size(); i++) if (num[i] != '1' && num[i] != '4') return false; if (num[0] == '4') return false; if (num.find("444") != num.npos) return false; return true; } int main() { string num; cin >> num; if (chk(num)) cout << "YES"; else cout << "NO"; return 0; }
### Prompt Please create a solution in cpp to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool chk(string num) { for (int i = 0; i < (int)num.size(); i++) if (num[i] != '1' && num[i] != '4') return false; if (num[0] == '4') return false; if (num.find("444") != num.npos) return false; return true; } int main() { string num; cin >> num; if (chk(num)) cout << "YES"; else cout << "NO"; return 0; } ```
#include <bits/stdc++.h> using namespace std; using ll = long long int; using pll = pair<ll, ll>; using vll = vector<ll>; ll testcases; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); testcases = 1; while (testcases--) { string n; cin >> n; ll o = 0, f = 0, fail = 0; for (ll i = 0; i < n.size(); i++) { if (n[i] == '4') { ++f; if (f > 2) fail = 1; else if (o < 1) fail = 1; } else if (n[i] == '1') f = 0, o++; else if (n[i] != '1' || n[i] != '4') fail = 1; } if (!fail) cout << "YES"; else cout << "NO"; } }
### Prompt Please formulate a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; using ll = long long int; using pll = pair<ll, ll>; using vll = vector<ll>; ll testcases; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); testcases = 1; while (testcases--) { string n; cin >> n; ll o = 0, f = 0, fail = 0; for (ll i = 0; i < n.size(); i++) { if (n[i] == '4') { ++f; if (f > 2) fail = 1; else if (o < 1) fail = 1; } else if (n[i] == '1') f = 0, o++; else if (n[i] != '1' || n[i] != '4') fail = 1; } if (!fail) cout << "YES"; else cout << "NO"; } } ```
#include <bits/stdc++.h> typedef long long LL; using namespace std; const int M = 1000000007; int main() { ios_base::sync_with_stdio(0); cout.precision(15); cout.setf(ios::fixed); LL n; cin >> n; LL cnt = 0, mx = 0; bool on = true; bool en = true; while (n != 0) { if (n % 10 == 4) cnt++; else cnt = 0; if (n % 10 != 4 && n % 10 != 1) on = false; mx = max(mx, cnt); if (n / 10 == 0) { if (n % 10 != 1) en = false; } n /= 10; } if (mx < 3 && on && en) cout << "YES" << '\n'; else cout << "NO" << '\n'; }
### Prompt Your challenge is to write a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> typedef long long LL; using namespace std; const int M = 1000000007; int main() { ios_base::sync_with_stdio(0); cout.precision(15); cout.setf(ios::fixed); LL n; cin >> n; LL cnt = 0, mx = 0; bool on = true; bool en = true; while (n != 0) { if (n % 10 == 4) cnt++; else cnt = 0; if (n % 10 != 4 && n % 10 != 1) on = false; mx = max(mx, cnt); if (n / 10 == 0) { if (n % 10 != 1) en = false; } n /= 10; } if (mx < 3 && on && en) cout << "YES" << '\n'; else cout << "NO" << '\n'; } ```
#include <bits/stdc++.h> using namespace std; int main() { int ans = 0; string s; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') ans++; else if (s[i] == '4' && s[i - 1] == '1' || s[i] == '4' && s[i - 1] == '1' && s[i + 1] == '4' || s[i] == '4' && s[i - 1] == '4' && s[i - 2] == '1') ans++; else ans = 0; } if (ans == s.size()) cout << "YES"; else cout << "NO"; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int ans = 0; string s; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') ans++; else if (s[i] == '4' && s[i - 1] == '1' || s[i] == '4' && s[i - 1] == '1' && s[i + 1] == '4' || s[i] == '4' && s[i - 1] == '4' && s[i - 2] == '1') ans++; else ans = 0; } if (ans == s.size()) cout << "YES"; else cout << "NO"; return 0; } ```
#include <bits/stdc++.h> using namespace std; set<string> sso; void gens(string curr) { if (curr.size() > 0 && stoll(curr) >= 1000000000) return; if (curr.size() > 0) { sso.insert(curr); } gens(curr + "14"); gens(curr + "1"); gens(curr + "144"); } int main(int argc, const char** argv) { std::ios_base::sync_with_stdio(false); ; string in; cin >> in; gens(""); if (sso.count(in)) cout << "YES" << "\n"; else cout << "NO" << "\n"; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; set<string> sso; void gens(string curr) { if (curr.size() > 0 && stoll(curr) >= 1000000000) return; if (curr.size() > 0) { sso.insert(curr); } gens(curr + "14"); gens(curr + "1"); gens(curr + "144"); } int main(int argc, const char** argv) { std::ios_base::sync_with_stdio(false); ; string in; cin >> in; gens(""); if (sso.count(in)) cout << "YES" << "\n"; else cout << "NO" << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { int flag = 0; while (n) { if (n % 1000 == 144) n /= 1000; else if (n % 100 == 14) n /= 100; else if (n % 10 == 1) n /= 10; else { flag = 1; break; } } if (flag == 0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
### Prompt Please create a solution in CPP to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { int flag = 0; while (n) { if (n % 1000 == 144) n /= 1000; else if (n % 100 == 14) n /= 100; else if (n % 10 == 1) n /= 10; else { flag = 1; break; } } if (flag == 0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { char a[12]; int i, n, mark = 0; deque<char> s; scanf("%s", a); n = strlen(a); for (i = 0; i < n; i++) { if (mark == 3 || (mark == 0 && a[i] != '1')) { s.push_back(a[i]); mark = 3; continue; } if (a[i] == '1' && mark == 0) { s.push_back(a[i]); mark++; } else if (mark == 1) { if (a[i] == '4') { s.push_back(a[i]); mark++; } else if (a[i] != '1') { s.push_back(a[i]); mark = 3; } } else if (mark == 2) { if (a[i] == '1') { s.clear(); s.push_back(a[i]); mark = 1; } else if (a[i] == '4') { s.clear(); mark = 0; } else { s.push_back(a[i]); mark = 3; } } } if (mark != 3) { while (s.empty() != true) { if (s.front() == '1') { s.pop_front(); if (s.empty() != true && s.front() == '4') s.pop_front(); } else break; } } if (s.empty() != true) printf("NO\n"); else printf("YES\n"); return 0; }
### Prompt Generate a cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { char a[12]; int i, n, mark = 0; deque<char> s; scanf("%s", a); n = strlen(a); for (i = 0; i < n; i++) { if (mark == 3 || (mark == 0 && a[i] != '1')) { s.push_back(a[i]); mark = 3; continue; } if (a[i] == '1' && mark == 0) { s.push_back(a[i]); mark++; } else if (mark == 1) { if (a[i] == '4') { s.push_back(a[i]); mark++; } else if (a[i] != '1') { s.push_back(a[i]); mark = 3; } } else if (mark == 2) { if (a[i] == '1') { s.clear(); s.push_back(a[i]); mark = 1; } else if (a[i] == '4') { s.clear(); mark = 0; } else { s.push_back(a[i]); mark = 3; } } } if (mark != 3) { while (s.empty() != true) { if (s.front() == '1') { s.pop_front(); if (s.empty() != true && s.front() == '4') s.pop_front(); } else break; } } if (s.empty() != true) printf("NO\n"); else printf("YES\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; regex r("^(1|14|144)*$"); if (regex_match(s, r)) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
### Prompt Please provide a CPP coded solution to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; regex r("^(1|14|144)*$"); if (regex_match(s, r)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } ```
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; string s = to_string(n); vector<long long> v1; int flag = 0; for (int i = 0; i < s.length(); i++) { v1.push_back(s[i] - '0'); if (s[i] != '1' && s[i] != '4') { flag = 1; break; } } if (count(v1.begin(), v1.end(), 1) != v1.size()) { if (flag == 0) { int flag1 = 0; int i = 0; while (i <= v1.size() - 1) { if (v1[i] == 1 && v1[i + 1] == 1) { i++; } else if (v1[i] == 1 && v1[i + 1] == 4 && v1[i + 2] == 4) { i += 3; } else if (v1[i] == 1 && v1[i + 1] == 4 && v1[i + 2] != 4) { i += 2; } else if (v1[i] == 4) { flag1 = 1; break; } else { flag = 1; break; } } if (flag1 == 1 && i != v1.size()) { cout << "NO"; } else { cout << "YES"; } } else { cout << "NO"; } } else { cout << "YES"; } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; string s = to_string(n); vector<long long> v1; int flag = 0; for (int i = 0; i < s.length(); i++) { v1.push_back(s[i] - '0'); if (s[i] != '1' && s[i] != '4') { flag = 1; break; } } if (count(v1.begin(), v1.end(), 1) != v1.size()) { if (flag == 0) { int flag1 = 0; int i = 0; while (i <= v1.size() - 1) { if (v1[i] == 1 && v1[i + 1] == 1) { i++; } else if (v1[i] == 1 && v1[i + 1] == 4 && v1[i + 2] == 4) { i += 3; } else if (v1[i] == 1 && v1[i + 1] == 4 && v1[i + 2] != 4) { i += 2; } else if (v1[i] == 4) { flag1 = 1; break; } else { flag = 1; break; } } if (flag1 == 1 && i != v1.size()) { cout << "NO"; } else { cout << "YES"; } } else { cout << "NO"; } } else { cout << "YES"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string num; cin >> num; int satu = 0, empat = 0; bool E; E = false; for (int i = 0; i < num.length();) { if (num[i] == '1') { while (num[i] == '1') { satu++; i++; } i--; } if (num[i] == '4') { if (satu == 0) { empat = 1; break; } else { while (num[i] == '4') { empat++; i++; } if (empat > 2) { break; } else if ((empat <= 2) && (satu > 0)) { satu--; empat = 0; } i--; } } if (((num[i] != '1') && (num[i] != '4')) || (satu < 0)) { empat = 1; break; } i++; } if (empat > 0) { cout << "NO" << endl; } else { cout << "YES" << endl; } return 0; }
### Prompt In Cpp, your task is to solve the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string num; cin >> num; int satu = 0, empat = 0; bool E; E = false; for (int i = 0; i < num.length();) { if (num[i] == '1') { while (num[i] == '1') { satu++; i++; } i--; } if (num[i] == '4') { if (satu == 0) { empat = 1; break; } else { while (num[i] == '4') { empat++; i++; } if (empat > 2) { break; } else if ((empat <= 2) && (satu > 0)) { satu--; empat = 0; } i--; } } if (((num[i] != '1') && (num[i] != '4')) || (satu < 0)) { empat = 1; break; } i++; } if (empat > 0) { cout << "NO" << endl; } else { cout << "YES" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; while (n > 0) { if (n % 10 == 1) n = n / 10; else if (n % 100 == 14) n = n / 100; else if (n % 1000 == 144) n = n / 1000; else break; if (n == 0) { cout << "YES"; return 0; } } cout << "NO"; }
### Prompt Please create a solution in cpp to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; while (n > 0) { if (n % 10 == 1) n = n / 10; else if (n % 100 == 14) n = n / 100; else if (n % 1000 == 144) n = n / 1000; else break; if (n == 0) { cout << "YES"; return 0; } } cout << "NO"; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; s.reserve(15); cin >> s; int flag = 0; for (int i = 0; i < s.size();) { if (s[i] == '1' && s[i + 1] == '4' && s[i + 2] == '4') i += 3; else if (s[i] == '1' && s[i + 1] == '4') i += 2; else if (s[i] == '1') i++; else { flag = 1; break; } } if (flag) cout << "NO\n"; else cout << "YES\n"; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; s.reserve(15); cin >> s; int flag = 0; for (int i = 0; i < s.size();) { if (s[i] == '1' && s[i + 1] == '4' && s[i + 2] == '4') i += 3; else if (s[i] == '1' && s[i + 1] == '4') i += 2; else if (s[i] == '1') i++; else { flag = 1; break; } } if (flag) cout << "NO\n"; else cout << "YES\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; bool is_magical(string number) { for (int i = 0; i < (int)number.size(); i++) if (number[i] != '1' && number[i] != '4') return false; if (number[0] == '4') return false; if (number.find("444") != number.npos) return false; return true; } int main() { string number; while (cin >> number) { if (is_magical(number)) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
### Prompt Generate a CPP solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool is_magical(string number) { for (int i = 0; i < (int)number.size(); i++) if (number[i] != '1' && number[i] != '4') return false; if (number[0] == '4') return false; if (number.find("444") != number.npos) return false; return true; } int main() { string number; while (cin >> number) { if (is_magical(number)) cout << "YES" << endl; else cout << "NO" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string str; int size, i = 0; bool islucky = true; cin >> str; size = str.length(); if (str.length() == 1 && str.at(0) != '1') islucky = false; else if (str.length() == 2 && str.at(1) != '1' && str.at(1) != '4') islucky = false; else { while (i < size && islucky) { if (i + 2 < size && str.at(i) == '1' && str.at(i + 1) == '4' && str.at(i + 2) == '4') i += 3; else if (i + 1 < size && str.at(i) == '1' && str.at(i + 1) == '4') i += 2; else if (str.at(i) == '1') i++; else islucky = false; } } if (islucky) cout << "YES"; else cout << "NO"; return 0; }
### Prompt Your task is to create a cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string str; int size, i = 0; bool islucky = true; cin >> str; size = str.length(); if (str.length() == 1 && str.at(0) != '1') islucky = false; else if (str.length() == 2 && str.at(1) != '1' && str.at(1) != '4') islucky = false; else { while (i < size && islucky) { if (i + 2 < size && str.at(i) == '1' && str.at(i + 1) == '4' && str.at(i + 2) == '4') i += 3; else if (i + 1 < size && str.at(i) == '1' && str.at(i + 1) == '4') i += 2; else if (str.at(i) == '1') i++; else islucky = false; } } if (islucky) cout << "YES"; else cout << "NO"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { char ch[1000000]; cin >> ch; int len = strlen(ch); int i, cnt = 0; for (i = 0; i < len; i++) { if (ch[i] != '1' && ch[i] != '4') { cout << "NO"; cnt++; break; } else if (ch[0] != '1') { cout << "NO"; cnt++; break; } else if (ch[i] == '4' && ch[i + 1] == '4' && ch[i + 2] == '4') { cout << "NO"; cnt++; break; } } if (cnt == 0) cout << "YES" << endl; }
### Prompt In cpp, your task is to solve the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { char ch[1000000]; cin >> ch; int len = strlen(ch); int i, cnt = 0; for (i = 0; i < len; i++) { if (ch[i] != '1' && ch[i] != '4') { cout << "NO"; cnt++; break; } else if (ch[0] != '1') { cout << "NO"; cnt++; break; } else if (ch[i] == '4' && ch[i + 1] == '4' && ch[i + 2] == '4') { cout << "NO"; cnt++; break; } } if (cnt == 0) cout << "YES" << endl; } ```
#include <bits/stdc++.h> using namespace std; int i; string second; int main() { getline(cin, second); for (i = 0; i < second.length(); i++) if (second[i] == '1') { if (i + 1 < second.length()) if (second[i + 1] == '4') { i++; if (i + 1 < second.length()) if (second[i + 1] == '4') i++; } } else { cout << "NO"; return 0; } cout << "YES"; return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int i; string second; int main() { getline(cin, second); for (i = 0; i < second.length(); i++) if (second[i] == '1') { if (i + 1 < second.length()) if (second[i + 1] == '4') { i++; if (i + 1 < second.length()) if (second[i + 1] == '4') i++; } } else { cout << "NO"; return 0; } cout << "YES"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long a, c, d, f; cin >> a; while (a > 0) { if (!((a % 10 == 1) || (a % 100) == 14 || (a % 1000) == 144)) { cout << "NO"; return 0; } a = a / 10; } cout << "YES"; return 0; }
### Prompt Generate a Cpp solution to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long a, c, d, f; cin >> a; while (a > 0) { if (!((a % 10 == 1) || (a % 100) == 14 || (a % 1000) == 144)) { cout << "NO"; return 0; } a = a / 10; } cout << "YES"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; getline(cin, s); int l = s.length(); int i = 0; for (; i < l;) { if (s[i] != '1') break; else { int step = 1; if (s[i + 1] == '4') { step++; if (s[i + 2] == '4') step++; } i += step; } } if (i == l) cout << "YES"; else cout << "NO"; return 0; }
### Prompt Please create a solution in CPP to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; getline(cin, s); int l = s.length(); int i = 0; for (; i < l;) { if (s[i] != '1') break; else { int step = 1; if (s[i + 1] == '4') { step++; if (s[i + 2] == '4') step++; } i += step; } } if (i == l) cout << "YES"; else cout << "NO"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long int n, l; l = s.size(); for (long long int i = 0; i < l; i++) { if ((s[i] != '1' && s[i] != '4') || s[0] == '4') { cout << "NO" << endl; return 0; } else if (s[i] == '4' && s[i + 1] == '4' && s[i + 2] == '4') { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
### Prompt Develop a solution in Cpp to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long int n, l; l = s.size(); for (long long int i = 0; i < l; i++) { if ((s[i] != '1' && s[i] != '4') || s[0] == '4') { cout << "NO" << endl; return 0; } else if (s[i] == '4' && s[i + 1] == '4' && s[i + 2] == '4') { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string x; getline(cin, x); unsigned int i, n = x.size(); char k = 0; string flag = "NO"; for (i = 0; i < n; i += k) { k = 0; if (x[i] == '1') { flag = "YES"; ++k; if (x[i + 1] == '4' && (i + 1) < n) { flag = "YES"; ++k; if (x[i + 2] == '4' && (i + 1) < n) { flag = "YES"; ++k; } else if (x[i + 2] != '1' && (i + 2) < n) { flag = "NO"; cout << flag << endl; return 0; } } else if (x[i + 1] != '1' && (i + 1) < n) { flag = "NO"; cout << flag << endl; return 0; } } else { flag = "NO"; cout << flag << endl; return 0; } } cout << flag << endl; return 0; }
### Prompt Create a solution in cpp for the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string x; getline(cin, x); unsigned int i, n = x.size(); char k = 0; string flag = "NO"; for (i = 0; i < n; i += k) { k = 0; if (x[i] == '1') { flag = "YES"; ++k; if (x[i + 1] == '4' && (i + 1) < n) { flag = "YES"; ++k; if (x[i + 2] == '4' && (i + 1) < n) { flag = "YES"; ++k; } else if (x[i + 2] != '1' && (i + 2) < n) { flag = "NO"; cout << flag << endl; return 0; } } else if (x[i + 1] != '1' && (i + 1) < n) { flag = "NO"; cout << flag << endl; return 0; } } else { flag = "NO"; cout << flag << endl; return 0; } } cout << flag << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { char n[1000]; cin >> n; int count = 0; int notmagical = 0; if (n[0] != '1') { cout << "NO"; return 0; } for (int i = 0; i < strlen(n); i++) { if (n[i] == '1') { count = 0; } else if (n[i] == '4' && count < 2) { count++; } else { notmagical = 1; break; } } if (notmagical == 1) cout << "NO"; else cout << "YES"; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { char n[1000]; cin >> n; int count = 0; int notmagical = 0; if (n[0] != '1') { cout << "NO"; return 0; } for (int i = 0; i < strlen(n); i++) { if (n[i] == '1') { count = 0; } else if (n[i] == '4' && count < 2) { count++; } else { notmagical = 1; break; } } if (notmagical == 1) cout << "NO"; else cout << "YES"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k = 0; cin >> n; while (n > 0) { if ((n % 10) == 1) { n = n / 10; k = 1; } else if ((n % 100) == 14) { n = n / 100; k = 1; } else if ((n % 1000) == 144) { n = n / 1000; k = 1; } else { k = 0; break; } } if (k == 1) cout << "YES"; else cout << "NO"; return 0; }
### Prompt Please create a solution in Cpp to the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { long long int n, k = 0; cin >> n; while (n > 0) { if ((n % 10) == 1) { n = n / 10; k = 1; } else if ((n % 100) == 14) { n = n / 100; k = 1; } else if ((n % 1000) == 144) { n = n / 1000; k = 1; } else { k = 0; break; } } if (k == 1) cout << "YES"; else cout << "NO"; return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); while (n) { if (n % 1000 == 144) { n /= 1000; continue; } if (n % 100 == 14) { n /= 100; continue; } if (n % 10 == 1) { n /= 10; continue; } printf("NO\n"); return 0; } printf("YES\n"); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); while (n) { if (n % 1000 == 144) { n /= 1000; continue; } if (n % 100 == 14) { n /= 100; continue; } if (n % 10 == 1) { n /= 10; continue; } printf("NO\n"); return 0; } printf("YES\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { char s[105]; cin >> s; for (int i = 0; s[i]; i++) if (s[i] != '1' && s[i] != '4') { cout << "NO" << endl; return 0; } for (int i = 0; s[i]; i++) { if (s[i] == '4') { cout << "NO" << endl; return 0; } else if (s[i + 1] == '1') ; else if (s[i + 2] == '1') i++; else i += 2; } cout << "YES" << endl; return 0; }
### Prompt In Cpp, your task is to solve the following problem: 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 Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { char s[105]; cin >> s; for (int i = 0; s[i]; i++) if (s[i] != '1' && s[i] != '4') { cout << "NO" << endl; return 0; } for (int i = 0; s[i]; i++) { if (s[i] == '4') { cout << "NO" << endl; return 0; } else if (s[i + 1] == '1') ; else if (s[i + 2] == '1') i++; else i += 2; } cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 190001; const int MAXM = 11111; const int INF = 0x3f3f3f3f; const long long mod = 1000000007; int n, m, l, r; int a[MAXN]; int b[MAXN]; int c[MAXN]; char s[MAXN]; int main() { cin >> s + 1; n = strlen(s + 1); memset(a, 0, sizeof(a)), memset(b, 0, sizeof(b)), memset(c, 0, sizeof(c)); for (int i = 1; i <= n; i++) { if (s[i] == 'x') a[i] = 1; if (s[i] == 'y') b[i] = 1; if (s[i] == 'z') c[i] = 1; } for (int i = 1; i <= n; i++) a[i] = a[i] + a[i - 1], b[i] = b[i] + b[i - 1], c[i] = c[i] + c[i - 1]; cin >> m; for (int i = 0; i < m; i++) { scanf("%d%d", &l, &r); if (r - l + 1 <= 2) { printf("YES\n"); continue; } int x = a[r] - a[l - 1]; int y = b[r] - b[l - 1]; int z = c[r] - c[l - 1]; int minn = min(x, min(y, z)); int maxx = max(x, max(y, z)); if (maxx - minn <= 1) puts("YES"); else puts("NO"); } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 190001; const int MAXM = 11111; const int INF = 0x3f3f3f3f; const long long mod = 1000000007; int n, m, l, r; int a[MAXN]; int b[MAXN]; int c[MAXN]; char s[MAXN]; int main() { cin >> s + 1; n = strlen(s + 1); memset(a, 0, sizeof(a)), memset(b, 0, sizeof(b)), memset(c, 0, sizeof(c)); for (int i = 1; i <= n; i++) { if (s[i] == 'x') a[i] = 1; if (s[i] == 'y') b[i] = 1; if (s[i] == 'z') c[i] = 1; } for (int i = 1; i <= n; i++) a[i] = a[i] + a[i - 1], b[i] = b[i] + b[i - 1], c[i] = c[i] + c[i - 1]; cin >> m; for (int i = 0; i < m; i++) { scanf("%d%d", &l, &r); if (r - l + 1 <= 2) { printf("YES\n"); continue; } int x = a[r] - a[l - 1]; int y = b[r] - b[l - 1]; int z = c[r] - c[l - 1]; int minn = min(x, min(y, z)); int maxx = max(x, max(y, z)); if (maxx - minn <= 1) puts("YES"); else puts("NO"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline long long minOf(long long x, long long y) { return (x < y ? x : y); } inline long long maxOf(long long x, long long y) { return (x > y ? x : y); } inline long long mabs(long long x) { if (x < 0) return -x; return x; } int main() { ios_base::sync_with_stdio(false); string s; cin >> s; int xc[100005], yc[100005], zc[100005]; for (int i = 0; i < s.length(); i++) { xc[i] = 0; yc[i] = 0; zc[i] = 0; } for (int i = 0; i < s.length(); i++) { if (i > 0) { xc[i] = xc[i - 1]; yc[i] = yc[i - 1]; zc[i] = zc[i - 1]; } if (s[i] == 'x') xc[i]++; else if (s[i] == 'y') yc[i]++; else zc[i]++; } int m; cin >> m; int l, r; int x, y, z; int dxy, dxz, dyz; for (int i = 0; i < m; i++) { cin >> l >> r; l--; r--; if (r - l < 2) { cout << "YES" << endl; continue; } x = xc[r] - (l == 0 ? 0 : xc[l - 1]); y = yc[r] - (l == 0 ? 0 : yc[l - 1]); z = zc[r] - (l == 0 ? 0 : zc[l - 1]); dxy = int(mabs(x - y)); dxz = int(mabs(x - z)); dyz = int(mabs(y - z)); if (dxy > 1 || dxz > 1 || dyz > 1) cout << "NO" << endl; else cout << "YES" << endl; } return 0; }
### Prompt In cpp, your task is to solve the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline long long minOf(long long x, long long y) { return (x < y ? x : y); } inline long long maxOf(long long x, long long y) { return (x > y ? x : y); } inline long long mabs(long long x) { if (x < 0) return -x; return x; } int main() { ios_base::sync_with_stdio(false); string s; cin >> s; int xc[100005], yc[100005], zc[100005]; for (int i = 0; i < s.length(); i++) { xc[i] = 0; yc[i] = 0; zc[i] = 0; } for (int i = 0; i < s.length(); i++) { if (i > 0) { xc[i] = xc[i - 1]; yc[i] = yc[i - 1]; zc[i] = zc[i - 1]; } if (s[i] == 'x') xc[i]++; else if (s[i] == 'y') yc[i]++; else zc[i]++; } int m; cin >> m; int l, r; int x, y, z; int dxy, dxz, dyz; for (int i = 0; i < m; i++) { cin >> l >> r; l--; r--; if (r - l < 2) { cout << "YES" << endl; continue; } x = xc[r] - (l == 0 ? 0 : xc[l - 1]); y = yc[r] - (l == 0 ? 0 : yc[l - 1]); z = zc[r] - (l == 0 ? 0 : zc[l - 1]); dxy = int(mabs(x - y)); dxz = int(mabs(x - z)); dyz = int(mabs(y - z)); if (dxy > 1 || dxz > 1 || dyz > 1) cout << "NO" << endl; else cout << "YES" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int fs[100000 + 1][3] = {}; for (int i = 0; i < s.length(); i++) { fs[i + 1][0] = fs[i][0]; fs[i + 1][1] = fs[i][1]; fs[i + 1][2] = fs[i][2]; if (s[i] == 'x') fs[i + 1][0]++; if (s[i] == 'y') fs[i + 1][1]++; if (s[i] == 'z') fs[i + 1][2]++; } int m; cin >> m; int l, r; for (int i = 0; i < m; i++) { cin >> l >> r; if (r - l < 2) cout << "YES" << endl; else { int c[3] = {fs[r][0] - fs[l - 1][0], fs[r][1] - fs[l - 1][1], fs[r][2] - fs[l - 1][2]}; int ma = 0, mn = 100000; for (int j = 0; j < 3; j++) { ma = (ma > c[j] ? ma : c[j]); mn = (mn < c[j] ? mn : c[j]); } if (ma == mn || ma == mn + 1) cout << "YES" << endl; else cout << "NO" << endl; } } return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int fs[100000 + 1][3] = {}; for (int i = 0; i < s.length(); i++) { fs[i + 1][0] = fs[i][0]; fs[i + 1][1] = fs[i][1]; fs[i + 1][2] = fs[i][2]; if (s[i] == 'x') fs[i + 1][0]++; if (s[i] == 'y') fs[i + 1][1]++; if (s[i] == 'z') fs[i + 1][2]++; } int m; cin >> m; int l, r; for (int i = 0; i < m; i++) { cin >> l >> r; if (r - l < 2) cout << "YES" << endl; else { int c[3] = {fs[r][0] - fs[l - 1][0], fs[r][1] - fs[l - 1][1], fs[r][2] - fs[l - 1][2]}; int ma = 0, mn = 100000; for (int j = 0; j < 3; j++) { ma = (ma > c[j] ? ma : c[j]); mn = (mn < c[j] ? mn : c[j]); } if (ma == mn || ma == mn + 1) cout << "YES" << endl; else cout << "NO" << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; char s[200 * 1000]; int z[1000 * 200], y[1000 * 200], x[1000 * 200]; int fls[200 * 1000]; int main() { cin >> s; int n; for (int i = 1; s[i - 1]; ++i) { z[i] = z[i - 1] + (s[i - 1] == 'z'); x[i] = x[i - 1] + (s[i - 1] == 'x'); y[i] = y[i - 1] + (s[i - 1] == 'y'); if (i >= 3) { int bl[3] = {}; for (int j = i - 3; j < i; ++j) bl[s[j] - 'x'] = 1; fls[i] = !(bl[0] && bl[1] && bl[2]); } fls[i] += fls[i - 1]; n = i; } int m; cin >> m; int L, R; int ss[3]; for (int i = (0), e_i = (m); i < e_i; ++i) { cin >> L >> R; ss[0] = z[R] - z[L - 1], ss[1] = y[R] - y[L - 1], ss[2] = x[R] - x[L - 1]; sort(ss, ss + 3); bool ok = ss[2] == ss[0] || (ss[2] == ss[1] && ss[1] == ss[0] + 1) || (ss[2] == ss[0] + 1 && ss[1] == ss[0]); if (R - L > 1 && !ok) cout << "NO" << endl; else cout << "YES" << endl; } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char s[200 * 1000]; int z[1000 * 200], y[1000 * 200], x[1000 * 200]; int fls[200 * 1000]; int main() { cin >> s; int n; for (int i = 1; s[i - 1]; ++i) { z[i] = z[i - 1] + (s[i - 1] == 'z'); x[i] = x[i - 1] + (s[i - 1] == 'x'); y[i] = y[i - 1] + (s[i - 1] == 'y'); if (i >= 3) { int bl[3] = {}; for (int j = i - 3; j < i; ++j) bl[s[j] - 'x'] = 1; fls[i] = !(bl[0] && bl[1] && bl[2]); } fls[i] += fls[i - 1]; n = i; } int m; cin >> m; int L, R; int ss[3]; for (int i = (0), e_i = (m); i < e_i; ++i) { cin >> L >> R; ss[0] = z[R] - z[L - 1], ss[1] = y[R] - y[L - 1], ss[2] = x[R] - x[L - 1]; sort(ss, ss + 3); bool ok = ss[2] == ss[0] || (ss[2] == ss[1] && ss[1] == ss[0] + 1) || (ss[2] == ss[0] + 1 && ss[1] == ss[0]); if (R - L > 1 && !ok) cout << "NO" << endl; else cout << "YES" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int *arra = new int[s.length() + 1]; int *arrb = new int[s.length() + 1]; int *arrc = new int[s.length() + 1]; arra[0] = 0; arrb[0] = 0; arrc[0] = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == 'x') { arra[i + 1] = arra[i] + 1; arrb[i + 1] = arrb[i]; arrc[i + 1] = arrc[i]; } else if (s[i] == 'y') { arra[i + 1] = arra[i]; arrb[i + 1] = arrb[i] + 1; arrc[i + 1] = arrc[i]; } else { arra[i + 1] = arra[i]; arrb[i + 1] = arrb[i]; arrc[i + 1] = arrc[i] + 1; } } int m; cin >> m; int t1, t2; while (m--) { cin >> t1 >> t2; if (t2 - t1 <= 1) { cout << "YES" << endl; continue; } int a = arra[t2] - arra[t1 - 1]; int b = arrb[t2] - arrb[t1 - 1]; int c = arrc[t2] - arrc[t1 - 1]; if (abs(a - b) <= 1 && abs(a - c) <= 1 && abs(b - c) <= 1) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int *arra = new int[s.length() + 1]; int *arrb = new int[s.length() + 1]; int *arrc = new int[s.length() + 1]; arra[0] = 0; arrb[0] = 0; arrc[0] = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == 'x') { arra[i + 1] = arra[i] + 1; arrb[i + 1] = arrb[i]; arrc[i + 1] = arrc[i]; } else if (s[i] == 'y') { arra[i + 1] = arra[i]; arrb[i + 1] = arrb[i] + 1; arrc[i + 1] = arrc[i]; } else { arra[i + 1] = arra[i]; arrb[i + 1] = arrb[i]; arrc[i + 1] = arrc[i] + 1; } } int m; cin >> m; int t1, t2; while (m--) { cin >> t1 >> t2; if (t2 - t1 <= 1) { cout << "YES" << endl; continue; } int a = arra[t2] - arra[t1 - 1]; int b = arrb[t2] - arrb[t1 - 1]; int c = arrc[t2] - arrc[t1 - 1]; if (abs(a - b) <= 1 && abs(a - c) <= 1 && abs(b - c) <= 1) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; char inn[100005] = {0}; int xs[100005] = {0}; int zs[100005] = {0}; int ys[100005] = {0}; int main() { scanf("%s ", inn); int n; scanf("%d ", &n); int Xs = 0; int Zs = 0; int Ys = 0; for (int i = 0; inn[i]; i++) { if (inn[i] == 'x') Xs++; if (inn[i] == 'z') Zs++; if (inn[i] == 'y') Ys++; xs[i + 1] = Xs; ys[i + 1] = Ys; zs[i + 1] = Zs; } for (int i = 0; i < n; i++) { int a, b; scanf("%d %d ", &a, &b); int cx = xs[b] - xs[a - 1]; int cy = ys[b] - ys[a - 1]; int cz = zs[b] - zs[a - 1]; if ((b - a + 1) < 3) { printf("YES\n"); continue; } vector<int> mas; mas.push_back(cx); mas.push_back(cy); mas.push_back(cz); sort(mas.begin(), mas.end()); bool res = false; if ((mas[0] == mas[1]) && (mas[2] == mas[1])) res = true; if (((mas[1] - mas[0]) == 1) && (mas[2] == mas[1])) res = true; if (((mas[2] - mas[1]) == 1) && (mas[1] == mas[0])) res = true; if (res) printf("YES\n"); else printf("NO\n"); } return 0; }
### Prompt Generate a Cpp solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; char inn[100005] = {0}; int xs[100005] = {0}; int zs[100005] = {0}; int ys[100005] = {0}; int main() { scanf("%s ", inn); int n; scanf("%d ", &n); int Xs = 0; int Zs = 0; int Ys = 0; for (int i = 0; inn[i]; i++) { if (inn[i] == 'x') Xs++; if (inn[i] == 'z') Zs++; if (inn[i] == 'y') Ys++; xs[i + 1] = Xs; ys[i + 1] = Ys; zs[i + 1] = Zs; } for (int i = 0; i < n; i++) { int a, b; scanf("%d %d ", &a, &b); int cx = xs[b] - xs[a - 1]; int cy = ys[b] - ys[a - 1]; int cz = zs[b] - zs[a - 1]; if ((b - a + 1) < 3) { printf("YES\n"); continue; } vector<int> mas; mas.push_back(cx); mas.push_back(cy); mas.push_back(cz); sort(mas.begin(), mas.end()); bool res = false; if ((mas[0] == mas[1]) && (mas[2] == mas[1])) res = true; if (((mas[1] - mas[0]) == 1) && (mas[2] == mas[1])) res = true; if (((mas[2] - mas[1]) == 1) && (mas[1] == mas[0])) res = true; if (res) printf("YES\n"); else printf("NO\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long gcd(long long x, long long y) { if (y == 0) return x; return gcd(y, x % y); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } struct letters { int x = 0, y = 0, z = 0; }; int main() { char arr[100005]; scanf("%s", arr); letters cum[100005]; cum[0].x = 0, cum[0].y = 0, cum[0].z = 0; for (int i = 0; arr[i] != '\0'; i++) { if (arr[i] == 'x') cum[i + 1].x = 1; else if (arr[i] == 'y') cum[i + 1].y = 1; else cum[i + 1].z = 1; cum[i + 1].x += cum[i].x; cum[i + 1].y += cum[i].y; cum[i + 1].z += cum[i].z; } int l, r, t; scanf("%d", &t); while (t--) { scanf("%d%d", &l, &r); int x = cum[r].x - cum[l - 1].x; int y = cum[r].y - cum[l - 1].y; int z = cum[r].z - cum[l - 1].z; int mn = min(x, min(y, z)); x -= mn; z -= mn; y -= mn; if ((r - l) > 1 && (x > 1 || y > 1 || z > 1)) printf("NO\n"); else printf("YES\n"); } return 0; }
### Prompt Please create a solution in Cpp to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long gcd(long long x, long long y) { if (y == 0) return x; return gcd(y, x % y); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } struct letters { int x = 0, y = 0, z = 0; }; int main() { char arr[100005]; scanf("%s", arr); letters cum[100005]; cum[0].x = 0, cum[0].y = 0, cum[0].z = 0; for (int i = 0; arr[i] != '\0'; i++) { if (arr[i] == 'x') cum[i + 1].x = 1; else if (arr[i] == 'y') cum[i + 1].y = 1; else cum[i + 1].z = 1; cum[i + 1].x += cum[i].x; cum[i + 1].y += cum[i].y; cum[i + 1].z += cum[i].z; } int l, r, t; scanf("%d", &t); while (t--) { scanf("%d%d", &l, &r); int x = cum[r].x - cum[l - 1].x; int y = cum[r].y - cum[l - 1].y; int z = cum[r].z - cum[l - 1].z; int mn = min(x, min(y, z)); x -= mn; z -= mn; y -= mn; if ((r - l) > 1 && (x > 1 || y > 1 || z > 1)) printf("NO\n"); else printf("YES\n"); } return 0; } ```
#include <bits/stdc++.h> char s[100012]; int m, l, r; int x[100010], y[100010], z[100010]; using namespace std; int main() { scanf("%s%*c", s); scanf("%d%*c", &m); x[0] = y[0] = z[0] = 0; for (int i = 0; s[i] != '\0'; i++) { x[i + 1] = x[i]; y[i + 1] = y[i]; z[i + 1] = z[i]; if (s[i] == 'x') { x[i + 1]++; } else if (s[i] == 'y') { y[i + 1]++; } else { z[i + 1]++; } } for (int i = 0; i < m; i++) { scanf("%d %d%*c", &l, &r); int maior, dx, dy, dz; dx = x[r] - x[l - 1]; dy = y[r] - y[l - 1]; dz = z[r] - z[l - 1]; maior = max(max(dx, dy), dz); dx = maior - dx; dy = maior - dy; dz = maior - dz; if ((r - (l - 1)) < 3) { printf("YES\n"); } else if (dx > 1 || dy > 1 || dz > 1) { printf("NO\n"); } else { printf("YES\n"); } } }
### Prompt Your challenge is to write a Cpp solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> char s[100012]; int m, l, r; int x[100010], y[100010], z[100010]; using namespace std; int main() { scanf("%s%*c", s); scanf("%d%*c", &m); x[0] = y[0] = z[0] = 0; for (int i = 0; s[i] != '\0'; i++) { x[i + 1] = x[i]; y[i + 1] = y[i]; z[i + 1] = z[i]; if (s[i] == 'x') { x[i + 1]++; } else if (s[i] == 'y') { y[i + 1]++; } else { z[i + 1]++; } } for (int i = 0; i < m; i++) { scanf("%d %d%*c", &l, &r); int maior, dx, dy, dz; dx = x[r] - x[l - 1]; dy = y[r] - y[l - 1]; dz = z[r] - z[l - 1]; maior = max(max(dx, dy), dz); dx = maior - dx; dy = maior - dy; dz = maior - dz; if ((r - (l - 1)) < 3) { printf("YES\n"); } else if (dx > 1 || dy > 1 || dz > 1) { printf("NO\n"); } else { printf("YES\n"); } } } ```
#include <bits/stdc++.h> using namespace std; string s; int x[100009], y[100009], z[100009], m, l, r; int main() { cin >> s; cin >> m; for (int i = 1; i <= s.size(); i++) { x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1]; if (s[i - 1] == 'x') x[i]++; if (s[i - 1] == 'y') y[i]++; if (s[i - 1] == 'z') z[i]++; } for (int i = 1; i <= m; i++) { cin >> l >> r; if (min(x[r] - x[l - 1], min(y[r] - y[l - 1], z[r] - z[l - 1])) + 1 >= max(x[r] - x[l - 1], max(y[r] - y[l - 1], z[r] - z[l - 1]))) { cout << "YES\n"; continue; } if (r - l + 1 <= 2) { cout << "YES\n"; continue; } cout << "NO\n"; } }
### Prompt Generate a cpp solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s; int x[100009], y[100009], z[100009], m, l, r; int main() { cin >> s; cin >> m; for (int i = 1; i <= s.size(); i++) { x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1]; if (s[i - 1] == 'x') x[i]++; if (s[i - 1] == 'y') y[i]++; if (s[i - 1] == 'z') z[i]++; } for (int i = 1; i <= m; i++) { cin >> l >> r; if (min(x[r] - x[l - 1], min(y[r] - y[l - 1], z[r] - z[l - 1])) + 1 >= max(x[r] - x[l - 1], max(y[r] - y[l - 1], z[r] - z[l - 1]))) { cout << "YES\n"; continue; } if (r - l + 1 <= 2) { cout << "YES\n"; continue; } cout << "NO\n"; } } ```
#include <bits/stdc++.h> const int mx = 1e5 + 10; using namespace std; char c; string s; int x[mx], y[mx], z[mx], n, a, b, k; int main() { x[0] = y[0] = z[0] = 0; cin >> s; for (int i = 0; i < (int)s.size(); i++) { c = s[i]; k = i + 1; x[k] = x[k - 1]; y[k] = y[k - 1]; z[k] = z[k - 1]; if (c == 'x') x[k]++; else if (c == 'y') y[k]++; else z[k]++; } scanf("%d", &n); while (n--) { scanf("%d%d", &a, &b); int nx = x[b] - x[a - 1], ny = y[b] - y[a - 1], nz = z[b] - z[a - 1]; if (nz == ny && nx == ny) printf("YES\n"); else { if (b - a < 2 || (ny + 1 == nx && (nz == ny || nx == nz)) || (nz + 1 == ny && (nx == ny || nx == nz)) || (nx + 1 == nz && (nx == ny || ny == nz))) printf("YES\n"); else printf("NO\n"); } } return 0; }
### Prompt Create a solution in Cpp for the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> const int mx = 1e5 + 10; using namespace std; char c; string s; int x[mx], y[mx], z[mx], n, a, b, k; int main() { x[0] = y[0] = z[0] = 0; cin >> s; for (int i = 0; i < (int)s.size(); i++) { c = s[i]; k = i + 1; x[k] = x[k - 1]; y[k] = y[k - 1]; z[k] = z[k - 1]; if (c == 'x') x[k]++; else if (c == 'y') y[k]++; else z[k]++; } scanf("%d", &n); while (n--) { scanf("%d%d", &a, &b); int nx = x[b] - x[a - 1], ny = y[b] - y[a - 1], nz = z[b] - z[a - 1]; if (nz == ny && nx == ny) printf("YES\n"); else { if (b - a < 2 || (ny + 1 == nx && (nz == ny || nx == nz)) || (nz + 1 == ny && (nx == ny || nx == nz)) || (nx + 1 == nz && (nx == ny || ny == nz))) printf("YES\n"); else printf("NO\n"); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; string s; int x[100005]; int y[100005]; int z[100005]; int main() { ios_base::sync_with_stdio(0); ; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == 'x') { x[i + 1]++; } else if (s[i] == 'y') { y[i + 1]++; } else { z[i + 1]++; } } for (int i = 1; i <= s.size(); i++) { x[i] = x[i] + x[i - 1]; y[i] = y[i] + y[i - 1]; z[i] = z[i] + z[i - 1]; } int q, l, r; cin >> q; while (q--) { cin >> l >> r; if (r - l + 1 <= 2) { cout << "YES\n"; continue; } int xx = x[r] - x[l - 1]; int yy = y[r] - y[l - 1]; int zz = z[r] - z[l - 1]; vector<int> v; v.push_back(xx); v.push_back(yy); v.push_back(zz); sort((v).begin(), (v).end()); int ok = 0; if (v[0] == v[1] && v[1] == v[2]) { ok = 1; } if (v[0] == v[1] && ((v[1] + 1) == (v[2]))) { ok = 1; } if (v[1] == v[2] && ((v[1] - 1) == v[0])) { ok = 1; } if (ok) cout << "YES\n"; else cout << "NO\n"; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; string s; int x[100005]; int y[100005]; int z[100005]; int main() { ios_base::sync_with_stdio(0); ; cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == 'x') { x[i + 1]++; } else if (s[i] == 'y') { y[i + 1]++; } else { z[i + 1]++; } } for (int i = 1; i <= s.size(); i++) { x[i] = x[i] + x[i - 1]; y[i] = y[i] + y[i - 1]; z[i] = z[i] + z[i - 1]; } int q, l, r; cin >> q; while (q--) { cin >> l >> r; if (r - l + 1 <= 2) { cout << "YES\n"; continue; } int xx = x[r] - x[l - 1]; int yy = y[r] - y[l - 1]; int zz = z[r] - z[l - 1]; vector<int> v; v.push_back(xx); v.push_back(yy); v.push_back(zz); sort((v).begin(), (v).end()); int ok = 0; if (v[0] == v[1] && v[1] == v[2]) { ok = 1; } if (v[0] == v[1] && ((v[1] + 1) == (v[2]))) { ok = 1; } if (v[1] == v[2] && ((v[1] - 1) == v[0])) { ok = 1; } if (ok) cout << "YES\n"; else cout << "NO\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m, x, y, z, p, q, r; int ara[5]; string str; int csum[4][200000]; int main() { cin >> str; str = "?" + str; for (int i = 1; i < str.size(); i++) { int ch = str[i] - 'x'; csum[ch][i]++; for (int j = 0; j < 3; j++) csum[j][i] += csum[j][i - 1]; } cin >> q; while (q--) { scanf("%d %d", &x, &y); for (int i = 0; i < 3; i++) ara[i] = csum[i][y] - csum[i][x - 1]; sort(ara, ara + 3); reverse(ara, ara + 3); if (ara[0] - ara[2] <= 1 || y - x <= 1) { printf("YES\n"); } else printf("NO\n"); } return 0; }
### Prompt In cpp, your task is to solve the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m, x, y, z, p, q, r; int ara[5]; string str; int csum[4][200000]; int main() { cin >> str; str = "?" + str; for (int i = 1; i < str.size(); i++) { int ch = str[i] - 'x'; csum[ch][i]++; for (int j = 0; j < 3; j++) csum[j][i] += csum[j][i - 1]; } cin >> q; while (q--) { scanf("%d %d", &x, &y); for (int i = 0; i < 3; i++) ara[i] = csum[i][y] - csum[i][x - 1]; sort(ara, ara + 3); reverse(ara, ara + 3); if (ara[0] - ara[2] <= 1 || y - x <= 1) { printf("YES\n"); } else printf("NO\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct element { int x; int y; int z; }; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); string s; cin >> s; element arr[100010]; int x = 0, y = 0, z = 0; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'x') { x++; } else if (s[i] == 'y') { y++; } else { z++; } arr[i + 1].x = x; arr[i + 1].y = y; arr[i + 1].z = z; } int n; cin >> n; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; if (b - a <= 1) { cout << "YES" << endl; continue; } x = arr[b].x - arr[a - 1].x; y = arr[b].y - arr[a - 1].y; z = arr[b].z - arr[a - 1].z; if ((x == y && y == z && x == z) || (x == y && abs(z - x) == 1) || (x == z && abs(y - x) == 1) || (y == z && abs(x - z) == 1)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }
### Prompt Develop a solution in CPP to the problem described below: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct element { int x; int y; int z; }; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); string s; cin >> s; element arr[100010]; int x = 0, y = 0, z = 0; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'x') { x++; } else if (s[i] == 'y') { y++; } else { z++; } arr[i + 1].x = x; arr[i + 1].y = y; arr[i + 1].z = z; } int n; cin >> n; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; if (b - a <= 1) { cout << "YES" << endl; continue; } x = arr[b].x - arr[a - 1].x; y = arr[b].y - arr[a - 1].y; z = arr[b].z - arr[a - 1].z; if ((x == y && y == z && x == z) || (x == y && abs(z - x) == 1) || (x == z && abs(y - x) == 1) || (y == z && abs(x - z) == 1)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; void solve(istream &in, ostream &out) { string str; in >> str; int n = str.size(); vector<int> numX(n + 1), numY(n + 1), numZ(n + 1); for (int i = 0; i < n; i++) { numX[i + 1] = numX[i] + (str[i] == 'x'); numY[i + 1] = numY[i] + (str[i] == 'y'); numZ[i + 1] = numZ[i] + (str[i] == 'z'); } int m; in >> m; while (m--) { int l, r; in >> l >> r; if (r - l <= 1) { out << "YES" << endl; continue; } vector<int> s(3); s[0] = numX[r] - numX[l - 1]; s[1] = numY[r] - numY[l - 1]; s[2] = numZ[r] - numZ[l - 1]; if (abs(s[0] - s[1]) <= 1 && abs(s[0] - s[2]) <= 1 && abs(s[1] - s[2]) <= 1) { out << "YES" << endl; continue; } out << "NO" << endl; } } int main() { solve(cin, cout); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void solve(istream &in, ostream &out) { string str; in >> str; int n = str.size(); vector<int> numX(n + 1), numY(n + 1), numZ(n + 1); for (int i = 0; i < n; i++) { numX[i + 1] = numX[i] + (str[i] == 'x'); numY[i + 1] = numY[i] + (str[i] == 'y'); numZ[i + 1] = numZ[i] + (str[i] == 'z'); } int m; in >> m; while (m--) { int l, r; in >> l >> r; if (r - l <= 1) { out << "YES" << endl; continue; } vector<int> s(3); s[0] = numX[r] - numX[l - 1]; s[1] = numY[r] - numY[l - 1]; s[2] = numZ[r] - numZ[l - 1]; if (abs(s[0] - s[1]) <= 1 && abs(s[0] - s[2]) <= 1 && abs(s[1] - s[2]) <= 1) { out << "YES" << endl; continue; } out << "NO" << endl; } } int main() { solve(cin, cout); return 0; } ```
#include <bits/stdc++.h> char str[100005]; int a[100005], b[100005], c[100005]; int main() { int m, i, len, x, y, xx, yy, zz; scanf("%s", str); len = strlen(str); for (i = 0; i < len; i++) { if (str[i] == 'x') { a[i + 1] = a[i] + 1; b[i + 1] = b[i]; c[i + 1] = c[i]; } if (str[i] == 'y') { a[i + 1] = a[i]; b[i + 1] = b[i] + 1; c[i + 1] = c[i]; } if (str[i] == 'z') { a[i + 1] = a[i]; b[i + 1] = b[i]; c[i + 1] = c[i] + 1; } } scanf("%d", &m); while (m--) { scanf("%d%d", &x, &y); if (y - x < 2) { printf("YES\n"); continue; } xx = a[y] - a[x - 1]; yy = b[y] - b[x - 1]; zz = c[y] - c[x - 1]; if (xx == yy && yy == zz) { printf("YES\n"); continue; } if (xx == yy && zz - yy == 1) { printf("YES\n"); continue; } if (yy == zz && zz - xx == 1) { printf("YES\n"); continue; } if (yy == zz && xx - yy == 1) { printf("YES\n"); continue; } if (xx == zz && xx - yy == 1) { printf("YES\n"); continue; } if (xx == zz && yy - xx == 1) { printf("YES\n"); continue; } if (xx == yy && xx - zz == 1) { printf("YES\n"); continue; } printf("NO\n"); } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> char str[100005]; int a[100005], b[100005], c[100005]; int main() { int m, i, len, x, y, xx, yy, zz; scanf("%s", str); len = strlen(str); for (i = 0; i < len; i++) { if (str[i] == 'x') { a[i + 1] = a[i] + 1; b[i + 1] = b[i]; c[i + 1] = c[i]; } if (str[i] == 'y') { a[i + 1] = a[i]; b[i + 1] = b[i] + 1; c[i + 1] = c[i]; } if (str[i] == 'z') { a[i + 1] = a[i]; b[i + 1] = b[i]; c[i + 1] = c[i] + 1; } } scanf("%d", &m); while (m--) { scanf("%d%d", &x, &y); if (y - x < 2) { printf("YES\n"); continue; } xx = a[y] - a[x - 1]; yy = b[y] - b[x - 1]; zz = c[y] - c[x - 1]; if (xx == yy && yy == zz) { printf("YES\n"); continue; } if (xx == yy && zz - yy == 1) { printf("YES\n"); continue; } if (yy == zz && zz - xx == 1) { printf("YES\n"); continue; } if (yy == zz && xx - yy == 1) { printf("YES\n"); continue; } if (xx == zz && xx - yy == 1) { printf("YES\n"); continue; } if (xx == zz && yy - xx == 1) { printf("YES\n"); continue; } if (xx == yy && xx - zz == 1) { printf("YES\n"); continue; } printf("NO\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, a[600000], b[600000], c[600000], l, r, g, ans; vector<long long> v; string s; char f; int main() { cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == 'x') a[i + 1] = 1; else if (s[i] == 'y') b[i + 1] = 1; else if (s[i] == 'z') c[i + 1] = 1; } for (int i = 1; i <= s.size(); i++) { a[i] += a[i - 1]; b[i] += b[i - 1]; c[i] += c[i - 1]; } cin >> n; for (int i = 0; i < n; i++) { cin >> l >> r; if (r - l + 1 < 3) { cout << "YES" << endl; continue; } long long a1 = a[r] - a[l - 1]; long long b1 = b[r] - b[l - 1]; long long c1 = c[r] - c[l - 1]; if (abs(a1 - b1) > 1 || abs(a1 - c1) > 1 || abs(b1 - c1) > 1) cout << "NO" << endl; else cout << "YES" << endl; } return 0; }
### Prompt In cpp, your task is to solve the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, a[600000], b[600000], c[600000], l, r, g, ans; vector<long long> v; string s; char f; int main() { cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == 'x') a[i + 1] = 1; else if (s[i] == 'y') b[i + 1] = 1; else if (s[i] == 'z') c[i + 1] = 1; } for (int i = 1; i <= s.size(); i++) { a[i] += a[i - 1]; b[i] += b[i - 1]; c[i] += c[i - 1]; } cin >> n; for (int i = 0; i < n; i++) { cin >> l >> r; if (r - l + 1 < 3) { cout << "YES" << endl; continue; } long long a1 = a[r] - a[l - 1]; long long b1 = b[r] - b[l - 1]; long long c1 = c[r] - c[l - 1]; if (abs(a1 - b1) > 1 || abs(a1 - c1) > 1 || abs(b1 - c1) > 1) cout << "NO" << endl; else cout << "YES" << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int M; string s; int A, B; int x[100001], y[100001], z[100001], d[3]; int main() { cin >> s; scanf("%d", &M); for (int(i) = 0; (i) < (s.size()); (i)++) { x[i + 1] = x[i] + (s[i] == 'x'); y[i + 1] = y[i] + (s[i] == 'y'); z[i + 1] = z[i] + (s[i] == 'z'); } while (M--) { scanf("%d%d", &A, &B); d[0] = x[B] - x[A - 1]; d[1] = y[B] - y[A - 1]; d[2] = z[B] - z[A - 1]; sort(d, d + 3); if (d[2] - d[0] <= 1 || d[0] + d[1] + d[2] < 3) puts("YES"); else puts("NO"); } }
### Prompt Develop a solution in cpp to the problem described below: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int M; string s; int A, B; int x[100001], y[100001], z[100001], d[3]; int main() { cin >> s; scanf("%d", &M); for (int(i) = 0; (i) < (s.size()); (i)++) { x[i + 1] = x[i] + (s[i] == 'x'); y[i + 1] = y[i] + (s[i] == 'y'); z[i + 1] = z[i] + (s[i] == 'z'); } while (M--) { scanf("%d%d", &A, &B); d[0] = x[B] - x[A - 1]; d[1] = y[B] - y[A - 1]; d[2] = z[B] - z[A - 1]; sort(d, d + 3); if (d[2] - d[0] <= 1 || d[0] + d[1] + d[2] < 3) puts("YES"); else puts("NO"); } } ```
#include <bits/stdc++.h> using namespace std; int s1[100005]; int s2[100005]; int s3[100005]; string s11 = "xzy"; string s22 = "yxz"; string s33 = "zyx"; bool ok(int l, int r) { int a, b, c, d; a = s1[r]; b = s2[r]; c = s3[r]; if (l != 0) { a -= s1[l - 1]; b -= s2[l - 1]; c -= s3[l - 1]; } if (r - l + 1 < 3) return true; if (a == 0 || b == 0 || c == 0) return false; d = (r - l + 1) / 3; if (d != a && d != a - 1) return false; if (d != b && d != b - 1) return false; if (d != c && d != c - 1) return false; return true; } bool ok1(string s1, string s2) { int cnt = 0; for (int i = 0; i < s1.size(); i++) { if (s1[i] == s2[cnt]) { cnt++; if (cnt == 3) return 1; } } return 0; } int main() { string s; cin >> s; int l = s.size(); for (int i = 0; i < l; i++) { if (s[i] == 'x') s1[i + 1]++; if (s[i] == 'y') s2[i + 1]++; if (s[i] == 'z') s3[i + 1]++; } for (int i = 1; i <= l; i++) { s1[i] += s1[i - 1]; s2[i] += s2[i - 1]; s3[i] += s3[i - 1]; } int k; cin >> k; for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; int a = s1[y] - s1[x - 1]; int b = s2[y] - s2[x - 1]; int c = s3[y] - s3[x - 1]; if (y - x + 1 < 3) { printf("YES\n"); continue; } if (max(a, max(b, c)) - min(a, min(b, c)) <= 1) { printf("YES\n"); } else printf("NO\n"); } }
### Prompt Your task is to create a CPP solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int s1[100005]; int s2[100005]; int s3[100005]; string s11 = "xzy"; string s22 = "yxz"; string s33 = "zyx"; bool ok(int l, int r) { int a, b, c, d; a = s1[r]; b = s2[r]; c = s3[r]; if (l != 0) { a -= s1[l - 1]; b -= s2[l - 1]; c -= s3[l - 1]; } if (r - l + 1 < 3) return true; if (a == 0 || b == 0 || c == 0) return false; d = (r - l + 1) / 3; if (d != a && d != a - 1) return false; if (d != b && d != b - 1) return false; if (d != c && d != c - 1) return false; return true; } bool ok1(string s1, string s2) { int cnt = 0; for (int i = 0; i < s1.size(); i++) { if (s1[i] == s2[cnt]) { cnt++; if (cnt == 3) return 1; } } return 0; } int main() { string s; cin >> s; int l = s.size(); for (int i = 0; i < l; i++) { if (s[i] == 'x') s1[i + 1]++; if (s[i] == 'y') s2[i + 1]++; if (s[i] == 'z') s3[i + 1]++; } for (int i = 1; i <= l; i++) { s1[i] += s1[i - 1]; s2[i] += s2[i - 1]; s3[i] += s3[i - 1]; } int k; cin >> k; for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; int a = s1[y] - s1[x - 1]; int b = s2[y] - s2[x - 1]; int c = s3[y] - s3[x - 1]; if (y - x + 1 < 3) { printf("YES\n"); continue; } if (max(a, max(b, c)) - min(a, min(b, c)) <= 1) { printf("YES\n"); } else printf("NO\n"); } } ```
#include <bits/stdc++.h> using namespace std; int main() { char str[100000 + 11]; int ant[3][100000 + 11]; int m; int l, r; scanf("%s", str + 1); ant[0][0] = ant[1][0] = ant[2][0] = 0; for (int i = 1; str[i]; i++) { if (str[i] == 'x') { ant[0][i] = ant[0][i - 1] + 1; ant[1][i] = ant[1][i - 1]; ant[2][i] = ant[2][i - 1]; } else if (str[i] == 'y') { ant[0][i] = ant[0][i - 1]; ant[1][i] = ant[1][i - 1] + 1; ant[2][i] = ant[2][i - 1]; } else { ant[0][i] = ant[0][i - 1]; ant[1][i] = ant[1][i - 1]; ant[2][i] = ant[2][i - 1] + 1; } } scanf("%d", &m); while (m--) { scanf("%d %d", &l, &r); int n = r - l + 1; if (n < 3) { puts("YES"); continue; } int a[4]; a[0] = ant[0][r] - ant[0][l - 1]; a[1] = ant[1][r] - ant[1][l - 1]; a[2] = ant[2][r] - ant[2][l - 1]; sort(a, a + 3); if (n % 3) { if (a[2] == n / 3 + 1 && a[0] == n / 3) puts("YES"); else puts("NO"); } else { if (a[0] == a[2]) puts("YES"); else puts("NO"); } } return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int main() { char str[100000 + 11]; int ant[3][100000 + 11]; int m; int l, r; scanf("%s", str + 1); ant[0][0] = ant[1][0] = ant[2][0] = 0; for (int i = 1; str[i]; i++) { if (str[i] == 'x') { ant[0][i] = ant[0][i - 1] + 1; ant[1][i] = ant[1][i - 1]; ant[2][i] = ant[2][i - 1]; } else if (str[i] == 'y') { ant[0][i] = ant[0][i - 1]; ant[1][i] = ant[1][i - 1] + 1; ant[2][i] = ant[2][i - 1]; } else { ant[0][i] = ant[0][i - 1]; ant[1][i] = ant[1][i - 1]; ant[2][i] = ant[2][i - 1] + 1; } } scanf("%d", &m); while (m--) { scanf("%d %d", &l, &r); int n = r - l + 1; if (n < 3) { puts("YES"); continue; } int a[4]; a[0] = ant[0][r] - ant[0][l - 1]; a[1] = ant[1][r] - ant[1][l - 1]; a[2] = ant[2][r] - ant[2][l - 1]; sort(a, a + 3); if (n % 3) { if (a[2] == n / 3 + 1 && a[0] == n / 3) puts("YES"); else puts("NO"); } else { if (a[0] == a[2]) puts("YES"); else puts("NO"); } } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int x[maxn]; int y[maxn]; int z[maxn]; char s[maxn]; void Init() { for (int i = 0; s[i] != '\0'; i++) { x[i + 1] = x[i]; y[i + 1] = y[i]; z[i + 1] = z[i]; if (s[i] == 'x') { x[i + 1]++; } else if (s[i] == 'y') { y[i + 1]++; } else { z[i + 1]++; } } } void Solve() { int n, l, r; cin >> n; for (int i = 0; i < n; i++) { cin >> l >> r; if (r - l + 1 < 3) { cout << "YES" << endl; } else { int num[3]; num[0] = x[r] - x[l - 1]; num[1] = y[r] - y[l - 1]; num[2] = z[r] - z[l - 1]; sort(num, num + 3); if (num[2] - num[0] < 2) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } } int main() { cin >> s; Init(); Solve(); return 0; }
### Prompt Generate a cpp solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 100005; int x[maxn]; int y[maxn]; int z[maxn]; char s[maxn]; void Init() { for (int i = 0; s[i] != '\0'; i++) { x[i + 1] = x[i]; y[i + 1] = y[i]; z[i + 1] = z[i]; if (s[i] == 'x') { x[i + 1]++; } else if (s[i] == 'y') { y[i + 1]++; } else { z[i + 1]++; } } } void Solve() { int n, l, r; cin >> n; for (int i = 0; i < n; i++) { cin >> l >> r; if (r - l + 1 < 3) { cout << "YES" << endl; } else { int num[3]; num[0] = x[r] - x[l - 1]; num[1] = y[r] - y[l - 1]; num[2] = z[r] - z[l - 1]; sort(num, num + 3); if (num[2] - num[0] < 2) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } } int main() { cin >> s; Init(); Solve(); return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; const long long N = 100005; long long cnt[N][3]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; long long n = s.length(); for (long long i = 0; i < n; i++) { for (long long j = 0; j < 3; j++) { cnt[i + 1][j] = cnt[i][j]; if ((s[i] - 'x') == j) cnt[i + 1][j]++; } } long long m, x, y; cin >> m; for (long long i = 0; i < m; i++) { cin >> x >> y; long long mn = 1e9, mx = -1e9; for (long long j = 0; j < 3; j++) { mx = max(mx, cnt[y][j] - cnt[x - 1][j]); mn = min(mn, cnt[y][j] - cnt[x - 1][j]); } if (mx - mn < 2 || y - x <= 1) cout << "YES\n"; else cout << "NO\n"; } return 0; }
### Prompt Generate a CPP solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; const long long N = 100005; long long cnt[N][3]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; long long n = s.length(); for (long long i = 0; i < n; i++) { for (long long j = 0; j < 3; j++) { cnt[i + 1][j] = cnt[i][j]; if ((s[i] - 'x') == j) cnt[i + 1][j]++; } } long long m, x, y; cin >> m; for (long long i = 0; i < m; i++) { cin >> x >> y; long long mn = 1e9, mx = -1e9; for (long long j = 0; j < 3; j++) { mx = max(mx, cnt[y][j] - cnt[x - 1][j]); mn = min(mn, cnt[y][j] - cnt[x - 1][j]); } if (mx - mn < 2 || y - x <= 1) cout << "YES\n"; else cout << "NO\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename typ> void vout(vector<typ>& v) { for (int vint = 0; vint < (int)(v).size(); vint++) cout << (v)[vint] << ' '; cout << endl; } template <typename typ> void arrout(typ* arr, int l) { for (int i = 0; i < l; i++) cout << arr[i] << ' '; cout << endl; } int main() { int n; string s; cin >> s; n = (int)(s).size(); int x[n + 1], y[n + 1], z[n + 1]; x[0] = y[0] = z[0] = 0; for (int i = 0; i < n; ++i) { x[i + 1] = x[i] + (s[i] == 'x'); y[i + 1] = y[i] + (s[i] == 'y'); z[i + 1] = z[i] + (s[i] == 'z'); } int m; cin >> m; int l, r; for (int i = 0; i < m; ++i) { cin >> l >> r; if (r - l + 1 < 3) { cout << ("YES") << endl; continue; } int xc = x[r] - x[l - 1]; int yc = y[r] - y[l - 1]; int zc = z[r] - z[l - 1]; int mx = max(xc, max(yc, zc)); if (xc >= mx - 1 && yc >= mx - 1 && zc >= mx - 1) cout << ("YES") << endl; else cout << ("NO") << endl; } }
### Prompt Please provide a cpp coded solution to the problem described below: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename typ> void vout(vector<typ>& v) { for (int vint = 0; vint < (int)(v).size(); vint++) cout << (v)[vint] << ' '; cout << endl; } template <typename typ> void arrout(typ* arr, int l) { for (int i = 0; i < l; i++) cout << arr[i] << ' '; cout << endl; } int main() { int n; string s; cin >> s; n = (int)(s).size(); int x[n + 1], y[n + 1], z[n + 1]; x[0] = y[0] = z[0] = 0; for (int i = 0; i < n; ++i) { x[i + 1] = x[i] + (s[i] == 'x'); y[i + 1] = y[i] + (s[i] == 'y'); z[i + 1] = z[i] + (s[i] == 'z'); } int m; cin >> m; int l, r; for (int i = 0; i < m; ++i) { cin >> l >> r; if (r - l + 1 < 3) { cout << ("YES") << endl; continue; } int xc = x[r] - x[l - 1]; int yc = y[r] - y[l - 1]; int zc = z[r] - z[l - 1]; int mx = max(xc, max(yc, zc)); if (xc >= mx - 1 && yc >= mx - 1 && zc >= mx - 1) cout << ("YES") << endl; else cout << ("NO") << endl; } } ```
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; long long x[N], y[N], z[N]; void solve() { string s; cin >> s; long long n = s.size(); for (long long i = 0; i < n; i++) { if (s[i] == 'x') x[i] = 1; else if (s[i] == 'y') y[i] = 1; else z[i] = 1; } for (long long i = 1; i < n; i++) { x[i] += x[i - 1]; y[i] += y[i - 1]; z[i] += z[i - 1]; } long long m; cin >> m; while (m--) { long long l, r; cin >> l >> r; --l; --r; if (r - l + 1 < 3) { cout << "YES" << endl; continue; } vector<long long> v; if (l != 0) v.push_back(x[r] - x[l - 1]); else v.push_back(x[r]); if (l != 0) v.push_back(y[r] - y[l - 1]); else v.push_back(y[r]); if (l != 0) v.push_back(z[r] - z[l - 1]); else v.push_back(z[r]); sort((v).begin(), (v).end()); if (v[2] - v[1] < 2 && v[2] - v[0] < 2) cout << "YES"; else cout << "NO"; cout << endl; } return; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; t = 1; while (t--) solve(); return 0; }
### Prompt Generate a cpp solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; long long x[N], y[N], z[N]; void solve() { string s; cin >> s; long long n = s.size(); for (long long i = 0; i < n; i++) { if (s[i] == 'x') x[i] = 1; else if (s[i] == 'y') y[i] = 1; else z[i] = 1; } for (long long i = 1; i < n; i++) { x[i] += x[i - 1]; y[i] += y[i - 1]; z[i] += z[i - 1]; } long long m; cin >> m; while (m--) { long long l, r; cin >> l >> r; --l; --r; if (r - l + 1 < 3) { cout << "YES" << endl; continue; } vector<long long> v; if (l != 0) v.push_back(x[r] - x[l - 1]); else v.push_back(x[r]); if (l != 0) v.push_back(y[r] - y[l - 1]); else v.push_back(y[r]); if (l != 0) v.push_back(z[r] - z[l - 1]); else v.push_back(z[r]); sort((v).begin(), (v).end()); if (v[2] - v[1] < 2 && v[2] - v[0] < 2) cout << "YES"; else cout << "NO"; cout << endl; } return; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; t = 1; while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> const double pi = 3.1415926535897; using namespace std; int xC[100005] = {}, yC[100005] = {}, zC[100005] = {}; int main() { string s; int m, l, r; cin >> s; for (int i = 1; i <= s.length(); i++) { xC[i] = xC[i - 1] + (s[i - 1] == 'x'); yC[i] = yC[i - 1] + (s[i - 1] == 'y'); zC[i] = zC[i - 1] + (s[i - 1] == 'z'); } cin >> m; int x, y, z, maxi, mini; for (int i = 0; i < m; i++) { cin >> l >> r; x = xC[r] - xC[l - 1]; y = yC[r] - yC[l - 1]; z = zC[r] - zC[l - 1]; maxi = max(x, max(y, z)); mini = min(x, min(y, z)); if (maxi - mini < 2 || (r - l < 2)) cout << "YES\n"; else cout << "NO\n"; } }
### Prompt Please formulate a Cpp solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> const double pi = 3.1415926535897; using namespace std; int xC[100005] = {}, yC[100005] = {}, zC[100005] = {}; int main() { string s; int m, l, r; cin >> s; for (int i = 1; i <= s.length(); i++) { xC[i] = xC[i - 1] + (s[i - 1] == 'x'); yC[i] = yC[i - 1] + (s[i - 1] == 'y'); zC[i] = zC[i - 1] + (s[i - 1] == 'z'); } cin >> m; int x, y, z, maxi, mini; for (int i = 0; i < m; i++) { cin >> l >> r; x = xC[r] - xC[l - 1]; y = yC[r] - yC[l - 1]; z = zC[r] - zC[l - 1]; maxi = max(x, max(y, z)); mini = min(x, min(y, z)); if (maxi - mini < 2 || (r - l < 2)) cout << "YES\n"; else cout << "NO\n"; } } ```
#include <bits/stdc++.h> using namespace std; int a[3][100005], t[3]; int main() { string s; cin >> s; int n, l, r; for (int i = 0; i < s.size(); i++) { if (s[i] >= 'x') { a[s[i] - 'x'][i + 1]++; } for (int j = 0; j < 3; j++) { a[j][i + 1] += a[j][i]; } } cin >> n; int zh; for (int i = 0; i < n; i++) { cin >> l >> r; if (r - l < 2) { cout << "YES\n"; continue; } zh = 0; for (int j = 0; j < 3; j++) { t[j] = a[j][r] - a[j][l - 1]; zh += t[j]; } if (zh != r - l + 1 || max(t[0], max(t[1], t[2])) - min(t[0], min(t[1], t[2])) > 1) { cout << "NO\n"; } else cout << "YES\n"; } return 0; }
### Prompt Please provide a cpp coded solution to the problem described below: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int a[3][100005], t[3]; int main() { string s; cin >> s; int n, l, r; for (int i = 0; i < s.size(); i++) { if (s[i] >= 'x') { a[s[i] - 'x'][i + 1]++; } for (int j = 0; j < 3; j++) { a[j][i + 1] += a[j][i]; } } cin >> n; int zh; for (int i = 0; i < n; i++) { cin >> l >> r; if (r - l < 2) { cout << "YES\n"; continue; } zh = 0; for (int j = 0; j < 3; j++) { t[j] = a[j][r] - a[j][l - 1]; zh += t[j]; } if (zh != r - l + 1 || max(t[0], max(t[1], t[2])) - min(t[0], min(t[1], t[2])) > 1) { cout << "NO\n"; } else cout << "YES\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; int x[100005], y[100005], z[100005]; int main() { string str; int n, i, j; cin >> str; if (str[0] == 'x') x[0] = 1; else if (str[0] == 'y') y[0] = 1; else z[0] = 1; for (i = 1; i < str.size(); i++) { x[i] = x[i - 1], y[i] = y[i - 1], z[i] = z[i - 1]; if (str[i] == 'x') x[i]++; else if (str[i] == 'y') y[i]++; else z[i]++; } scanf("%d", &n); for (i = 0; i < n; i++) { int a, b; scanf("%d", &a); scanf("%d", &b); a--, b--; if (b - a + 1 < 3) { printf("YES\n"); continue; } int s, d, f; s = x[b]; if (a) s -= x[a - 1]; d = y[b]; if (a) d -= y[a - 1]; f = z[b]; if (a) f -= z[a - 1]; int u = max(s, max(d, f)); int v = min(s, min(d, f)); if (u - v > 1) printf("NO\n"); else printf("YES\n"); } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int x[100005], y[100005], z[100005]; int main() { string str; int n, i, j; cin >> str; if (str[0] == 'x') x[0] = 1; else if (str[0] == 'y') y[0] = 1; else z[0] = 1; for (i = 1; i < str.size(); i++) { x[i] = x[i - 1], y[i] = y[i - 1], z[i] = z[i - 1]; if (str[i] == 'x') x[i]++; else if (str[i] == 'y') y[i]++; else z[i]++; } scanf("%d", &n); for (i = 0; i < n; i++) { int a, b; scanf("%d", &a); scanf("%d", &b); a--, b--; if (b - a + 1 < 3) { printf("YES\n"); continue; } int s, d, f; s = x[b]; if (a) s -= x[a - 1]; d = y[b]; if (a) d -= y[a - 1]; f = z[b]; if (a) f -= z[a - 1]; int u = max(s, max(d, f)); int v = min(s, min(d, f)); if (u - v > 1) printf("NO\n"); else printf("YES\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxN = 110000; int t[maxN][3]; int main() { int n, m; string s; cin >> s; cin >> m; n = s.size(); for (int i = 1; i <= n; i++) for (int j = 0; j < 3; j++) if (s[i - 1] - 'x' == j) t[i][j] = t[i - 1][j] + 1; else t[i][j] = t[i - 1][j]; for (int i = 0; i < m; i++) { int l, r, x, y, z; cin >> l >> r; x = t[r][0] - t[l - 1][0]; y = t[r][1] - t[l - 1][1]; z = t[r][2] - t[l - 1][2]; if (l + 2 > r || (abs(x - y) < 2 && abs(y - z) < 2 && abs(x - z) < 2)) cout << "YES\n"; else cout << "NO\n"; } cin >> n; }
### Prompt Please create a solution in Cpp to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxN = 110000; int t[maxN][3]; int main() { int n, m; string s; cin >> s; cin >> m; n = s.size(); for (int i = 1; i <= n; i++) for (int j = 0; j < 3; j++) if (s[i - 1] - 'x' == j) t[i][j] = t[i - 1][j] + 1; else t[i][j] = t[i - 1][j]; for (int i = 0; i < m; i++) { int l, r, x, y, z; cin >> l >> r; x = t[r][0] - t[l - 1][0]; y = t[r][1] - t[l - 1][1]; z = t[r][2] - t[l - 1][2]; if (l + 2 > r || (abs(x - y) < 2 && abs(y - z) < 2 && abs(x - z) < 2)) cout << "YES\n"; else cout << "NO\n"; } cin >> n; } ```
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } const int maxn = 110005; int n, m, t, k; int vis[maxn]; int x[maxn], y[maxn], z[maxn]; char s[maxn]; int main() { scanf("%s", s); n = strlen(s); for (int i = 1; i <= n; i++) { x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1]; if (s[i - 1] == 'x') x[i]++; if (s[i - 1] == 'y') y[i]++; if (s[i - 1] == 'z') z[i]++; } scanf("%d", &m); while (m--) { int a, b; scanf("%d%d", &a, &b); t = b - a + 1; int t1 = x[b] - x[a - 1], t2 = y[b] - y[a - 1], t3 = z[b] - z[a - 1]; int m1, m2, m3, flag = 1; m1 = max(max(t1, t2), t3); m3 = min(min(t1, t2), t3); m2 = t1 + t2 + t3 - m1 - m3; if ((t - 1) / 3 + 1 < m1) flag = 0; if ((t - 1 - 1) / 3 + 1 < m2) flag = 0; if ((t - 1 - 1 - 1) / 3 + 1 < m3) flag = 0; if (flag || t <= 2) printf("YES\n"); else printf("NO\n"); } return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } const int maxn = 110005; int n, m, t, k; int vis[maxn]; int x[maxn], y[maxn], z[maxn]; char s[maxn]; int main() { scanf("%s", s); n = strlen(s); for (int i = 1; i <= n; i++) { x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1]; if (s[i - 1] == 'x') x[i]++; if (s[i - 1] == 'y') y[i]++; if (s[i - 1] == 'z') z[i]++; } scanf("%d", &m); while (m--) { int a, b; scanf("%d%d", &a, &b); t = b - a + 1; int t1 = x[b] - x[a - 1], t2 = y[b] - y[a - 1], t3 = z[b] - z[a - 1]; int m1, m2, m3, flag = 1; m1 = max(max(t1, t2), t3); m3 = min(min(t1, t2), t3); m2 = t1 + t2 + t3 - m1 - m3; if ((t - 1) / 3 + 1 < m1) flag = 0; if ((t - 1 - 1) / 3 + 1 < m2) flag = 0; if ((t - 1 - 1 - 1) / 3 + 1 < m3) flag = 0; if (flag || t <= 2) printf("YES\n"); else printf("NO\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long mod(long long a, long long m) { return (a % m + m) % m; } int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string s; int x[100100], y[100100], z[100100]; int m; int main() { cin >> s; switch (s[0]) { case 'x': x[1] = 1; break; case 'y': y[1] = 1; break; case 'z': z[1] = 1; break; } for (int i = 2; i < 1 + (int)s.length(); i++) { switch (s[i - 1]) { case 'x': x[i] = x[i - 1] + 1; y[i] = y[i - 1]; z[i] = z[i - 1]; break; case 'y': x[i] = x[i - 1]; y[i] = y[i - 1] + 1; z[i] = z[i - 1]; break; case 'z': x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1] + 1; break; } } cin >> m; for (int i = 0; i < m; i++) { int l, r; cin >> l >> r; int xn, yn, zn; xn = x[r] - x[l - 1]; yn = y[r] - y[l - 1]; zn = z[r] - z[l - 1]; bool ok = true; if (xn == 0 || yn == 0 || zn == 0) ok = false; if (max(xn, max(yn, zn)) - min(xn, min(yn, zn)) >= 2) ok = false; if (r - l < 2) ok = true; if (ok && (xn != 0 && yn != 0 && zn != 0) && xn * yn * zn == 0) cout << xn << " " << yn << " " << zn << endl; printf((ok) ? "YES\n" : "NO\n"); } }
### Prompt Your task is to create a Cpp solution to the following problem: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long mod(long long a, long long m) { return (a % m + m) % m; } int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string s; int x[100100], y[100100], z[100100]; int m; int main() { cin >> s; switch (s[0]) { case 'x': x[1] = 1; break; case 'y': y[1] = 1; break; case 'z': z[1] = 1; break; } for (int i = 2; i < 1 + (int)s.length(); i++) { switch (s[i - 1]) { case 'x': x[i] = x[i - 1] + 1; y[i] = y[i - 1]; z[i] = z[i - 1]; break; case 'y': x[i] = x[i - 1]; y[i] = y[i - 1] + 1; z[i] = z[i - 1]; break; case 'z': x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1] + 1; break; } } cin >> m; for (int i = 0; i < m; i++) { int l, r; cin >> l >> r; int xn, yn, zn; xn = x[r] - x[l - 1]; yn = y[r] - y[l - 1]; zn = z[r] - z[l - 1]; bool ok = true; if (xn == 0 || yn == 0 || zn == 0) ok = false; if (max(xn, max(yn, zn)) - min(xn, min(yn, zn)) >= 2) ok = false; if (r - l < 2) ok = true; if (ok && (xn != 0 && yn != 0 && zn != 0) && xn * yn * zn == 0) cout << xn << " " << yn << " " << zn << endl; printf((ok) ? "YES\n" : "NO\n"); } } ```
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 1, -1, 0}; int dy[] = {1, 0, 0, -1}; long double PI = 3.14159265358979323846264338327950; const int N = 1e5 + 10; int x[N], y[N], z[N]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; for (int i = 1; i <= (int)s.size(); i++) { if (s[i - 1] == 'z') z[i] = 1; else if (s[i - 1] == 'x') x[i] = 1; else y[i] = 1; x[i] += x[i - 1]; y[i] += y[i - 1]; z[i] += z[i - 1]; } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; int nx = x[r] - x[l - 1]; int ny = y[r] - y[l - 1]; int nz = z[r] - z[l - 1]; if ((abs(nx - ny) > 1 || abs(nx - nz) > 1 || abs(ny - nz) > 1) && (r - l) >= 2) cout << "NO\n"; else cout << "YES\n"; } return 0; }
### Prompt Construct a cpp code solution to the problem outlined: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int dx[] = {0, 1, -1, 0}; int dy[] = {1, 0, 0, -1}; long double PI = 3.14159265358979323846264338327950; const int N = 1e5 + 10; int x[N], y[N], z[N]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; for (int i = 1; i <= (int)s.size(); i++) { if (s[i - 1] == 'z') z[i] = 1; else if (s[i - 1] == 'x') x[i] = 1; else y[i] = 1; x[i] += x[i - 1]; y[i] += y[i - 1]; z[i] += z[i - 1]; } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; int nx = x[r] - x[l - 1]; int ny = y[r] - y[l - 1]; int nz = z[r] - z[l - 1]; if ((abs(nx - ny) > 1 || abs(nx - nz) > 1 || abs(ny - nz) > 1) && (r - l) >= 2) cout << "NO\n"; else cout << "YES\n"; } return 0; } ```
#include <bits/stdc++.h> using namespace std; void fast() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } bool comp(char s1, char s2) { return tolower(s1) < tolower(s2); } int main() { fast(); int l, r, n, m, a, b, c, i; string s; cin >> s; n = s.length(); int x[n], y[n], z[n], flag = 0; for (i = 0; i < n; i++) { x[i] = 0; y[i] = 0; z[i] = 0; if (i != 0) { x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1]; } if (s[i] == 'x') x[i]++; else if (s[i] == 'y') y[i]++; else z[i]++; } cin >> m; while (m--) { cin >> l >> r; if (r - l < 2) cout << "YES" << endl; else { l--; r--; a = x[r] - x[l]; b = y[r] - y[l]; c = z[r] - z[l]; if (s[l] == 'x') a++; else if (s[l] == 'y') b++; else c++; if ((a == b && b == c && a == c) || ((b == c && abs(a - b) == 1)) || (a == c && abs(a - b) == 1) || (a == b && abs(b - c) == 1)) cout << "YES" << endl; else cout << "NO" << endl; } } return 0; }
### Prompt Construct a CPP code solution to the problem outlined: Sereja loves all sorts of algorithms. He has recently come up with a new algorithm, which receives a string as an input. Let's represent the input string of the algorithm as q = q1q2... qk. The algorithm consists of two steps: 1. Find any continuous subsequence (substring) of three characters of string q, which doesn't equal to either string "zyx", "xzy", "yxz". If q doesn't contain any such subsequence, terminate the algorithm, otherwise go to step 2. 2. Rearrange the letters of the found subsequence randomly and go to step 1. Sereja thinks that the algorithm works correctly on string q if there is a non-zero probability that the algorithm will be terminated. But if the algorithm anyway will work for infinitely long on a string, then we consider the algorithm to work incorrectly on this string. Sereja wants to test his algorithm. For that, he has string s = s1s2... sn, consisting of n characters. The boy conducts a series of m tests. As the i-th test, he sends substring slisli + 1... sri (1 ≀ li ≀ ri ≀ n) to the algorithm input. Unfortunately, the implementation of his algorithm works too long, so Sereja asked you to help. For each test (li, ri) determine if the algorithm works correctly on this test or not. Input The first line contains non-empty string s, its length (n) doesn't exceed 105. It is guaranteed that string s only contains characters: 'x', 'y', 'z'. The second line contains integer m (1 ≀ m ≀ 105) β€” the number of tests. Next m lines contain the tests. The i-th line contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). Output For each test, print "YES" (without the quotes) if the algorithm works correctly on the corresponding test and "NO" (without the quotes) otherwise. Examples Input zyxxxxxxyyz 5 5 5 1 3 1 11 1 4 3 6 Output YES YES NO YES NO Note In the first example, in test one and two the algorithm will always be terminated in one step. In the fourth test you can get string "xzyx" on which the algorithm will terminate. In all other tests the algorithm doesn't work correctly. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void fast() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } bool comp(char s1, char s2) { return tolower(s1) < tolower(s2); } int main() { fast(); int l, r, n, m, a, b, c, i; string s; cin >> s; n = s.length(); int x[n], y[n], z[n], flag = 0; for (i = 0; i < n; i++) { x[i] = 0; y[i] = 0; z[i] = 0; if (i != 0) { x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1]; } if (s[i] == 'x') x[i]++; else if (s[i] == 'y') y[i]++; else z[i]++; } cin >> m; while (m--) { cin >> l >> r; if (r - l < 2) cout << "YES" << endl; else { l--; r--; a = x[r] - x[l]; b = y[r] - y[l]; c = z[r] - z[l]; if (s[l] == 'x') a++; else if (s[l] == 'y') b++; else c++; if ((a == b && b == c && a == c) || ((b == c && abs(a - b) == 1)) || (a == c && abs(a - b) == 1) || (a == b && abs(b - c) == 1)) cout << "YES" << endl; else cout << "NO" << endl; } } return 0; } ```