buggy_code
stringlengths
11
625k
fixed_code
stringlengths
17
625k
bug_type
stringlengths
2
4.45k
language
int64
0
8
token_count
int64
5
200k
change_count
int64
0
100
#include <stdio.h> int gcd(int a, int b) { if (!b) { return (a); } return (gcd(b, a % b)); } int main(void) { int a1, a2, a3; int m1, m2, m3; int x, y, z; int x_time, y_time, z_time; int lcm, lcm_2; while (1) { scanf("%d%d%d%d%d%d", &a1, &m1, &a2, &m2, &a3, &m3); if (a1 + m1 + a2 + m2 + a3 + m3 == 0) { break; } x = 1; y = 1; z = 1; x_time = y_time = z_time = 0; do { x = (a1 * x) % m1; x_time++; } while (x != 1); do { y = (a2 * y) % m2; y_time++; } while (y != 1); do { z = (a3 * z) % m3; z_time++; } while (z != 1); if (x_time > y_time) { lcm = gcd(x_time, y_time); } else { lcm = gcd(y_time, x_time); } lcm = (x_time / lcm) * y_time; if (lcm > z_time) { lcm_2 = gcd(lcm, z_time); } else { lcm_2 = gcd(z_time, lcm); } lcm_2 = (lcm / lcm_2) * z_time; printf("%d\n", lcm_2); } return (0); }
#include <stdio.h> int gcd(int a, int b) { if (!b) { return (a); } return (gcd(b, a % b)); } int main(void) { int a1, a2, a3; int m1, m2, m3; int x, y, z; long long x_time, y_time, z_time; long long lcm, lcm_2; while (1) { scanf("%d%d%d%d%d%d", &a1, &m1, &a2, &m2, &a3, &m3); if (a1 + m1 + a2 + m2 + a3 + m3 == 0) { break; } x = 1; y = 1; z = 1; x_time = y_time = z_time = 0; do { x = (a1 * x) % m1; x_time++; } while (x != 1); do { y = (a2 * y) % m2; y_time++; } while (y != 1); do { z = (a3 * z) % m3; z_time++; } while (z != 1); if (x_time > y_time) { lcm = gcd(x_time, y_time); } else { lcm = gcd(y_time, x_time); } lcm = (x_time / lcm) * y_time; if (lcm > z_time) { lcm_2 = gcd(lcm, z_time); } else { lcm_2 = gcd(z_time, lcm); } lcm_2 = (lcm / lcm_2) * z_time; printf("%lld\n", lcm_2); } return (0); }
[["-", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 14, 8, 9, 0, 43, 39, 86, 0, 96], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
310
8
int G(x, y) { return y ? G(y, x % y) : x; } #define L(x, y) ((long long)x / G(x, y) * y) n[3], a[3], m[3]; main(i, x) { for (; scanf("%d%d%d%d%d%d", a, m, a + 1, m + 1, a + 2, m + 2), *m; n[0] *= n[1] / G(n[0], n[1]), printf("%d\n", n[2] / G(n[0], n[2]) * n[0])) for (i = 3; i--;) for (x = n[i] = 1; (x = a[i] * x % m[i]) > 1; n[i]++) ; }
int G(x, y) { return y ? G(y, x % y) : x; } #define L(x, y) ((long long)x / G(x, y) * y) n[3], a[3], m[3]; main(i, x) { for (; scanf("%d%d%d%d%d%d", a, m, a + 1, m + 1, a + 2, m + 2), *m; n[0] *= n[1] / G(n[0], n[1]), printf("%lld\n", (long long)n[2] / G(n[0], n[2]) * n[0])) for (i = 3; i--;) for (x = n[i] = 1; (x = a[i] * x % m[i]) > 1; n[i]++) ; }
[["-", 26, 34, 12, 2, 3, 4, 0, 5, 0, 6], ["+", 26, 34, 12, 2, 3, 4, 0, 5, 0, 6], ["+", 3, 4, 0, 16, 31, 16, 31, 74, 0, 24], ["+", 31, 16, 31, 74, 39, 77, 39, 86, 0, 96], ["+", 3, 4, 0, 16, 31, 16, 31, 74, 0, 25]]
0
188
14
#include <stdio.h> #include <stdlib.h> #include <string.h> int a1, a2, a3, m1, m2, m3; int count, x, y, z, sx, sy, sz; int lcm(int a, int b) { int xx, yy, zz; if (a > b) { xx = a; yy = b; } else { xx = b; yy = a; } while (yy > 0) { zz = xx % yy; xx = yy; yy = zz; } return xx * (a / xx) * (b / xx); } int main() { while (scanf("%d %d %d %d %d %d", &a1, &m1, &a2, &m2, &a3, &m3) * a1 * m1) { count = 0; x = 1; y = 1; z = 1; sx = 0; sy = 0; sz = 0; while (sx * sy * sz == 0) { count++; x = (x * a1) % m1; y = (y * a2) % m2; z = (z * a3) % m3; if (sx == 0 && x == 1) sx = count; if (sy == 0 && y == 1) sy = count; if (sz == 0 && z == 1) sz = count; } printf("%d\n", lcm(sx, lcm(sy, sz))); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> int a1, a2, a3, m1, m2, m3; int count, x, y, z, sx, sy, sz; long long int lcm(int a, int b) { long long int xx, yy, zz; if (a > b) { xx = a; yy = b; } else { xx = b; yy = a; } while (yy > 0) { zz = xx % yy; xx = yy; yy = zz; } return xx * (a / xx) * (b / xx); } int main() { while (scanf("%d %d %d %d %d %d", &a1, &m1, &a2, &m2, &a3, &m3) * a1 * m1) { count = 0; x = 1; y = 1; z = 1; sx = 0; sy = 0; sz = 0; while (sx * sy * sz == 0) { count++; x = (x * a1) % m1; y = (y * a2) % m2; z = (z * a3) % m3; if (sx == 0 && x == 1) sx = count; if (sy == 0 && y == 1) sy = count; if (sz == 0 && z == 1) sz = count; } printf("%lld\n", lcm(sx, lcm(sy, sz))); } return 0; }
[["+", 36, 36, 0, 30, 0, 14, 39, 86, 0, 96], ["+", 0, 14, 8, 9, 0, 43, 39, 86, 0, 96], ["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
293
6
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <fstream> #include <iostream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> //#include <tuple> #include <functional> #include <set> #include <string.h> #define X first #define Y second #define MP make_pair //#define MT make_tuple #define REP(i, a, n) for (int(i) = (a); (i) < (n); ++(i)) #define FOR(i, n) REP(i, 0, n) typedef long long ll; using namespace std; const int MAX = 500; template <class T, class U> void convert(T &t, U &u) { stringstream ss; ss << t; ss >> u; } int gcd(int m, int n) { return n != 0 ? gcd(n, m % n) : m; } int lcm(int m, int n) { if ((0 == m) || (0 == n)) return 0; return ((m / gcd(m, n)) * n); } void SearchPattern(map<ll, ll> &a, ll b, ll c) { ll x = 1; while (!a.count((b * x) % c)) { a[(b * x) % c] = 1; x = (b * x) % c; } } map<ll, ll> aa, bb, cc; int main() { ll a, b, c, d, e, f; while (cin >> a >> b >> c >> d >> e >> f && a | b | c | d | e | f) { aa.clear(); bb.clear(); cc.clear(); SearchPattern(aa, a, b); SearchPattern(bb, c, d); SearchPattern(cc, e, f); ll ans = 1; ll tmp = lcm(aa.size(), bb.size()); if (tmp == 1) { tmp = aa.size() * bb.size(); ans = tmp; } else ans = tmp; tmp = lcm(cc.size(), tmp); if (tmp == 1) ans *= cc.size(); else ans = tmp; cout << ans << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <fstream> #include <iostream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> //#include <tuple> #include <functional> #include <set> #include <string.h> #define X first #define Y second #define MP make_pair //#define MT make_tuple #define REP(i, a, n) for (int(i) = (a); (i) < (n); ++(i)) #define FOR(i, n) REP(i, 0, n) typedef long long ll; using namespace std; const int MAX = 500; template <class T, class U> void convert(T &t, U &u) { stringstream ss; ss << t; ss >> u; } ll gcd(ll m, ll n) { return n != 0 ? gcd(n, m % n) : m; } ll lcm(ll m, ll n) { if ((0 == m) || (0 == n)) return 0; return ((m / gcd(m, n)) * n); } void SearchPattern(map<ll, ll> &a, ll b, ll c) { ll x = 1; while (!a.count((b * x) % c)) { a[(b * x) % c] = 1; x = (b * x) % c; } } map<ll, ll> aa, bb, cc; int main() { ll a, b, c, d, e, f; while (cin >> a >> b >> c >> d >> e >> f && a | b | c | d | e | f) { aa.clear(); bb.clear(); cc.clear(); SearchPattern(aa, a, b); SearchPattern(bb, c, d); SearchPattern(cc, e, f); ll ans = 1; ll tmp = lcm(aa.size(), bb.size()); if (tmp == 1) { tmp = aa.size() * bb.size(); ans = tmp; } else ans = tmp; tmp = lcm(cc.size(), tmp); if (tmp == 1) ans *= cc.size(); else ans = tmp; cout << ans << endl; } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 14, 39, 40], ["+", 36, 36, 36, 36, 0, 30, 0, 14, 39, 78], ["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 40], ["+", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78]]
1
446
12
#include <algorithm> #include <cstdio> #include <cstring> #include <ctype.h> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define INF 114514810 #define ll long long //#define scanf scanf_s int lcm(int x, int y) { int X = x; int Y = y; if (x < y) swap(x, y); while (1) { if (y == 0) break; int tmpy = y; y = x % y; x = tmpy; } return X / x * Y; } int main() { ll a[3]; ll m[3]; while (1) { REP(i, 3) cin >> a[i] >> m[i]; if (!a[0] && !a[1] && !a[2] && !m[0] && !m[1] && !m[2]) break; ll t[3]; REP(i, 3) { ll x = 1; x = (a[i] * x % m[i]); for (t[i] = 1; x != 1; t[i]++) { x = (a[i] * x % m[i]); } } cout << lcm(t[0], lcm(t[1], t[2])) << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <ctype.h> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define INF 114514810 #define ll long long //#define scanf scanf_s ll lcm(int x, int y) { ll X = x; ll Y = y; if (x < y) swap(x, y); while (1) { if (y == 0) break; ll tmpy = y; y = x % y; x = tmpy; } return X / x * Y; } int main() { ll a[3]; ll m[3]; while (1) { REP(i, 3) cin >> a[i] >> m[i]; if (!a[0] && !a[1] && !a[2] && !m[0] && !m[1] && !m[2]) break; ll t[3]; REP(i, 3) { ll x = 1; x = (a[i] * x % m[i]); for (t[i] = 1; x != 1; t[i]++) { x = (a[i] * x % m[i]); } } cout << lcm(t[0], lcm(t[1], t[2])) << endl; } return 0; }
[["-", 36, 36, 36, 36, 0, 30, 0, 14, 39, 40], ["+", 36, 36, 36, 36, 0, 30, 0, 14, 39, 78], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 39, 78], ["-", 8, 9, 0, 52, 8, 9, 0, 43, 39, 40], ["+", 8, 9, 0, 52, 8, 9, 0, 43, 39, 78]]
1
316
8
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll f(ll x, ll a, ll m) { if (x == 1) return 0; return f(a * x % m, a, m) + 1; } int main() { while (true) { ll A, M, ans = 1; for (ll i = 0; i < 3; i++) { cin >> A >> M; if (!A || !M) return 0; ans = lcm(ans, f(A % M, A, M) + 1); } printf("ll%d\n", ans); } }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> using namespace std; typedef long long ll; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll f(ll x, ll a, ll m) { if (x == 1) return 0; return f(a * x % m, a, m) + 1; } int main() { while (true) { ll A, M, ans = 1; for (ll i = 0; i < 3; i++) { cin >> A >> M; if (!A || !M) return 0; ans = lcm(ans, f(A % M, A, M) + 1); } printf("%lld\n", ans); } }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
1
205
2
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<ll, ll> pii; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back #define INF 999999999 #define eps 1e-9 int a1, b1, a2, b2, a3, b3; int main() { while (cin >> a1 >> b1 >> a2 >> b2 >> a3 >> b3 && a1 && b1 && a2 && b2 && a3 && b3) { int a = 0, b = 0, c = 0; int x = 1, y = 1, z = 1; while (1) { a++; x = (a1 * x) % b1; if (x == 1) break; } while (1) { b++; y = (a2 * y) % b2; if (y == 1) break; } while (1) { c++; z = (a3 * z) % b3; if (z == 1) break; } int m = b / __gcd(a, b) * a; cout << c / __gcd(m, c) * m << endl; } }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<ll, ll> pii; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back #define INF 999999999 #define eps 1e-9 ll a1, b1, a2, b2, a3, b3; int main() { while (cin >> a1 >> b1 >> a2 >> b2 >> a3 >> b3 && a1 && b1 && a2 && b2 && a3 && b3) { ll a = 0, b = 0, c = 0; ll x = 1, y = 1, z = 1; while (1) { a++; x = (a1 * x) % b1; if (x == 1) break; } while (1) { b++; y = (a2 * y) % b2; if (y == 1) break; } while (1) { c++; z = (a3 * z) % b3; if (z == 1) break; } ll m = b / __gcd(a, b) * a; cout << c / __gcd(m, c) * m << endl; } }
[["-", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 39, 78], ["-", 8, 9, 0, 52, 8, 9, 0, 43, 39, 40], ["+", 8, 9, 0, 52, 8, 9, 0, 43, 39, 78]]
1
230
8
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; long long int LCM(long long int a, long long int b) { long long int dummy1, dummy2, dummy; dummy1 = max(a, b); dummy2 = min(a, b); while (true) { dummy = dummy1 % dummy2; dummy1 = dummy2; dummy2 = dummy; if (dummy == 0) { break; } } return (a / dummy1) * (b / dummy1) * dummy1; } int main() { long long int a[3], m[3], span[3]; for (int i = 0; i < 3; i++) { cin >> a[i] >> m[i]; } while (a[0] != 0) { long long int dummy; for (int i = 0; i < 3; i++) { span[i] = 1; dummy = a[i] % m[i]; for (int loop = 1; loop < 1000000; loop++) { if (dummy == 1) { span[i] = loop; break; } dummy = (dummy * a[i]) % m[i]; } } /* for(int i = 0; i < 3; i++){ cout << span[i] << " "; } cout << endl; */ int ans; ans = LCM(LCM(span[0], span[1]), span[2]); cout << ans << endl; for (int i = 0; i < 3; i++) { cin >> a[i] >> m[i]; } } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; long long int LCM(long long int a, long long int b) { long long int dummy1, dummy2, dummy; dummy1 = max(a, b); dummy2 = min(a, b); while (true) { dummy = dummy1 % dummy2; dummy1 = dummy2; dummy2 = dummy; if (dummy == 0) { break; } } return (a / dummy1) * (b / dummy1) * dummy1; } int main() { long long int a[3], m[3], span[3]; for (int i = 0; i < 3; i++) { cin >> a[i] >> m[i]; } while (a[0] != 0) { long long int dummy; for (int i = 0; i < 3; i++) { span[i] = 1; dummy = a[i] % m[i]; for (int loop = 1; loop < 1000000; loop++) { if (dummy == 1) { span[i] = loop; break; } dummy = (dummy * a[i]) % m[i]; } } /* for(int i = 0; i < 3; i++){ cout << span[i] << " "; } cout << endl; */ long long int ans; ans = LCM(LCM(span[0], span[1]), span[2]); cout << ans << endl; for (int i = 0; i < 3; i++) { cin >> a[i] >> m[i]; } } return 0; }
[["+", 0, 52, 8, 9, 0, 43, 39, 86, 0, 96]]
1
315
2
#include "bits/stdc++.h" using namespace std; long long int gcd(long long int l, long long int r) { assert(l > 0 && r > 0); if (l > r) return gcd(r, l); else { const long long int num = r % l; if (num) { return gcd(l, num); } else { return l; } } } long long int lca(long long int l, long long int r) { return l / gcd(l, r) * r; } long long int gcd(vector<long long int> nums) { if (nums.size() == 1) return nums[0]; long long int num = nums[0]; for (int i = 1; i < nums.size(); ++i) { num = gcd(num, nums[i]); } return num; } long long int lca(const vector<long long int> &nums) { if (nums.size() == 1) return nums[0]; else { int num = nums[0]; for (int i = 1; i < nums.size(); ++i) { num = lca(num, nums[i]); } return num; } } int main() { while (1) { vector<long long int> as(3), ms(3), backs(3); for (int i = 0; i < 3; ++i) cin >> as[i] >> ms[i]; if (!as[0]) break; for (int i = 0; i < 3; ++i) { long long int now = 1; while (backs[i]++, true) { now = (now * as[i]) % ms[i]; if (now == 1) break; } } long long int ans = lca(backs); cout << ans << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; long long int gcd(long long int l, long long int r) { assert(l > 0 && r > 0); if (l > r) return gcd(r, l); else { const long long int num = r % l; if (num) { return gcd(l, num); } else { return l; } } } long long int lca(long long int l, long long int r) { return l / gcd(l, r) * r; } long long int gcd(vector<long long int> nums) { if (nums.size() == 1) return nums[0]; long long int num = nums[0]; for (int i = 1; i < nums.size(); ++i) { num = gcd(num, nums[i]); } return num; } long long int lca(const vector<long long int> &nums) { if (nums.size() == 1) return nums[0]; else { long long int num = nums[0]; for (int i = 1; i < nums.size(); ++i) { num = lca(num, nums[i]); } return num; } } int main() { while (1) { vector<long long int> as(3), ms(3), backs(3); for (int i = 0; i < 3; ++i) cin >> as[i] >> ms[i]; if (!as[0]) break; for (int i = 0; i < 3; ++i) { long long int now = 1; while (backs[i]++, true) { now = (now * as[i]) % ms[i]; if (now == 1) break; } } long long int ans = lca(backs); cout << ans << endl; } return 0; }
[["+", 75, 76, 0, 9, 0, 43, 39, 86, 0, 96]]
1
414
2
#include <algorithm> #include <cctype> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; long long int cnt(int kk, int a, int m) { long long int step = 0; do { kk = (kk * a) % m; step++; } while (kk != 1); return step; } long long int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } long long int lcm(int a, int b) { return a * b / gcd(a, b); } int main() { int a1, m1, a2, m2, a3, m3; while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) { int step; long long int step_final; if (a1 == 0 && a2 == 0 && a3 == 0 && m1 == 0 && m2 == 0 && m3 == 0) break; else { int x = 1; int y = 1; int z = 1; long long int step_x = cnt(x, a1, m1); long long int step_y = cnt(y, a2, m2); long long int step_z = cnt(z, a3, m3); step_final = lcm(step_x, lcm(step_y, step_z)); } cout << step_final << endl; } // while(1); return 0; }
#include <algorithm> #include <cctype> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; long long int cnt(int kk, int a, int m) { long long int step = 0; do { kk = (kk * a) % m; step++; } while (kk != 1); return step; } long long int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } long long int lcm(int a, int b) { return a / gcd(a, b) * b; } int main() { int a1, m1, a2, m2, a3, m3; while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3) { int step; long long int step_final; if (a1 == 0 && a2 == 0 && a3 == 0 && m1 == 0 && m2 == 0 && m3 == 0) break; else { int x = 1; int y = 1; int z = 1; long long int step_x = cnt(x, a1, m1); long long int step_y = cnt(y, a2, m2); long long int step_z = cnt(z, a3, m3); step_final = lcm(step_x, lcm(step_y, step_z)); } cout << step_final << endl; } // while(1); return 0; }
[["-", 8, 9, 0, 37, 0, 16, 31, 16, 17, 48], ["-", 8, 9, 0, 37, 0, 16, 31, 16, 12, 22], ["+", 0, 14, 8, 9, 0, 37, 0, 16, 17, 48], ["+", 0, 14, 8, 9, 0, 37, 0, 16, 12, 22]]
1
292
4
#include <algorithm> #include <iostream> #include <vector> typedef long long ll; using namespace std; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } ll lcm_n(vector<ll> &num) { int l = num[0]; for (int i = 0; i < num.size(); i++) l = lcm(l, num[i]); return l; } ll func(ll a, ll m) { ll cnt = 1, x = a % m; while (x != 1) x = (a * x) % m, cnt++; return cnt; } int main(void) { ll m[4], a[4]; while (true) { int b = 0; for (int i = 1; i < 4; i++) { cin >> a[i] >> m[i]; b = a[i] + m[i]; } if (b == 0) break; ll cnt = 0, x = 1, y = 1, z = 1; vector<ll> num; for (int i = 1; i < 4; i++) num.push_back(func(a[i], m[i])); cout << lcm_n(num) << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> typedef long long ll; using namespace std; ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } ll lcm_n(vector<ll> num) { ll l = num[0]; for (int i = 0; i < num.size(); i++) l = lcm(l, num[i]); return l; } ll func(ll a, ll m) { ll cnt = 1, x = a % m; while (x != 1) x = (a * x) % m, cnt++; return cnt; } int main(void) { ll m[4], a[4]; while (true) { int b = 0; for (int i = 1; i < 4; i++) { cin >> a[i] >> m[i]; b = a[i] + m[i]; } if (b == 0) break; ll cnt = 0, x = 1, y = 1, z = 1; vector<ll> num; for (int i = 1; i < 4; i++) num.push_back(func(a[i], m[i])); cout << lcm_n(num) << endl; // cout << lcm(lcm(num[0],num[1]),num[2])<< endl; } return 0; }
[["-", 8, 9, 0, 37, 0, 16, 31, 23, 0, 24], ["-", 8, 9, 0, 37, 0, 16, 31, 23, 0, 25], ["-", 49, 53, 54, 55, 0, 56, 49, 352, 0, 67], ["-", 0, 30, 0, 14, 8, 9, 0, 43, 39, 40], ["+", 0, 30, 0, 14, 8, 9, 0, 43, 39, 78]]
1
311
5
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; #define gcd(a, b) __gcd(a, b) #define lcm(a, b) a *b / gcd(a, b) int main() { int a1, m1, a2, m2, a3, m3; while (true) { cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3; if (a1 + m1 + a2 + m2 + a3 + m3 == 0) break; int xx = 0, yy = 0, zz = 0; ll x = 1, y = 1, z = 1; while (true) { x = a1 * x % m1; xx++; if (x == 1) break; } while (true) { y = a2 * y % m2; yy++; if (y == 1) break; } while (true) { z = a3 * z % m3; zz++; if (z == 1) break; } cout << lcm(xx, lcm(yy, zz)) << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; #define gcd(a, b) __gcd(a, b) #define lcm(a, b) a *b / gcd(a, b) int main() { int a1, m1, a2, m2, a3, m3; while (true) { cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3; if (a1 + m1 + a2 + m2 + a3 + m3 == 0) break; ll xx = 0, yy = 0, zz = 0; ll x = 1, y = 1, z = 1; while (true) { x = a1 * x % m1; xx++; if (x == 1) break; } while (true) { y = a2 * y % m2; yy++; if (y == 1) break; } while (true) { z = a3 * z % m3; zz++; if (z == 1) break; } cout << lcm(xx, lcm(yy, zz)) << endl; } return 0; }
[["-", 8, 9, 0, 52, 8, 9, 0, 43, 39, 40], ["+", 8, 9, 0, 52, 8, 9, 0, 43, 39, 78]]
1
206
2
#include <iostream> using namespace std; typedef long long ll; ll GCD(int a, int b) { if (a > b) { a ^= b; b = a xor b; a = a xor b; } return (a == 0) ? b : GCD(b % a, a); } ll LCM(int a, int b) { return a * b / GCD(a, b); } int main() { int a[3], m[3]; while (1) { cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2]; if (!a[0] && !m[0] && !a[1] && !m[1] && !a[2] && !m[2]) { break; } int move[3]; for (int i = 0; i < 3; ++i) { int val = 1; for (int j = 1; j <= m[i]; ++j) { val = a[i] * val % m[i]; if (val == 1) { move[i] = j; break; } } } ll ans = LCM(LCM(move[0], move[1]), move[2]); cout << ans << endl; } }
#include <iostream> using namespace std; typedef long long ll; ll GCD(ll a, ll b) { if (a > b) { a ^= b; b = a xor b; a = a xor b; } return (a == 0) ? b : GCD(b % a, a); } ll LCM(ll a, ll b) { return a * b / GCD(a, b); } int main() { int a[3], m[3]; while (1) { cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2]; if (!a[0] && !m[0] && !a[1] && !m[1] && !a[2] && !m[2]) { break; } int move[3]; for (int i = 0; i < 3; ++i) { int val = 1; for (int j = 1; j <= m[i]; ++j) { val = a[i] * val % m[i]; if (val == 1) { move[i] = j; break; } } } ll ans = LCM(LCM(move[0], move[1]), move[2]); cout << ans << endl; } }
[["-", 0, 14, 49, 53, 54, 55, 0, 56, 39, 40], ["+", 0, 14, 49, 53, 54, 55, 0, 56, 39, 78]]
1
291
8
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) range(i, 0, b) #define all(a) (a).begin(), (a).end() #define debug(x) cout << "debug " << x << endl; #define INF (1 << 29) using namespace std; void warshallFloyd(int alt[100][100], int n) { range(k, 1, n + 1) { range(i, 1, n + 1) { range(j, 1, n + 1) { alt[i][j] = min(alt[i][j], alt[i][k] + alt[k][j]); } } } } int main() { int a, b, c, d; int n, m; int atl[100][100]; rep(i, 100) rep(j, 100) atl[i][j] = INF; cin >> n >> m; rep(i, m) { scanf("%d%*c%d%*c%d%*c%d", &a, &b, &c, &d); atl[a][b] = c; atl[b][a] = d; } warshallFloyd(atl, n); scanf("%d%*c%d%*c%d%*c%d", &a, &b, &c, &d); cout << 50 - atl[a][b] - atl[b][a] - 30 << endl; }
#include <bits/stdc++.h> #define range(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, b) range(i, 0, b) #define all(a) (a).begin(), (a).end() #define debug(x) cout << "debug " << x << endl; #define INF (1 << 29) using namespace std; void warshallFloyd(int alt[100][100], int n) { range(k, 0, n + 1) { range(i, 0, n + 1) { range(j, 0, n + 1) { alt[i][j] = min(alt[i][j], alt[i][k] + alt[k][j]); } } } } int main() { int a, b, c, d; int n, m; int atl[100][100]; rep(i, 100) rep(j, 100) atl[i][j] = INF; cin >> n >> m; rep(i, m) { scanf("%d%*c%d%*c%d%*c%d", &a, &b, &c, &d); atl[a][b] = c; atl[b][a] = d; } warshallFloyd(atl, n); scanf("%d%*c%d%*c%d%*c%d", &a, &b, &c, &d); cout << c - atl[a][b] - atl[b][a] - d << endl; }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["-", 31, 16, 12, 16, 31, 16, 31, 16, 31, 13], ["+", 31, 16, 12, 16, 31, 16, 31, 16, 31, 22], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13], ["+", 0, 1, 0, 16, 31, 16, 12, 16, 12, 22]]
1
285
10
#include <stdio.h> #define MG 210000000 main() { int n, m; int i, j; int a[40], b[40], c[40], d[40]; int e, f, g = MG, h, l; int k[21][21], flg[21], dk[21], dkn; int flg2[21], dk2[21]; scanf("%d", &n); scanf("%d", &m); for (i = 1; i <= n; i++) { dk[i] = MG; flg[i] = 0; dk2[i] = MG; flg2[i] = 0; for (j = 0; j <= n; j++) { k[i][j] = MG; } } for (i = 1; i <= m; i++) { scanf("%d,%d,%d,%d", &a[i], &b[i], &c[i], &d[i]); } scanf("%d,%d,%d,%d", &e, &f, &dkn, &h); for (i = 1; i <= m; i++) { k[a[i]][b[i]] = c[i]; k[b[i]][a[i]] = d[i]; } dk[e] = 0; for (i = 0; i <= n; i++) { g = MG; for (j = 0; j <= n; j++) { if (flg[j] == 0 && dk[j] < g) { g = dk[j]; l = j; } } flg[l] = 1; for (j = 0; j <= n; j++) { if (dk[l] + k[l][j] < dk[j]) { dk[j] = dk[l] + k[l][j]; } } } dk2[f] = 0; for (i = 0; i <= n; i++) { g = MG; for (j = 0; j <= n; j++) { if (flg2[j] == 0 && dk2[j] < g) { g = dk2[j]; l = j; } } flg2[l] = 1; for (j = 0; j <= n; j++) { if (dk2[l] + k[l][j] < dk2[j]) { dk2[j] = dk2[l] + k[l][j]; } } } dkn = dkn - (h + dk[f] + dk2[e]); printf("%d\n", dkn); return 0; }
#include <stdio.h> #define MG 210000000 main() { int n, m; int i, j; int a[40], b[40], c[40], d[40]; int e, f, g = MG, h, l; int k[21][21], flg[21], dk[21], dkn; int flg2[21], dk2[21]; scanf("%d", &n); scanf("%d", &m); for (i = 0; i <= n; i++) { dk[i] = MG; flg[i] = 0; dk2[i] = MG; flg2[i] = 0; for (j = 0; j <= n; j++) { k[i][j] = MG; } } for (i = 1; i <= m; i++) { scanf("%d,%d,%d,%d", &a[i], &b[i], &c[i], &d[i]); } scanf("%d,%d,%d,%d", &e, &f, &dkn, &h); for (i = 1; i <= m; i++) { k[a[i]][b[i]] = c[i]; k[b[i]][a[i]] = d[i]; } dk[e] = 0; for (i = 0; i <= n; i++) { g = MG; for (j = 0; j <= n; j++) { if (flg[j] == 0 && dk[j] < g) { g = dk[j]; l = j; } } flg[l] = 1; for (j = 0; j <= n; j++) { if (dk[l] + k[l][j] < dk[j]) { dk[j] = dk[l] + k[l][j]; } } } dk2[f] = 0; for (i = 0; i <= n; i++) { g = MG; for (j = 0; j <= n; j++) { if (flg2[j] == 0 && dk2[j] < g) { g = dk2[j]; l = j; } } flg2[l] = 1; for (j = 0; j <= n; j++) { if (dk2[l] + k[l][j] < dk2[j]) { dk2[j] = dk2[l] + k[l][j]; } } } dkn = dkn - (h + dk[f] + dk2[e]); printf("%d\n", dkn); return 0; }
[["-", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13], ["+", 0, 14, 8, 9, 0, 7, 10, 11, 12, 13]]
1
587
2
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <iostream> #include <stdio.h> #define INF 50000000 using namespace std; int graf[20][20]; int kyo[20]; bool use[20]; void dijk(int ve, int st) { fill(kyo, kyo + ve, INF); fill(use, use + ve, false); kyo[st - 1] = 0; while (1) { int v = -1; for (int i = 0; i < ve; i++) { if (!use[i] && (v == -1 || kyo[i] < kyo[v])) v = i; } if (v == -1) break; use[v] = true; for (int i = 0; i < ve; i++) { kyo[i] = min(kyo[i], kyo[v] + graf[v][i]); } } return; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) graf[i][j] = INF; } for (int i = 0; i < m; i++) { int a, b, c, d; scanf("%d,%d,%d,%d", &a, &b, &c, &d); graf[a - 1][b - 1] = c; graf[b - 1][a - 1] = d; } int s, g, V, P; scanf("%d,%d,%d,%d", &s, &g, &V, &P); int sum = 0; dijk(n, s - 1); sum += kyo[g - 1]; dijk(n, g - 1); sum += kyo[s - 1]; cout << V - P - sum << endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <iostream> #include <stdio.h> #define INF 50000000 using namespace std; int graf[20][20]; int kyo[20]; bool use[20]; void dijk(int ve, int st) { fill(kyo, kyo + ve, INF); fill(use, use + ve, false); kyo[st] = 0; while (1) { int v = -1; for (int i = 0; i < ve; i++) { if (!use[i] && (v == -1 || kyo[i] < kyo[v])) v = i; } if (v == -1) break; use[v] = true; for (int i = 0; i < ve; i++) { kyo[i] = min(kyo[i], kyo[v] + graf[v][i]); } } return; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) graf[i][j] = INF; } for (int i = 0; i < m; i++) { int a, b, c, d; scanf("%d,%d,%d,%d", &a, &b, &c, &d); graf[a - 1][b - 1] = c; graf[b - 1][a - 1] = d; } int s, g, V, P; scanf("%d,%d,%d,%d", &s, &g, &V, &P); int sum = 0; dijk(n, s - 1); sum += kyo[g - 1]; dijk(n, g - 1); sum += kyo[s - 1]; cout << V - P - sum << endl; return 0; }
[["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
1
403
2
#include <iostream> using namespace std; const int INF = 10000000; int map[21][21]; int main(void) { int a, b, c; int d[4]; int ans; int n, m; for (a = 0; a <= 20; a++) { for (b = 0; b <= 20; b++) { map[b][a] = INF; if (b == a) map[b][a] = 0; } } cin >> n; cin >> m; for (a = 0; a < m; a++) { scanf("%d,%d,%d,%d", &d[0], &d[1], &d[2], &d[3]); map[d[0]][d[1]] = d[2]; map[d[1]][d[0]] = d[3]; } for (a = 1; a <= n; a++) { for (b = 1; b <= n; b++) { for (c = 1; c <= n; c++) { map[a][b] = min(map[a][b], map[a][c] + map[c][b]); } } } scanf("%d,%d,%d,%d", &d[0], &d[1], &d[2], &d[3]); ans = d[2] - d[3] - map[d[0]][d[1]] - map[d[1]][d[0]]; cout << ans << endl; return 0; }
#include <iostream> using namespace std; const int INF = 10000000; int map[21][21]; int main(void) { int a, b, c; int d[4]; int ans; int n, m; for (a = 0; a <= 20; a++) { for (b = 0; b <= 20; b++) { map[b][a] = INF; if (b == a) map[b][a] = 0; } } cin >> n; cin >> m; for (a = 0; a < m; a++) { scanf("%d,%d,%d,%d", &d[0], &d[1], &d[2], &d[3]); map[d[0]][d[1]] = d[2]; map[d[1]][d[0]] = d[3]; } for (a = 1; a <= n; a++) { for (b = 1; b <= n; b++) { for (c = 1; c <= n; c++) { map[c][b] = min(map[c][b], map[c][a] + map[a][b]); } } } scanf("%d,%d,%d,%d", &d[0], &d[1], &d[2], &d[3]); ans = d[2] - d[3] - map[d[0]][d[1]] - map[d[1]][d[0]]; cout << ans << endl; return 0; }
[["-", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 11, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 69, 28, 69, 341, 342, 0, 22], ["-", 0, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 16, 31, 69, 28, 69, 341, 342, 0, 22], ["-", 3, 4, 0, 16, 31, 69, 341, 342, 0, 22], ["+", 3, 4, 0, 16, 31, 69, 341, 342, 0, 22], ["-", 0, 16, 12, 69, 28, 69, 341, 342, 0, 22], ["+", 0, 16, 12, 69, 28, 69, 341, 342, 0, 22]]
1
357
10
#include <algorithm> #include <iostream> #include <vector> using namespace std; int n, m; int D[20][20]; int V; const int INF = 1 << 20; void solve() { for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { D[i][j] = min(D[i][j], D[i][k] + D[k][j]); } } } } int main() { fill(D[0], D[0] + 400, INF); char comma; cin >> n; V = n; cin >> m; for (int i = 0; i < m; i++) { int a, b, c, d; cin >> a >> comma >> b >> comma >> c >> comma >> d; D[a][b] = c; D[b][a] = d; } int x1, y1, x2, y2; cin >> x1 >> comma >> x2 >> comma >> y1 >> comma >> y2; solve(); cout << y1 - D[x1][x2] - D[x2][x1] - y2 + 1 << endl; cin >> n; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int n, m; int D[20][20]; int V; const int INF = 1 << 20; void solve() { for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { D[i][j] = min(D[i][j], D[i][k] + D[k][j]); } } } } int main() { fill(D[0], D[0] + 400, INF); char comma; cin >> n; V = n + 1; cin >> m; for (int i = 0; i < m; i++) { int a, b, c, d; cin >> a >> comma >> b >> comma >> c >> comma >> d; D[a][b] = c; D[b][a] = d; } int x1, y1, x2, y2; cin >> x1 >> comma >> x2 >> comma >> y1 >> comma >> y2; solve(); cout << y1 - D[x1][x2] - D[x2][x1] - y2 << endl; cin >> n; return 0; }
[["+", 8, 9, 0, 1, 0, 11, 12, 16, 17, 72], ["+", 8, 9, 0, 1, 0, 11, 12, 16, 12, 13], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 17, 72], ["-", 0, 1, 0, 16, 31, 16, 12, 16, 12, 13]]
1
285
10
def get_path(current, goal, edges, path) next_edges = edges.select{ |e| e[0,2].include?(current) } next_nodes = next_edges.map{ |e| e[0,2].select{|x| x!=current}.first } next_nodes.map do |n| if n == goal path + [n] elsif path.include?(n) nil else get_path(n, goal, edges, path + [n]) end end end def construct_paths(start, goal, edges, edges_cost) paths = [] i = 0 get_path(start, goal, edges, [start]).flatten.each do |x| if x.nil? i += 1 next elsif x == goal paths[i] << x i += 1 elsif paths[i].nil? paths[i] = [x] else paths[i] << x end end paths.compact end def cost_of_paths(start, goal, edges, edges_cost) costs = get_path(start, goal, edges, edges_cost).map do |path| cost = 0 path.each.with_index { |x,i|cost += edges_cost[[path[i], path[i+1]]] unless i == path.size-1 } cost end costs.min end if __FILE__ == $0 n = gets.to_i m = gets.to_i edges = [] edges_cost = {} m.times do a, b, c, d = gets.chomp.split(',').map(&:to_i) edges << [a, b, c, d] edges_cost[[a,b]] = c edges_cost[[b,a]] = d end start, goal, money, tree_cost = gets.chomp.split(',').map(&:to_i) total_cost = tree_cost + cost_of_paths(start, goal, edges, edges_cost) + cost_of_paths(goal, start, edges, edges_cost) p money - total_cost end
def get_path(current, goal, edges, path) next_edges = edges.select{ |e| e[0,2].include?(current) } next_nodes = next_edges.map{ |e| e[0,2].select{|x| x!=current}.first } next_nodes.map do |n| if n == goal path + [n] elsif path.include?(n) nil else get_path(n, goal, edges, path + [n]) end end end def construct_paths(start, goal, edges, edges_cost) paths = [] i = 0 get_path(start, goal, edges, [start]).flatten.each do |x| if x.nil? i += 1 next elsif x == goal paths[i] << x i += 1 elsif paths[i].nil? paths[i] = [x] else paths[i] << x end end paths.compact end def cost_of_paths(start, goal, edges, edges_cost) costs = construct_paths(start, goal, edges, edges_cost).map do |path| cost = 0 path.each.with_index { |x,i|cost += edges_cost[[path[i], path[i+1]]] unless i == path.size-1 } cost end costs.min end if __FILE__ == $0 n = gets.to_i m = gets.to_i edges = [] edges_cost = {} m.times do a, b, c, d = gets.chomp.split(',').map(&:to_i) edges << [a, b, c, d] edges_cost[[a,b]] = c edges_cost[[b,a]] = d end start, goal, money, tree_cost = gets.chomp.split(',').map(&:to_i) total_cost = tree_cost + cost_of_paths(start, goal, edges, edges_cost) + cost_of_paths(goal, start, edges, edges_cost) p money - total_cost end
[["-", 8, 736, 0, 662, 12, 652, 486, 652, 735, 22], ["+", 8, 736, 0, 662, 12, 652, 486, 652, 735, 22]]
4
401
2
#include <algorithm> #include <array> #include <bitset> #include <cctype> #include <cmath> #include <cstddef> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <valarray> #include <vector> #if (__cplusplus >= 201703L) #include <filesystem> namespace fs = std::filesystem; #endif // boost //#define USE_BOOST_IN_CODE #ifdef USE_BOOST_IN_CODE #include <boost/format.hpp> #include <boost/range.hpp> #include <boost/range/adaptor/strided.hpp> #include <boost/range/adaptor/transformed.hpp> #include <boost/range/algorithm.hpp> #include <boost/range/irange.hpp> #include <boost/range/numeric.hpp> #endif namespace { using ll = long long; using ull = unsigned long long; // vector template <class T> using vec = std::vector<T>; template <class T> using vv = vec<vec<T>>; constexpr std::size_t operator""_sz(ull n) { return std::size_t(n); } template <class T, class BinaryOperation> constexpr T fold(std::initializer_list<T> args, T init, BinaryOperation op) { return std::accumulate(args.begin(), args.end(), init, op); } // numeric_low namespace numeric { template <typename T> constexpr bool isOdd(T x) { return x % 2 != 0; } template <typename T> constexpr bool isEven(T x) { return x % 2 == 0; } // 最大公約数 template <class T> constexpr T gcd(const T x, const T y) { if (x < 0) return gcd(-x, y); if (y < 0) return gcd(x, -y); return (!y) ? x : gcd(y, x % y); } // 最小公倍数 template <class T> constexpr T lcm(const T x, const T y) { if (x < 0) return lcm(-x, y); if (y < 0) return lcm(x, -y); return x * (y / gcd(x, y)); } // 素数判定 template <class T> constexpr bool isPrime(const T x) { if (x <= 1) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; } } // namespace numeric // ビット演算 namespace bitOp { template <int N> constexpr std::size_t distanceBetween(const std::bitset<N> bit, std::size_t current, bool isNext = true) { if (current > N || current < 0) return -1; const int dir = isNext ? 1 : -1; for (auto i = current + dir; i >= 0 && i < N; i += dir) if (bit[i]) return (i - current) * dir; return -1; } // 次の立っているビットまでの距離 template <int N> constexpr std::size_t distanceBetweenNext(const std::bitset<N> bit, std::size_t current) { return distanceBetween(bit, current, true); } // 前の立っているビットまでの距離 template <int N> constexpr std::size_t distanceBetweenPrev(const std::bitset<N> bit, std::size_t current) { return distanceBetween(bit, current, false); } } // namespace bitOp #ifdef _MSVC_LANG #pragma region Vector #endif /////////////////////////////////////////////////////////// // Vector template <class T> struct Vec3D { T x, y, z; Vec3D() = default; constexpr Vec3D(T x, T y, T z) : x{x}, y{y}, z{z} {} constexpr Vec3D(T x, T y) : Vec3D(x, y, 0) {} static constexpr T Dot(const Vec3D &lhs, const Vec3D &rhs) { return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; } static constexpr Vec3D Cross(const Vec3D &lhs, const Vec3D &rhs) { return {lhs.y * rhs.z - lhs.z * rhs.y, lhs.z * rhs.x - lhs.x * rhs.z, lhs.x * rhs.y - lhs.y * rhs.x}; } constexpr T dot(const Vec3D &rhs) const { Dot(*this, rhs); } constexpr Vec3D cross(const Vec3D &rhs) const { Dot(*this, rhs); } T magnitude() const { return std::sqrt(sqrMagnitude()); } constexpr T sqrMagnitude() const { return x * x + y * y + z * z; } Vec3D normalized() const { return *this / magnitude(); } Vec3D normalize() { *this = normalized(); return *this; } constexpr bool isZero() const { return x == 0 && y == 0 && z == 0; } constexpr Vec3D operator+() const { return *this; } constexpr Vec3D operator-() const { return {-x, -y, -z}; } constexpr Vec3D operator+(Vec3D rhs) const { return {x + rhs.x, y + rhs.y, z + rhs.z}; } constexpr Vec3D operator-(Vec3D rhs) const { return {x - rhs.x, y - rhs.y, z - rhs.z}; } template <class U> constexpr Vec3D operator*(U rhs) const { return {x * rhs, y * rhs, z * rhs}; } template <class U> constexpr Vec3D operator/(U rhs) const { return {x / rhs, y / rhs, z / rhs}; } constexpr Vec3D &operator+=(const Vec3D &other) { x += other.x; y += other.y; z += other.z; return *this; } constexpr Vec3D &operator-=(const Vec3D &other) { x -= other.x; y -= other.y; z -= other.z; return *this; } template <class U> constexpr Vec3D &operator*=(U s) { x *= s; y *= s; z *= s; return *this; } template <class U> constexpr Vec3D &operator/=(U s) { x /= s; y /= s; z /= s; return *this; } }; template <class T, class U> inline constexpr Vec3D<T> operator*(U s, Vec3D<T> v) { return {s * v.x, s * v.y, s * v.z}; } // Vector /////////////////////////////////////////////////////////// #ifdef _MSVC_LANG #pragma endregion Vector #endif // argsからハッシュ値を作成 template <class... Args> std::size_t hash(Args... args) { return fold({static_cast<std::size_t>(args)...}, 0_sz, [](std::size_t seed, std::size_t x) { // uses magic number from boost return seed ^= x + 0x9e3779b9 + (seed << 6) + (seed >> 2); }); } template <class Begin, class End> void printAll(Begin beginIter, End endIter, std::string delimiter) { for (auto iter{beginIter}; iter != endIter; ++iter) { std::cout << *iter; if (std::distance(iter, endIter) != 1) std::cout << delimiter; } } template <class Container> void printAll(const Container &c, std::string delimiter) { printAll(std::cbegin(c), std::cend(c), delimiter); } template <class T> void printAll(const std::initializer_list<T> &ini, std::string delimiter) { printAll(ini.begin(), ini.end(), delimiter); } // template <class Container> // std::istream& operator>>(std::istream& is, Container& c) //{ // const auto end = std::end(c); // for (auto iter = std::begin(c); iter != end; ++iter) // is >> *iter; // return is; //} using State = std::string::const_iterator; class ParseError {}; int expression(State &begin); int term(State &begin); int number(State &begin); int factor(State &begin); // 四則演算の式をパースして、その評価結果を返す int expression(State &begin) { int ret{term(begin)}; while (true) { if (*begin == '+') { ++begin; ret += term(begin); } else if (*begin == '-') { ++begin; ret -= term(begin); } else { break; } } return ret; } // 乗算除算の式をパースして、その評価結果を返す int term(State &begin) { int ret{number(begin)}; while (true) { if (*begin == '*') { ++begin; ret *= factor(begin); } else if (*begin == '/') { ++begin; ret /= factor(begin); } else { break; } } return ret; } // 数をパースして、数を返す。 int number(State &begin) { int ret{}; while (isdigit(*begin)) { ret *= 10; ret += *begin - '0'; ++begin; } return ret; } int factor(State &begin) { if (*begin == '(') { ++begin; int ret = expression(begin); ++begin; return ret; } else { return number(begin); } } void solve() { using namespace std; int n; cin >> n; cin.ignore(); for (int i = 0; i < n; ++i) { string s; getline(cin, s); auto begin = s.cbegin(); cout << expression(begin) << endl; } } } // namespace int main() { solve(); return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cctype> #include <cmath> #include <cstddef> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <utility> #include <valarray> #include <vector> #if (__cplusplus >= 201703L) #include <filesystem> namespace fs = std::filesystem; #endif // boost //#define USE_BOOST_IN_CODE #ifdef USE_BOOST_IN_CODE #include <boost/format.hpp> #include <boost/range.hpp> #include <boost/range/adaptor/strided.hpp> #include <boost/range/adaptor/transformed.hpp> #include <boost/range/algorithm.hpp> #include <boost/range/irange.hpp> #include <boost/range/numeric.hpp> #endif namespace { using ll = long long; using ull = unsigned long long; // vector template <class T> using vec = std::vector<T>; template <class T> using vv = vec<vec<T>>; constexpr std::size_t operator""_sz(ull n) { return std::size_t(n); } template <class T, class BinaryOperation> constexpr T fold(std::initializer_list<T> args, T init, BinaryOperation op) { return std::accumulate(args.begin(), args.end(), init, op); } // numeric_low namespace numeric { template <typename T> constexpr bool isOdd(T x) { return x % 2 != 0; } template <typename T> constexpr bool isEven(T x) { return x % 2 == 0; } // 最大公約数 template <class T> constexpr T gcd(const T x, const T y) { if (x < 0) return gcd(-x, y); if (y < 0) return gcd(x, -y); return (!y) ? x : gcd(y, x % y); } // 最小公倍数 template <class T> constexpr T lcm(const T x, const T y) { if (x < 0) return lcm(-x, y); if (y < 0) return lcm(x, -y); return x * (y / gcd(x, y)); } // 素数判定 template <class T> constexpr bool isPrime(const T x) { if (x <= 1) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; } } // namespace numeric // ビット演算 namespace bitOp { template <int N> constexpr std::size_t distanceBetween(const std::bitset<N> bit, std::size_t current, bool isNext = true) { if (current > N || current < 0) return -1; const int dir = isNext ? 1 : -1; for (auto i = current + dir; i >= 0 && i < N; i += dir) if (bit[i]) return (i - current) * dir; return -1; } // 次の立っているビットまでの距離 template <int N> constexpr std::size_t distanceBetweenNext(const std::bitset<N> bit, std::size_t current) { return distanceBetween(bit, current, true); } // 前の立っているビットまでの距離 template <int N> constexpr std::size_t distanceBetweenPrev(const std::bitset<N> bit, std::size_t current) { return distanceBetween(bit, current, false); } } // namespace bitOp #ifdef _MSVC_LANG #pragma region Vector #endif /////////////////////////////////////////////////////////// // Vector template <class T> struct Vec3D { T x, y, z; Vec3D() = default; constexpr Vec3D(T x, T y, T z) : x{x}, y{y}, z{z} {} constexpr Vec3D(T x, T y) : Vec3D(x, y, 0) {} static constexpr T Dot(const Vec3D &lhs, const Vec3D &rhs) { return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z; } static constexpr Vec3D Cross(const Vec3D &lhs, const Vec3D &rhs) { return {lhs.y * rhs.z - lhs.z * rhs.y, lhs.z * rhs.x - lhs.x * rhs.z, lhs.x * rhs.y - lhs.y * rhs.x}; } constexpr T dot(const Vec3D &rhs) const { Dot(*this, rhs); } constexpr Vec3D cross(const Vec3D &rhs) const { Dot(*this, rhs); } T magnitude() const { return std::sqrt(sqrMagnitude()); } constexpr T sqrMagnitude() const { return x * x + y * y + z * z; } Vec3D normalized() const { return *this / magnitude(); } Vec3D normalize() { *this = normalized(); return *this; } constexpr bool isZero() const { return x == 0 && y == 0 && z == 0; } constexpr Vec3D operator+() const { return *this; } constexpr Vec3D operator-() const { return {-x, -y, -z}; } constexpr Vec3D operator+(Vec3D rhs) const { return {x + rhs.x, y + rhs.y, z + rhs.z}; } constexpr Vec3D operator-(Vec3D rhs) const { return {x - rhs.x, y - rhs.y, z - rhs.z}; } template <class U> constexpr Vec3D operator*(U rhs) const { return {x * rhs, y * rhs, z * rhs}; } template <class U> constexpr Vec3D operator/(U rhs) const { return {x / rhs, y / rhs, z / rhs}; } constexpr Vec3D &operator+=(const Vec3D &other) { x += other.x; y += other.y; z += other.z; return *this; } constexpr Vec3D &operator-=(const Vec3D &other) { x -= other.x; y -= other.y; z -= other.z; return *this; } template <class U> constexpr Vec3D &operator*=(U s) { x *= s; y *= s; z *= s; return *this; } template <class U> constexpr Vec3D &operator/=(U s) { x /= s; y /= s; z /= s; return *this; } }; template <class T, class U> inline constexpr Vec3D<T> operator*(U s, Vec3D<T> v) { return {s * v.x, s * v.y, s * v.z}; } // Vector /////////////////////////////////////////////////////////// #ifdef _MSVC_LANG #pragma endregion Vector #endif // argsからハッシュ値を作成 template <class... Args> std::size_t hash(Args... args) { return fold({static_cast<std::size_t>(args)...}, 0_sz, [](std::size_t seed, std::size_t x) { // uses magic number from boost return seed ^= x + 0x9e3779b9 + (seed << 6) + (seed >> 2); }); } template <class Begin, class End> void printAll(Begin beginIter, End endIter, std::string delimiter) { for (auto iter{beginIter}; iter != endIter; ++iter) { std::cout << *iter; if (std::distance(iter, endIter) != 1) std::cout << delimiter; } } template <class Container> void printAll(const Container &c, std::string delimiter) { printAll(std::cbegin(c), std::cend(c), delimiter); } template <class T> void printAll(const std::initializer_list<T> &ini, std::string delimiter) { printAll(ini.begin(), ini.end(), delimiter); } // template <class Container> // std::istream& operator>>(std::istream& is, Container& c) //{ // const auto end = std::end(c); // for (auto iter = std::begin(c); iter != end; ++iter) // is >> *iter; // return is; //} using State = std::string::const_iterator; class ParseError {}; int expression(State &begin); int term(State &begin); int number(State &begin); int factor(State &begin); // 四則演算の式をパースして、その評価結果を返す int expression(State &begin) { int ret{term(begin)}; while (true) { if (*begin == '+') { ++begin; ret += term(begin); } else if (*begin == '-') { ++begin; ret -= term(begin); } else { break; } } return ret; } // 乗算除算の式をパースして、その評価結果を返す int term(State &begin) { int ret{factor(begin)}; while (true) { if (*begin == '*') { ++begin; ret *= factor(begin); } else if (*begin == '/') { ++begin; ret /= factor(begin); } else { break; } } return ret; } // 数をパースして、数を返す。 int number(State &begin) { int ret{}; while (isdigit(*begin)) { ret *= 10; ret += *begin - '0'; ++begin; } return ret; } int factor(State &begin) { if (*begin == '(') { ++begin; //( int ret = expression(begin); ++begin; //) return ret; } else { return number(begin); } } void solve() { using namespace std; int n; cin >> n; cin.ignore(); for (int i = 0; i < n; ++i) { string s; getline(cin, s); auto begin = s.cbegin(); cout << expression(begin) << endl; } } } // namespace int main() { solve(); return 0; }
[["-", 0, 43, 49, 50, 51, 83, 0, 2, 63, 22], ["+", 0, 43, 49, 50, 51, 83, 0, 2, 63, 22]]
1
1,881
2
#include <iostream> #include <string> using namespace std; string S; size_t cur; int digit() { return S[cur++] - '0'; } int number() { int n = digit(); while (cur < S.size() && isdigit(S[cur])) { n = n * 10 + digit(); } return n; } int expression(); int factor() { if (isdigit(S[cur])) return number(); cur++; int n = expression(); cur++; return n; } int term() { int t = factor(); for (; cur < S.size() && (S[cur] == '*' || S[cur] == '/');) { if (S[cur++] == '*') t *= factor(); else t /= factor(); } return t; } int expression() { int t = term(); for (; cur < S.size() && (S[cur] == '+' || S[cur] == '-');) { if (S[cur++] == '+') t += term(); else t -= term(); } return t; } int main() { int n; cin >> n; for (; n--;) { cin >> S; cout << expression() << endl; } return 0; }
#include <iostream> #include <string> using namespace std; string S; size_t cur; int digit() { return S[cur++] - '0'; } int number() { int n = digit(); while (cur < S.size() && isdigit(S[cur])) { n = n * 10 + digit(); } return n; } int expression(); int factor() { if (isdigit(S[cur])) return number(); cur++; int n = expression(); cur++; return n; } int term() { int t = factor(); for (; cur < S.size() && (S[cur] == '*' || S[cur] == '/');) { if (S[cur++] == '*') t *= factor(); else t /= factor(); } return t; } int expression() { int t = term(); for (; cur < S.size() && (S[cur] == '+' || S[cur] == '-');) { if (S[cur++] == '+') t += term(); else t -= term(); } return t; } int main() { int n; cin >> n; for (; n--;) { cur = 0; cin >> S; cout << expression() << endl; } return 0; }
[["+", 0, 7, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35]]
1
306
4
#include <iostream> #include <string> using namespace std; int expression(); int term(); int factor(); string exp; int p; int expression() { int value = term(); if (exp[p] == '+' || exp[p] == '-') { if (exp[p] == '+') { p++; value += term(); } else { p++; value -= term(); } } return value; } int term() { int value = factor(); if (exp[p] == '*' || exp[p] == '/') { if (exp[p] == '*') { p++; value *= factor(); } else { p++; value /= factor(); } } return value; } int factor() { int value = 0; if (exp[p] == '(') { p++; value = expression(); p++; } else { while (isdigit(exp[p])) { value = value * 10 + exp[p++] - '0'; } } return value; } int main() { int tcase; cin >> tcase; for (int i = 0; i < tcase; i++) { cin >> exp; p = 0; cout << expression() << endl; } return 0; }
#include <iostream> #include <string> using namespace std; int expression(); int term(); int factor(); string exp; int p; int expression() { int value = term(); while (exp[p] == '+' || exp[p] == '-') { if (exp[p] == '+') { p++; value += term(); } else { p++; value -= term(); } } return value; } int term() { int value = factor(); while (exp[p] == '*' || exp[p] == '/') { if (exp[p] == '*') { p++; value *= factor(); } else { p++; value /= factor(); } } return value; } int factor() { int value = 0; if (exp[p] == '(') { p++; value = expression(); p++; } else { while (isdigit(exp[p])) { value = value * 10 + exp[p++] - '0'; } } return value; } int main() { int tcase; cin >> tcase; for (int i = 0; i < tcase; i++) { cin >> exp; p = 0; cout << expression() << endl; } return 0; }
[["-", 0, 30, 0, 14, 8, 9, 0, 57, 0, 121], ["+", 0, 30, 0, 14, 8, 9, 0, 52, 0, 89]]
1
291
4
#include <cstdlib> #include <iostream> #include <sstream> #include <stack> #include <string> using namespace std; string its(int n) { stringstream s; s << n; return s.str(); } stack<string> mep(stack<string> me, string s) { int a, b; b = atoi(me.top().c_str()); me.pop(); a = atoi(me.top().c_str()); me.pop(); if (s == "+") me.push(its(a + b)); else if (s == "-") me.push(its(a - b)); else if (s == "*") me.push(its(a * b)); else if (s == "/") me.push(its(a / b)); return me; } int main() { int h, i, j; int n, len; string s; stack<string> me, st; cin >> n; for (h = 0; h < n; h++) { cin >> s; len = s.length() - 1; for (i = 0; i < len; i++) { if ('1' <= s[i] && s[i] <= '9') { for (j = i; j < len; j++) if (s[j] < '0' || '9' < s[j]) break; me.push(s.substr(i, j - i)); i = j - 1; } else if (s[i] == '(') { st.push(s.substr(i, 1)); } else if (s[i] == ')') { while (st.top() != "(") { me = mep(me, st.top()); st.pop(); } st.pop(); } else if (s[i] == '+' || s[i] == '-') { if (st.empty()) ; else if (st.top() == "(") ; else { while (st.empty() == 0) { if (st.top() == "(") break; else { me = mep(me, st.top()); st.pop(); } } } st.push(s.substr(i, 1)); } else if (s[i] == '*' || s[i] == '/') { if (st.empty()) ; else if (st.top() == "(") ; else if (st.top() == "+" || st.top() == "-") ; else { while (st.empty() == 0) { if (st.top() == "(") break; else if (st.top() == "+" || st.top() == "-") break; else { me = mep(me, st.top()); st.pop(); } } } st.push(s.substr(i, 1)); } } while (st.empty() == 0) { me = mep(me, st.top()); st.pop(); } cout << me.top() << endl; me.pop(); } return 0; }
#include <cstdlib> #include <iostream> #include <sstream> #include <stack> #include <string> using namespace std; string its(int n) { stringstream s; s << n; return s.str(); } stack<string> mep(stack<string> me, string s) { int a, b; b = atoi(me.top().c_str()); me.pop(); a = atoi(me.top().c_str()); me.pop(); if (s == "+") me.push(its(a + b)); else if (s == "-") me.push(its(a - b)); else if (s == "*") me.push(its(a * b)); else if (s == "/") me.push(its(a / b)); return me; } int main() { int h, i, j; int n, len; string s; stack<string> me, st; cin >> n; for (h = 0; h < n; h++) { cin >> s; len = s.length() - 1; for (i = 0; i < len; i++) { if ('0' <= s[i] && s[i] <= '9') { for (j = i; j < len; j++) if (s[j] < '0' || '9' < s[j]) break; me.push(s.substr(i, j - i)); i = j - 1; } else if (s[i] == '(') { st.push(s.substr(i, 1)); } else if (s[i] == ')') { while (st.top() != "(") { me = mep(me, st.top()); st.pop(); } st.pop(); } else if (s[i] == '+' || s[i] == '-') { if (st.empty()) ; else if (st.top() == "(") ; else { while (st.empty() == 0) { if (st.top() == "(") break; else { me = mep(me, st.top()); st.pop(); } } } st.push(s.substr(i, 1)); } else if (s[i] == '*' || s[i] == '/') { if (st.empty()) ; else if (st.top() == "(") ; else if (st.top() == "+" || st.top() == "-") ; else { while (st.empty() == 0) { if (st.top() == "(") break; else if (st.top() == "+" || st.top() == "-") break; else { me = mep(me, st.top()); st.pop(); } } } st.push(s.substr(i, 1)); } } while (st.empty() == 0) { me = mep(me, st.top()); st.pop(); } cout << me.top() << endl; me.pop(); } return 0; }
[["-", 15, 339, 51, 16, 31, 16, 31, 103, 0, 125], ["+", 15, 339, 51, 16, 31, 16, 31, 103, 0, 125]]
1
744
2
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vint; typedef pair<int, int> pint; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) template <class T, class C> void chmax(T &a, C b) { if (a < b) a = b; } template <class T, class C> void chmin(T &a, C b) { if (b < a) a = b; } template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); } class CalcExpression { typedef string::const_iterator State; class ParseError {}; public: int expression(State &begin) { int ret = term(begin); while (true) { if (*begin == '+') { begin++; ret += term(begin); } else if (*begin == '-') { begin++; ret -= term(begin); } else break; } return ret; } private: int term(State &begin) { int ret = number(begin); while (true) { if (*begin == '*') { begin++; ret *= factor(begin); } else if (*begin == '/') { begin++; ret /= factor(begin); } else break; } return ret; } int factor(State &begin) { int ret = 0; if (*begin == '(') { begin++; ret = expression(begin); begin++; } else return number(begin); return ret; } int number(State &begin) { int ret = 0; while (isdigit(*begin)) { ret *= 10; ret += *begin - '0'; begin++; } return ret; } }; int main() { int n; cin >> n; CalcExpression ce; while (n--) { string expression, buf; cin >> expression; string::const_iterator it = expression.begin(); cout << ce.expression(it) << endl; } }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const double EPS = 1e-9; typedef vector<int> vint; typedef pair<int, int> pint; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) template <class T, class C> void chmax(T &a, C b) { if (a < b) a = b; } template <class T, class C> void chmin(T &a, C b) { if (b < a) a = b; } template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); } class CalcExpression { typedef string::const_iterator State; class ParseError {}; public: int expression(State &begin) { int ret = term(begin); while (true) { if (*begin == '+') { begin++; ret += term(begin); } else if (*begin == '-') { begin++; ret -= term(begin); } else break; } return ret; } private: int term(State &begin) { int ret = factor(begin); while (true) { if (*begin == '*') { begin++; ret *= factor(begin); } else if (*begin == '/') { begin++; ret /= factor(begin); } else break; } return ret; } int factor(State &begin) { int ret = 0; if (*begin == '(') { begin++; ret = expression(begin); begin++; } else return number(begin); return ret; } int number(State &begin) { int ret = 0; while (isdigit(*begin)) { ret *= 10; ret += *begin - '0'; begin++; } return ret; } }; int main() { int n; cin >> n; CalcExpression ce; while (n--) { string expression, buf; cin >> expression; string::const_iterator it = expression.begin(); cout << ce.expression(it) << endl; } }
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
1
510
2
#include <cctype> #include <iostream> #include <string> using namespace std; typedef string::const_iterator State; int expression(State &begin); int number(State &begin) { int ret = 0; for (; isdigit(*begin); begin++) { ret = ret * 10 + *begin - '0'; } return ret; } int factor(State &begin) { if (*begin == '(') { begin++; int ret = expression(begin); begin++; return ret; } else { return number(begin); } } int term(State &begin) { int ret = number(begin); while (true) { if (*begin == '*') { begin++; ret *= factor(begin); } else if (*begin == '/') { begin++; ret /= factor(begin); } else { break; } } return ret; } int expression(State &begin) { int ret = term(begin); while (true) { if (*begin == '+') { begin++; ret += term(begin); } else if (*begin == '-') { begin++; ret -= term(begin); } else { break; } } return ret; } int main() { int N; cin >> N; cin.ignore(); for (int i = 0; i < N; i++) { string s; getline(cin, s); State begin = s.begin(); cout << expression(begin) << endl; } return 0; }
#include <cctype> #include <iostream> #include <string> using namespace std; typedef string::const_iterator State; int expression(State &begin); int number(State &begin) { int ret = 0; for (; isdigit(*begin); begin++) { ret = ret * 10 + *begin - '0'; } return ret; } int factor(State &begin) { if (*begin == '(') { begin++; int ret = expression(begin); begin++; return ret; } else { return number(begin); } } int term(State &begin) { int ret = factor(begin); while (true) { if (*begin == '*') { begin++; ret *= factor(begin); } else if (*begin == '/') { begin++; ret /= factor(begin); } else { break; } } return ret; } int expression(State &begin) { int ret = term(begin); while (true) { if (*begin == '+') { begin++; ret += term(begin); } else if (*begin == '-') { begin++; ret -= term(begin); } else { break; } } return ret; } int main() { int N; cin >> N; cin.ignore(); for (int i = 0; i < N; i++) { string s; getline(cin, s); State begin = s.begin(); cout << expression(begin) << endl; } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
1
328
2
#include <cctype> #include <iostream> #include <string> using namespace std; typedef string::const_iterator State; int number(State &begin); int term(State &begin); int expression(State &begin); int factor(State &begin); int number(State &begin) { int ret = 0; while (isdigit(*begin)) { ret *= 10; ret += *begin - '0'; begin++; } // cout << "number :: " << ret << endl; return ret; } int term(State &begin) { int ret = number(begin); while (1) { if (*begin == '*') { begin++; // cout << "go factor // *" << endl; ret *= factor(begin); } else if (*begin == '/') { begin++; // cout << "go factor // /" << endl; ret /= factor(begin); } else break; } // cout << "term :: " << ret << endl; return ret; } int expression(State &begin) { int ret = term(begin); while (1) { if (*begin == '+') { begin++; // cout << "go term // +" << endl; ret += term(begin); } else if (*begin == '-') { begin++; // cout << "go term // -" << endl; ret -= term(begin); } else break; } // cout << "expression :: " << ret << endl; return ret; } int factor(State &begin) { if (*begin == '(') { begin++; // cout << "go expression" << endl; int ret = expression(begin); // cout << "factor :: " << ret << endl; begin++; return ret; } else return number(begin); } int main(void) { int n; cin >> n; cin.ignore(); for (int i = 0; i < n; i++) { string s; getline(cin, s); State begin = s.begin(); // cout << "Start !!!" << endl; cout << expression(begin) << endl; } return 0; }
#include <cctype> #include <iostream> #include <string> using namespace std; typedef string::const_iterator State; int number(State &begin); int term(State &begin); int expression(State &begin); int factor(State &begin); int number(State &begin) { int ret = 0; while (isdigit(*begin)) { ret *= 10; ret += *begin - '0'; begin++; } // cout << "number :: " << ret << endl; return ret; } int term(State &begin) { int ret = factor(begin); while (1) { if (*begin == '*') { begin++; // cout << "go factor // *" << endl; ret *= factor(begin); } else if (*begin == '/') { begin++; // cout << "go factor // /" << endl; ret /= factor(begin); } else break; } // // cout << "term :: " << ret << endl; return ret; } int expression(State &begin) { int ret = term(begin); while (1) { if (*begin == '+') { begin++; // // cout << "go term // +" << endl; ret += term(begin); } else if (*begin == '-') { begin++; // // cout << "go term // -" << endl; ret -= term(begin); } else break; } // // cout << "expression :: " << ret << endl; return ret; } int factor(State &begin) { if (*begin == '(') { begin++; // // cout << "go expression" << endl; int ret = expression(begin); // // cout << "factor :: " << ret << endl; begin++; return ret; } else return number(begin); } int main(void) { int n; cin >> n; cin.ignore(); for (int i = 0; i < n; i++) { string s; getline(cin, s); State begin = s.begin(); // cout << "Start !!!" << endl; cout << expression(begin) << endl; } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
1
356
2
#include <algorithm> #include <cctype> #include <cstdio> #include <iostream> #include <string> using namespace std; typedef string::const_iterator State; int expr(State &begin); int term(State &begin); int number(State &begin); int factor(State &begin); int number(State &begin) { int ret = 0; while (isdigit(*begin)) { ret *= 10; ret += (*begin) - '0'; begin++; } return ret; } int term(State &begin) { int ret = number(begin); for (;;) { if (*begin == '*') { begin++; ret *= factor(begin); } else if (*begin == '/') { begin++; ret /= factor(begin); } else break; } return ret; } int expr(State &begin) { int ret = term(begin); for (;;) { if (*begin == '+') { begin++; ret += term(begin); } else if (*begin == '-') { begin++; ret -= term(begin); } else { break; } } return ret; } int factor(State &begin) { if (*begin == '(') { begin++; int ret = expr(begin); begin++; return ret; } else { return number(begin); } } int main() { int N; cin >> N; cin.ignore(); for (int i = 0; i < N; i++) { string str; getline(cin, str); State begin = str.begin(); int ans = expr(begin); cout << ans << endl; } return 0; }
#include <algorithm> #include <cctype> #include <cstdio> #include <iostream> #include <string> using namespace std; typedef string::const_iterator State; int expr(State &begin); int term(State &begin); int number(State &begin); int factor(State &begin); int number(State &begin) { int ret = 0; while (isdigit(*begin)) { ret *= 10; ret += (*begin) - '0'; begin++; } return ret; } int term(State &begin) { int ret = factor(begin); for (;;) { if (*begin == '*') { begin++; ret *= factor(begin); } else if (*begin == '/') { begin++; ret /= factor(begin); } else break; } return ret; } int expr(State &begin) { int ret = term(begin); for (;;) { if (*begin == '+') { begin++; ret += term(begin); } else if (*begin == '-') { begin++; ret -= term(begin); } else { break; } } return ret; } int factor(State &begin) { if (*begin == '(') { begin++; int ret = expr(begin); begin++; return ret; } else { return number(begin); } } int main() { int N; cin >> N; cin.ignore(); for (int i = 0; i < N; i++) { string str; getline(cin, str); State begin = str.begin(); int ans = expr(begin); cout << ans << endl; } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22], ["+", 8, 9, 0, 43, 49, 50, 51, 2, 63, 22]]
1
362
2
# coding: utf-8 # Convert String to List def String2List(s): L = []; tmp = "" for i in s: if i.isdigit(): tmp += i else: if tmp != "": L.append(tmp) tmp = "" L.append(i) if tmp != "": L.append(tmp) return L # generate Reverse Polish Notation def Generate_RPN(L): S, L2 = [], [] table = {"*": 1, "/": 1, "+": 0, "-": 0, "(": -1, ")": -1} for i in L: if i.isdigit(): L2.append(i) elif i == "(": S.append(i) elif i == ")": while S[-1] != "(": L2.append(S.pop()) S.pop() else: while len(S) != 0 and (table[S[-1]] >= table[i]): L2.append(S.pop()) S.append(i) while len(S) != 0: L2.append(S.pop()) return L2 def Calculate_RPN(L): St = [] for i in L: if i == '+': St.append(int(St.pop()) + int(St.pop())) elif i == '-': St.append(-int(St.pop()) + int(St.pop())) elif i == '*': St.append(int(St.pop()) * int(St.pop())) elif i == '/': a = int(St.pop()) b = float(St.pop()) St.append(b/a) else: St.append(i) return St[0] n = int(input()) for _ in range(n): s = input() print(Calculate_RPN(Generate_RPN(String2List(s))))
# coding: utf-8 # Convert String to List def String2List(s): L = []; tmp = "" for i in s: if i.isdigit(): tmp += i else: if tmp != "": L.append(tmp) tmp = "" L.append(i) if tmp != "": L.append(tmp) return L # generate Reverse Polish Notation def Generate_RPN(L): S, L2 = [], [] table = {"*": 1, "/": 1, "+": 0, "-": 0, "(": -1, ")": -1} for i in L: if i.isdigit(): L2.append(i) elif i == "(": S.append(i) elif i == ")": while S[-1] != "(": L2.append(S.pop()) S.pop() else: while len(S) != 0 and (table[S[-1]] >= table[i]): L2.append(S.pop()) S.append(i) while len(S) != 0: L2.append(S.pop()) return L2 def Calculate_RPN(L): St = [] for i in L: if i == '+': St.append(int(St.pop()) + int(St.pop())) elif i == '-': St.append(-int(St.pop()) + int(St.pop())) elif i == '*': St.append(int(St.pop()) * int(St.pop())) elif i == '/': a = int(St.pop()) b = float(St.pop()) St.append(b/a) else: St.append(i) return St[0] n = int(input()) for _ in range(n): s = input() print(Calculate_RPN(Generate_RPN(String2List(s[:-1]))))
[["+", 3, 4, 0, 652, 3, 4, 0, 206, 0, 70], ["+", 0, 652, 3, 4, 0, 206, 206, 663, 0, 102], ["+", 3, 4, 0, 206, 206, 663, 0, 664, 17, 33], ["+", 3, 4, 0, 206, 206, 663, 0, 664, 28, 612], ["+", 3, 4, 0, 652, 3, 4, 0, 206, 0, 73]]
5
438
5
R={"*":2,"/":2,"+":1,"-":1,"(":0,")":0} for _ in[0]*int(input()): L=[];t='' for e in input()[:-1]: if e.isdigit():t+=e else: if t:L+=[t];t='' L+=e if t:L+=[t] P,S=[],[] for i in L: if"("==i:S+=i elif")"==i: while"("!=S[-1]:P+=S.pop() S.pop() elif i in R: while S and R[S[-1]]>=R[i]:P+=S.pop() S+=i else:P+=[i] while S:P+=S.pop() for x in P: S+=[str(eval(S.pop(-2)+p+S.pop()))if x in"+-*/"else x] print(*S)
R={"*":2,"/":2,"+":1,"-":1,"(":0,")":0} for _ in[0]*int(input()): L=[];t='' for e in input()[:-1]: if e.isdigit():t+=e else: if t:L+=[t];t='' L+=e if t:L+=[t] P,S=[],[] for i in L: if"("==i:S+=i elif")"==i: while"("!=S[-1]:P+=S.pop() S.pop() elif i in R: while S and R[S[-1]]>=R[i]:P+=S.pop() S+=i else:P+=[i] while S:P+=S.pop() for x in P:S+=[str(int(eval(S.pop(-2)+x+S.pop())))if x in"+-*/"else x] print(*S)
[["+", 0, 41, 0, 652, 3, 4, 0, 652, 63, 22], ["+", 0, 652, 3, 4, 0, 652, 3, 4, 0, 24], ["-", 0, 652, 3, 4, 0, 657, 31, 657, 12, 22], ["+", 0, 652, 3, 4, 0, 657, 31, 657, 12, 22], ["+", 3, 4, 0, 657, 12, 652, 3, 4, 0, 25]]
5
256
5
# -*- coding: utf-8 -*- # 写経した import re class Num: def __str__(self): return str(self.x) def __init__(self, value): self.x = value def __add__(self, value): return o(self.x + value.x) def __sub__(self, value): return o(self.x - value.x) def __mul__(self, value): return o(self.x * value.x) def __truediv__(self, value): return o(int(self.x / value.x)) N = int(input()) for i in range(N): s = input()[:-1] s = re.sub(r'(\d+)',r'Num(\1)',s) print(eval(s))
# -*- coding: utf-8 -*- # 写経した import re class Num: def __str__(self): return str(self.x) def __init__(self, value): self.x = value def __add__(self, value): return Num(self.x + value.x) def __sub__(self, value): return Num(self.x - value.x) def __mul__(self, value): return Num(self.x * value.x) def __truediv__(self, value): return Num(int(self.x / value.x)) N = int(input()) for i in range(N): s = input()[:-1] s = re.sub(r'(\d+)',r'Num(\1)',s) print(eval(s))
[["-", 0, 14, 8, 196, 0, 37, 0, 652, 63, 22], ["+", 0, 14, 8, 196, 0, 37, 0, 652, 63, 22]]
5
161
8
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] l = "11010: :11011:.:11100:,:11101:-:11110:':11111:?:00000:A:00001:B:00010:C:00011:D:00100:E:00101:F:00110:G:00111:H:01000:I:01001:J:01010:K:01011:L:01100:M:01101:N:01110:O:01111:P:10000:Q:10001:R:10010:S:10011:T:10100:U:10101:V:10110:W:10111:X:11000:Y:11001:Z" .split(":"); String[] l2 = "101:000000:000011:10010001:010001:000001:100101:10011010:0101:0001:110:01001:10011011:010000:0111:10011000:0110:00100:10011001:10011110:00101:111:10011111:1000:00110:00111:10011100:10011101:000010:10010010:10010011:10010000" .split(":"); String[] n2 = new String[l2.length]; String[] n = new String[l.length / 2]; char[] c = new char[l.length / 2]; for (int i = 0; i < l.length / 2; i++) { n[i] = l[i * 2]; n2[i] = l2[i]; c[i] = l[i * 2 + 1].charAt(0); } while (sc.hasNext()) { String t = sc.nextLine(); String a = ""; for (char cc : t.toCharArray()) { for (int i = 0; i < c.length; i++) { if (cc == c[i]) { a += n[i]; } } } int start = 0; String r = ""; boolean f = true; while (f) { for (int i = 0; i < l2.length; i++) { if (start + l2[i].length() > a.length()) { continue; } if (a.substring(start, start + l2[i].length()).equals(l2[i])) { r += c[i]; start += l2[i].length(); f = false; break; } } if (f) { break; } else { f = true; } } System.out.println(r); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String[] l = "11010: :11011:.:11100:,:11101:-:11110:':11111:?:00000:A:00001:B:00010:C:00011:D:00100:E:00101:F:00110:G:00111:H:01000:I:01001:J:01010:K:01011:L:01100:M:01101:N:01110:O:01111:P:10000:Q:10001:R:10010:S:10011:T:10100:U:10101:V:10110:W:10111:X:11000:Y:11001:Z" .split(":"); String[] l2 = "101:010001:000011:10010001:000000:000001:100101:10011010:0101:0001:110:01001:10011011:010000:0111:10011000:0110:00100:10011001:10011110:00101:111:10011111:1000:00110:00111:10011100:10011101:000010:10010010:10010011:10010000" .split(":"); String[] n2 = new String[l2.length]; String[] n = new String[l.length / 2]; char[] c = new char[l.length / 2]; for (int i = 0; i < l.length / 2; i++) { n[i] = l[i * 2]; n2[i] = l2[i]; c[i] = l[i * 2 + 1].charAt(0); } while (sc.hasNext()) { String t = sc.nextLine(); String a = ""; for (char cc : t.toCharArray()) { for (int i = 0; i < c.length; i++) { if (cc == c[i]) { a += n[i]; } } } int start = 0; String r = ""; boolean f = true; while (f) { for (int i = 0; i < l2.length; i++) { if (start + l2[i].length() > a.length()) { continue; } if (a.substring(start, start + l2[i].length()).equals(l2[i])) { r += c[i]; start += l2[i].length(); f = false; break; } } if (f) { break; } else { f = true; } } System.out.println(r); } } }
[["-", 0, 503, 49, 200, 51, 492, 500, 5, 0, 491], ["+", 0, 503, 49, 200, 51, 492, 500, 5, 0, 491]]
3
386
2
import java.util.*; class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); String p[] = {"101", "000000", "000011", "10010001", "010001", "000001", "100101", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000"}; String c = " ',-.?ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String q[] = {"11010", "11110", "11100", "11101", "11011", "11111", "00000", "00001", "00010", "00011", "00100", "00101", "00110", "00111", "01000", "01001", "01010", "01011", "01100", "01101", "01110", "01111", "10000", "10001", "10010", "10011", "10100", "10101", "10110", "10111", "11000", "11001"}; while (s.hasNext()) { String k = s.nextLine(); String a = ""; for (int i = 0; i < k.length(); i++) for (int j = 0; j < 32; j++) if (k.charAt(i) == c.charAt(j)) a += q[j]; // while(a.length()%5!=0)a+="0"; String e = ""; int at = 0; for (int i = 0; i < a.length() / 5; i++) { for (int j = 0; j < 32; j++) { if (at + p[j].length() < a.length() && a.substring(at, at + p[j].length()).equals(p[j])) { e += c.charAt(j); at += p[j].length(); } } } System.out.println(e); } } }
import java.util.*; class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); String p[] = {"101", "000000", "000011", "10010001", "010001", "000001", "100101", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000"}; String c = " ',-.?ABCDEFGHIJKLMNOPQRSTUVWXYZ"; String q[] = {"11010", "11110", "11100", "11101", "11011", "11111", "00000", "00001", "00010", "00011", "00100", "00101", "00110", "00111", "01000", "01001", "01010", "01011", "01100", "01101", "01110", "01111", "10000", "10001", "10010", "10011", "10100", "10101", "10110", "10111", "11000", "11001"}; while (s.hasNext()) { String k = s.nextLine(); String a = ""; for (int i = 0; i < k.length(); i++) for (int j = 0; j < 32; j++) if (k.charAt(i) == c.charAt(j)) a += q[j]; // while(a.length()%5!=0)a+="0"; String e = ""; int at = 0; for (int i = 0; i < a.length() / 5; i++) { for (int j = 0; j < 32; j++) { if (at + p[j].length() <= a.length() && a.substring(at, at + p[j].length()).equals(p[j])) { e += c.charAt(j); at += p[j].length(); } } } System.out.println(e); } } }
[["-", 0, 57, 15, 15, 0, 16, 31, 16, 17, 18], ["+", 0, 57, 15, 15, 0, 16, 31, 16, 17, 19]]
3
517
2
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; public class Main { static HashMap<String, String> map = new HashMap<String, String>(); static HashMap<String, String> key = new HashMap<String, String>(); static String[] keys = { "101", "000000", "000011", "10010001", "010001", "000001", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000" }; public static void main(String[] args) throws IOException { a(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (true) { String str = br.readLine(); if (str == null) { break; } String hugou = ""; for (int i = 0; i < str.length(); i++) { hugou += map.get(String.valueOf(str.charAt(i))); } for (; hugou.length() != 0;) { int i; for (i = 0; i < keys.length; i++) { if (hugou.length() >= keys[i].length()) if (hugou.substring(0, keys[i].length()).equals(keys[i])) { hugou = hugou.substring(keys[i].length()); System.out.print(key.get(keys[i])); break; } } if (i == keys.length) { break; } } System.out.println(); } } static void a() { map.put("A", "00000"); map.put("B", "00001"); map.put("C", "00010"); map.put("D", "00011"); map.put("E", "00100"); map.put("F", "00101"); map.put("G", "00110"); map.put("H", "00111"); map.put("I", "01000"); map.put("J", "01001"); map.put("K", "01010"); map.put("L", "01011"); map.put("M", "01100"); map.put("N", "01101"); map.put("O", "01110"); map.put("P", "01111"); map.put("Q", "10000"); map.put("R", "10001"); map.put("S", "10010"); map.put("T", "10011"); map.put("U", "10100"); map.put("V", "10101"); map.put("W", "10110"); map.put("X", "10111"); map.put("Y", "11000"); map.put("Z", "11001"); map.put(" ", "11010"); map.put(".", "11011"); map.put(",", "11100"); map.put("-", "11101"); map.put("'", "11110"); map.put("?", "11111"); key.put("101", " "); key.put("000000", "'"); key.put("000011", ","); key.put("10010001", "-"); key.put("010001", "."); key.put("000001", "?"); key.put("10011010", "A"); key.put("10011010", "B"); key.put("0101", "C"); key.put("0001", "D"); key.put("110", "E"); key.put("01001", "F"); key.put("10011011", "G"); key.put("010000", "H"); key.put("0111", "I"); key.put("10011000", "J"); key.put("0110", "K"); key.put("00100", "L"); key.put("10011001", "M"); key.put("10011110", "N"); key.put("00101", "O"); key.put("111", "P"); key.put("10011111", "Q"); key.put("1000", "R"); key.put("00110", "S"); key.put("00111", "T"); key.put("10011100", "U"); key.put("10011101", "V"); key.put("000010", "W"); key.put("10010010", "X"); key.put("10010011", "Y"); key.put("10010000", "Z"); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; public class Main { static HashMap<String, String> map = new HashMap<String, String>(); static HashMap<String, String> key = new HashMap<String, String>(); static String[] keys = { "101", "000000", "000011", "10010001", "010001", "000001", "100101", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000" }; public static void main(String[] args) throws IOException { a(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (true) { String str = br.readLine(); if (str == null) { break; } String hugou = ""; for (int i = 0; i < str.length(); i++) { hugou += map.get(String.valueOf(str.charAt(i))); } for (; hugou.length() != 0;) { // System.out.println(hugou); int i; for (i = 0; i < keys.length; i++) { if (hugou.length() >= keys[i].length()) if (hugou.substring(0, keys[i].length()).equals(keys[i])) { hugou = hugou.substring(keys[i].length()); System.out.print(key.get(keys[i])); break; } } if (i == keys.length) { break; } } System.out.println(); } } static void a() { map.put("A", "00000"); map.put("B", "00001"); map.put("C", "00010"); map.put("D", "00011"); map.put("E", "00100"); map.put("F", "00101"); map.put("G", "00110"); map.put("H", "00111"); map.put("I", "01000"); map.put("J", "01001"); map.put("K", "01010"); map.put("L", "01011"); map.put("M", "01100"); map.put("N", "01101"); map.put("O", "01110"); map.put("P", "01111"); map.put("Q", "10000"); map.put("R", "10001"); map.put("S", "10010"); map.put("T", "10011"); map.put("U", "10100"); map.put("V", "10101"); map.put("W", "10110"); map.put("X", "10111"); map.put("Y", "11000"); map.put("Z", "11001"); map.put(" ", "11010"); map.put(".", "11011"); map.put(",", "11100"); map.put("-", "11101"); map.put("\'", "11110"); map.put("?", "11111"); key.put("101", " "); key.put("000000", "'"); key.put("000011", ","); key.put("10010001", "-"); key.put("010001", "."); key.put("000001", "?"); key.put("100101", "A"); key.put("10011010", "B"); key.put("0101", "C"); key.put("0001", "D"); key.put("110", "E"); key.put("01001", "F"); key.put("10011011", "G"); key.put("010000", "H"); key.put("0111", "I"); key.put("10011000", "J"); key.put("0110", "K"); key.put("00100", "L"); key.put("10011001", "M"); key.put("10011110", "N"); key.put("00101", "O"); key.put("111", "P"); key.put("10011111", "Q"); key.put("1000", "R"); key.put("00110", "S"); key.put("00111", "T"); key.put("10011100", "U"); key.put("10011101", "V"); key.put("000010", "W"); key.put("10010010", "X"); key.put("10010011", "Y"); key.put("10010000", "Z"); } }
[["+", 0, 124, 49, 200, 51, 518, 0, 5, 0, 491], ["+", 0, 124, 49, 200, 51, 518, 0, 5, 0, 62], ["+", 8, 498, 0, 124, 49, 200, 51, 518, 0, 21], ["-", 0, 1, 0, 492, 3, 4, 0, 5, 0, 491], ["+", 0, 1, 0, 492, 3, 4, 0, 5, 0, 44], ["+", 0, 1, 0, 492, 3, 4, 0, 5, 0, 491]]
3
1,279
8
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String[] c000 = new String[] {"'", "?", "W", ","}; String[] c10010 = new String[] {"Z", "-", "X", "Y"}; String[] c10011 = new String[] {"J", "M", "B", "G", "U", "V", "N", "Q"}; String s = sc.nextLine(); StringBuffer stn = new StringBuffer(); StringBuffer ans = new StringBuffer(); String t, u, v, w; int p; for (int i = 0; i < s.length(); i++) { p = s.codePointAt(i); if (p == 32) stn.append("11010"); else if (p == 46) stn.append("11011"); else if (p == 44) stn.append("11100"); else if (p == 45) stn.append("11101"); else if (p == 39) stn.append("11110"); else if (p == 63) stn.append("11111"); else { t = Integer.toBinaryString(p - 65); for (int j = 0; j < 5 - t.length(); j++) stn = stn.append("0"); stn.append(t); } } for (int i = 0; i < stn.length() - 3; i++) { while (true) { p = ans.length(); t = stn.substring(i, i + 3); if (t.compareTo("101") == 0) ans.append(" "); else if (t.compareTo("110") == 0) ans.append("E"); else if (t.compareTo("111") == 0) ans.append("P"); if (p < ans.length() || i + 3 == stn.length()) { i += 2; break; } u = stn.substring(i, i + 4); if (u.compareTo("0001") == 0) ans.append("D"); else if (u.compareTo("0101") == 0) ans.append("C"); else if (u.compareTo("0110") == 0) ans.append("K"); else if (u.compareTo("0111") == 0) ans.append("I"); else if (u.compareTo("1000") == 0) ans.append("R"); if (p < ans.length() || i + 4 == stn.length()) { i += 3; break; } v = stn.substring(i, i + 5); if (v.compareTo("01001") == 0) ans.append("F"); else if (v.compareTo("00100") == 0) ans.append("L"); else if (v.compareTo("00101") == 0) ans.append("O"); else if (v.compareTo("00110") == 0) ans.append("S"); else if (v.compareTo("00111") == 0) ans.append("T"); if (p < ans.length() || i + 5 == stn.length()) { i += 4; break; } w = stn.substring(i, i + 6); if (t.compareTo("000") == 0) ans.append(c000[(stn.codePointAt(i + 4) - 48) * 2 + (stn.codePointAt(i + 5) - 48)]); else if (w.compareTo("010000") == 0) ans.append("H"); else if (w.compareTo("010001") == 0) ans.append("."); else if (w.compareTo("100101") == 0) ans.append("A"); if (p < ans.length() || i + 7 == stn.length()) { i += 5; break; } if (v.compareTo("10010") == 0) ans.append(c10010[(stn.codePointAt(i + 6) - 48) * 2 + (stn.codePointAt(i + 7) - 48)]); else if (v.compareTo("10011") == 0) ans.append(c10011[(stn.codePointAt(i + 5) - 48) * 4 + (stn.codePointAt(i + 6) - 48) * 2 + (stn.codePointAt(i + 7) - 48)]); i += 7; break; } } System.out.println(ans); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String[] c000 = new String[] {"'", "?", "W", ","}; String[] c10010 = new String[] {"Z", "-", "X", "Y"}; String[] c10011 = new String[] {"J", "M", "B", "G", "U", "V", "N", "Q"}; String s = sc.nextLine(); StringBuffer stn = new StringBuffer(); StringBuffer ans = new StringBuffer(); String t, u, v, w; int p; for (int i = 0; i < s.length(); i++) { p = s.codePointAt(i); if (p == 32) stn.append("11010"); else if (p == 46) stn.append("11011"); else if (p == 44) stn.append("11100"); else if (p == 45) stn.append("11101"); else if (p == 39) stn.append("11110"); else if (p == 63) stn.append("11111"); else { t = Integer.toBinaryString(p - 65); for (int j = 0; j < 5 - t.length(); j++) stn = stn.append("0"); stn.append(t); } } for (int i = 0; i < stn.length() - 2; i++) { while (true) { p = ans.length(); t = stn.substring(i, i + 3); if (t.compareTo("101") == 0) ans.append(" "); else if (t.compareTo("110") == 0) ans.append("E"); else if (t.compareTo("111") == 0) ans.append("P"); if (p < ans.length() || i + 3 == stn.length()) { i += 2; break; } u = stn.substring(i, i + 4); if (u.compareTo("0001") == 0) ans.append("D"); else if (u.compareTo("0101") == 0) ans.append("C"); else if (u.compareTo("0110") == 0) ans.append("K"); else if (u.compareTo("0111") == 0) ans.append("I"); else if (u.compareTo("1000") == 0) ans.append("R"); if (p < ans.length() || i + 4 == stn.length()) { i += 3; break; } v = stn.substring(i, i + 5); if (v.compareTo("01001") == 0) ans.append("F"); else if (v.compareTo("00100") == 0) ans.append("L"); else if (v.compareTo("00101") == 0) ans.append("O"); else if (v.compareTo("00110") == 0) ans.append("S"); else if (v.compareTo("00111") == 0) ans.append("T"); if (p < ans.length() || i + 5 == stn.length()) { i += 4; break; } w = stn.substring(i, i + 6); if (t.compareTo("000") == 0) ans.append(c000[(stn.codePointAt(i + 4) - 48) * 2 + (stn.codePointAt(i + 5) - 48)]); else if (w.compareTo("010000") == 0) ans.append("H"); else if (w.compareTo("010001") == 0) ans.append("."); else if (w.compareTo("100101") == 0) ans.append("A"); if (p < ans.length() || i + 7 == stn.length()) { i += 5; break; } if (v.compareTo("10010") == 0) ans.append(c10010[(stn.codePointAt(i + 6) - 48) * 2 + (stn.codePointAt(i + 7) - 48)]); else if (v.compareTo("10011") == 0) ans.append(c10011[(stn.codePointAt(i + 5) - 48) * 4 + (stn.codePointAt(i + 6) - 48) * 2 + (stn.codePointAt(i + 7) - 48)]); i += 7; break; } } System.out.println(ans); } } }
[["-", 8, 196, 0, 7, 15, 16, 12, 16, 12, 499], ["+", 8, 196, 0, 7, 15, 16, 12, 16, 12, 499]]
3
1,098
2
#include <stdio.h> #include <string.h> char S[6000000], T[60000000], *x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .,-'?", q[10]; char *t[35] = {"000001", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000", "101", "000000", "000011", "10010001", "010001", "000001"}; int i, j, k, s[128], p; int main() { for (j = 0; x[j]; j++) s[x[j]] = k++; for (; gets(S);) { memset(T, 0, sizeof(T)); for (i = j = 0; S[i]; i++) { p = s[S[i]]; for (k = 4; k >= 0; k--) T[j++] = (p >> k & 1) + '0'; } p = 0; memset(q, 0, sizeof(q)); for (i = 0; T[i]; i++) { q[p++] = T[i]; for (j = 0; j < 32; j++) { if (strcmp(q, t[j]) == 0) { putchar(x[j]); memset(q, 0, sizeof(q)); p = 0; } } } puts(""); } return 0; }
#include <stdio.h> #include <string.h> char S[6000000], T[60000000], *x = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .,-'?", q[10]; char *t[35] = {"100101", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000", "101", "010001", "000011", "10010001", "000000", "000001"}; int i, j, k, s[128], p; int main() { for (j = 0; x[j]; j++) s[x[j]] = k++; for (; gets(S);) { memset(T, 0, sizeof(T)); for (i = j = 0; S[i]; i++) { p = s[S[i]]; for (k = 4; k >= 0; k--) T[j++] = (p >> k & 1) + '0'; } p = 0; memset(q, 0, sizeof(q)); for (i = 0; T[i]; i++) { q[p++] = T[i]; for (j = 0; j < 32; j++) { if (strcmp(q, t[j]) == 0) { putchar(x[j]); memset(q, 0, sizeof(q)); p = 0; } } } puts(""); } return 0; }
[["-", 0, 43, 49, 50, 51, 83, 0, 5, 0, 6], ["+", 0, 43, 49, 50, 51, 83, 0, 5, 0, 6]]
0
398
6
#include <stdio.h> #include <string.h> int main(void) { char map_1[32][2][6] = { {"A", "00000"}, {"B", "00001"}, {"C", "00010"}, {"D", "00011"}, {"E", "00100"}, {"F", "00101"}, {"G", "00110"}, {"H", "00111"}, {"I", "01000"}, {"J", "01001"}, {"K", "01010"}, {"L", "01011"}, {"M", "01100"}, {"N", "01101"}, {"O", "01110"}, {"P", "01111"}, {"Q", "10000"}, {"R", "10001"}, {"S", "10010"}, {"T", "10011"}, {"U", "10100"}, {"V", "10101"}, {"W", "10110"}, {"X", "10111"}, {"Y", "11000"}, {"Z", "11001"}, {" ", "11010"}, {".", "11011"}, {",", "11100"}, {"-", "11101"}, {"'", "11110"}, {"?", "11111"}}; char map_2[32][2][9] = { {"A", "100101"}, {"B", "10011010"}, {"C", "0101"}, {"D", "0001"}, {"E", "110"}, {"F", "01001"}, {"G", "10011011"}, {"H", "010000"}, {"I", "0111"}, {"J", "10011000"}, {"K", "0110"}, {"L", "00100"}, {"M", "10011001"}, {"N", "10011110"}, {"O", "00101"}, {"P", "111"}, {"Q", "10111111"}, {"R", "1000"}, {"S", "00110"}, {"T", "00111"}, {"U", "10011100"}, {"V", "10011101"}, {"W", "000010"}, {"X", "10010010"}, {"Y", "10010011"}, {"Z", "10010000"}, {" ", "101"}, {".", "010001"}, {",", "000011"}, {"-", "10010001"}, {"'", "000000"}, {"?", "000001"}}; char str[50001]; char temp[50001], *ptr; int i, flag; int ch; memset(str, '\0', sizeof(str)); while (scanf("%[^\n]", str) != EOF) { getchar(); memset(temp, 0, sizeof(temp)); i = 0; while (str[i] != '\0') { if ('A' <= str[i] && str[i] <= 'Z') ch = str[i] - 'A'; else { switch (str[i]) { case ' ': ch = 26; break; case '.': ch = 27; break; case ',': ch = 28; break; case '-': ch = 29; break; case '\'': ch = 30; break; case '?': ch = 31; break; } } strcat(temp, map_1[ch][1]); i++; } memset(str, '\0', sizeof(str)); flag = 1; ptr = &temp[0]; while (flag) { flag = 0; for (i = 0; i < 32; i++) { if (strncmp(map_2[i][1], ptr, strlen(map_2[i][1])) == 0) { strcat(str, map_2[i][0]); ptr += strlen(map_2[i][1]); flag = 1; } } } printf("%s\n", str); memset(str, '\0', sizeof(str)); } return (0); }
#include <stdio.h> #include <string.h> int main(void) { char map_1[32][2][6] = { {"A", "00000"}, {"B", "00001"}, {"C", "00010"}, {"D", "00011"}, {"E", "00100"}, {"F", "00101"}, {"G", "00110"}, {"H", "00111"}, {"I", "01000"}, {"J", "01001"}, {"K", "01010"}, {"L", "01011"}, {"M", "01100"}, {"N", "01101"}, {"O", "01110"}, {"P", "01111"}, {"Q", "10000"}, {"R", "10001"}, {"S", "10010"}, {"T", "10011"}, {"U", "10100"}, {"V", "10101"}, {"W", "10110"}, {"X", "10111"}, {"Y", "11000"}, {"Z", "11001"}, {" ", "11010"}, {".", "11011"}, {",", "11100"}, {"-", "11101"}, {"'", "11110"}, {"?", "11111"}}; char map_2[32][2][9] = { {"A", "100101"}, {"B", "10011010"}, {"C", "0101"}, {"D", "0001"}, {"E", "110"}, {"F", "01001"}, {"G", "10011011"}, {"H", "010000"}, {"I", "0111"}, {"J", "10011000"}, {"K", "0110"}, {"L", "00100"}, {"M", "10011001"}, {"N", "10011110"}, {"O", "00101"}, {"P", "111"}, {"Q", "10011111"}, {"R", "1000"}, {"S", "00110"}, {"T", "00111"}, {"U", "10011100"}, {"V", "10011101"}, {"W", "000010"}, {"X", "10010010"}, {"Y", "10010011"}, {"Z", "10010000"}, {" ", "101"}, {".", "010001"}, {",", "000011"}, {"-", "10010001"}, {"'", "000000"}, {"?", "000001"}}; char str[50001]; char temp[50001], *ptr; int i, flag; int ch; memset(str, '\0', sizeof(str)); while (scanf("%[^\n]", str) != EOF) { getchar(); memset(temp, 0, sizeof(temp)); i = 0; while (str[i] != '\0') { if ('A' <= str[i] && str[i] <= 'Z') ch = str[i] - 'A'; else { switch (str[i]) { case ' ': ch = 26; break; case '.': ch = 27; break; case ',': ch = 28; break; case '-': ch = 29; break; case '\'': ch = 30; break; case '?': ch = 31; break; } } strcat(temp, map_1[ch][1]); i++; } memset(str, '\0', sizeof(str)); flag = 1; ptr = &temp[0]; while (flag) { flag = 0; for (i = 0; i < 32; i++) { if (strncmp(map_2[i][1], ptr, strlen(map_2[i][1])) == 0) { strcat(str, map_2[i][0]); ptr += strlen(map_2[i][1]); flag = 1; } } } printf("%s\n", str); memset(str, '\0', sizeof(str)); } return (0); }
[["-", 49, 50, 51, 83, 0, 83, 0, 5, 0, 6], ["+", 49, 50, 51, 83, 0, 83, 0, 5, 0, 6]]
0
1,031
2
#include <bits/stdc++.h> using namespace std; string a[32] = {"101", "000000", "000011", "10010001", "010001", "000001", "100101", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000"}; string b = " ',-.?ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string c[32] = {"00000", "00001", "00010", "00011", "00100", "00101", "00110", "00111", "01000", "01001", "01010", "01011", "01100", "01101", "01110", "01111", "10000", "10001", "10010", "10011", "10100", "10101", "10110", "10111", "11000", "11001", "11010", "11011", "11100", "11101", "11110", "11111"}; string d = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .,-'?"; int main() { string s; map<string, string> p, q; for (int i = 0; i < 32; i++) { p[a[i]] = b.substr(i, 1); q[d.substr(i, 1)] = c[i]; } while (getline(cin, s)) { string t, ret; for (int i = 0; i < s.size(); i++) t += q[s.substr(i, 1)]; for (int i = 0; i < t.size(); i++) { for (int j = min(8, (int)(t.size() - i - 1)); 1 <= j; j--) { if (p[t.substr(i, j)].size() != 0) { ret += p[t.substr(i, j)]; i += j - 1; break; } } } cout << ret << endl; } }
#include <bits/stdc++.h> using namespace std; string a[32] = {"101", "000000", "000011", "10010001", "010001", "000001", "100101", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000"}; string b = " ',-.?ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string c[32] = {"00000", "00001", "00010", "00011", "00100", "00101", "00110", "00111", "01000", "01001", "01010", "01011", "01100", "01101", "01110", "01111", "10000", "10001", "10010", "10011", "10100", "10101", "10110", "10111", "11000", "11001", "11010", "11011", "11100", "11101", "11110", "11111"}; string d = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .,-'?"; int main() { string s; map<string, string> p, q; for (int i = 0; i < 32; i++) { p[a[i]] = b.substr(i, 1); q[d.substr(i, 1)] = c[i]; } while (getline(cin, s)) { string t, ret; for (int i = 0; i < s.size(); i++) t += q[s.substr(i, 1)]; for (int i = 0; i < t.size(); i++) { for (int j = min(8, (int)(t.size() - i)); 1 <= j; j--) { if (p[t.substr(i, j)].size() != 0) { ret += p[t.substr(i, j)]; i += j - 1; break; } } } cout << ret << endl; } }
[["-", 3, 4, 0, 74, 51, 23, 0, 16, 17, 33], ["-", 3, 4, 0, 74, 51, 23, 0, 16, 12, 13]]
1
513
2
#include <bits/stdc++.h> using namespace std; int main() { map<string, char> t2; map<char, string> t1; t1['A'] = "00000"; t1['R'] = "10001"; t1['B'] = "00001"; t1['S'] = "10010"; t1['C'] = "00010"; t1['T'] = "10011"; t1['D'] = "00011"; t1['U'] = "10100"; t1['E'] = "00100"; t1['V'] = "10101"; t1['F'] = "00101"; t1['W'] = "10110"; t1['G'] = "00110"; t1['X'] = "10111"; t1['H'] = "00111"; t1['Y'] = "11000"; t1['I'] = "01000"; t1['Z'] = "11001"; t1['J'] = "01001"; t1[' '] = "11010"; t1['K'] = "01010"; t1['.'] = "11011"; t1['L'] = "01011"; t1[','] = "11100"; t1['M'] = "01100"; t1['-'] = "11101"; t1['N'] = "01101"; t1['\''] = "11110"; t1['O'] = "01110"; t1['?'] = "11111"; t1['P'] = "01111"; t1['Q'] = "10000"; t2["101"] = ' '; t2["100101"] = 'A'; t2["000000"] = '\''; t2["10011010"] = 'B'; t2["000011"] = ','; t2["0101"] = 'C'; t2["10010001"] = '-'; t2["0001"] = 'D'; t2["010001"] = '.'; t2["110"] = 'E'; t2["000001"] = '?'; t2["01001"] = 'F'; t2["10011011"] = 'G'; t2["010000"] = 'H'; t2["011"] = 'I'; t2["10011000"] = 'J'; t2["0110"] = 'K'; t2["00100"] = 'L'; t2["10011001"] = 'M'; t2["10011110"] = 'N'; t2["00101"] = 'O'; t2["111"] = 'P'; t2["10011111"] = 'Q'; t2["1000"] = 'R'; t2["00110"] = 'S'; t2["00111"] = 'T'; t2["10011100"] = 'U'; t2["10011101"] = 'V'; t2["000010"] = 'W'; t2["10010011"] = 'X'; t2["10010011"] = 'Y'; t2["10010000"] = 'Z'; string s, t; while (getline(cin, s)) { t = ""; for (int i = 0; i < s.size(); i++) t += t1[s[i]]; string q; for (int i = 0; i < t.size(); i++) { q += t[i]; if (t2[q]) { cout << t2[q]; q = ""; } } cout << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { map<string, char> t2; map<char, string> t1; t1['A'] = "00000"; t1['R'] = "10001"; t1['B'] = "00001"; t1['S'] = "10010"; t1['C'] = "00010"; t1['T'] = "10011"; t1['D'] = "00011"; t1['U'] = "10100"; t1['E'] = "00100"; t1['V'] = "10101"; t1['F'] = "00101"; t1['W'] = "10110"; t1['G'] = "00110"; t1['X'] = "10111"; t1['H'] = "00111"; t1['Y'] = "11000"; t1['I'] = "01000"; t1['Z'] = "11001"; t1['J'] = "01001"; t1[' '] = "11010"; t1['K'] = "01010"; t1['.'] = "11011"; t1['L'] = "01011"; t1[','] = "11100"; t1['M'] = "01100"; t1['-'] = "11101"; t1['N'] = "01101"; t1['\''] = "11110"; t1['O'] = "01110"; t1['?'] = "11111"; t1['P'] = "01111"; t1['Q'] = "10000"; t2["101"] = ' '; t2["100101"] = 'A'; t2["000000"] = '\''; t2["10011010"] = 'B'; t2["000011"] = ','; t2["0101"] = 'C'; t2["10010001"] = '-'; t2["0001"] = 'D'; t2["010001"] = '.'; t2["110"] = 'E'; t2["000001"] = '?'; t2["01001"] = 'F'; t2["10011011"] = 'G'; t2["010000"] = 'H'; t2["0111"] = 'I'; t2["10011000"] = 'J'; t2["0110"] = 'K'; t2["00100"] = 'L'; t2["10011001"] = 'M'; t2["10011110"] = 'N'; t2["00101"] = 'O'; t2["111"] = 'P'; t2["10011111"] = 'Q'; t2["1000"] = 'R'; t2["00110"] = 'S'; t2["00111"] = 'T'; t2["10011100"] = 'U'; t2["10011101"] = 'V'; t2["000010"] = 'W'; t2["10010010"] = 'X'; t2["10010011"] = 'Y'; t2["10010000"] = 'Z'; string s, t; while (getline(cin, s)) { t = ""; for (int i = 0; i < s.size(); i++) t += t1[s[i]]; string q; for (int i = 0; i < t.size(); i++) { q += t[i]; if (t2[q]) { cout << t2[q]; q = ""; } } cout << endl; } }
[["-", 0, 11, 31, 69, 341, 342, 0, 5, 0, 6], ["+", 0, 11, 31, 69, 341, 342, 0, 5, 0, 6]]
1
836
4
#include <iostream> #include <map> #include <string> using namespace std; // •ÏŠ·ƒe[ƒuƒ‹ map<char, string> M; map<string, char> D; int main(void) { M['A'] = "00000"; M['B'] = "00001"; M['C'] = "00010"; M['D'] = "00011"; M['E'] = "00100"; M['F'] = "00101"; M['G'] = "00110"; M['H'] = "00111"; M['I'] = "01000"; M['J'] = "01001"; M['K'] = "01010"; M['L'] = "01011"; M['M'] = "01100"; M['N'] = "01101"; M['O'] = "01110"; M['P'] = "01111"; M['Q'] = "10000"; M['R'] = "10001"; M['S'] = "10010"; M['T'] = "10011"; M['U'] = "10100"; M['V'] = "10101"; M['W'] = "10110"; M['X'] = "10111"; M['Y'] = "11000"; M['Z'] = "11001"; M[' '] = "11010"; M['.'] = "11011"; M[','] = "11100"; M['-'] = "11101"; M['\''] = "11110"; M['?'] = "11111"; D["101"] = ' '; D["000000"] = '\''; D["000011"] = ','; D["10010001"] = '-'; D["010001"] = '.'; D["000001"] = '?'; D["100101"] = 'A'; D["10011010"] = 'B'; D["0101"] = 'C'; D["0001"] = 'D'; D["110"] = 'E'; D["01001"] = 'F'; D["10011011"] = 'G'; D["010000"] = 'H'; D["0111"] = 'I'; D["10011000"] = 'J'; D["0110"] = 'K'; D["00100"] = 'L'; D["10011001"] = 'M'; D["10011110"] = 'N'; D["00101"] = 'O'; D["111"] = 'P'; D["10011111"] = 'Q'; D["1000"] = 'R'; D["00110"] = 'S'; D["00111"] = 'T'; D["10011100"] = 'U'; D["10011101"] = 'V'; D["000010"] = 'W'; D["10010010"] = 'X'; D["10010011"] = 'Y'; D["10010000"] = 'Z'; string in; while (1) { getline(cin, in); if (cin.eof()) break; string encode; for (string::iterator it = in.begin(); it != in.end(); it++) { encode += M[*it]; } string decode = ""; int index = 0; while (1) { bool found = false; for (int i = 3; i < 9; i++) { if (encode.size() <= index + i) break; string t = encode.substr(index, i); if (D.find(t) != D.end()) { // ”­Œ© decode += D[t]; index += i; found = true; } } if (!found) break; } cout << decode << endl; } return 0; }
#include <iostream> #include <map> #include <string> using namespace std; // •ÏŠ·ƒe[ƒuƒ‹ map<char, string> M; map<string, char> D; int main(void) { M['A'] = "00000"; M['B'] = "00001"; M['C'] = "00010"; M['D'] = "00011"; M['E'] = "00100"; M['F'] = "00101"; M['G'] = "00110"; M['H'] = "00111"; M['I'] = "01000"; M['J'] = "01001"; M['K'] = "01010"; M['L'] = "01011"; M['M'] = "01100"; M['N'] = "01101"; M['O'] = "01110"; M['P'] = "01111"; M['Q'] = "10000"; M['R'] = "10001"; M['S'] = "10010"; M['T'] = "10011"; M['U'] = "10100"; M['V'] = "10101"; M['W'] = "10110"; M['X'] = "10111"; M['Y'] = "11000"; M['Z'] = "11001"; M[' '] = "11010"; M['.'] = "11011"; M[','] = "11100"; M['-'] = "11101"; M['\''] = "11110"; M['?'] = "11111"; D["101"] = ' '; D["000000"] = '\''; D["000011"] = ','; D["10010001"] = '-'; D["010001"] = '.'; D["000001"] = '?'; D["100101"] = 'A'; D["10011010"] = 'B'; D["0101"] = 'C'; D["0001"] = 'D'; D["110"] = 'E'; D["01001"] = 'F'; D["10011011"] = 'G'; D["010000"] = 'H'; D["0111"] = 'I'; D["10011000"] = 'J'; D["0110"] = 'K'; D["00100"] = 'L'; D["10011001"] = 'M'; D["10011110"] = 'N'; D["00101"] = 'O'; D["111"] = 'P'; D["10011111"] = 'Q'; D["1000"] = 'R'; D["00110"] = 'S'; D["00111"] = 'T'; D["10011100"] = 'U'; D["10011101"] = 'V'; D["000010"] = 'W'; D["10010010"] = 'X'; D["10010011"] = 'Y'; D["10010000"] = 'Z'; string in; while (1) { getline(cin, in); if (cin.eof()) break; string encode; for (string::iterator it = in.begin(); it != in.end(); it++) { encode += M[*it]; } string decode = ""; int index = 0; while (1) { bool found = false; for (int i = 3; i < 9; i++) { if (encode.size() < index + i) break; string t = encode.substr(index, i); if (D.find(t) != D.end()) { // ”­Œ© decode += D[t]; index += i; found = true; } } if (!found) break; } cout << decode << endl; } return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18]]
1
914
2
#include <iostream> #include <map> #include <string> using namespace std; int main() { map<char, string> en; en['A'] = "00000"; en['B'] = "00001"; en['C'] = "00010"; en['D'] = "00011"; en['E'] = "00100"; en['F'] = "00101"; en['G'] = "00110"; en['H'] = "00111"; en['I'] = "01000"; en['J'] = "01001"; en['K'] = "01010"; en['L'] = "01011"; en['M'] = "01100"; en['N'] = "01101"; en['O'] = "01110"; en['P'] = "01111"; en['Q'] = "10000"; en['R'] = "10001"; en['S'] = "10010"; en['T'] = "10011"; en['U'] = "10100"; en['V'] = "10101"; en['W'] = "10110"; en['X'] = "10111"; en['Y'] = "11000"; en['Z'] = "11001"; en[' '] = "11010"; en['.'] = "11011"; en[','] = "11100"; en['-'] = "11101"; en['\''] = "11110"; en['?'] = "11111"; map<string, char> de; de["101"] = ' '; de["000000"] = '\''; de["000011"] = ','; de["10010001"] = '-'; de["010001"] = '.'; de["000001"] = '?'; de["100101"] = 'A'; de["10011010"] = 'B'; de["0101"] = 'C'; de["0001"] = 'D'; de["110"] = 'E'; de["01001"] = 'F'; de["10011011"] = 'G'; de["010000"] = 'H'; de["0111"] = 'I'; de["10011000"] = 'J'; de["0110"] = 'K'; de["00100"] = 'L'; de["10011001"] = 'M'; de["10011110"] = 'N'; de["00101"] = 'O'; de["111"] = 'P'; de["10011111"] = 'Q'; de["1000"] = 'R'; de["00110"] = 'S'; de["00111"] = 'T'; de["10011100"] = 'U'; de["10011101"] = 'V'; de["000010"] = 'W'; de["10010010"] = 'X'; de["10010011"] = 'Y'; de["10010000"] = 'Z'; string s; while (getline(cin, s)) { string text; for (int i = 0; i < s.length(); i++) text += en[s[i]]; for (int i = 0, len; i < text.length(); i += len) { bool found = false; for (len = 3; i + len < text.length(); len++) { if (de.count(text.substr(i, len)) != 0) { found = true; break; } } if (!found) break; cout << de[text.substr(i, len)]; } cout << endl; } return 0; }
#include <iostream> #include <map> #include <string> using namespace std; int main() { map<char, string> en; en['A'] = "00000"; en['B'] = "00001"; en['C'] = "00010"; en['D'] = "00011"; en['E'] = "00100"; en['F'] = "00101"; en['G'] = "00110"; en['H'] = "00111"; en['I'] = "01000"; en['J'] = "01001"; en['K'] = "01010"; en['L'] = "01011"; en['M'] = "01100"; en['N'] = "01101"; en['O'] = "01110"; en['P'] = "01111"; en['Q'] = "10000"; en['R'] = "10001"; en['S'] = "10010"; en['T'] = "10011"; en['U'] = "10100"; en['V'] = "10101"; en['W'] = "10110"; en['X'] = "10111"; en['Y'] = "11000"; en['Z'] = "11001"; en[' '] = "11010"; en['.'] = "11011"; en[','] = "11100"; en['-'] = "11101"; en['\''] = "11110"; en['?'] = "11111"; map<string, char> de; de["101"] = ' '; de["000000"] = '\''; de["000011"] = ','; de["10010001"] = '-'; de["010001"] = '.'; de["000001"] = '?'; de["100101"] = 'A'; de["10011010"] = 'B'; de["0101"] = 'C'; de["0001"] = 'D'; de["110"] = 'E'; de["01001"] = 'F'; de["10011011"] = 'G'; de["010000"] = 'H'; de["0111"] = 'I'; de["10011000"] = 'J'; de["0110"] = 'K'; de["00100"] = 'L'; de["10011001"] = 'M'; de["10011110"] = 'N'; de["00101"] = 'O'; de["111"] = 'P'; de["10011111"] = 'Q'; de["1000"] = 'R'; de["00110"] = 'S'; de["00111"] = 'T'; de["10011100"] = 'U'; de["10011101"] = 'V'; de["000010"] = 'W'; de["10010010"] = 'X'; de["10010011"] = 'Y'; de["10010000"] = 'Z'; string s; while (getline(cin, s)) { string text; for (int i = 0; i < s.length(); i++) text += en[s[i]]; for (int i = 0, len; i < text.length(); i += len) { bool found = false; for (len = 3; i + len <= text.length(); len++) { if (de.count(text.substr(i, len)) != 0) { found = true; break; } } if (!found) break; cout << de[text.substr(i, len)]; } cout << endl; } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
884
2
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define reep(i, f, n) for (int i = f; i < int(n); ++i) #define rep(i, n) reep(i, 0, n) typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef complex<double> Point; struct Trie { char charactor; Trie *child[2]; Trie(char c = '\0') : charactor(c) { rep(i, 2) child[i] = NULL; } void insert(const char *str, char c) { if (str[0] == '\0') charactor = c; else { int n = str[0] - '0'; if (!child[n]) child[n] = new Trie(); child[n]->insert(str + 1, c); } } pair<char, int> get(const char *str) { if (charactor != '\0') return make_pair(charactor, 0); else { if (str[0] == '\0' || !child[str[0] - '0']) return make_pair('\0', -1); pair<char, int> pci = child[str[0] - '0']->get(str + 1); return make_pair(pci.first, pci.second + 1); } } }; void disposeTrie(Trie *t) { rep(i, 2) { if (t->child[i] != NULL) { disposeTrie(t->child[i]); delete t->child[i]; t->child[i] = NULL; } } } int main() { const char *chas = " ',-.?ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const char *strs[] = { "101", "000000", "000011", "10010001", "010001", "000001", "100101", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000"}; Trie root; rep(i, strlen(chas)) { root.insert(strs[i], chas[i]); } map<char, const char *> table; table['A'] = "00000"; table['B'] = "00001"; table['C'] = "00010"; table['D'] = "00011"; table['E'] = "00100"; table['F'] = "00101"; table['G'] = "00110"; table['H'] = "00111"; table['I'] = "01000"; table['J'] = "01001"; table['K'] = "01010"; table['L'] = "01011"; table['M'] = "01100"; table['N'] = "01101"; table['O'] = "01110"; table['P'] = "01111"; table['Q'] = "10000"; table['R'] = "10001"; table['S'] = "10010"; table['T'] = "10011"; table['U'] = "10100"; table['V'] = "10101"; table['W'] = "10110"; table['X'] = "10111"; table['Y'] = "11000"; table['Z'] = "11001"; table[' '] = "11010"; table['.'] = "11011"; table[','] = "11100"; table['-'] = "11101"; table['\''] = "11110"; table['?'] = "11111"; char str[256]; while (fgets(str, 1024, stdin)) { char binary[2048]; int n = strlen(str); if (str[n - 1] == '\n') str[--n] = '\0'; rep(i, n) { const char *b = table[str[i]]; rep(j, 5) binary[i * 5 + j] = b[j]; } binary[n * 5] = '\0'; puts(binary); int pos = 0; while (pos < n * 5) { pair<char, int> pci = root.get(binary + pos); if (pci.first == '\0') break; putchar(pci.first); pos += pci.second; } putchar('\n'); } disposeTrie(&root); return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define reep(i, f, n) for (int i = f; i < int(n); ++i) #define rep(i, n) reep(i, 0, n) typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef complex<double> Point; struct Trie { char charactor; Trie *child[2]; Trie(char c = '\0') : charactor(c) { rep(i, 2) child[i] = NULL; } void insert(const char *str, char c) { if (str[0] == '\0') charactor = c; else { int n = str[0] - '0'; if (!child[n]) child[n] = new Trie(); child[n]->insert(str + 1, c); } } pair<char, int> get(const char *str) { if (charactor != '\0') return make_pair(charactor, 0); else { if (str[0] == '\0' || !child[str[0] - '0']) return make_pair('\0', -1); pair<char, int> pci = child[str[0] - '0']->get(str + 1); return make_pair(pci.first, pci.second + 1); } } }; void disposeTrie(Trie *t) { rep(i, 2) { if (t->child[i] != NULL) { disposeTrie(t->child[i]); delete t->child[i]; t->child[i] = NULL; } } } int main() { const char *chas = " ',-.?ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const char *strs[] = { "101", "000000", "000011", "10010001", "010001", "000001", "100101", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000"}; Trie root; rep(i, strlen(chas)) { root.insert(strs[i], chas[i]); } map<char, const char *> table; table['A'] = "00000"; table['B'] = "00001"; table['C'] = "00010"; table['D'] = "00011"; table['E'] = "00100"; table['F'] = "00101"; table['G'] = "00110"; table['H'] = "00111"; table['I'] = "01000"; table['J'] = "01001"; table['K'] = "01010"; table['L'] = "01011"; table['M'] = "01100"; table['N'] = "01101"; table['O'] = "01110"; table['P'] = "01111"; table['Q'] = "10000"; table['R'] = "10001"; table['S'] = "10010"; table['T'] = "10011"; table['U'] = "10100"; table['V'] = "10101"; table['W'] = "10110"; table['X'] = "10111"; table['Y'] = "11000"; table['Z'] = "11001"; table[' '] = "11010"; table['.'] = "11011"; table[','] = "11100"; table['-'] = "11101"; table['\''] = "11110"; table['?'] = "11111"; char str[256]; while (fgets(str, 1024, stdin)) { char binary[2048]; int n = strlen(str); if (str[n - 1] == '\n') str[--n] = '\0'; rep(i, n) { const char *b = table[str[i]]; rep(j, 5) binary[i * 5 + j] = b[j]; } binary[n * 5] = '\0'; int pos = 0; while (pos < n * 5) { pair<char, int> pci = root.get(binary + pos); if (pci.first == '\0') break; putchar(pci.first); pos += pci.second; } putchar('\n'); } disposeTrie(&root); return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 2, 63, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25], ["-", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
1,102
5
#define _USE_MATH_DEFINES #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const int MODULO = 1000000007; const int INF = 100000000; // 1e8 typedef long long ll; typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; typedef complex<double> Cd; const char t[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .,-'?"; const char r[] = " ',-.?ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const string o[] = {"101", "000000", "000011", "10010001", "010001", "000001", "100101", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000"}; map<string, char> mp; string get_dec_char(char c) { int a = strchr(t, c) - t; string ret(5, '0'); for (int i = 0; i < 5; i++) { if (a & (1 << i)) ret[4 - i]++; } return ret; } string decode(string s) { string ret; for (int i = 0; i < (int)s.size(); i++) ret.append(get_dec_char(s[i])); return ret; } string encode(string s) { string ret; int cur = 0; for (int i = 0; i < (int)s.size(); i++) { string sub = s.substr(cur, i - cur); if (mp.find(sub) != mp.end()) { cur = i; ret.push_back(mp[sub]); } } return ret; } string solve(string s) { s = decode(s); return encode(s); } int main() { for (int i = 0; i < 32; i++) { mp[o[i]] = r[i]; } string s; while (getline(cin, s)) { cout << solve(s) << endl; } return 0; }
#define _USE_MATH_DEFINES #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; const int MODULO = 1000000007; const int INF = 100000000; // 1e8 typedef long long ll; typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; typedef complex<double> Cd; const char t[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ .,-'?"; const char r[] = " ',-.?ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const string o[] = {"101", "000000", "000011", "10010001", "010001", "000001", "100101", "10011010", "0101", "0001", "110", "01001", "10011011", "010000", "0111", "10011000", "0110", "00100", "10011001", "10011110", "00101", "111", "10011111", "1000", "00110", "00111", "10011100", "10011101", "000010", "10010010", "10010011", "10010000"}; map<string, char> mp; string get_dec_char(char c) { int a = strchr(t, c) - t; string ret(5, '0'); for (int i = 0; i < 5; i++) { if (a & (1 << i)) ret[4 - i]++; } return ret; } string decode(string s) { string ret; for (int i = 0; i < (int)s.size(); i++) ret.append(get_dec_char(s[i])); return ret; } string encode(string s) { string ret; int cur = 0; for (int i = 0; i <= (int)s.size(); i++) { string sub = s.substr(cur, i - cur); if (mp.find(sub) != mp.end()) { cur = i; ret.push_back(mp[sub]); } } return ret; } string solve(string s) { s = decode(s); return encode(s); } int main() { for (int i = 0; i < 32; i++) { mp[o[i]] = r[i]; } string s; while (getline(cin, s)) { cout << solve(s) << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 7, 15, 16, 17, 19]]
1
550
2
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <queue> using namespace std; typedef pair<string, string> P; int main() { map<char, string> tr1; tr1['A'] = "00000"; tr1['B'] = "00001"; tr1['C'] = "00010"; tr1['D'] = "00011"; tr1['E'] = "00100"; tr1['F'] = "00101"; tr1['G'] = "00110"; tr1['H'] = "00111"; tr1['I'] = "01000"; tr1['J'] = "01001"; tr1['K'] = "01010"; tr1['L'] = "01011"; tr1['M'] = "01100"; tr1['N'] = "01101"; tr1['O'] = "01110"; tr1['P'] = "01111"; tr1['Q'] = "10000"; tr1['R'] = "10001"; tr1['S'] = "10010"; tr1['T'] = "10011"; tr1['U'] = "10100"; tr1['V'] = "10101"; tr1['W'] = "10110"; tr1['X'] = "10111"; tr1['Y'] = "11000"; tr1['Z'] = "11001"; tr1[' '] = "11010"; tr1['.'] = "11011"; tr1[','] = "11100"; tr1['-'] = "11101"; tr1['\''] = "11110"; tr1['?'] = "11111"; map<char, string> tr2; tr2[' '] = "101"; tr2['\''] = "000000"; tr2[','] = "000011"; tr2['-'] = "10010001"; tr2['.'] = "010001"; tr2['?'] = "000001"; tr2['A'] = "100101"; tr2['B'] = "10011010"; tr2['C'] = "0101"; tr2['D'] = "0001"; tr2['E'] = "110"; tr2['F'] = "01001"; tr2['G'] = "10011011"; tr2['H'] = "01000"; tr2['I'] = "0111"; tr2['J'] = "10011000"; tr2['K'] = "0110"; tr2['L'] = "00100"; tr2['M'] = "10011001"; tr2['N'] = "10011110"; tr2['O'] = "00101"; tr2['P'] = "111"; tr2['Q'] = "10011111"; tr2['R'] = "1000"; tr2['S'] = "00110"; tr2['T'] = "00111"; tr2['U'] = "10011100"; tr2['V'] = "10011101"; tr2['W'] = "000010"; tr2['X'] = "10010010"; tr2['Y'] = "10010011"; tr2['Z'] = "10010000"; string s = ""; while (getline(cin, s)) { string code = ""; for (int i = 0; i < s.size(); ++i) code += tr1[s[i]]; int max_sz = 0; string ans = ""; queue<P> Q; Q.push(P("", "")); while (!Q.empty()) { P p = Q.front(); Q.pop(); string str = p.first; if (str.size() > code.size()) continue; bool f = true; for (int i = 0; i < str.size(); ++i) if (str[i] != code[i]) f = false; if (!f) continue; // for (int i = str.size(); i < code.size(); ++i) // if (code[i] == '1') f = false; ans = p.second; map<char, string>::iterator pos = tr2.begin(); while (pos != tr2.end()) { Q.push(P(str + (*pos).second, p.second + (*pos).first)); ++pos; } } cout << ans << endl; } }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <queue> using namespace std; typedef pair<string, string> P; int main() { map<char, string> tr1; tr1['A'] = "00000"; tr1['B'] = "00001"; tr1['C'] = "00010"; tr1['D'] = "00011"; tr1['E'] = "00100"; tr1['F'] = "00101"; tr1['G'] = "00110"; tr1['H'] = "00111"; tr1['I'] = "01000"; tr1['J'] = "01001"; tr1['K'] = "01010"; tr1['L'] = "01011"; tr1['M'] = "01100"; tr1['N'] = "01101"; tr1['O'] = "01110"; tr1['P'] = "01111"; tr1['Q'] = "10000"; tr1['R'] = "10001"; tr1['S'] = "10010"; tr1['T'] = "10011"; tr1['U'] = "10100"; tr1['V'] = "10101"; tr1['W'] = "10110"; tr1['X'] = "10111"; tr1['Y'] = "11000"; tr1['Z'] = "11001"; tr1[' '] = "11010"; tr1['.'] = "11011"; tr1[','] = "11100"; tr1['-'] = "11101"; tr1['\''] = "11110"; tr1['?'] = "11111"; map<char, string> tr2; tr2[' '] = "101"; tr2['\''] = "000000"; tr2[','] = "000011"; tr2['-'] = "10010001"; tr2['.'] = "010001"; tr2['?'] = "000001"; tr2['A'] = "100101"; tr2['B'] = "10011010"; tr2['C'] = "0101"; tr2['D'] = "0001"; tr2['E'] = "110"; tr2['F'] = "01001"; tr2['G'] = "10011011"; tr2['H'] = "010000"; tr2['I'] = "0111"; tr2['J'] = "10011000"; tr2['K'] = "0110"; tr2['L'] = "00100"; tr2['M'] = "10011001"; tr2['N'] = "10011110"; tr2['O'] = "00101"; tr2['P'] = "111"; tr2['Q'] = "10011111"; tr2['R'] = "1000"; tr2['S'] = "00110"; tr2['T'] = "00111"; tr2['U'] = "10011100"; tr2['V'] = "10011101"; tr2['W'] = "000010"; tr2['X'] = "10010010"; tr2['Y'] = "10010011"; tr2['Z'] = "10010000"; string s = ""; while (getline(cin, s)) { string code = ""; for (int i = 0; i < s.size(); ++i) code += tr1[s[i]]; int max_sz = 0; string ans = ""; queue<P> Q; Q.push(P("", "")); while (!Q.empty()) { P p = Q.front(); Q.pop(); string str = p.first; if (str.size() > code.size()) continue; bool f = true; for (int i = 0; i < str.size(); ++i) if (str[i] != code[i]) f = false; if (!f) continue; // for (int i = str.size(); i < code.size(); ++i) // if (code[i] == '1') f = false; ans = p.second; map<char, string>::iterator pos = tr2.begin(); while (pos != tr2.end()) { Q.push(P(str + (*pos).second, p.second + (*pos).first)); ++pos; } } cout << ans << endl; } }
[["-", 8, 9, 0, 1, 0, 11, 12, 5, 0, 6], ["+", 8, 9, 0, 1, 0, 11, 12, 5, 0, 6]]
1
999
2
#include <iostream> #include <map> #include <string> using namespace std; map<char, string> encode_table; map<string, char> decode_table; char decode(string s) { // cout << s << endl; return decode_table[s]; } string encode(string s) { string tmp; for (string::iterator si = s.begin(); si != s.end(); si++) { tmp.append(encode_table[*si]); } // cout << tmp << endl; string ret; while (true) { bool found = false; for (int i = 3; i <= 9; i++) { if (decode_table[tmp.substr(0, i)] != 0) { ret.push_back(decode_table[tmp.substr(0, i)]); tmp = tmp.substr(i); found = true; } } if (!found) break; } return ret; } void init() { decode_table["101"] = ' '; decode_table["000000"] = '\''; decode_table["000011"] = ','; decode_table["10010001"] = '-'; decode_table["010001"] = '.'; decode_table["000001"] = '?'; decode_table["100101"] = 'A'; decode_table["10011010"] = 'B'; decode_table["0101"] = 'C'; decode_table["0001"] = 'D'; decode_table["110"] = 'E'; decode_table["01001"] = 'F'; decode_table["10011011"] = 'G'; decode_table["010000"] = 'H'; decode_table["0111"] = 'I'; decode_table["10011000"] = 'J'; decode_table["0110"] = 'K'; decode_table["00100"] = 'L'; decode_table["10011001"] = 'M'; decode_table["10011110"] = 'N'; decode_table["00101"] = 'O'; decode_table["111"] = 'P'; decode_table["10011111"] = 'Q'; decode_table["1000"] = 'R'; decode_table["00110"] = 'S'; decode_table["00111"] = 'T'; decode_table["10011100"] = 'U'; decode_table["10011101"] = 'V'; decode_table["000010"] = 'W'; decode_table["10010010"] = 'X'; decode_table["10010011"] = 'Y'; decode_table["10010000"] = 'Z'; encode_table['A'] = string("00000"); encode_table['B'] = string("00001"); encode_table['C'] = string("00010"); encode_table['D'] = string("00011"); encode_table['E'] = string("00100"); encode_table['F'] = string("00101"); encode_table['G'] = string("00110"); encode_table['H'] = string("00111"); encode_table['I'] = string("01000"); encode_table['J'] = string("01001"); encode_table['K'] = string("01010"); encode_table['L'] = string("01011"); encode_table['M'] = string("01100"); encode_table['N'] = string("01101"); encode_table['O'] = string("01110"); encode_table['P'] = string("01111"); encode_table['Q'] = string("10000"); encode_table['R'] = string("10001"); encode_table['S'] = string("10010"); encode_table['T'] = string("10011"); encode_table['U'] = string("10100"); encode_table['V'] = string("10101"); encode_table['W'] = string("10110"); encode_table['X'] = string("10111"); encode_table['Y'] = string("11000"); encode_table['Z'] = string("11001"); encode_table[' '] = string("11010"); encode_table['.'] = string("11011"); encode_table[','] = string("11100"); encode_table['-'] = string("11101"); encode_table['\''] = string("11110"); encode_table['?'] = string("11111"); } int main() { string s; init(); while (getline(cin, s)) { cout << encode(s) << endl; } return 0; }
#include <iostream> #include <map> #include <string> using namespace std; map<char, string> encode_table; map<string, char> decode_table; char decode(string s) { // cout << s << endl; return decode_table[s]; } string encode(string s) { string tmp; for (string::iterator si = s.begin(); si != s.end(); si++) { tmp.append(encode_table[*si]); } // cout << tmp << endl; string ret; while (true) { bool found = false; for (int i = 3; i <= 9; i++) { if (decode_table[tmp.substr(0, i)] != 0) { ret.push_back(decode_table[tmp.substr(0, i)]); tmp = tmp.substr(i); found = true; break; } } if (!found) break; } return ret; } void init() { decode_table["101"] = ' '; decode_table["000000"] = '\''; decode_table["000011"] = ','; decode_table["10010001"] = '-'; decode_table["010001"] = '.'; decode_table["000001"] = '?'; decode_table["100101"] = 'A'; decode_table["10011010"] = 'B'; decode_table["0101"] = 'C'; decode_table["0001"] = 'D'; decode_table["110"] = 'E'; decode_table["01001"] = 'F'; decode_table["10011011"] = 'G'; decode_table["010000"] = 'H'; decode_table["0111"] = 'I'; decode_table["10011000"] = 'J'; decode_table["0110"] = 'K'; decode_table["00100"] = 'L'; decode_table["10011001"] = 'M'; decode_table["10011110"] = 'N'; decode_table["00101"] = 'O'; decode_table["111"] = 'P'; decode_table["10011111"] = 'Q'; decode_table["1000"] = 'R'; decode_table["00110"] = 'S'; decode_table["00111"] = 'T'; decode_table["10011100"] = 'U'; decode_table["10011101"] = 'V'; decode_table["000010"] = 'W'; decode_table["10010010"] = 'X'; decode_table["10010011"] = 'Y'; decode_table["10010000"] = 'Z'; encode_table['A'] = string("00000"); encode_table['B'] = string("00001"); encode_table['C'] = string("00010"); encode_table['D'] = string("00011"); encode_table['E'] = string("00100"); encode_table['F'] = string("00101"); encode_table['G'] = string("00110"); encode_table['H'] = string("00111"); encode_table['I'] = string("01000"); encode_table['J'] = string("01001"); encode_table['K'] = string("01010"); encode_table['L'] = string("01011"); encode_table['M'] = string("01100"); encode_table['N'] = string("01101"); encode_table['O'] = string("01110"); encode_table['P'] = string("01111"); encode_table['Q'] = string("10000"); encode_table['R'] = string("10001"); encode_table['S'] = string("10010"); encode_table['T'] = string("10011"); encode_table['U'] = string("10100"); encode_table['V'] = string("10101"); encode_table['W'] = string("10110"); encode_table['X'] = string("10111"); encode_table['Y'] = string("11000"); encode_table['Z'] = string("11001"); encode_table[' '] = string("11010"); encode_table['.'] = string("11011"); encode_table[','] = string("11100"); encode_table['-'] = string("11101"); encode_table['\''] = string("11110"); encode_table['?'] = string("11111"); } int main() { string s; init(); while (getline(cin, s)) { cout << encode(s) << endl; } return 0; }
[["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 94], ["+", 8, 9, 0, 57, 64, 9, 0, 93, 0, 35]]
1
1,020
2
#include <cstring> #include <iostream> #include <map> using namespace std; #define mk make_pair int main() { map<char, string> former; former['A'] = "00000"; former['B'] = "00001"; former['C'] = "00010"; former['D'] = "00011"; former['E'] = "00100"; former['F'] = "00101"; former['G'] = "00110"; former['H'] = "00111"; former['I'] = "01000"; former['J'] = "01001"; former['K'] = "01010"; former['L'] = "01011"; former['M'] = "01100"; former['N'] = "01101"; former['O'] = "01110"; former['P'] = "01111"; former['Q'] = "10000"; former['R'] = "10001"; former['S'] = "10010"; former['T'] = "10011"; former['U'] = "10100"; former['V'] = "10101"; former['W'] = "10110"; former['X'] = "10111"; former['Y'] = "11000"; former['Z'] = "11001"; former[' '] = "11010"; former['.'] = "11011"; former[','] = "11100"; former['-'] = "11101"; former['\''] = "11110"; former['?'] = "11111"; map<string, char> later; later["101"] = ' '; later["000000"] = '\''; later["000011"] = ','; later["10010001"] = '-'; later["010001"] = '.'; later["000001"] = '?'; later["100101"] = 'A'; later["10011010"] = 'B'; later["0101"] = 'C'; later["0001"] = 'D'; later["110"] = 'E'; later["01001"] = 'F'; later["10011011"] = 'G'; later["010000"] = 'H'; later["0111"] = 'I'; later["10011000"] = 'J'; later["0110"] = 'K'; later["00100"] = 'L'; later["10011001"] = 'M'; later["10011110"] = 'N'; later["00101"] = 'O'; later["111"] = 'P'; later["10011111"] = 'Q'; later["1000"] = 'R'; later["00110"] = 'S'; later["00111"] = 'T'; later["10011100"] = 'U'; later["10011101"] = 'V'; later["000010"] = 'W'; later["10010010"] = 'X'; later["10010011"] = 'Y'; later["10010000"] = 'Z'; string s; while (getline(cin, s)) { string ans = ""; for (int i = 0; i < s.size(); i++) { ans += former[s[i]]; } s = ans; ans = ""; for (int i = 0; i < s.size(); i++) { for (int j = 1; i + j < s.size(); j++) { if (later.find(s.substr(i, j)) == later.end()) continue; ans += later[s.substr(i, j)]; i = i + j - 1; break; } } cout << ans << endl; } }
#include <cstring> #include <iostream> #include <map> using namespace std; #define mk make_pair int main() { map<char, string> former; former['A'] = "00000"; former['B'] = "00001"; former['C'] = "00010"; former['D'] = "00011"; former['E'] = "00100"; former['F'] = "00101"; former['G'] = "00110"; former['H'] = "00111"; former['I'] = "01000"; former['J'] = "01001"; former['K'] = "01010"; former['L'] = "01011"; former['M'] = "01100"; former['N'] = "01101"; former['O'] = "01110"; former['P'] = "01111"; former['Q'] = "10000"; former['R'] = "10001"; former['S'] = "10010"; former['T'] = "10011"; former['U'] = "10100"; former['V'] = "10101"; former['W'] = "10110"; former['X'] = "10111"; former['Y'] = "11000"; former['Z'] = "11001"; former[' '] = "11010"; former['.'] = "11011"; former[','] = "11100"; former['-'] = "11101"; former['\''] = "11110"; former['?'] = "11111"; map<string, char> later; later["101"] = ' '; later["000000"] = '\''; later["000011"] = ','; later["10010001"] = '-'; later["010001"] = '.'; later["000001"] = '?'; later["100101"] = 'A'; later["10011010"] = 'B'; later["0101"] = 'C'; later["0001"] = 'D'; later["110"] = 'E'; later["01001"] = 'F'; later["10011011"] = 'G'; later["010000"] = 'H'; later["0111"] = 'I'; later["10011000"] = 'J'; later["0110"] = 'K'; later["00100"] = 'L'; later["10011001"] = 'M'; later["10011110"] = 'N'; later["00101"] = 'O'; later["111"] = 'P'; later["10011111"] = 'Q'; later["1000"] = 'R'; later["00110"] = 'S'; later["00111"] = 'T'; later["10011100"] = 'U'; later["10011101"] = 'V'; later["000010"] = 'W'; later["10010010"] = 'X'; later["10010011"] = 'Y'; later["10010000"] = 'Z'; string s; while (getline(cin, s)) { string ans = ""; for (int i = 0; i < s.size(); i++) { ans += former[s[i]]; } s = ans; ans = ""; for (int i = 0; i < s.size(); i++) { for (int j = 1; i + j <= s.size(); j++) { if (later.find(s.substr(i, j)) == later.end()) continue; ans += later[s.substr(i, j)]; i = i + j - 1; break; } } cout << ans << endl; } }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
1
894
2
# Aizu Problem 0111: Doctor's Memorable Code # import sys, math, os # read input: PYDEV = os.environ.get('PYDEV') if PYDEV=="True": sys.stdin = open("sample-input.txt", "rt") def bin5(k): b = bin(k)[2:] while len(b) < 5: b = '0' + b return b CODE1 = {' ': '101', "'": '000000', ",": '000011', '-': '10010001', '.': '010001', '?': '000001', 'A': '100101', 'B': '10011010', 'C': '0101', 'D': '0001', 'E': '110', 'F': '01001', 'G': '10011011', 'H': '010000', 'I': '0111', 'J': '10011000', 'K': '0110', 'L': '00100', 'M': '10011001', 'N': '10011110', 'O': '00101', 'P': '111', 'Q': '10011111', 'R': '1000', 'S': '00110', 'T': '00111', 'U': '10011100', 'V': '10011101', 'W': '000010', 'X': '10010010', 'Y': '10010011', 'Z': '10010000'} CODE1_rev = {value: key for key, value in CODE1.items()} CODE2 = {bin5(k): chr(65+k) for k in range(26)} symbols = [' ', '.', ',', '-', "'", '?'] for k in range(len(symbols)): CODE2[bin5(26+k)] = symbols[k] CODE2_rev = {value: key for key, value in CODE2.items()} def encode(string): enc = ''.join([CODE1[char] for char in string]) while len(enc) % 5 != 0: enc += '0' return ''.join([CODE2[enc[5*k:5*k+5]] for k in range(len(enc) // 5)]) def decode(string): bitstring = ''.join([CODE2_rev[char] for char in string]) decoded = "" while len(bitstring) > 0: k = 0 while True: k += 1 cand = bitstring[:k] if cand in CODE1_rev: decoded += CODE1_rev[cand] bitstring = bitstring[k:] break if k >= len(bitstring): return decoded for line in sys.stdin: print(decode(line.replace('\n', '')))
# Aizu Problem 0111: Doctor's Memorable Code # import sys, math, os # read input: PYDEV = os.environ.get('PYDEV') if PYDEV=="True": sys.stdin = open("sample-input.txt", "rt") def bin5(k): b = bin(k)[2:] while len(b) < 5: b = '0' + b return b CODE1 = {' ': '101', "'": '000000', ",": '000011', '-': '10010001', '.': '010001', '?': '000001', 'A': '100101', 'B': '10011010', 'C': '0101', 'D': '0001', 'E': '110', 'F': '01001', 'G': '10011011', 'H': '010000', 'I': '0111', 'J': '10011000', 'K': '0110', 'L': '00100', 'M': '10011001', 'N': '10011110', 'O': '00101', 'P': '111', 'Q': '10011111', 'R': '1000', 'S': '00110', 'T': '00111', 'U': '10011100', 'V': '10011101', 'W': '000010', 'X': '10010010', 'Y': '10010011', 'Z': '10010000'} CODE1_rev = {value: key for key, value in CODE1.items()} CODE2 = {bin5(k): chr(65+k) for k in range(26)} symbols = [' ', '.', ',', '-', "'", '?'] for k in range(len(symbols)): CODE2[bin5(26+k)] = symbols[k] CODE2_rev = {value: key for key, value in CODE2.items()} def encode(string): enc = ''.join([CODE1[char] for char in string]) while len(enc) % 5 != 0: enc += '0' return ''.join([CODE2[enc[5*k:5*k+5]] for k in range(len(enc) // 5)]) def decode(string): bitstring = ''.join([CODE2_rev[char] for char in string]) decoded = "" while len(bitstring) > 0: k = 0 while True: k += 1 cand = bitstring[:k] if cand in CODE1_rev: decoded += CODE1_rev[cand] bitstring = bitstring[k:] break if k >= len(bitstring): return decoded return decoded for line in sys.stdin: print(decode(line.replace('\n', '')))
[["+", 0, 656, 0, 14, 8, 196, 0, 37, 0, 38], ["+", 0, 656, 0, 14, 8, 196, 0, 37, 0, 22]]
5
622
2
#include <cmath> #include <iostream> using namespace std; static const double EPS = 1e-6; int main(void) { double vec[3][4]; double a[3]; double v[3]; cin >> a[0] >> a[1] >> a[2]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { cin >> vec[j][3 - i]; vec[j][3 - i] -= a[j]; } } for (int i = 0; i < 3; i++) { /* case vec[i][i] == 0 : swap*/ if (fabs(vec[i][i]) < EPS) { for (int j = i + 1; j < 3; j++) { if (fabs(vec[j][i]) < EPS) { for (int k = 0; k < 4; k++) { double tmp; tmp = vec[i][k]; vec[i][k] = vec[j][k]; vec[j][k] = tmp; } break; } } } for (int j = i + 1; j < 3; j++) { double mul = vec[j][i] / vec[i][i]; for (int k = i; k < 4; k++) { vec[j][k] -= vec[i][k] * mul; } } } if (fabs(vec[2][2]) < EPS) { cout << "HIT" << endl; return 0; } for (int i = 0; i < 3; i++) { double d = vec[2 - i][3]; for (int j = 3 - i; j < 3; j++) { d -= vec[2 - i][j] * v[j]; } v[2 - i] = d / vec[2 - i][2 - i]; } /* for(int i = 0; i < 3; i++) { for(int j = 0; j < 4; j++) { cout << vec[i][j] << " "; } cout << endl; } // */ double sum = 0; for (int i = 0; i < 3; i++) { if (v[i] < 0) { cout << "HIT" << endl; return 0; } sum += v[i]; } if (sum < 1) { cout << "HIT" << endl; return 0; } cout << "MISS" << endl; return 0; }
#include <cmath> #include <iostream> using namespace std; static const double EPS = 1e-6; int main(void) { double vec[3][4]; double a[3]; double v[3]; cin >> a[0] >> a[1] >> a[2]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { cin >> vec[j][3 - i]; vec[j][3 - i] -= a[j]; } } for (int i = 0; i < 3; i++) { /* [vec[i][i] == 0] swap */ if (fabs(vec[i][i]) < EPS) { for (int j = i + 1; j < 3; j++) { if (fabs(vec[j][i]) > EPS) { for (int k = 0; k < 4; k++) { double tmp; tmp = vec[i][k]; vec[i][k] = vec[j][k]; vec[j][k] = tmp; } break; } } } for (int j = i + 1; j < 3; j++) { double mul = vec[j][i] / vec[i][i]; for (int k = i; k < 4; k++) { vec[j][k] -= vec[i][k] * mul; } } } if (fabs(vec[2][2]) < EPS) { cout << "HIT" << endl; return 0; } for (int i = 0; i < 3; i++) { double d = vec[2 - i][3]; for (int j = 3 - i; j < 3; j++) { d -= vec[2 - i][j] * v[j]; } v[2 - i] = d / vec[2 - i][2 - i]; } /* for(int i = 0; i < 3; i++) { for(int j = 0; j < 4; j++) { cout << vec[i][j] << " "; } cout << endl; } // */ double sum = 0; for (int i = 0; i < 3; i++) { if (v[i] < 0) { cout << "HIT" << endl; return 0; } sum += v[i]; } if (sum < 1) { cout << "HIT" << endl; return 0; } cout << "MISS" << endl; return 0; }
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47]]
1
510
2
import java.awt.geom.Point2D; import java.io.*; import java.math.BigInteger; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Stack; import java.util.TreeMap; public class Main { static PrintWriter out = new PrintWriter(System.out); static FastScanner sc = new FastScanner(); static Scanner stdIn = new Scanner(System.in); static HashMap<String, Integer> map = new HashMap<String, Integer>(); static ArrayList<HashSet<String>> set = new ArrayList<HashSet<String>>(); public static void main(String[] args) { while (true) { int h = sc.nextInt(); int w = sc.nextInt(); if (h == 0 && w == 0) break; char[][] map = new char[h][w]; for (int i = 0; i < h; i++) { map[i] = sc.next().toCharArray(); } int[][] smap = new int[h + 1][w + 1]; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { smap[i][j] += smap[i][j - 1] + ((map[i - 1][j - 1] == '*') ? 1 : 0); } } for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { smap[i][j] += smap[i - 1][j]; } } int max = 0; for (int i = 0; i <= h; i++) { IN: for (int j = 0; j <= w; j++) { for (int k = i; k <= h; k++) { for (int l = j; l <= w; l++) { if (smap[i][j] + smap[k][l] - smap[k][j] - smap[i][l] == 0) { max = Math.max(max, (k - i) * (l - j)); } else { continue IN; } } } } } out.println(max); } out.flush(); } } //------------------------------// //-----------// class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public boolean hasNext() { skipUnprintable(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public double nextDouble() { return Double.parseDouble(next()); } }
import java.awt.geom.Point2D; import java.io.*; import java.math.BigInteger; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Stack; import java.util.TreeMap; public class Main { static PrintWriter out = new PrintWriter(System.out); static FastScanner sc = new FastScanner(); static Scanner stdIn = new Scanner(System.in); static HashMap<String, Integer> map = new HashMap<String, Integer>(); static ArrayList<HashSet<String>> set = new ArrayList<HashSet<String>>(); public static void main(String[] args) { while (true) { int h = sc.nextInt(); int w = sc.nextInt(); if (h == 0 && w == 0) break; char[][] map = new char[h][w]; for (int i = 0; i < h; i++) { map[i] = sc.next().toCharArray(); } int[][] smap = new int[h + 1][w + 1]; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { smap[i][j] += smap[i][j - 1] + ((map[i - 1][j - 1] == '*') ? 1 : 0); } } for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { smap[i][j] += smap[i - 1][j]; } } int max = 0; for (int i = 0; i <= h; i++) { for (int j = 0; j <= w; j++) { IN: for (int k = i; k <= h; k++) { for (int l = j; l <= w; l++) { if (smap[i][j] + smap[k][l] - smap[k][j] - smap[i][l] == 0) { max = Math.max(max, (k - i) * (l - j)); } else { continue IN; } } } } } out.println(max); } out.flush(); } } //------------------------------// //-----------// class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public boolean hasNext() { skipUnprintable(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { if (!hasNext()) throw new NoSuchElementException(); int n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public double nextDouble() { return Double.parseDouble(next()); } }
[["-", 8, 196, 0, 7, 8, 196, 0, 97, 0, 22], ["-", 8, 196, 0, 7, 8, 196, 0, 97, 0, 102], ["+", 8, 196, 0, 7, 8, 196, 0, 97, 0, 22], ["+", 8, 196, 0, 7, 8, 196, 0, 97, 0, 102]]
3
1,135
4
import java.io.*; import java.util.*; public class Main { public void solve() throws IOException { int h, w; while ((h = nextInt()) != 0 | (w = nextInt()) != 0) { char[][] rect = new char[h][w]; for (int i = 0; i < h; i++) { rect[i] = nextToken().toCharArray(); } int[][] data = new int[h + 2][w + 2]; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (rect[i - 1][j - 1] == '.') { data[i][j] = data[i][j - 1] + 1; } } } int max = 0; for (int i = 1; i <= w; i++) { int[] count = new int[w + 1]; int min = w + 1; for (int j = 1; j <= h; j++) { if (data[j][i] > 0) { for (int k = 1; k <= data[j][i]; k++) { count[k]++; max = Math.max(count[k] * k, max); } for (int k = data[j][i] + 1; k <= h; k++) { if (count[k] == 0) { break; } count[k] = 0; } } else { for (int k = 1; k <= h; k++) { if (count[k] == 0) { break; } count[k] = 0; } } } } writer.println(max); } writer.flush(); } public static void main(String args[]) throws IOException { new Main().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() throws IOException { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
import java.io.*; import java.util.*; public class Main { public void solve() throws IOException { int h, w; while ((h = nextInt()) != 0 | (w = nextInt()) != 0) { char[][] rect = new char[h][w]; for (int i = 0; i < h; i++) { rect[i] = nextToken().toCharArray(); } int[][] data = new int[h + 2][w + 2]; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (rect[i - 1][j - 1] == '.') { data[i][j] = data[i][j - 1] + 1; } } } int max = 0; for (int i = 1; i <= w; i++) { int[] count = new int[w + 1]; int min = w + 1; for (int j = 1; j <= h; j++) { if (data[j][i] > 0) { for (int k = 1; k <= data[j][i]; k++) { count[k]++; /* if( count[k]*k > max ){ writer.print(count[k]*k + " at " + j + ", " + i + ", " + k + " "); } */ max = Math.max(count[k] * k, max); } for (int k = data[j][i] + 1; k <= w; k++) { if (count[k] == 0) { break; } count[k] = 0; } } else { for (int k = 1; k <= w; k++) { if (count[k] == 0) { break; } count[k] = 0; } } } } // writer.println(); writer.println(max); } writer.flush(); } public static void main(String args[]) throws IOException { new Main().run(); } BufferedReader reader; StringTokenizer tokenizer; PrintWriter writer; public void run() throws IOException { try { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } }
[["-", 0, 57, 64, 196, 0, 7, 15, 16, 12, 22], ["+", 0, 57, 64, 196, 0, 7, 15, 16, 12, 22], ["-", 0, 57, 75, 196, 0, 7, 15, 16, 12, 22], ["+", 0, 57, 75, 196, 0, 7, 15, 16, 12, 22]]
3
573
4
#include <stdio.h> #include <strings.h> int main(void) { int i, j, k, h, w; int m[500][500]; int area, hmin; char str[501]; while (scanf("%d %d", &h, &w), h + w) { area = 0; for (i = 0; i < h; i++) { scanf("%s", str); for (j = 0; j < w; j++) { int c; if (str[j] == '*') c = 0; else { if (i == 0) c = 1; else c = m[i - 1][j] + 1; } m[i][j] = c; } } for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { hmin = 501; for (k = j; k < w || m[i][k] != 0; k++) { if (m[i][k] < hmin) hmin = m[i][k]; if (area < hmin * (k - j + 1)) area = hmin * (k - j + 1); } } } printf("%d\n", area); } return 0; }
#include <stdio.h> #include <strings.h> int main(void) { int i, j, k, h, w; int m[500][500]; int area, hmin; char str[501]; while (scanf("%d %d", &h, &w), h + w) { area = 0; for (i = 0; i < h; i++) { scanf("%s", str); for (j = 0; j < w; j++) { int c; if (str[j] == '*') c = 0; else { if (i == 0) c = 1; else c = m[i - 1][j] + 1; } m[i][j] = c; } } for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { hmin = 501; for (k = j; k < w && m[i][k] != 0; k++) { if (m[i][k] < hmin) hmin = m[i][k]; if (area < hmin * (k - j + 1)) area = hmin * (k - j + 1); } } } printf("%d\n", area); } return 0; }
[["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 106], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 98]]
0
282
2
#include <algorithm> #include <cstdlib> #include <cstring> #include <iostream> #include <string> #include <utility> #include <vector> #define YNUM(X) (X < 0 ? X + 3 : X) typedef std::pair<int, int> WH; typedef std::vector<WH> whvec; int h, w; bool map[502][502]; std::string in; std::vector<std::vector<whvec>> data(3, std::vector<whvec>(502)); int data_max = 0; int height_sum[502][502], width_sum[502][502]; WH wh; int main() { while (std::cin >> h >> w, w + h != 0) { memset(map, 0, sizeof(map)); memset(height_sum, 0, sizeof(height_sum)); memset(width_sum, 0, sizeof(width_sum)); for (int i = 1; i <= h; ++i) { std::cin >> in; for (int l = 1; l <= w; ++l) { map[i][l] = (in[l - 1] == '.'); } } for (int i = 1; i <= h; ++i) { for (int l = 1; l <= w; ++l) { if (map[i][l]) { width_sum[i][l] = width_sum[i][l - 1] + 1; height_sum[i][l] = height_sum[i - 1][l] + 1; } } } for (int i = 1, n = 1; n <= h; n++, i = n % 3) { for (int l = 1; l <= w; l++) { data[i][l].clear(); if (map[n][l]) { if (map[n - 1][l] && map[n][l - 1]) { int x_cnt = width_sum[n][l], y_cnt = height_sum[n][l]; for (int m = 0; m < data[YNUM(i - 1)][l].size(); ++m) { wh = WH(std::min(data[YNUM(i - 1)][l][m].first, x_cnt), data[YNUM(i - 1)][l][m].second + 1); if (std::find(data[i][l].begin(), data[i][l].end(), wh) == data[i][l].end()) { data[i][l].push_back(wh); data_max = std::max(data_max, wh.first * wh.second); } } for (int m = 0; m < data[i][l - 1].size(); ++m) { wh = WH(data[i][l - 1][m].first + 1, std::min(data[i][l - 1][m].second, y_cnt)); if (std::find(data[i][l].begin(), data[i][l].end(), wh) == data[i][l].end()) { data[i][l].push_back(wh); data_max = std::max(data_max, wh.first * wh.second); } } } else if (map[n - 1][l]) { for (int m = 0; m < data[YNUM(i - 1)][l].size(); ++m) { data[i][l].push_back( wh = WH(1, data[YNUM(i - 1)][l][m].second + 1)); data_max = std::max(data_max, wh.first * wh.second); } } else if (map[n][l - 1]) { for (int m = 0; m < data[i][l - 1].size(); ++m) { data[i][l].push_back(wh = WH(data[i][l - 1][m].first + 1, 1)); data_max = std::max(data_max, wh.first * wh.second); } } else data[i][l].push_back(wh = WH(1, 1)), data_max = std::max(data_max, wh.first * wh.second); } } } std::cout << data_max << std::endl; } return 0; }
#include <algorithm> #include <cstdlib> #include <cstring> #include <iostream> #include <string> #include <utility> #include <vector> #define YNUM(X) (X < 0 ? X + 3 : X) typedef std::pair<int, int> WH; typedef std::vector<WH> whvec; int h, w; bool map[502][502]; std::string in; std::vector<std::vector<whvec>> data(3, std::vector<whvec>(502)); int data_max = 0; int height_sum[502][502], width_sum[502][502]; WH wh; int main() { while (std::cin >> h >> w, w + h != 0) { data_max = 0; memset(map, 0, sizeof(map)); memset(height_sum, 0, sizeof(height_sum)); memset(width_sum, 0, sizeof(width_sum)); for (int i = 1; i <= h; ++i) { std::cin >> in; for (int l = 1; l <= w; ++l) { map[i][l] = (in[l - 1] == '.'); } } for (int i = 1; i <= h; ++i) { for (int l = 1; l <= w; ++l) { if (map[i][l]) { width_sum[i][l] = width_sum[i][l - 1] + 1; height_sum[i][l] = height_sum[i - 1][l] + 1; } } } for (int i = 1, n = 1; n <= h; n++, i = n % 3) { for (int l = 1; l <= w; l++) { data[i][l].clear(); if (map[n][l]) { if (map[n - 1][l] && map[n][l - 1]) { int x_cnt = width_sum[n][l], y_cnt = height_sum[n][l]; for (int m = 0; m < data[YNUM(i - 1)][l].size(); ++m) { wh = WH(std::min(data[YNUM(i - 1)][l][m].first, x_cnt), data[YNUM(i - 1)][l][m].second + 1); if (std::find(data[i][l].begin(), data[i][l].end(), wh) == data[i][l].end()) { data[i][l].push_back(wh); data_max = std::max(data_max, wh.first * wh.second); } } for (int m = 0; m < data[i][l - 1].size(); ++m) { wh = WH(data[i][l - 1][m].first + 1, std::min(data[i][l - 1][m].second, y_cnt)); if (std::find(data[i][l].begin(), data[i][l].end(), wh) == data[i][l].end()) { data[i][l].push_back(wh); data_max = std::max(data_max, wh.first * wh.second); } } } else if (map[n - 1][l]) { for (int m = 0; m < data[YNUM(i - 1)][l].size(); ++m) { data[i][l].push_back( wh = WH(1, data[YNUM(i - 1)][l][m].second + 1)); data_max = std::max(data_max, wh.first * wh.second); } } else if (map[n][l - 1]) { for (int m = 0; m < data[i][l - 1].size(); ++m) { data[i][l].push_back(wh = WH(data[i][l - 1][m].first + 1, 1)); data_max = std::max(data_max, wh.first * wh.second); } } else data[i][l].push_back(wh = WH(1, 1)), data_max = std::max(data_max, wh.first * wh.second); } } } std::cout << data_max << std::endl; } return 0; }
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
1
978
4
#include <cstdio> int main(void) { int n = 0, s[21] = {0}; bool r[501][501]; int h, w, i, j, k, l; char c[501][501]; do { scanf("%d%d", &h, &w); for (i = 0; i < h; i++) { scanf("%s", c[i]); for (j = 0; j < w; j++) { if (c[i][j] == '*') { r[i][j] = false; } else { r[i][j] = true; } } } for (i = 0; i < h; i++) { r[i][w] = false; } for (j = 0; j < w; j++) { r[h][j] = false; } for (i = 0; i < h; i++) { // printf("iok\n"); for (j = 0; j < w; j++) { // printf("jok\n"); if (r[i][j] == true) { // printf("kansoku\n"); int a, b = 0; while (r[i + b][j] == true) { a = 0; while (true) { l = 0; for (k = i; k <= i + b; k++) { if (r[k][j + a] == false) l++; if (r[k][j + a] == false) break; } // printf("i=%d,j=%d,b=%d,a=%d,l=%d\n",i,j,b,a,l); if (l != 0 || a + j == w) { if (s[n] < (a) * (b + 1)) { // printf("i=%d,j=%d,a=%d,b+1=%d\n",i,j,a,b+1); s[n] = (a) * (b + 1); } } else { a++; } if (l != 0) break; } b++; } } } } n++; } while (w != 0 || h != 0); for (i = 0; i < n; i++) { printf("%d\n", s[i]); } return 0; }
#include <cstdio> int main(void) { int n = 0, s[21] = {0}; bool r[501][501]; int h, w, i, j, k, l; char c[501][501]; do { scanf("%d%d", &h, &w); for (i = 0; i < h; i++) { scanf("%s", c[i]); for (j = 0; j < w; j++) { if (c[i][j] == '*') { r[i][j] = false; } else { r[i][j] = true; } } } for (i = 0; i < h; i++) { r[i][w] = false; } for (j = 0; j < w; j++) { r[h][j] = false; } for (i = 0; i < h; i++) { // printf("iok\n"); for (j = 0; j < w; j++) { // printf("jok\n"); if (r[i][j] == true) { // printf("kansoku\n"); int a, b = 0; while (r[i + b][j] == true) { a = 1; while (true) { l = 0; for (k = i; k <= i + b; k++) { if (r[k][j + a] == false) l++; if (r[k][j + a] == false) break; } // printf("i=%d,j=%d,b=%d,a=%d,l=%d\n",i,j,b,a,l); if (l != 0 || a + j == w) { if (s[n] < (a) * (b + 1)) { // printf("i=%d,j=%d,a=%d,b+1=%d\n",i,j,a,b+1); s[n] = (a) * (b + 1); } } else { a++; } if (l != 0) break; } b++; } } } } n++; } while (w != 0 || h != 0); for (i = 0; i < n - 1; i++) { printf("%d\n", s[i]); } return 0; }
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
1
447
4
#include <algorithm> #include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; int m[501][501]; int main() { int h, w; while (cin >> h >> w, h) { memset(m, 0, sizeof(m)); int x, y; string s; for (y = 1; y <= h; y++) { cin >> s; for (x = 1; x <= w; x++) { if (s[x - 1] == '.') m[y][x] = m[y][x - 1] + 1; } } int ret = 0; for (y = 1; y <= h; y++) { for (x = 1; x <= w; x++) { if (m[y][x] != 0) { int j = 0, k = m[y][x]; for (int i = y; m[i][x] != 0; i++) { k = min(k, m[i][x]); j++; } ret = max(ret, j * k); } } } cout << ret << endl; } return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <string> #include <vector> using namespace std; int m[501][501]; int main() { int h, w; while (cin >> h >> w, h) { memset(m, 0, sizeof(m)); int x, y; string s; for (y = 1; y <= h; y++) { cin >> s; for (x = 1; x <= w; x++) { if (s[x - 1] == '.') m[y][x] = m[y][x - 1] + 1; } } int ret = 0; for (y = 1; y <= h; y++) { for (x = 1; x <= w; x++) { if (m[y][x] != 0) { int j = 0, k = m[y][x]; for (int i = y; m[i][x] != 0; i++) { k = min(k, m[i][x]); j++; ret = max(ret, j * k); } } } } cout << ret << endl; } return 0; }
[["-", 0, 57, 64, 9, 0, 7, 8, 9, 0, 46], ["+", 0, 57, 64, 9, 0, 7, 8, 9, 0, 46]]
1
257
2
#include <algorithm> #include <cstdio> #include <iostream> #include <stack> #include <vector> using namespace std; const int MAX_N = 505; const int BLOCK = 1; struct Rect { int h, pos; Rect(int h_, int pos_) { h = h_; pos = pos_; } Rect() {} }; int getLargestRectangle(vector<int> buffer) { int size = buffer.size(); stack<Rect> S; int maxv = 0; buffer.push_back(0); for (int i = 0; i <= size; i++) { Rect rect(buffer[i], i); if (S.empty()) { S.push(rect); } else { if (S.top().h < rect.h) { S.push(rect); } else if (S.top().h > rect.h) { int target = i; while (!S.empty() && S.top().h >= rect.h) { Rect prev = S.top(); S.pop(); int area = prev.h * (i - prev.pos); maxv = max(maxv, area); target = prev.pos; } rect.pos = target; S.push(rect); } } } return maxv; } int N; int buffer[MAX_N][MAX_N]; int getLargestRectangle() { vector<int> T[MAX_N]; for (int i = 0; i < N; i++) { T[i] = vector<int>(N); } for (int j = 0; j < N; j++) { int sequence = 0; for (int i = 0; i < N; i++) { if (buffer[i][j] == BLOCK) { sequence = T[i][j] = 0; } else { T[i][j] = ++sequence; } } } int maxv = 0; for (int i = 0; i < N; i++) { maxv = max(maxv, getLargestRectangle(T[i])); } return maxv; } int main() { int w, h; while (cin >> h >> w, w || h) { N = max(w, h); vector<string> s(h); for (int y = 0; y < h; y++) { cin >> s[y]; } for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { buffer[y][x] = 1; } } for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { buffer[y][x] = (s[y][x] == '.') ? 0 : 1; } } // 0 or 1 の値の二次元配列 bufferで1のみで構成される最大の長方形の面積を出力 cout << getLargestRectangle() << endl; } }
#include <algorithm> #include <cstdio> #include <iostream> #include <stack> #include <vector> using namespace std; const int MAX_N = 500; const int BLOCK = 1; struct Rect { int h, pos; Rect(int h_, int pos_) { h = h_; pos = pos_; } Rect() {} }; int getLargestRectangle(vector<int> buffer) { int size = buffer.size(); stack<Rect> S; int maxv = 0; buffer.push_back(0); for (int i = 0; i <= size; i++) { Rect rect(buffer[i], i); if (S.empty()) { S.push(rect); } else { if (S.top().h < rect.h) { S.push(rect); } else if (S.top().h > rect.h) { int target = i; while (!S.empty() && S.top().h >= rect.h) { Rect prev = S.top(); S.pop(); int area = prev.h * (i - prev.pos); maxv = max(maxv, area); target = prev.pos; } rect.pos = target; S.push(rect); } } } return maxv; } int N; int buffer[MAX_N][MAX_N]; int getLargestRectangle() { vector<int> T[MAX_N]; for (int i = 0; i < N; i++) { T[i] = vector<int>(N); } for (int j = 0; j < N; j++) { int sequence = 0; for (int i = 0; i < N; i++) { if (buffer[i][j] == BLOCK) { sequence = T[i][j] = 0; } else { T[i][j] = ++sequence; } } } int maxv = 0; for (int i = 0; i < N; i++) { maxv = max(maxv, getLargestRectangle(T[i])); } return maxv; } int main() { int w, h; while (cin >> h >> w, w || h) { N = max(w, h); vector<string> s(h); for (int y = 0; y < h; y++) { cin >> s[y]; } for (int y = 0; y < N; y++) { for (int x = 0; x < N; x++) { buffer[y][x] = 1; } } for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { buffer[y][x] = (s[y][x] == '.') ? 0 : 1; } } // 0 or 1 の値の二次元配列 bufferで1のみで構成される最大の長方形の面積を出力 cout << getLargestRectangle() << endl; } }
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 22]]
1
620
6
// from PKU1125 main() { int i, n, k, a, b, A, B, dist[101][101]; scanf("%d%d", &n, &k); /*initialize*/ memset(dist, 99, sizeof(dist)); for (i = 1; i <= n; i++) dist[i][i] = 0; for (; k; k--) { scanf("%d,%d,%d,%d", &a, &b, &A, &B); dist[a][b] = A, dist[b][a] = B; } { /*warshall-floyd*/ int k, i, j; for (k = 1; k <= n; k++) for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) if (dist[i][j] > dist[i][k] + dist[k][j]) dist[i][j] = dist[i][k] + dist[k][j]; } { /*query*/ scanf("%d,%d,%d,%d", &a, &b, &A, &B); printf("%d\n", A - B - dist[a][b] - dist[b][a]); } exit(0); }
// from PKU1125 main() { int i, n, k, a, b, A, B, dist[101][101]; scanf("%d%d", &n, &k); /*initialize*/ memset(dist, 11, sizeof(dist)); for (i = 1; i <= n; i++) dist[i][i] = 0; for (; k; k--) { scanf("%d,%d,%d,%d", &a, &b, &A, &B); dist[a][b] = A, dist[b][a] = B; } { /*warshall-floyd*/ int k, i, j; for (k = 1; k <= n; k++) for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) if (dist[i][j] > dist[i][k] + dist[k][j]) dist[i][j] = dist[i][k] + dist[k][j]; } { /*query*/ scanf("%d,%d,%d,%d", &a, &b, &A, &B); printf("%d\n", A - B - dist[a][b] - dist[b][a]); } exit(0); }
[["-", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 0, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
0
281
2
#include <math.h> #include <stdio.h> #include <stdlib.h> int i, j, k, n, m, l, x, y, route[2][21][21]; int main() { scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i != j) route[0][i][j] = 999999999; } } scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%d,%d,%d,%d", &j, &k, &x, &y); route[0][j - 1][k - 1] = x; route[0][k - 1][j - 1] = y; } for (l = 1; pow(2.0, l) < n - 1; l++) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { route[l % 2][i][j] = route[(l + 1) % 2][i][j]; for (k = 0; k < n; k++) { if (route[l % 2][i][j] > route[(l + 1) % 2][i][k] + route[(l + 1) % 2][k][j]) { route[l % 2][i][j] = route[(l + 1) % 2][i][k] + route[(l + 1) % 2][k][j]; } } } } } l = (l - 1) % 2; scanf("%d,%d,%d,%d", &x, &y, &j, &k); printf("%d\n", j - k - route[l][x - 1][y - 1] - route[l][y - 1][x - 1]); return 0; }
#include <math.h> #include <stdio.h> #include <stdlib.h> int i, j, k, n, m, l, x, y, route[2][21][21]; int main() { scanf("%d", &n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (i != j) route[0][i][j] = 999999999; } } scanf("%d", &m); for (i = 0; i < m; i++) { scanf("%d,%d,%d,%d", &j, &k, &x, &y); route[0][j - 1][k - 1] = x; route[0][k - 1][j - 1] = y; } for (l = 1; pow(2.0, l - 1) < n - 1; l++) { for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { route[l % 2][i][j] = route[(l + 1) % 2][i][j]; for (k = 0; k < n; k++) { if (route[l % 2][i][j] > route[(l + 1) % 2][i][k] + route[(l + 1) % 2][k][j]) { route[l % 2][i][j] = route[(l + 1) % 2][i][k] + route[(l + 1) % 2][k][j]; } } } } } l = (l - 1) % 2; scanf("%d,%d,%d,%d", &x, &y, &j, &k); printf("%d\n", j - k - route[l][x - 1][y - 1] - route[l][y - 1][x - 1]); return 0; }
[["+", 15, 16, 31, 2, 3, 4, 0, 16, 17, 33], ["+", 15, 16, 31, 2, 3, 4, 0, 16, 12, 13]]
0
446
2
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <sstream> #include <string> #define INF 10000000 int n, m, st, gl, money, pole; int roads[21][21]; int in[4]; std::string input; int check(int start, int goal) { int cost[21]; int used = 0; std::fill(cost, cost + 21, INF); cost[start] = 0; while (1) { int v = -1; for (int i = 1; i <= m; i++) { if (!(used & 1 << i) && (v == -1 || cost[i] < cost[v])) v = i; } if (v == -1) break; used = used | 1 << v; for (int i = 1; i <= m; ++i) { cost[i] = std::min(cost[i], cost[v] + roads[v][i]); } } return cost[goal]; } int main() { for (int i = 0; i < 21; ++i) std::fill(roads[i], roads[i] + 21, INF); std::cin >> n >> m; for (int i = 0; i < m; ++i) { std::cin >> input; sscanf(input.c_str(), "%d,%d,%d,%d", in, &in[1], &in[2], &in[3]); roads[in[0]][in[1]] = in[2]; roads[in[1]][in[0]] = in[3]; } std::cin >> input; sscanf(input.c_str(), "%d,%d,%d,%d", &st, &gl, &money, &pole); money -= pole; int cost = check(st, gl); cost += check(gl, st); std::cout << money - cost << std::endl; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <sstream> #include <string> #define INF 10000000 int n, m, st, gl, money, pole; int roads[21][21]; int in[4]; std::string input; int check(int start, int goal) { int cost[21]; int used = 0; std::fill(cost, cost + 21, INF); cost[start] = 0; while (1) { int v = -1; for (int i = 1; i <= n; i++) { if (!(used & 1 << i) && (v == -1 || cost[i] < cost[v])) v = i; } if (v == -1) break; used = used | 1 << v; for (int i = 1; i <= n; ++i) { cost[i] = std::min(cost[i], cost[v] + roads[v][i]); } } return cost[goal]; } int main() { for (int i = 0; i < 21; ++i) std::fill(roads[i], roads[i] + 21, INF); std::cin >> n >> m; for (int i = 0; i < m; ++i) { std::cin >> input; sscanf(input.c_str(), "%d,%d,%d,%d", in, &in[1], &in[2], &in[3]); roads[in[0]][in[1]] = in[2]; roads[in[1]][in[0]] = in[3]; } std::cin >> input; sscanf(input.c_str(), "%d,%d,%d,%d", &st, &gl, &money, &pole); money -= pole; int cost = check(st, gl); cost += check(gl, st); std::cout << money - cost << std::endl; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 22]]
1
422
4
#include <cstdio> #include <iostream> using namespace std; int N, M, A, B, C, D, x1, x2, y1, y2; int K[32][32]; const int inf = 1001001001; int main() { for (int i = 0; i < 32; ++i) { for (int j = 0; j < 32; ++j) { K[i][j] = inf; } } scanf("%d%d", &N, &M); for (int i = 0; i < M; ++i) { scanf("%d,%d,%d,%d", &A, &B, &C, &D); // cerr << "read " << A << " " << B << " " << C << " " << D << endl; K[A - 1][B - 1] = C; K[B - 1][A - 1] = D; } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (K[i][j] > (K[i][k] + K[k][j])) { K[i][j] = K[i][k] + K[k][j]; } } } } scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2); cerr << y1 - K[x1 - 1][x2 - 1] - y2 - K[x2 - 1][x1 - 1] << endl; }
#include <cstdio> #include <iostream> using namespace std; int N, M, A, B, C, D, x1, x2, y1, y2; int K[32][32]; const int inf = 1001001001; int main() { for (int i = 0; i < 32; ++i) { for (int j = 0; j < 32; ++j) { K[i][j] = inf; } } scanf("%d%d", &N, &M); for (int i = 0; i < M; ++i) { scanf("%d,%d,%d,%d", &A, &B, &C, &D); // cerr << "read " << A << " " << B << " " << C << " " << D << endl; K[A - 1][B - 1] = C; K[B - 1][A - 1] = D; } for (int k = 0; k < N; k++) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (K[i][j] > (K[i][k] + K[k][j])) { K[i][j] = K[i][k] + K[k][j]; } } } } scanf("%d,%d,%d,%d", &x1, &x2, &y1, &y2); cout << y1 - K[x1 - 1][x2 - 1] - y2 - K[x2 - 1][x1 - 1] << endl; }
[["-", 8, 9, 0, 1, 0, 16, 31, 16, 31, 22], ["+", 8, 9, 0, 1, 0, 16, 31, 16, 31, 22]]
1
322
2
#include <algorithm> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <vector> using namespace std; int dx[12] = {2, 2, 2, 1, 0, -1, -2, -2, -2, -1, 0, 1}; int dy[12] = {1, 0, -1, -2, -2, -2, -1, 0, 1, 2, 2, 2}; class State { public: int x; int y; int time; }; int main(void) { int visited[10][10]; int posx, posy, num; int tmpx, tmpy; deque<State> q; vector<pair<int, int>> sp; while (cin >> posx >> posy) { if ((posx | posy) == 0) break; State s, t; bool end = false; cin >> num; q.clear(); sp.clear(); s.x = posx; s.y = posy; s.time = 0; q.push_back(s); memset(visited, 0, sizeof(visited)); for (int i = 0; i < num; i++) { cin >> tmpx >> tmpy; sp.push_back(make_pair(tmpx, tmpy)); } if (num == 0) { cout << "NG" << endl; continue; } while (!q.empty()) { s = q.front(); q.pop_front(); // printf("[%d]x = %d, y = %d\n", s.time, s.x, s.y); // printf("next sprinkler = %d, %d\n", sp[s.time%num].first, // sp[s.time%num].second); if (s.time == num) { end = true; cout << "OK" << endl; break; } for (int i = 0; i < 12; i++) { //”ò‚яo‚µ‚Ä‚¢‚È‚­‚Ä‚©‚ƒXƒvƒŠƒ“ƒNƒ‰“à‚È‚ç // printf("try %d, %d\n", s.x+dx[i], s.y+dy[i]); if (0 <= s.x + dx[i] && s.x + dx[i] <= 9 && 0 <= s.y + dy[i] && s.y + dy[i] <= 9 && abs(sp[s.time % num].first - (s.x + dx[i])) <= 1 && abs(sp[s.time % num].second - (s.y + dy[i])) <= 1) { t.x = s.x + dx[i]; t.y = s.y + dy[i]; t.time = s.time + 1; q.push_back(t); // cout << "push " << t.x << " " << t.y << endl; } } } if (!end) { cout << "NG" << endl; } } }
#include <algorithm> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <vector> using namespace std; int dx[12] = {2, 2, 2, 1, 0, -1, -2, -2, -2, -1, 0, 1}; int dy[12] = {1, 0, -1, -2, -2, -2, -1, 0, 1, 2, 2, 2}; class State { public: int x; int y; int time; }; int main(void) { int visited[10][10]; int posx, posy, num; int tmpx, tmpy; deque<State> q; vector<pair<int, int>> sp; while (cin >> posx >> posy) { if ((posx | posy) == 0) break; State s, t; bool end = false; cin >> num; q.clear(); sp.clear(); s.x = posx; s.y = posy; s.time = 0; q.push_back(s); memset(visited, 0, sizeof(visited)); for (int i = 0; i < num; i++) { cin >> tmpx >> tmpy; sp.push_back(make_pair(tmpx, tmpy)); } if (num == 0) { cout << "NA" << endl; continue; } while (!q.empty()) { s = q.front(); q.pop_front(); // printf("[%d]x = %d, y = %d\n", s.time, s.x, s.y); // printf("next sprinkler = %d, %d\n", sp[s.time%num].first, // sp[s.time%num].second); if (s.time == num) { end = true; cout << "OK" << endl; break; } for (int i = 0; i < 12; i++) { //”ò‚яo‚µ‚Ä‚¢‚È‚­‚Ä‚©‚ƒXƒvƒŠƒ“ƒNƒ‰“à‚È‚ç // printf("try %d, %d\n", s.x+dx[i], s.y+dy[i]); if (0 <= s.x + dx[i] && s.x + dx[i] <= 9 && 0 <= s.y + dy[i] && s.y + dy[i] <= 9 && abs(sp[s.time % num].first - (s.x + dx[i])) <= 1 && abs(sp[s.time % num].second - (s.y + dy[i])) <= 1) { t.x = s.x + dx[i]; t.y = s.y + dy[i]; t.time = s.time + 1; q.push_back(t); // cout << "push " << t.x << " " << t.y << endl; } } } if (!end) { cout << "NA" << endl; } } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
509
4
#include <cstring> #include <iostream> #include <queue> using namespace std; typedef pair<int, int> P; typedef pair<P, int> PP; int spx[10], spy[10]; const int jux[12] = {-2, -2, -2, 2, 2, 2, -1, 0, 1, -1, 0, 1}; const int juy[12] = {-1, 0, 1, -1, 0, 1, -2, -2, -2, 2, 2, 2}; int x, y, n; bool pyon() { queue<PP> M; M.push(PP(P(x, y), 1)); while (!M.empty()) { PP m = M.front(); M.pop(); int mx = m.first.first, my = m.first.second, ms = m.second; for (int a = 0; a < 12; a++) { int nx = mx + jux[a], ny = my + juy[a]; if (nx >= 0 && nx <= 9 && ny >= 0 && ny <= 9) { if (nx >= spx[ms] - 1 && nx <= spx[ms] + 1 && ny >= spy[ms] - 1 && ny <= spy[ms] + 1) { if (ms == n) return true; else { M.push(PP(P(nx, ny), ms + 1)); } } } } } return false; } int main(void) { while (1) { int a; scanf("%d%d", &x, &y); if (x == 0 && y == 0) break; scanf("%d", &n); for (a = 1; a <= n; a++) { int xx, yy; scanf("%d%d", &xx, &yy); spx[a] = xx, spy[a] = yy; } if (pyon()) cout << "OK" << endl; else cout << "NA" << endl; } return 0; }
#include <cstring> #include <iostream> #include <queue> using namespace std; typedef pair<int, int> P; typedef pair<P, int> PP; int spx[11], spy[11]; const int jux[12] = {-2, -2, -2, 2, 2, 2, -1, 0, 1, -1, 0, 1}; const int juy[12] = {-1, 0, 1, -1, 0, 1, -2, -2, -2, 2, 2, 2}; int x, y, n; bool pyon() { queue<PP> M; M.push(PP(P(x, y), 1)); while (!M.empty()) { PP m = M.front(); M.pop(); int mx = m.first.first, my = m.first.second, ms = m.second; for (int a = 0; a < 12; a++) { int nx = mx + jux[a], ny = my + juy[a]; if (nx >= 0 && nx <= 9 && ny >= 0 && ny <= 9) { if (nx >= spx[ms] - 1 && nx <= spx[ms] + 1 && ny >= spy[ms] - 1 && ny <= spy[ms] + 1) { if (ms == n) return true; else { M.push(PP(P(nx, ny), ms + 1)); } } } } } return false; } int main(void) { while (1) { int a; scanf("%d%d", &x, &y); if (x == 0 && y == 0) break; scanf("%d", &n); for (a = 1; a <= n; a++) { int xx, yy; scanf("%d%d", &xx, &yy); spx[a] = xx, spy[a] = yy; } if (pyon()) cout << "OK" << endl; else cout << "NA" << endl; } return 0; }
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13]]
1
443
4
#include <algorithm> #include <iostream> using namespace std; const int MAXN = 11; const int di[12] = {-1, 0, 1, 2, 2, 2, 1, 0, -1, -2, -2, -2}; const int dj[12] = {2, 2, 2, 1, 0, -1, -2, -2, -2, -1, 0, 1}; const int H = 10; const int W = 10; int si, sj; int n; int ti[MAXN], tj[MAXN]; bool dfs(int step, int pi, int pj) { if (step == n) return true; for (int k = 0; k < 12; ++k) { int ni = pi + di[k]; int nj = pj + dj[k]; if (ni < 0 || ni >= H) continue; if (nj < 0 || nj >= W) continue; if (max(abs(ti[step] - ni), abs(tj[step] - nj)) >= 2) continue; if (dfs(step + 1, ni, nj)) return true; } return false; } int main() { while (cin >> sj >> si && (si | sj)) { cin >> n; for (int i = 0; i < n; ++i) { cin >> tj[i] >> ti[i]; } if (dfs(0, si, sj)) { cout << "OK" << endl; } else { cout << "NG" << endl; } } return 0; }
#include <algorithm> #include <iostream> using namespace std; const int MAXN = 11; const int di[12] = {-1, 0, 1, 2, 2, 2, 1, 0, -1, -2, -2, -2}; const int dj[12] = {2, 2, 2, 1, 0, -1, -2, -2, -2, -1, 0, 1}; const int H = 10; const int W = 10; int si, sj; int n; int ti[MAXN], tj[MAXN]; bool dfs(int step, int pi, int pj) { if (step == n) return true; for (int k = 0; k < 12; ++k) { int ni = pi + di[k]; int nj = pj + dj[k]; if (ni < 0 || ni >= H) continue; if (nj < 0 || nj >= W) continue; if (max(abs(ti[step] - ni), abs(tj[step] - nj)) >= 2) continue; if (dfs(step + 1, ni, nj)) return true; } return false; } int main() { while (cin >> sj >> si && (si | sj)) { cin >> n; for (int i = 0; i < n; ++i) { cin >> tj[i] >> ti[i]; } if (dfs(0, si, sj)) { cout << "OK" << endl; } else { cout << "NA" << endl; } } return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
331
2
#include <iostream> using namespace std; int dx[12] = {-2, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 2}; int dy[12] = {1, 0, -1, -2, 2, -2, 2, -2, 2, 1, 0, -1}; int fy[9] = {1, 0, -1, 1, 0, -1, 1, 0, -1}; int fx[9] = {1, 1, 1, 0, 0, 0, -1, -1, -1}; int n, x, y, flg; int sx, sy; int t[10][10][10]; void init(); void dfs(int, int, int); int main() { while (cin >> x >> y) { if (x == 0 && y == 0) break; init(); cin >> n; for (int i = 0; i < n; i++) { cin >> sx >> sy; for (int j = 0; j < 9; j++) { int nx, ny; nx = sx + fx[j]; ny = sy + fy[j]; if (nx < 0 || ny < 0 || nx >= 10 || ny >= 10) continue; t[i][nx][ny] = 1; } } flg = 0; dfs(y, x, 0); if (flg) cout << "OK" << endl; else cout << "NA" << endl; } return 0; } void init() { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) for (int k = 0; k < 10; k++) t[i][j][k] = 0; } void dfs(int y, int x, int c) { if (c == n - 1) { flg = 1; return; } int nx, ny; for (int i = 0; i < 12; i++) { nx = x + dx[i]; ny = y + dy[i]; if (nx < 0 || ny < 0 || nx >= 10 || ny >= 10) continue; if (t[c + 1][ny][nx] == 0) continue; dfs(ny, nx, c + 1); } }
#include <iostream> using namespace std; int dx[12] = {-2, -2, -2, -1, -1, 0, 0, 1, 1, 2, 2, 2}; int dy[12] = {1, 0, -1, -2, 2, -2, 2, -2, 2, 1, 0, -1}; int fy[9] = {1, 0, -1, 1, 0, -1, 1, 0, -1}; int fx[9] = {1, 1, 1, 0, 0, 0, -1, -1, -1}; int n, x, y, flg; int sx, sy; int t[10][10][10]; void init(); void dfs(int, int, int); int main() { while (cin >> x >> y) { if (x == 0 && y == 0) break; init(); cin >> n; for (int i = 0; i < n; i++) { cin >> sx >> sy; for (int j = 0; j < 9; j++) { int nx, ny; nx = sx + fx[j]; ny = sy + fy[j]; if (nx < 0 || ny < 0 || nx >= 10 || ny >= 10) continue; t[i][nx][ny] = 1; } /* cout<<endl; for(int j=0;j<9;j++){ for(int k=0;k<9;k++){ if(k==sy&&j==sx)cout<<'x'; else cout<<t[i][j][k]; } cout<<endl; } */ } flg = 0; dfs(x, y, -1); if (flg) cout << "OK" << endl; else cout << "NA" << endl; } return 0; } void init() { for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) for (int k = 0; k < 10; k++) t[i][j][k] = 0; } void dfs(int y, int x, int c) { if (c == n - 1) { flg = 1; return; } int nx, ny; for (int i = 0; i < 12; i++) { nx = x + dx[i]; ny = y + dy[i]; if (nx < 0 || ny < 0 || nx >= 10 || ny >= 10) continue; if (t[c + 1][ny][nx] == 0) continue; dfs(ny, nx, c + 1); } }
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 21], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
1
510
6
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { static String classify(double a, double b) { if (a < 35.5 && b < 71) { return "AAA"; } else if (a < 37.5 && b < 77) { return "AA"; } else if (a < 40 && b < 83) { return "A"; } else if (a < 43 && b < 89) { return "B"; } else if (a < 50 && b < 105) { return "C"; } else if (a < 55.5 && b < 116) { return "D"; } else if (a < 70 && b < 148) { return "E"; } else { return "NA"; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null) { String[] input = line.split(" "); double a = Double.parseDouble(input[0]); double b = Double.parseDouble(input[1]); System.out.println(classify(a, b)); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class Main { static String classify(double a, double b) { if (a < 35.5 && b < 71) { return "AAA"; } else if (a < 37.5 && b < 77) { return "AA"; } else if (a < 40 && b < 83) { return "A"; } else if (a < 43 && b < 89) { return "B"; } else if (a < 50 && b < 105) { return "C"; } else if (a < 55 && b < 116) { return "D"; } else if (a < 70 && b < 148) { return "E"; } else { return "NA"; } } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = br.readLine()) != null) { String[] input = line.split(" "); double a = Double.parseDouble(input[0]); double b = Double.parseDouble(input[1]); System.out.println(classify(a, b)); } } }
[["-", 75, 57, 15, 15, 0, 16, 31, 16, 12, 515], ["+", 75, 57, 15, 15, 0, 16, 31, 16, 12, 499]]
3
272
2
import java.util.Scanner; class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { double n = sc.nextDouble(), m = sc.nextDouble(); if (n < 35.50 && m < 71.00) { System.out.println("AAA"); } else if (n < 37.50 && m < 77.00) { System.out.println("AA"); } else if (n < 40.00 && m < 83.00) { System.out.println("A"); } else if (n < 43.00 && m < 89.00) { System.out.println("B"); } else if (n < 50 && m < 105.00) { System.out.println("C"); } else if (n < 55.00 && m < 116.00) { System.out.println("D"); } else if (n < 70.00 && m < 148.00) { System.out.println("D"); } else { System.out.println("NA"); } } } }
import java.util.Scanner; class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { double n = sc.nextDouble(), m = sc.nextDouble(); if (n < 35.50 && m < 71.00) { System.out.println("AAA"); } else if (n < 37.50 && m < 77.00) { System.out.println("AA"); } else if (n < 40.00 && m < 83.00) { System.out.println("A"); } else if (n < 43.00 && m < 89.00) { System.out.println("B"); } else if (n < 50 && m < 105.00) { System.out.println("C"); } else if (n < 55.00 && m < 116.00) { System.out.println("D"); } else if (n < 70.00 && m < 148.00) { System.out.println("E"); } else { System.out.println("NA"); } } } }
[["-", 0, 1, 0, 492, 3, 4, 0, 5, 0, 491], ["+", 0, 1, 0, 492, 3, 4, 0, 5, 0, 491]]
3
242
2
import java.util.Scanner; public class Main { final static double[] LV500 = {35.5, 37.5, 40, 43, 50, 55, 70}; final static double[] LV1000 = {71, 77, 83, 89, 105, 116, 148}; final static String[] lv = {"AAA", "AA", "A", "B", "C", "D", "E"}; public static void main(String[] arg) { Scanner in = new Scanner(System.in); while (in.hasNext()) { double _500 = in.nextDouble(); double _1000 = in.nextDouble(); String s = "NA"; for (int i = 0; i < 8; i++) { if (LV500[i] > _500 && LV1000[i] > _1000) { s = lv[i]; break; } } System.out.println(s); } } }
import java.util.Scanner; public class Main { final static double[] LV500 = {35.5, 37.5, 40, 43, 50, 55, 70}; final static double[] LV1000 = {71, 77, 83, 89, 105, 116, 148}; final static String[] lv = {"AAA", "AA", "A", "B", "C", "D", "E"}; public static void main(String[] arg) { Scanner in = new Scanner(System.in); while (in.hasNext()) { double _500 = in.nextDouble(); double _1000 = in.nextDouble(); String s = "NA"; for (int i = 0; i < 7; i++) { if (LV500[i] > _500 && LV1000[i] > _1000) { s = lv[i]; break; } } System.out.println(s); } } }
[["-", 0, 52, 8, 196, 0, 7, 15, 16, 12, 499], ["+", 0, 52, 8, 196, 0, 7, 15, 16, 12, 499]]
3
205
2
#include <stdio.h> int main(void) { float t1, t2; while (scanf("%f %f", &t1, &t2) != EOF) { if (t1 <= 35.50 && t2 < 71.00) printf("AAA\n"); else if (t1 < 37.50 && t2 < 77.00) printf("AA\n"); else if (t1 < 40.00 && t2 < 83.00) printf("A\n"); else if (t1 < 43.00 && t2 < 89.00) printf("B\n"); else if (t1 < 50.00 && t2 < 95.00) printf("C\n"); else if (t1 < 55.00 && t2 < 116.00) printf("D\n"); else if (t1 < 70.00 && t2 < 148.00) printf("AA\n"); else printf("NA\n"); } return 0; }
#include <stdio.h> int main(void) { float t1, t2; while (scanf("%f %f", &t1, &t2) != EOF) { if (t1 < 35.50 && t2 < 71.00) printf("AAA\n"); else if (t1 < 37.50 && t2 < 77.00) printf("AA\n"); else if (t1 < 40.00 && t2 < 83.00) printf("A\n"); else if (t1 < 43.00 && t2 < 89.00) printf("B\n"); else if (t1 < 50.00 && t2 < 105.00) printf("C\n"); else if (t1 < 55.00 && t2 < 116.00) printf("D\n"); else if (t1 < 70.00 && t2 < 148.00) printf("E\n"); else printf("NA\n"); } return 0; }
[["-", 0, 57, 15, 23, 0, 16, 31, 16, 17, 19], ["+", 0, 57, 15, 23, 0, 16, 31, 16, 17, 18], ["-", 0, 57, 15, 23, 0, 16, 12, 16, 12, 13], ["+", 0, 57, 15, 23, 0, 16, 12, 16, 12, 13], ["-", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
177
6
#include <stdio.h> double map[7][2] = {{35.5, 71.0}, {37.5, 77.0}, {40.0, 83.0}, {43.0, 89.0}, {50.0, 105.0}, {55.0, 116.0}, {70.0, 148.0}}; char map1[7][4] = {"AAA", "AA", "A", "B", "C", "D", "E"}; int main(void) { int i; double t1, t2; for (; ~scanf("%lf %lf", &t1, &t2);) { for (i = 0; i < 7; i++) if (map[i][0] > t1 && map[i][1] > t2) break; printf("%s", i == 7 ? "NA" : map1[i]); } return 0; }
#include <stdio.h> double map[7][2] = {{35.5, 71.0}, {37.5, 77.0}, {40.0, 83.0}, {43.0, 89.0}, {50.0, 105.0}, {55.0, 116.0}, {70.0, 148.0}}; char map1[7][4] = {"AAA", "AA", "A", "B", "C", "D", "E"}; int main(void) { int i; double t1, t2; for (; ~scanf("%lf %lf", &t1, &t2);) { for (i = 0; i < 7; i++) if (map[i][0] > t1 && map[i][1] > t2) break; printf("%s\n", i == 7 ? "NA" : map1[i]); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
0
189
1
#include <stdio.h> int main(void) { double t1, t2; while (scanf("%lf %lf", &t1, &t2) != EOF) { if (t1 < 35.5 && t2 < 71) printf("AAA\n"); else if (t1 < 37.5 && t2 < 77) printf("AA\n"); else if (t1 < 40 && t2 < 83) printf("A\n"); else if (t1 < 43 && t2 < 89) printf("B\n"); else if (t1 < 50 && t2 < 105) printf("C\n"); else if (t1 < 55 && t2 < 116) printf("D\n"); else if (t1 < 70 && t2 < 148) printf("E\n"); else printf("NA"); } return 0; }
#include <stdio.h> int main(void) { double t1, t2; while (scanf("%lf %lf", &t1, &t2) != EOF) { if (t1 < 35.5 && t2 < 71) printf("AAA\n"); else if (t1 < 37.5 && t2 < 77) printf("AA\n"); else if (t1 < 40 && t2 < 83) printf("A\n"); else if (t1 < 43 && t2 < 89) printf("B\n"); else if (t1 < 50 && t2 < 105) printf("C\n"); else if (t1 < 55 && t2 < 116) printf("D\n"); else if (t1 < 70 && t2 < 148) printf("E\n"); else printf("NA\n"); } return 0; }
[["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
0
176
1
#include <stdio.h> /** Application main entry point. */ int main(int argc, char *argv[]) { double a, b; while (scanf("%lf%lf", &a, &b) == 2) { if (a < 35.5 && b < 71.0) puts("AAA"); else if (a < 37.5 && b < 77.0) puts("AA"); else if (a < 40.0 && b < 83.0) puts("A"); else if (a < 43.0 && b < 89.0) puts("B"); else if (a < 50.0 && b < 105.0) puts("C"); else if (a < 55.0 && b < 116.0) puts("D"); else if (a < 70.0 && b < 148.0) puts("D"); else puts("NA"); } return (0); }
#include <stdio.h> /** Application main entry point. */ int main(int argc, char *argv[]) { double a, b; while (scanf("%lf%lf", &a, &b) == 2) { if (a < 35.5 && b < 71.0) puts("AAA"); else if (a < 37.5 && b < 77.0) puts("AA"); else if (a < 40.0 && b < 83.0) puts("A"); else if (a < 43.0 && b < 89.0) puts("B"); else if (a < 50.0 && b < 105.0) puts("C"); else if (a < 55.0 && b < 116.0) puts("D"); else if (a < 70.0 && b < 148.0) puts("E"); else puts("NA"); } return (0); }
[["-", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 64, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
179
2
#include <stdio.h> int main(void) { float a, b; while (scanf("%f %f", &a, &b) != EOF) { if (a < 35.5 && b < 71.0) { printf("AAA\n"); } else if (a < 37.5 && b < 77.0) { printf("AA\n"); } else if (a < 40.0 && b < 83.0) { printf("A\n"); } else if (a < 43.0 && b < 89.0) { printf("B\n"); } else if (a < 50.0 && b < 105.0) { printf("c\n"); } else if (a < 55.0 && b < 116.0) { printf("D\n"); } else if (a < 70.0 && b < 148.0) { printf("E\n"); } else { printf("NA\n"); } } return 0; }
#include <stdio.h> int main(void) { float a, b; while (scanf("%f %f", &a, &b) != EOF) { if (a < 35.5 && b < 71.0) { printf("AAA\n"); } else if (a < 37.5 && b < 77.0) { printf("AA\n"); } else if (a < 40.0 && b < 83.0) { printf("A\n"); } else if (a < 43.0 && b < 89.0) { printf("B\n"); } else if (a < 50.0 && b < 105.0) { printf("C\n"); } else if (a < 55.0 && b < 116.0) { printf("D\n"); } else if (a < 70.0 && b < 148.0) { printf("E\n"); } else { printf("NA\n"); } } return 0; }
[["-", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 6]]
0
193
2
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define M_PI 3.14159265358979323846 using namespace std; // conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } inline int readInt() { int x; scanf("%d", &x); return x; } // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<int, PII> TIII; typedef long long LL; typedef unsigned long long ULL; typedef vector<LL> VLL; typedef vector<VLL> VVLL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define SQ(a) ((a) * (a)) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, s, n) for (int i = s; i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define MOD 1000000007 #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; int N, K; bool cmp(const pii &a, const pii &b) { if (a.first == b.first) return a.second < b.second; return a.first < b.first; } struct Edge { int to, cost; Edge(int to, int cost) : to(to), cost(cost) {} }; typedef vector<vector<Edge>> AdjList; AdjList graph; const int INF = 100000000; vi dist; int d[110][110]; bool bellman_ford(int n, int s) { // nは頂点数、sは開始頂点 dist = vector<int>(n, INF); dist[s] = 0; // 開始点の距離は0 for (int i = 0; i < n; i++) { for (int v = 0; v < n; v++) { for (int u = 0; u < n; u++) { if (dist[v] != INF && dist[u] > dist[v] + d[v][u]) { dist[u] = dist[v] + d[v][u]; if (i == n - 1) return true; // n回目にも更新があるなら負の閉路が存在 } } } } return false; } int main() { // cout << fixed << setprecision(15); double a, b; while (cin >> a >> b) { a *= 100; b *= 100; if (a < 3500 && b < 7100) { cout << "AAA" << endl; } else if (a < 3700 && b < 7700) { cout << "AA" << endl; } else if (a < 4000 && b < 8300) { cout << "A" << endl; } else if (a < 4300 && b < 8900) { cout << "B" << endl; } else if (a < 5000 && b < 10500) { cout << "C" << endl; } else if (a < 5500 && b < 11600) { cout << "D" << endl; } else if (a < 7000 && b > 14800) { cout << "E" << endl; } else cout << "NA" << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define M_PI 3.14159265358979323846 using namespace std; // conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } inline int readInt() { int x; scanf("%d", &x); return x; } // typedef //------------------------------------------ typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<int, PII> TIII; typedef long long LL; typedef unsigned long long ULL; typedef vector<LL> VLL; typedef vector<VLL> VVLL; // container util //------------------------------------------ #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define PB push_back #define MP make_pair #define SZ(a) int((a).size()) #define SQ(a) ((a) * (a)) #define EACH(i, c) \ for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort((c).begin(), (c).end()) // repetition //------------------------------------------ #define FOR(i, s, n) for (int i = s; i < (int)n; ++i) #define REP(i, n) FOR(i, 0, n) #define MOD 1000000007 #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; int N, K; bool cmp(const pii &a, const pii &b) { if (a.first == b.first) return a.second < b.second; return a.first < b.first; } struct Edge { int to, cost; Edge(int to, int cost) : to(to), cost(cost) {} }; typedef vector<vector<Edge>> AdjList; AdjList graph; const int INF = 100000000; vi dist; int d[110][110]; bool bellman_ford(int n, int s) { // nは頂点数、sは開始頂点 dist = vector<int>(n, INF); dist[s] = 0; // 開始点の距離は0 for (int i = 0; i < n; i++) { for (int v = 0; v < n; v++) { for (int u = 0; u < n; u++) { if (dist[v] != INF && dist[u] > dist[v] + d[v][u]) { dist[u] = dist[v] + d[v][u]; if (i == n - 1) return true; // n回目にも更新があるなら負の閉路が存在 } } } } return false; } int main() { // cout << fixed << setprecision(15); double a, b; while (cin >> a >> b) { a *= 100; b *= 100; if (a < 3550 && b < 7100) { cout << "AAA" << endl; } else if (a < 3750 && b < 7700) { cout << "AA" << endl; } else if (a < 4000 && b < 8300) { cout << "A" << endl; } else if (a < 4300 && b < 8900) { cout << "B" << endl; } else if (a < 5000 && b < 10500) { cout << "C" << endl; } else if (a < 5500 && b < 11600) { cout << "D" << endl; } else if (a < 7000 && b < 14800) { cout << "E" << endl; } else cout << "NA" << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 18]]
1
777
6
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <vector> using namespace std; int main() { string G[8] = { "AAA", "AA", "A", "B", "C", "D", "E", "NA", }; double a, b; while (cin >> a >> b) { int p = (int)(a * 1000), q = (int)(b * 1000); int gp, gq; if (p < 35500) gp = 0; else if (p < 37500) gp = 1; else if (p < 40000) gp = 2; else if (p < 43000) gp = 3; else if (p < 50000) gp = 4; else if (p < 55000) gp = 5; else if (p < 70000) gp = 6; else gp = 7; if (q < 71000) gq = 0; else if (q < 77000) gq = 1; else if (q < 83000) gq = 2; else if (q < 89000) gq = 3; else if (q < 105000) gq = 4; else if (q < 116000) gq = 5; else if (q < 148000) gq = 6; else gq = 7; int g = max(gp, gq); cout << G[g]; } }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <vector> using namespace std; int main() { string G[8] = { "AAA", "AA", "A", "B", "C", "D", "E", "NA", }; double a, b; while (cin >> a >> b) { int p = (int)(a * 1000), q = (int)(b * 1000); int gp, gq; if (p < 35500) gp = 0; else if (p < 37500) gp = 1; else if (p < 40000) gp = 2; else if (p < 43000) gp = 3; else if (p < 50000) gp = 4; else if (p < 55000) gp = 5; else if (p < 70000) gp = 6; else gp = 7; if (q < 71000) gq = 0; else if (q < 77000) gq = 1; else if (q < 83000) gq = 2; else if (q < 89000) gq = 3; else if (q < 105000) gq = 4; else if (q < 116000) gq = 5; else if (q < 148000) gq = 6; else gq = 7; int g = max(gp, gq); cout << G[g] << endl; } }
[["+", 0, 52, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 0, 52, 8, 9, 0, 1, 0, 16, 12, 22]]
1
283
2
#include <algorithm> #include <iostream> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; int main() { double t5, t10; string ans; while (cin) { cin >> t5 >> t10; if (t5 < 35.50 && t10 < 71.00) { ans = "AAA"; } else if (t5 < 37.50 && t10 < 77.00) { ans = "AA"; } else if (t5 < 40.00 && t10 < 83.00) { ans = "A"; } else if (t5 < 43.00 && t10 < 89.00) { ans = "B"; } else if (t5 < 50.00 && t10 < 105.00) { ans = "C"; } else if (t5 < 55.00 && t10 < 116.00) { ans = "D"; } else if (t5 < 70.00 && t10 < 148.00) { ans = "E"; } else { ans = "NA"; } cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; int main() { double t5, t10; string ans; while (cin >> t5 >> t10) { if (t5 < 35.50 && t10 < 71.00) { ans = "AAA"; } else if (t5 < 37.50 && t10 < 77.00) { ans = "AA"; } else if (t5 < 40.00 && t10 < 83.00) { ans = "A"; } else if (t5 < 43.00 && t10 < 89.00) { ans = "B"; } else if (t5 < 50.00 && t10 < 105.00) { ans = "C"; } else if (t5 < 55.00 && t10 < 116.00) { ans = "D"; } else if (t5 < 70.00 && t10 < 148.00) { ans = "E"; } else { ans = "NA"; } cout << ans << endl; } return 0; }
[["-", 0, 14, 8, 9, 0, 52, 15, 339, 0, 25], ["-", 0, 14, 8, 9, 0, 52, 8, 9, 0, 45], ["-", 8, 9, 0, 1, 0, 16, 31, 16, 31, 22], ["-", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 25], ["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 45]]
1
192
6
#include <stdio.h> int main() { float n500, n1000; while (scanf("%f %f", &n500, &n1000) != EOF) { if (n500 < 35.5 && n1000 < 71) printf("AAA\n"); else if (n500 < 37.5 && n1000 < 77) printf("AA\n"); else if (n500 < 40 && n1000 < 83) printf("A\n"); else if (n500 < 43 && n1000 < 89) printf("B\n"); else if (n500 < 50 && n1000 < 105) printf("C\n"); else if (n500 < 55 && n1000 < 116) printf("D\n"); else if (n500 < 70.002 && n1000 < 148) printf("E\n"); else printf("NA"); } }
#include <stdio.h> int main() { float n500, n1000; while (scanf("%f %f", &n500, &n1000) != EOF) { if (n500 < 35.5 && n1000 < 71) printf("AAA\n"); else if (n500 < 37.5 && n1000 < 77) printf("AA\n"); else if (n500 < 40 && n1000 < 83) printf("A\n"); else if (n500 < 43 && n1000 < 89) printf("B\n"); else if (n500 < 50 && n1000 < 105) printf("C\n"); else if (n500 < 55 && n1000 < 116) printf("D\n"); else if (n500 < 70 && n1000 < 148) printf("E\n"); else printf("NA\n"); } }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 5, 0, 44]]
1
172
3
#include <stdio.h> int main(void) { float a, b; while (scanf("%f %f", &a, &b) != EOF) { if (a < 35.5 && b < 71) { printf("AAA\n"); } else if (a <= 7.5 && b < 77) { printf("AA\n"); } else if (a < 40 && b < 83) { printf("A\n"); } else if (a < 43 && b < 89) { printf("B\n"); } else if (a < 50 && b < 105) { printf("C\n"); } else if (a < 55 && b < 116) { printf("D\n"); } else if (a > 60 && b > 148) { printf("E\n"); } else { printf("NA\n"); } // scanf("%d %d",&a,&b); } return 0; }
#include <stdio.h> int main(void) { float a, b; while (scanf("%f %f", &a, &b) != EOF) { if (a < 35.5 && b < 71) { printf("AAA\n"); } else if (a < 37.5 && b < 77) { printf("AA\n"); } else if (a < 40 && b < 83) { printf("A\n"); } else if (a < 43 && b < 89) { printf("B\n"); } else if (a < 50 && b < 105) { printf("C\n"); } else if (a < 55 && b < 116) { printf("D\n"); } else if (a < 70 && b < 148) { printf("E\n"); } else { printf("NA\n"); } // scanf("%d %d",&a,&b); } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 19], ["-", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 18], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["-", 0, 57, 15, 339, 51, 16, 31, 16, 17, 47], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 18]]
1
194
10
#include <iostream> using namespace std; int main() { double t, ti; cin >> t >> ti; if (t < 35.50 && ti < 71.00) { cout << "AAA" << endl; } else if (t < 37.50 && ti < 77.00) { cout << "AA" << endl; } else if (t < 40.00 && ti < 83.00) { cout << "A" << endl; } else if (t < 43.00 && ti < 89.00) { cout << "B" << endl; } else if (t < 50.00 && ti < 105.00) { cout << "C" << endl; } else if (t < 55.00 && ti < 116.00) { cout << "D" << endl; } else if (t < 70 && ti < 148.00) { cout << "E" << endl; } else cout << "NA" << endl; return 0; }
#include <iostream> using namespace std; int main() { double t, ti; while (cin >> t >> ti) { if (t < 35.50 && ti < 71.00) { cout << "AAA" << endl; } else if (t < 37.50 && ti < 77.00) { cout << "AA" << endl; } else if (t < 40.00 && ti < 83.00) { cout << "A" << endl; } else if (t < 43.00 && ti < 89.00) { cout << "B" << endl; } else if (t < 50.00 && ti < 105.00) { cout << "C" << endl; } else if (t < 55.00 && ti < 116.00) { cout << "D" << endl; } else if (t < 70 && ti < 148.00) { cout << "E" << endl; } else cout << "NA" << endl; } return 0; }
[["+", 0, 30, 0, 14, 8, 9, 0, 52, 0, 89], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 24], ["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 25], ["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 45], ["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46]]
1
181
6
#include <bits/stdc++.h> using namespace std; int main() { double a, b; while (cin >> a >> b) { if (a < 35.50 && b < 71.00) cout << "AAA" << endl; else if (a < 37.50 && b < 77.00) cout << "AA" << endl; else if (a < 40.00 && b < 83.00) cout << "A" << endl; else if (a < 43.00 && b < 89.00) cout << "B" << endl; else if (a < 50.00 && b < 105.00) cout << "C" << endl; else if (a < 55.00 && b < 116.00) cout << "D" << endl; else if (a < 70.00 && b < 148.00) cout << "D" << endl; else cout << "NA" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { double a, b; while (cin >> a >> b) { if (a < 35.50 && b < 71.00) cout << "AAA" << endl; else if (a < 37.50 && b < 77.00) cout << "AA" << endl; else if (a < 40.00 && b < 83.00) cout << "A" << endl; else if (a < 43.00 && b < 89.00) cout << "B" << endl; else if (a < 50.00 && b < 105.00) cout << "C" << endl; else if (a < 55.00 && b < 116.00) cout << "D" << endl; else if (a < 70.00 && b < 148.00) cout << "E" << endl; else cout << "NA" << endl; } return 0; }
[["-", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 64, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
171
2
#include <iostream> using namespace std; int main() { double a, b; cin >> a >> b; if (a < 35.50 && b < 71.00) cout << "AAA\n"; else if (a < 37.50 && b < 77.00) cout << "AA\n"; else if (a < 40.00 && b < 83.00) cout << "A\n"; else if (a < 43.00 && b < 89.00) cout << "B\n"; else if (a < 50.00 && b < 105.00) cout << "C\n"; else if (a < 55.00 && b < 116.00) cout << "D\n"; else if (a < 70.00 && b < 148.00) cout << "E\n"; else cout << "NA\n"; return 0; }
#include <iostream> using namespace std; int main() { double a, b; while (cin >> a >> b) { if (a < 35.50 && b < 71.00) cout << "AAA\n"; else if (a < 37.50 && b < 77.00) cout << "AA\n"; else if (a < 40.00 && b < 83.00) cout << "A\n"; else if (a < 43.00 && b < 89.00) cout << "B\n"; else if (a < 50.00 && b < 105.00) cout << "C\n"; else if (a < 55.00 && b < 116.00) cout << "D\n"; else if (a < 70.00 && b < 148.00) cout << "E\n"; else cout << "NA\n"; } return 0; }
[["+", 0, 30, 0, 14, 8, 9, 0, 52, 0, 89], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 24], ["-", 0, 30, 0, 14, 8, 9, 0, 1, 0, 35], ["+", 0, 14, 8, 9, 0, 52, 15, 339, 0, 25], ["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 45], ["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46]]
1
159
6
#include <stdio.h> int bfs(int h, int w, char farm[][102], int vector[][2]); int main(void) { int i, j, k; int H, W; char farm[102][102]; int vector[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int ans = 0; while (scanf("%d %d\n", &H, &W) != EOF) { if (H == 0 && W == 0) { break; } for (i = 0; i < H + 2; i++) { if (i == 0 || i == H + 1) { for (j = 0; j < W + 2; j++) { farm[i][j] = 'o'; } } else { farm[i][0] = 'o'; scanf("%s", &farm[i][1]); farm[i][W + 1] = 'o'; } } ans = 1; for (i = 1; i < H + 1; i++) { for (j = 1; j < W + 1; j++) { if (farm[i][j] != 'o') { ans += bfs(i, j, farm, vector); } } } printf("%d\n", ans); } return 0; } int bfs(int h, int w, char farm[][102], int vector[][2]) { int i, j; char now = farm[h][w]; farm[h][w] = 'o'; for (i = 0; i < 4; i++) { if (now == farm[h + vector[i][0]][w + vector[i][1]]) { bfs(h + vector[i][0], w + vector[i][1], farm, vector); } } return 1; }
#include <stdio.h> int bfs(int h, int w, char farm[][102], int vector[][2]); int main(void) { int i, j, k; int H, W; char farm[102][102]; int vector[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int ans = 0; while (scanf("%d %d\n", &H, &W) != EOF) { if (H == 0 && W == 0) { break; } for (i = 0; i < H + 2; i++) { if (i == 0 || i == H + 1) { for (j = 0; j < W + 2; j++) { farm[i][j] = 'o'; } } else { farm[i][0] = 'o'; scanf("%s", &farm[i][1]); farm[i][W + 1] = 'o'; } } ans = 0; for (i = 1; i < H + 1; i++) { for (j = 1; j < W + 1; j++) { if (farm[i][j] != 'o') { ans += bfs(i, j, farm, vector); } } } printf("%d\n", ans); } return 0; } int bfs(int h, int w, char farm[][102], int vector[][2]) { int i, j; char now = farm[h][w]; farm[h][w] = 'o'; for (i = 0; i < 4; i++) { if (now == farm[h + vector[i][0]][w + vector[i][1]]) { bfs(h + vector[i][0], w + vector[i][1], farm, vector); } } return 1; }
[["-", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13]]
0
445
2
#include <cstdio> using namespace std; void func(int, int); int h, w; char za[10][10]; int cnt; int flg, flg1, flg2; int main(void) { int i, j; scanf("%d %d", &h, &w); while (!(h == 0 && w == 0)) { for (i = 0; i < h; i++) { scanf("%s", za[i]); } cnt = 0; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { flg = 0; flg1 = 0; flg2 = 0; if (za[i][j] == '#') { cnt++; flg = 1; func(i, j); } else if (za[i][j] == '*') { cnt++; flg1 = 1; func(i, j); } else if (za[i][j] == '@') { cnt++; flg2 = 1; func(i, j); } } } printf("%d\n", cnt); scanf("%d %d", &h, &w); } return 0; } void func(int y, int x) { int i, j; if (y + 1 <= h) { if (flg == 1) { if (za[y + 1][x] == '#') { za[y + 1][x] = '0'; func(y + 1, x); } } else if (flg1 == 1) { if (za[y + 1][x] == '*') { za[y + 1][x] = '0'; func(y + 1, x); } } else if (flg2 == 1) { if (za[y + 1][x] == '@') { za[y + 1][x] = '0'; func(y + 1, x); } } } if (x + 1 <= w) { if (flg == 1) { if (za[y][x + 1] == '#') { za[y][x + 1] = '0'; func(y, x + 1); } } else if (flg1 == 1) { if (za[y][x + 1] == '*') { za[y][x + 1] = '0'; func(y, x + 1); } } else if (flg2 == 1) { if (za[y][x + 1] == '@') { za[y][x + 1] = '0'; func(y, x + 1); } } } if (y - 1 >= 0) { if (flg == 1) { if (za[y - 1][x] == '#') { za[y - 1][x] = '0'; func(y - 1, x); } } else if (flg1 == 1) { if (za[y - 1][x] == '*') { za[y - 1][x] = '0'; func(y - 1, x); } } else if (flg2 == 1) { if (za[y - 1][x] == '@') { za[y - 1][x] = '0'; func(y - 1, x); } } } if (x - 1 >= 0) { if (flg == 1) { if (za[y][x - 1] == '#') { za[y][x - 1] = '0'; func(y, x - 1); } } else if (flg1 == 1) { if (za[y][x - 1] == '*') { za[y][x - 1] = '0'; func(y, x - 1); } } else if (flg2 == 1) { if (za[y][x - 1] == '@') { za[y][x - 1] = '0'; func(y, x - 1); } } } }
#include <cstdio> using namespace std; void func(int, int); int h, w; char za[101][101]; int cnt; int flg, flg1, flg2; int main(void) { int i, j; scanf("%d %d", &h, &w); while (!(h == 0 && w == 0)) { for (i = 0; i < h; i++) { scanf("%s", za[i]); } cnt = 0; for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { flg = 0; flg1 = 0; flg2 = 0; if (za[i][j] == '#') { cnt++; flg = 1; func(i, j); } else if (za[i][j] == '*') { cnt++; flg1 = 1; func(i, j); } else if (za[i][j] == '@') { cnt++; flg2 = 1; func(i, j); } } } printf("%d\n", cnt); scanf("%d %d", &h, &w); } return 0; } void func(int y, int x) { int i, j; if (y + 1 <= h) { if (flg == 1) { if (za[y + 1][x] == '#') { za[y + 1][x] = '0'; func(y + 1, x); } } else if (flg1 == 1) { if (za[y + 1][x] == '*') { za[y + 1][x] = '0'; func(y + 1, x); } } else if (flg2 == 1) { if (za[y + 1][x] == '@') { za[y + 1][x] = '0'; func(y + 1, x); } } } if (x + 1 <= w) { if (flg == 1) { if (za[y][x + 1] == '#') { za[y][x + 1] = '0'; func(y, x + 1); } } else if (flg1 == 1) { if (za[y][x + 1] == '*') { za[y][x + 1] = '0'; func(y, x + 1); } } else if (flg2 == 1) { if (za[y][x + 1] == '@') { za[y][x + 1] = '0'; func(y, x + 1); } } } if (y - 1 >= 0) { if (flg == 1) { if (za[y - 1][x] == '#') { za[y - 1][x] = '0'; func(y - 1, x); } } else if (flg1 == 1) { if (za[y - 1][x] == '*') { za[y - 1][x] = '0'; func(y - 1, x); } } else if (flg2 == 1) { if (za[y - 1][x] == '@') { za[y - 1][x] = '0'; func(y - 1, x); } } } if (x - 1 >= 0) { if (flg == 1) { if (za[y][x - 1] == '#') { za[y][x - 1] = '0'; func(y, x - 1); } } else if (flg1 == 1) { if (za[y][x - 1] == '*') { za[y][x - 1] = '0'; func(y, x - 1); } } else if (flg2 == 1) { if (za[y][x - 1] == '@') { za[y][x - 1] = '0'; func(y, x - 1); } } } }
[["-", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["+", 0, 30, 0, 43, 49, 80, 49, 80, 81, 13], ["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13]]
1
921
4
#include <cstring> #include <iostream> #include <queue> using namespace std; #define MAX_N 2000 int x[MAX_N][MAX_N]; int col[MAX_N][MAX_N], cnt; int h, w; queue<int> Q; void bfs(int c) { while (!Q.empty()) { int a = Q.front() / 10000; int b = Q.front() % 10000; Q.pop(); if (x[a][b] == x[a - 1][b] && col[a - 1][b] == 0) { col[a - 1][b] = cnt; Q.push((a - 1) * 10000 + b); } if (x[a][b] == x[a + 1][b] && col[a + 1][b] == 0) { col[a + 1][b] = cnt; Q.push((a + 1) * 10000 + b); } if (x[a][b] == x[a][b - 1] && col[a][b - 1] == 0) { col[a][b - 1] = cnt; Q.push(a * 10000 + (b - 1)); } if (x[a][b] == x[a][b + 1] && col[a][b + 1] == 0) { col[a][b + 1] = cnt; Q.push(a * 10000 + (b + 1)); } } } int main() { while (true) { memset(col, 0, sizeof(col)); memset(x, 0, sizeof(x)); cin >> h >> w; if (h == 0 && w == 0) { break; } for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { char c; cin >> c; if (c == '@') { x[i][j] = 1; } if (c == '#') { x[i][j] = 2; } if (c == '*') { x[i][j] = 3; } } } cnt = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (col[i][j] == 0) { Q.push(i * MAX_N + j); cnt++; col[i][j] = cnt; bfs(x[i][j]); } } } cout << cnt << endl; } }
#include <cstring> #include <iostream> #include <queue> using namespace std; #define MAX_N 2000 int x[MAX_N][MAX_N]; int col[MAX_N][MAX_N], cnt; int h, w; queue<int> Q; void bfs(int c) { while (!Q.empty()) { int a = Q.front() / 10000; int b = Q.front() % 10000; Q.pop(); if (x[a][b] == x[a - 1][b] && col[a - 1][b] == 0) { col[a - 1][b] = cnt; Q.push((a - 1) * 10000 + b); } if (x[a][b] == x[a + 1][b] && col[a + 1][b] == 0) { col[a + 1][b] = cnt; Q.push((a + 1) * 10000 + b); } if (x[a][b] == x[a][b - 1] && col[a][b - 1] == 0) { col[a][b - 1] = cnt; Q.push(a * 10000 + (b - 1)); } if (x[a][b] == x[a][b + 1] && col[a][b + 1] == 0) { col[a][b + 1] = cnt; Q.push(a * 10000 + (b + 1)); } } } int main() { while (true) { memset(col, 0, sizeof(col)); memset(x, 0, sizeof(x)); cin >> h >> w; if (h == 0 && w == 0) { break; } for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { char c; cin >> c; if (c == '@') { x[i][j] = 1; } if (c == '#') { x[i][j] = 2; } if (c == '*') { x[i][j] = 3; } } } cnt = 0; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { if (col[i][j] == 0) { Q.push(i * 10000 + j); cnt++; col[i][j] = cnt; bfs(x[i][j]); } } } cout << cnt << endl; } }
[["-", 0, 2, 3, 4, 0, 16, 31, 16, 12, 22], ["+", 0, 2, 3, 4, 0, 16, 31, 16, 12, 13]]
1
581
2
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <queue> #include <string> #include <utility> #include <vector> #define INF_LL 9000000000000000000 #define INF 2000000000 #define REP(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) using namespace std; typedef long long ll; typedef pair<int, int> PII; class Union_find { private: vector<int> par; vector<int> rank; int n; public: Union_find(int a) { n = a; for (int i = 0; i < n; i++) { par.push_back(i); rank.push_back(0); } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } }; int h, w; string b[102]; void dfs(int y, int x) { int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; char a = b[y][x]; b[y][x] = '.'; REP(i, 4) { if (b[y + dy[i]][x + dx[i]] == a) { dfs(y + dy[i], x + dx[i]); } } } int main(void) { cin >> h >> w; while (h == 0 && w == 0) { int res = 0; b[0] = string(w + 2, '.'); b[h + 1] = string(w + 2, '.'); for (int i = 0; i < h; i++) { cin >> b[i + 1]; b[i + 1] = '.' + b[i + 1] + '.'; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (b[i + 1][j + 1] != '.') { dfs(i + 1, j + 1); res++; } } } cout << res << endl; cin >> h >> w; } return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <queue> #include <string> #include <utility> #include <vector> #define INF_LL 9000000000000000000 #define INF 2000000000 #define REP(i, n) for (int i = 0; i < (n); i++) #define FOR(i, a, b) for (int i = (a); i < (b); i++) using namespace std; typedef long long ll; typedef pair<int, int> PII; class Union_find { private: vector<int> par; vector<int> rank; int n; public: Union_find(int a) { n = a; for (int i = 0; i < n; i++) { par.push_back(i); rank.push_back(0); } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } }; int h, w; string b[102]; void dfs(int y, int x) { int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; char a = b[y][x]; b[y][x] = '.'; REP(i, 4) { if (b[y + dy[i]][x + dx[i]] == a) { dfs(y + dy[i], x + dx[i]); } } } int main(void) { cin >> h >> w; while (h != 0 || w != 0) { int res = 0; b[0] = string(w + 2, '.'); b[h + 1] = string(w + 2, '.'); for (int i = 0; i < h; i++) { cin >> b[i + 1]; b[i + 1] = '.' + b[i + 1] + '.'; } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (b[i + 1][j + 1] != '.') { dfs(i + 1, j + 1); res++; } } } cout << res << endl; cin >> h >> w; } return 0; }
[["-", 0, 52, 15, 339, 51, 16, 31, 16, 17, 60], ["+", 0, 52, 15, 339, 51, 16, 31, 16, 17, 79], ["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 98], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 106], ["-", 0, 52, 15, 339, 51, 16, 12, 16, 17, 60], ["+", 0, 52, 15, 339, 51, 16, 12, 16, 17, 79]]
1
585
6
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) struct UnionFind { vector<int> par; vector<int> rank; UnionFind(int n) { init(n); } void init(int n) { par.resize(n); REP(i, n) par[i] = i; rank.resize(n, 0); } int find(int x) { if (par[x] == x) return x; else par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] > rank[y]) swap(x, y); par[x] = y; if (rank[x] == rank[y]) rank[x]++; } }; int main() { int h, w; while (cin >> h >> w, h || w) { string str[100]; REP(i, h) cin >> str[i]; UnionFind uf(h * w); REP(i, h) REP(j, w) { int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; REP(k, 4) { int nx = j + dx[k], ny = i + dy[k]; if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) continue; if (str[i][j] != str[ny][nx]) continue; uf.unite(i * w + j, ny * w + nx); } } set<int> s; REP(i, h * w) s.insert(uf.find(i)); cout << s.size() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) struct UnionFind { vector<int> par; vector<int> rank; UnionFind() {} UnionFind(int n) { init(n); } void init(int n) { par.resize(n); REP(i, n) par[i] = i; rank.resize(n, 0); } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] > rank[y]) swap(x, y); par[x] = y; if (rank[x] == rank[y]) rank[x]++; } }; int main() { int h, w; while (cin >> h >> w, h || w) { string str[100]; REP(i, h) cin >> str[i]; UnionFind uf(h * w); REP(i, h) REP(j, w) { int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; REP(k, 4) { int nx = j + dx[k], ny = i + dy[k]; if (!(nx >= 0 && nx < w && ny >= 0 && ny < h)) continue; if (str[i][j] != str[ny][nx]) continue; uf.unite(i * w + j, ny * w + nx); } } set<int> s; REP(i, h * w) s.insert(uf.find(i)); cout << s.size() << endl; } return 0; }
[["+", 8, 123, 0, 14, 49, 53, 54, 55, 0, 25], ["+", 0, 122, 8, 123, 0, 14, 8, 9, 0, 45], ["+", 0, 122, 8, 123, 0, 14, 8, 9, 0, 46], ["+", 0, 122, 8, 123, 0, 14, 49, 53, 49, 22], ["+", 8, 123, 0, 14, 49, 53, 54, 55, 0, 24], ["+", 8, 9, 0, 57, 75, 76, 0, 37, 0, 38]]
1
428
6
#include <iostream> using namespace std; char table[100][100]; int w, h; const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; void Dfs(int x, int y, char cur) { table[x][y] = 'X'; for (int i = 0; i < 4; ++i) { int next_x = x + dir[i][0]; int next_y = y + dir[i][1]; if (next_x >= 0 && next_x < w && next_y >= 0 && next_y < h && table[next_x][next_y] == cur) { Dfs(next_x, next_y, cur); } } } int main() { while (cin >> h >> w, w > 0) { int res = 0; int x, y; for (y = 0; y < h; ++y) { for (x = 0; x < w; ++x) { cin >> table[x][y]; } } for (y = 0; y < h; ++y) { for (x = 0; x < w; ++x) { if (table[x][y] != 'x') { Dfs(x, y, table[x][y]); ++res; } } } cout << res << endl; } return 0; }
#include <iostream> using namespace std; char table[100][100]; int w, h; const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; void Dfs(int x, int y, char cur) { table[x][y] = 'X'; for (int i = 0; i < 4; ++i) { int next_x = x + dir[i][0]; int next_y = y + dir[i][1]; if (next_x >= 0 && next_x < w && next_y >= 0 && next_y < h && table[next_x][next_y] == cur) { Dfs(next_x, next_y, cur); } } } int main() { while (cin >> h >> w, w > 0) { int res = 0; int x, y; for (y = 0; y < h; ++y) { for (x = 0; x < w; ++x) { cin >> table[x][y]; } } for (y = 0; y < h; ++y) { for (x = 0; x < w; ++x) { if (table[x][y] != 'X') { Dfs(x, y, table[x][y]); ++res; } } } cout << res << endl; } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 12, 103, 0, 125], ["+", 0, 57, 15, 339, 51, 16, 12, 103, 0, 125]]
1
306
2
#include <iostream> using namespace std; char map[101][101]; int vis[101][101]; int ans; int H, W; void dfs(int x, int y, char c) { if (c != map[x][y]) return; if (vis[x][y]) return; vis[x][y] = 1; if (x > 0) dfs(x - 1, y, map[x][y]); if (x < H - 1) dfs(x + 1, y, map[x][y]); if (y > 0) dfs(x, y - 1, map[x][y]); if (y < W - 1) dfs(x, y + 1, map[x][y]); } int main() { while (cin >> H >> W && H && W) { ans = 0; for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) { cin >> map[i][j]; vis[i][j] = 0; } for (int i = 0; i <= H; ++i) for (int j = 0; j <= W; ++j) if (!vis[i][j]) { dfs(i, j, map[i][j]); ans++; } cout << ans << endl; return 0; } }
#include <iostream> using namespace std; char map[101][101]; int vis[101][101]; int ans; int H, W; void dfs(int x, int y, char c) { if (c != map[x][y]) return; if (vis[x][y]) return; vis[x][y] = 1; if (x > 0) dfs(x - 1, y, map[x][y]); if (x < H - 1) dfs(x + 1, y, map[x][y]); if (y > 0) dfs(x, y - 1, map[x][y]); if (y < W - 1) dfs(x, y + 1, map[x][y]); } int main() { while (cin >> H >> W && H && W) { ans = 0; for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) { cin >> map[i][j]; vis[i][j] = 0; } for (int i = 0; i < H; ++i) for (int j = 0; j < W; ++j) if (!vis[i][j]) { dfs(i, j, map[i][j]); ans++; } cout << ans << endl; } return 0; }
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["-", 8, 9, 0, 7, 8, 7, 15, 16, 17, 19], ["+", 8, 9, 0, 7, 8, 7, 15, 16, 17, 18], ["+", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46], ["-", 0, 14, 8, 9, 0, 52, 8, 9, 0, 46]]
1
320
6
#include <cstdio> #include <iostream> using namespace std; const int MAX_N = 100; char yard[MAX_N][MAX_N]; int h, w; int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; void dfs(int, int, char); int main() { while ((scanf("%d%d", &h, &w)) && h != 0 && w != 0) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { scanf(" %c", &yard[i][j]); } } int cnt = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (yard[i][j] != '.') dfs(i, j, yard[i][j]); cnt++; } } printf("%d\n", cnt); } } void dfs(int x, int y, char ch) { yard[x][y] = '.'; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx >= 0 && nx < h && ny >= 0 && ny < w && yard[nx][ny] == ch) dfs(nx, ny, ch); } }
#include <cstdio> #include <iostream> using namespace std; const int MAX_N = 100; char yard[MAX_N][MAX_N]; int h, w; int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; void dfs(int, int, char); int main() { while ((scanf("%d%d", &h, &w)) && h != 0 && w != 0) { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { scanf(" %c", &yard[i][j]); } } int cnt = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (yard[i][j] != '.') { dfs(i, j, yard[i][j]); cnt++; } } } printf("%d\n", cnt); } } void dfs(int x, int y, char ch) { yard[x][y] = '.'; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx >= 0 && nx < h && ny >= 0 && ny < w && yard[nx][ny] == ch) dfs(nx, ny, ch); } }
[["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46]]
1
327
2
#include <iostream> using namespace std; char f[102][102]; char cnt_c[1]; void DFS(int y, int x) { if (f[y][x] == '.' || f[y][x] == 0 || f[y][x] != cnt_c[0]) return; if (f[y][x] == cnt_c[0]) { f[y][x] = '.'; } DFS(y + 1, x); DFS(y - 1, x); DFS(y, x + 1); DFS(y, x - 1); } int main() { int H, W; while (cin >> H >> W) { if (H == W == 0) return 0; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { cin >> f[i][j]; } } int cnt = 0; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (f[i][j] == '@' || f[i][j] == '#' || f[i][j] == '*') { cnt_c[0] = f[i][j]; DFS(i, j); cnt++; } } } cout << cnt << endl; } }
#include <iostream> using namespace std; char f[102][102]; char cnt_c[1]; void DFS(int y, int x) { if (f[y][x] == '.' || f[y][x] == 0 || f[y][x] != cnt_c[0]) return; if (f[y][x] == cnt_c[0]) { f[y][x] = '.'; } DFS(y + 1, x); DFS(y - 1, x); DFS(y, x + 1); DFS(y, x - 1); } int main() { int H, W; while (cin >> H >> W) { if (H == 0 && W == 0) return 0; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { cin >> f[i][j]; } } int cnt = 0; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (f[i][j] == '@' || f[i][j] == '#' || f[i][j] == '*') { cnt_c[0] = f[i][j]; DFS(i, j); cnt++; } } } cout << cnt << endl; } }
[["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 98]]
1
316
2
#include <cstdlib> #include <iostream> #include <string> #include <vector> using namespace std; void destroy(vector<string> &field, int i, int j, int symbol) { if (field[i][j] == symbol) { int symbol = field[i][j]; field[i][j] = '-'; destroy(field, i - 1, j, symbol); destroy(field, i + 1, j, symbol); destroy(field, i, j - 1, symbol); destroy(field, i, j + 1, symbol); } } int search(vector<string> &field, int i, int j) { static int counter = 0; if (i == 1 && j == 1) { counter = 0; } if (field[i][j] == '-') { return counter; } counter++; int symbol = field[i][j]; field[i][j] = '-'; destroy(field, i - 1, j, symbol); destroy(field, i + 1, j, symbol); destroy(field, i, j - 1, symbol); destroy(field, i, j + 1, symbol); return counter; } int main() { while (1) { int h, w; cin >> h >> w; if (!(h | w)) { return EXIT_SUCCESS; } vector<string> field; field.reserve(h + 2); field[0] = ""; field[h + 1] = ""; for (int j = 0; j <= h + 1; j++) { field[0] += "-"; field[h + 1] += "-"; } for (int i = 1; i <= h; i++) { cin >> field[i]; field[i] = "-" + field[i] + "-"; } int n; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { n = search(field, i, j); } } cout << n << endl; } }
#include <cstdlib> #include <iostream> #include <string> #include <vector> using namespace std; void destroy(vector<string> &field, int i, int j, int symbol) { if (field[i][j] == symbol) { int symbol = field[i][j]; field[i][j] = '-'; destroy(field, i - 1, j, symbol); destroy(field, i + 1, j, symbol); destroy(field, i, j - 1, symbol); destroy(field, i, j + 1, symbol); } } int search(vector<string> &field, int i, int j) { static int counter = 0; if (i == 1 && j == 1) { counter = 0; } if (field[i][j] == '-') { return counter; } counter++; int symbol = field[i][j]; field[i][j] = '-'; destroy(field, i - 1, j, symbol); destroy(field, i + 1, j, symbol); destroy(field, i, j - 1, symbol); destroy(field, i, j + 1, symbol); return counter; } int main() { while (1) { int h, w; cin >> h >> w; if (!(h | w)) { return EXIT_SUCCESS; } vector<string> field(h + 2); field[0] = ""; field[h + 1] = ""; for (int j = 0; j <= w + 1; j++) { field[0] += "-"; field[h + 1] += "-"; } for (int i = 1; i <= h; i++) { cin >> field[i]; field[i] = "-" + field[i] + "-"; } int n; for (int i = 1; i <= h; i++) { for (int j = 1; j <= w; j++) { n = search(field, i, j); } } cout << n << endl; } }
[["-", 8, 9, 0, 52, 8, 9, 0, 43, 0, 35], ["-", 8, 9, 0, 1, 0, 2, 63, 118, 28, 22], ["-", 8, 9, 0, 1, 0, 2, 63, 118, 17, 131], ["-", 8, 9, 0, 1, 0, 2, 63, 118, 119, 120], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 31, 22], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 31, 22]]
1
463
6
#include <cstdio> using namespace std; const int MAX_H = 101; const int MAX_W = 101; int H, W; char blocks[MAX_H][MAX_W]; int init() { scanf("%d%d", &H, &W); if (H == 0 && W == 0) return -1; for (int i = 0; i < H; i++) { scanf("%s", blocks[i]); } return 0; } void fill(int row, int col) { char type = blocks[row][col]; blocks[row][col] = ' '; int di[] = {1, -1, 0, 0}; int dj[] = {0, 0, 1, -1}; for (int k = 0; k < 4; k++) { int ti = row + di[k]; int tj = col + dj[k]; if (0 <= ti && ti < H && 0 <= tj && tj < W) { if (blocks[ti][tj] == type) { fill(row, col); } } } } void solve() { int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (blocks[i][j] != ' ') { fill(i, j); ans++; } } } printf("%d\n", ans); } int main() { while (!init()) { solve(); } }
#include <cstdio> using namespace std; const int MAX_H = 101; const int MAX_W = 101; int H, W; char blocks[MAX_H][MAX_W]; int init() { scanf("%d%d", &H, &W); if (H == 0 && W == 0) return -1; for (int i = 0; i < H; i++) { scanf("%s", blocks[i]); } return 0; } void fill(int row, int col) { char type = blocks[row][col]; blocks[row][col] = ' '; int di[] = {1, -1, 0, 0}; int dj[] = {0, 0, 1, -1}; for (int k = 0; k < 4; k++) { int ti = row + di[k]; int tj = col + dj[k]; if (0 <= ti && ti < H && 0 <= tj && tj < W) { if (blocks[ti][tj] == type) { fill(ti, tj); } } } } void solve() { int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (blocks[i][j] != ' ') { fill(i, j); ans++; } } } printf("%d\n", ans); } int main() { while (!init()) { solve(); } }
[["-", 64, 9, 0, 1, 0, 2, 3, 4, 0, 22], ["+", 64, 9, 0, 1, 0, 2, 3, 4, 0, 22]]
1
334
4
# -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10**6) def search(values,hp,vp,item): if not (0<=hp<len(values)): return if not (0<=vp<len(values[hp])): return if item!=values[hp][vp]: return values[hp][vp]=True for dh,dv in list([0,1],[0,-1],[1,0],[-1,0]): search(values,hp+dh,vp+dv,item) # search(values,hp-1,vp,item) # search(values,hp+1,vp,item) # search(values,hp,vp-1,item) # search(values,hp,vp+1,item) def solve(values): count,valid_items=0,set(['@','#','*']) for i in range(len(values)): for j in range(len(values[i])): if values[i][j] in valid_items: search(values,i,j,values[i][j]) count+=1 return count def main(): line = input().strip() while line!='0 0': H,W = list(map(int,line.split(' '))) values = list() for _ in range(H): values.append(list(input().strip())) print(solve(values)) line = input().strip() if __name__=='__main__': main()
# -*- coding: utf-8 -*- import sys sys.setrecursionlimit(10**6) def search(values,hp,vp,item): if not (0<=hp<len(values)): return if not (0<=vp<len(values[hp])): return if item!=values[hp][vp]: return values[hp][vp]=True for dh,dv in [[0,1],[0,-1],[1,0],[-1,0]]: search(values,hp+dh,vp+dv,item) # search(values,hp-1,vp,item) # search(values,hp+1,vp,item) # search(values,hp,vp-1,item) # search(values,hp,vp+1,item) def solve(values): count,valid_items=0,set(['@','#','*']) for i in range(len(values)): for j in range(len(values[i])): if values[i][j] in valid_items: search(values,i,j,values[i][j]) count+=1 return count def main(): line = input().strip() while line!='0 0': H,W = list(map(int,line.split(' '))) values = list() for _ in range(H): values.append(list(input().strip())) print(solve(values)) line = input().strip() if __name__=='__main__': main()
[["-", 0, 14, 8, 196, 0, 7, 12, 652, 63, 22], ["-", 8, 196, 0, 7, 12, 652, 3, 4, 0, 24], ["+", 8, 196, 0, 7, 12, 634, 0, 634, 0, 70], ["-", 8, 196, 0, 7, 12, 652, 3, 4, 0, 25], ["+", 0, 14, 8, 196, 0, 7, 12, 634, 0, 73]]
5
307
5
def setLine(tempH, line): for i in range(0, len(line)): geo[tempH][i] = line[i:i+1] def solve(): person = 0 for i in range(0,H): for j in range(0,W): if geo[i][j] is not "_": search(i,j) person += 1 print(person) def search(i,j): temp = geo[i][j] geo[i][j] = "_" dx = [-1,0,1,0] dy = [0,-1,0,1] for a in range(0,4): idx = i + dx[a] jdy = j + dy[a] if(isOnMap(idx, jdy)): if(isNeededToSolve(temp, idx, jdy)): search(idx,jdy) def isOnMap(i,j): return (0<=i and 0<=j and i<H and j<W) def isNeededToSolve(temp,i,j): target = geo[i][j] return (target is not "_" and temp is target) limit = 10**10 sys.getrecursionlimit(limit) H = -1 W = -1 tempH = 0 geo = [[0 for i in range(1)]for j in range(1)] repeat = True while repeat: line = input() if H is -1 and W is -1: H = int(line.split(" ")[0]) W = int(line.split(" ")[1]) geo = [[0 for i in range(W)] for j in range(H)] else: setLine(tempH, line) tempH+=1 #break if H is 0 and W is 0: break #solve if tempH is H: solve() H = -1 W = -1 tempH = 0
import sys def setLine(tempH, line): for i in range(0, len(line)): geo[tempH][i] = line[i:i+1] def solve(): person = 0 for i in range(0,H): for j in range(0,W): if geo[i][j] is not "_": search(i,j) person += 1 print(person) def search(i,j): temp = geo[i][j] geo[i][j] = "_" dx = [-1,0,1,0] dy = [0,-1,0,1] for a in range(0,4): idx = i + dx[a] jdy = j + dy[a] if(isOnMap(idx, jdy)): if(isNeededToSolve(temp, idx, jdy)): search(idx,jdy) def isOnMap(i,j): return (0<=i and 0<=j and i<H and j<W) def isNeededToSolve(temp,i,j): target = geo[i][j] return (target is not "_" and temp is target) limit = 10**7 sys.setrecursionlimit(limit) H = -1 W = -1 tempH = 0 geo = [[0 for i in range(1)]for j in range(1)] repeat = True while repeat: line = input() if H is -1 and W is -1: H = int(line.split(" ")[0]) W = int(line.split(" ")[1]) geo = [[0 for i in range(W)] for j in range(H)] else: setLine(tempH, line) tempH+=1 #break if H is 0 and W is 0: break #solve if tempH is H: solve() H = -1 W = -1 tempH = 0
[["+", 36, 36, 36, 36, 0, 656, 0, 596, 0, 487], ["+", 36, 36, 0, 656, 0, 596, 141, 673, 0, 22], ["-", 0, 656, 0, 1, 0, 662, 12, 657, 12, 612], ["+", 0, 656, 0, 1, 0, 662, 12, 657, 12, 612], ["-", 0, 656, 0, 1, 0, 652, 63, 319, 319, 22], ["+", 0, 656, 0, 1, 0, 652, 63, 319, 319, 22]]
5
416
6
#define N 12 double M[N][1 << N], r, t; D[N], n; double dfs(p, f) { if (M[p][f] < 0) { int i = 0; M[p][f] = 0; for (; i < n; i++) { if (f & 1 << i) continue; if (t = dfs(i, f | 1 << i) + D[p] + D[i] - 2 * sqrt(D[p] * D[i] * 1.0), M[p][f] < t) M[p][f] = t; } } return M[p][f]; } main(l, i, j, d) { for (; ~scanf("%d", &l);) { d = 0; do { scanf("%d", D + n); d += D[n++] * 2; } while (getchar() != '\n'); for (i = 0; i < N; i++) for (j = 0; j < 1 << N; j++) M[i][j] = -1; for (r = i = 0; i < n; i++) if (t = dfs(i, 1 << i), r < t) r = t; puts(d - r < l + 1e-5 ? "OK" : "NA"); } exit(0); }
#define N 12 double M[N][1 << N], r, t; D[N], n; double dfs(p, f) { if (M[p][f] < 0) { int i = 0; M[p][f] = 0; for (; i < n; i++) { if (f & 1 << i) continue; if (t = dfs(i, f | 1 << i) + D[p] + D[i] - 2 * sqrt(D[p] * D[i] * 1.0), M[p][f] < t) M[p][f] = t; } } return M[p][f]; } main(l, i, j, d) { for (; ~scanf("%d", &l);) { d = n = 0; do { scanf("%d", D + n); d += D[n++] * 2; } while (getchar() != '\n'); for (i = 0; i < N; i++) for (j = 0; j < 1 << N; j++) M[i][j] = -1; for (r = i = 0; i < n; i++) if (t = dfs(i, 1 << i), r < t) r = t; puts(d - r < l + 1e-5 ? "OK" : "NA"); } exit(0); }
[["+", 8, 9, 0, 1, 0, 11, 12, 11, 31, 22], ["+", 8, 9, 0, 1, 0, 11, 12, 11, 17, 32]]
0
323
2
#include <cmath> #include <cstdio> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; string str; const int INF = 1 << 30; double min(double a, double b) { return a > b ? b : a; } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } double dist(int a, int b) { return b + sqrt((a + b) * (a + b) - (a - b) * (a - b)) - a; } int main() { while (getline(cin, str)) { // begin input stringstream ss(str); int X; ss >> X; int radius[13]; int i = 0; while (ss >> radius[i]) { i++; } int N = i; // end input vector<vector<double>> dp(1 << N, vector<double>(N, INF)); for (int j = 0; j < N; j++) { dp[1 << j][j] = 2 * radius[j]; } for (int i = 1; i < (1 << N); i++) { for (int j = 0; j < N; j++) { if ((i >> j) & 1) { for (int k = 0; k < N; k++) { if (!((i >> k) & 1)) { // cout << i << " to " << (i | (1 << k)) << " : " << j << ", " << // k << ", " << radius[j] << ", " << radius[k] << endl; dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + dist(radius[j], radius[k])); } } } } } double mini = INF; for (int j = 0; j < N; j++) { mini = min(mini, dp[(1 << N) - 1][j]); } if (mini <= X) { // cout << mini << endl; cout << "YES" << endl; } else { // cout << mini << endl; cout << "NO" << endl; } } return 0; }
#include <cmath> #include <cstdio> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; string str; const int INF = 1 << 30; double min(double a, double b) { return a > b ? b : a; } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } double dist(int a, int b) { return b + sqrt((a + b) * (a + b) - (a - b) * (a - b)) - a; } int main() { while (getline(cin, str)) { // begin input stringstream ss(str); int X; ss >> X; int radius[13]; int i = 0; while (ss >> radius[i]) { i++; } int N = i; // end input vector<vector<double>> dp(1 << N, vector<double>(N, INF)); for (int j = 0; j < N; j++) { dp[1 << j][j] = 2 * radius[j]; } for (int i = 1; i < (1 << N); i++) { for (int j = 0; j < N; j++) { if ((i >> j) & 1) { for (int k = 0; k < N; k++) { if (!((i >> k) & 1)) { // cout << i << " to " << (i | (1 << k)) << " : " << j << ", " << // k << ", " << radius[j] << ", " << radius[k] << endl; dp[i | (1 << k)][k] = min(dp[i | (1 << k)][k], dp[i][j] + dist(radius[j], radius[k])); } } } } } double mini = INF; for (int j = 0; j < N; j++) { mini = min(mini, dp[(1 << N) - 1][j]); } if (mini <= X) { // cout << mini << endl; cout << "OK" << endl; } else { // cout << mini << endl; cout << "NA" << endl; } } return 0; }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
446
4
#include <bits/stdc++.h> using namespace std; #define INF 10000000 int main() { string str; while (getline(cin, str)) { vector<double> vec; stringstream ss(str); int wid, n = 0; ss >> wid; while (!ss.eof()) { double s; ss >> s; vec.push_back(s); n++; } double dp[1 << n][n]; for (int i = 0; i < (1 << n); i++) { for (int j = 0; j < n; j++) { dp[i][j] = INF; } } for (int i = 0; i < n; i++) { dp[(1 << n) - 1 - (1 << i)][i] = vec[i]; } for (int i = (1 << n) - 2; i > -1; i--) { for (int j = 0; j < n; j++) { if (!(i & (1 << j))) { for (int k = 0; k < n; k++) { dp[i][j] = min(dp[i][j], dp[i | (1 << j)][k] + 2 * sqrt(vec[j] * vec[k])); } } } } double ans = INF; for (int i = 0; i < n; i++) { ans = min(ans, dp[0][i] + vec[i]); } if (ans <= wid) cout << "OK" << endl; else cout << "NG" << endl; } }
#include <bits/stdc++.h> using namespace std; #define INF 10000000 int main() { string str; while (getline(cin, str)) { vector<double> vec; stringstream ss(str); int wid, n = 0; ss >> wid; while (!ss.eof()) { double s; ss >> s; vec.push_back(s); n++; } double dp[1 << n][n]; for (int i = 0; i < (1 << n); i++) { for (int j = 0; j < n; j++) { dp[i][j] = INF; } } for (int i = 0; i < n; i++) { dp[(1 << n) - 1 - (1 << i)][i] = vec[i]; } for (int i = (1 << n) - 2; i > -1; i--) { for (int j = 0; j < n; j++) { if (!(i & (1 << j))) { for (int k = 0; k < n; k++) { dp[i][j] = min(dp[i][j], dp[i | (1 << j)][k] + 2 * sqrt(vec[j] * vec[k])); } } } } double ans = INF; for (int i = 0; i < n; i++) { ans = min(ans, dp[0][i] + vec[i]); } if (ans <= wid) cout << "OK" << endl; else cout << "NA" << endl; } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
359
2
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <sstream> #include <string> using namespace std; const int LOOP = 10000; //??????????????° const double EPS = 1e-6; //??????????????¨(?°??????????????????????) int L; int N; //???????????±???????????° //???????????±????????????????????????????????¨?????¨???????¨????????¨?????????? double Evaluate(int r[12]) { double val = r[0] + r[N - 1]; for (int i = 0; i < N - 1; i++) { int a = abs(r[i] - r[i + 1]), c = r[i] + r[i + 1]; val += sqrt(c * c - a * a); } return val; } //?????¨?????????????????°???????????¨??????????????????????§???????????±??????? //(????§?????????????true) bool Changes(int cnt) { return cnt < rand() % LOOP; } //?????????????????????????????¢??° //?????????????°??????????????¨????????????? // int r[12] : // ???????????±?????????0??????N-1???????????????????????????????????????????????????????´??????? double Solve(int r[12]) { double score = Evaluate(r); //?????¨?????¶???r[]????¨???????????´????????????° double mi = score; //???????????§?????¶???????¨???????????°???????????´????????????° for (int cnt = 0; cnt < LOOP; cnt++) { //=============================================== //??????????????? int a = rand() % 12, b = rand() % 12; swap(r[a], r[b]); score = Evaluate(r); if (mi >= score) { mi = score; continue; } if (mi < score) { if (Changes(cnt)) { mi = score; continue; } swap(r[a], r[b]); } //=============================================== } return mi; //????°????????????? } //??\?????¢????????????????????¢????????£????????§??????????????? int main() { while (cin >> L) { N = 0; int r[12]; string S; getline(cin, S); stringstream SS(S); while (!SS.eof()) SS >> r[N++]; double ans; if (N == 1) ans = r[0] * 2; else ans = Solve(r); if (L - ans + EPS > 0) puts("OK"); else puts("NA"); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <sstream> #include <string> using namespace std; const int LOOP = 10000; //??????????????° const double EPS = 1e-6; //??????????????¨(?°??????????????????????) int L; int N; //???????????±???????????° //???????????±????????????????????????????????¨?????¨???????¨????????¨?????????? double Evaluate(int r[12]) { double val = r[0] + r[N - 1]; for (int i = 0; i < N - 1; i++) { int a = abs(r[i] - r[i + 1]), c = r[i] + r[i + 1]; val += sqrt(c * c - a * a); } return val; } //?????¨?????????????????°???????????¨??????????????????????§???????????±??????? //(????§?????????????true) bool Changes(int cnt) { return cnt < rand() % LOOP; } //?????????????????????????????¢??° //?????????????°??????????????¨????????????? // int r[12] : // ???????????±?????????0??????N-1???????????????????????????????????????????????????????´??????? double Solve(int r[12]) { double score = Evaluate(r); //?????¨?????¶???r[]????¨???????????´????????????° double mi = score; //???????????§?????¶???????¨???????????°???????????´????????????° for (int cnt = 0; cnt < LOOP; cnt++) { //=============================================== //??????????????? int a = rand() % N, b = rand() % N; swap(r[a], r[b]); score = Evaluate(r); if (mi > score) { mi = score; continue; } if (mi < score) { if (Changes(cnt)) { mi = score; continue; } swap(r[a], r[b]); } //=============================================== } return mi; //????°????????????? } //??\?????¢????????????????????¢????????£????????§??????????????? int main() { while (cin >> L) { N = 0; int r[12]; string S; getline(cin, S); stringstream SS(S); while (!SS.eof()) SS >> r[N++]; double ans; if (N == 1) ans = r[0] * 2; else ans = Solve(r); if (L - ans + EPS > 0) puts("OK"); else puts("NA"); } return 0; }
[["-", 8, 9, 0, 43, 49, 50, 51, 16, 12, 13], ["+", 8, 9, 0, 43, 49, 50, 51, 16, 12, 22], ["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 20], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 47]]
1
401
6
#include <bits/stdc++.h> using namespace std; typedef long long ll; bool f; double w; vector<double> v; int n; double calc(int i, int j) { return sqrt((v[i] + v[j]) * (v[i] + v[j]) - (v[i] - v[j]) * (v[i] - v[j])); } double memo[1 << 12][12]; void dfs(double l, int b, int p) { if (~p) { if (memo[b][p] <= l) return; memo[b][p] = l; } if (b == (1 << n) - 1) { f |= l + v[p] <= w; return; } if (p < 0) for (int i = 0; i < n; i++) dfs(v[i], b + (1 << i), i); else for (int i = 0; i < n; i++) if (!((b >> i) & 1)) dfs(l + calc(p, i), b + (1 << i), i); } int main() { string s; while (getline(cin, s)) { v.clear(); stringstream ss(s); ss >> w; double t; while (ss >> t) v.push_back(t); n = v.size(); f = 0; for (int i = 0; i < (1 << n); i++) for (int j = 0; j < n; j++) memo[i][j] = 1e9; dfs(0, 0, -1); cout << (f ? "OK" : "NG") << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; bool f; double w; vector<double> v; int n; double calc(int i, int j) { return sqrt((v[i] + v[j]) * (v[i] + v[j]) - (v[i] - v[j]) * (v[i] - v[j])); } double memo[1 << 12][12]; void dfs(double l, int b, int p) { if (~p) { if (memo[b][p] <= l) return; memo[b][p] = l; } if (b == (1 << n) - 1) { f |= l + v[p] <= w; return; } if (p < 0) for (int i = 0; i < n; i++) dfs(v[i], b + (1 << i), i); else for (int i = 0; i < n; i++) if (!((b >> i) & 1)) dfs(l + calc(p, i), b + (1 << i), i); } int main() { string s; while (getline(cin, s)) { v.clear(); stringstream ss(s); ss >> w; double t; while (ss >> t) v.push_back(t); n = v.size(); f = 0; for (int i = 0; i < (1 << n); i++) for (int j = 0; j < n; j++) memo[i][j] = 1e9; dfs(0, 0, -1); cout << (f ? "OK" : "NA") << endl; } return 0; }
[["-", 31, 16, 12, 23, 0, 41, 75, 5, 0, 6], ["+", 31, 16, 12, 23, 0, 41, 75, 5, 0, 6]]
1
394
2
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { string s; while (getline(cin, s)) { double w; vector<double> v; stringstream ss(s); ss >> w; double t; while (ss >> t) v.push_back(t); int n = v.size(); double dp[1 << n][n], inf = 1 << 25; for (int i = 0; i < (1 << n); i++) for (int j = 0; j < n; j++) dp[i][j] = inf; for (int i = 0; i < n; i++) dp[1 << i][i] = v[i]; for (int i = 1; i < (1 << n); i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) if (((i >> j) & 1) && !((i >> k) & 1)) dp[i + (1 << k)][k] = min(dp[i + (1 << k)][k], dp[i][j] + sqrt((v[j] + v[k]) * (v[j] + v[k]) - (v[j] - v[k]) * (v[j] - v[k]))); bool f = v.size() == 0; for (int i = 0; i < n; i++) f |= dp[(1 << n) - 1][i] + v[i] <= w + 1e-7; cout << (f ? "OK" : "NG") << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { string s; while (getline(cin, s)) { double w; vector<double> v; stringstream ss(s); ss >> w; double t; while (ss >> t) v.push_back(t); int n = v.size(); double dp[1 << n][n], inf = 1 << 25; for (int i = 0; i < (1 << n); i++) for (int j = 0; j < n; j++) dp[i][j] = inf; for (int i = 0; i < n; i++) dp[1 << i][i] = v[i]; for (int i = 1; i < (1 << n); i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) if (((i >> j) & 1) && !((i >> k) & 1)) dp[i + (1 << k)][k] = min(dp[i + (1 << k)][k], dp[i][j] + sqrt((v[j] + v[k]) * (v[j] + v[k]) - (v[j] - v[k]) * (v[j] - v[k]))); bool f = v.size() == 0; for (int i = 0; i < n; i++) f |= dp[(1 << n) - 1][i] + v[i] <= w + 1e-7; cout << (f ? "OK" : "NA") << endl; } return 0; }
[["-", 31, 16, 12, 23, 0, 41, 75, 5, 0, 6], ["+", 31, 16, 12, 23, 0, 41, 75, 5, 0, 6]]
1
391
2
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <stack> #include <stdio.h> #include <vector> typedef long long int ll; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.00000001 using namespace std; struct Info { double r; }; double ***dp; void func(char buf[300]) { double length; int tmp; int index; tmp = 0; for (index = 0; buf[index] != ' ' && buf[index] != '\0'; index++) { tmp = 10 * tmp + buf[index] - '0'; } length = (double)tmp; Info info[15]; int info_index = 0; while (buf[index] != '\0') { tmp = 0; for (++index; buf[index] != ' ' && buf[index] != '\0'; index++) { tmp = 10 * tmp + buf[index] - '0'; } info[info_index].r = (double)tmp; info_index++; } double sum = 0; for (int i = 0; i < info_index; i++) { sum += info[i].r; } if (length > 2 * sum + EPS) { printf("OK\n"); return; } double distance[info_index][info_index]; for (int i = 0; i < info_index; i++) { for (int k = 0; k < info_index; k++) { if (i != k) { distance[i][k] = sqrt( (info[i].r + info[k].r) * (info[i].r + info[k].r) - (info[i].r - info[k].r) * (info[i].r - info[k].r)); // i??????k??? } } } int limit = pow(2, info_index); double minimum = 4000.0; for (int i = 0; i <= info_index; i++) { for (int k = 0; k < info_index; k++) { for (int p = 0; p < limit; p++) dp[i][k][p] = 3000.0; } } int next_state; for (int i = 0; i < info_index; i++) { next_state = pow(2, i); dp[0][i][next_state] = info[i].r; } for (int i = 1; i <= info_index - 1; i++) { for (int k = 0; k < info_index; k++) { for (int state = 0; state < limit; state++) { if (dp[i - 1][k][state] < 2000.0) { for (int a = 0; a < info_index; a++) { if (state & (1 << a)) { // Do nothing } else { next_state = state + pow(2, a); if (i != index - 1) { dp[i][a][next_state] = min( dp[i][a][next_state], dp[i - 1][k][state] + distance[k][a]); } else { dp[i][a][next_state] = min(dp[i][a][next_state], dp[i - 1][k][state] + distance[k][a] + info[a].r); } } } } } } } for (int i = 0; i < info_index; i++) { minimum = min(minimum, dp[info_index - 1][i][limit - 1]); } if (minimum < length || fabs(minimum - length) < EPS) { printf("OK\n"); } else { printf("NA\n"); } } int main() { dp = new double **[13]; for (int i = 0; i < 13; i++) { dp[i] = new double *[12]; for (int k = 0; k < 12; k++) { dp[i][k] = new double[4096]; } } char buf[300]; int index; char ch; while (true) { index = 0; ch = getchar(); if (ch == '\n' || ch == '\0' || ch == EOF) break; while (ch != '\n' && ch != '\0' && ch != EOF) { buf[index++] = ch; ch = getchar(); } buf[index] = '\0'; func(buf); } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <queue> #include <stack> #include <stdio.h> #include <vector> typedef long long int ll; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.00000001 using namespace std; struct Info { double r; }; double ***dp; void func(char buf[300]) { double length; int tmp; int index; tmp = 0; for (index = 0; buf[index] != ' ' && buf[index] != '\0'; index++) { tmp = 10 * tmp + buf[index] - '0'; } length = (double)tmp; Info info[15]; int info_index = 0; while (buf[index] != '\0') { tmp = 0; for (++index; buf[index] != ' ' && buf[index] != '\0'; index++) { tmp = 10 * tmp + buf[index] - '0'; } info[info_index].r = (double)tmp; info_index++; } double sum = 0; for (int i = 0; i < info_index; i++) { sum += info[i].r; } if (length > 2 * sum + EPS) { printf("OK\n"); return; } double distance[info_index][info_index]; for (int i = 0; i < info_index; i++) { for (int k = 0; k < info_index; k++) { if (i != k) { distance[i][k] = sqrt( (info[i].r + info[k].r) * (info[i].r + info[k].r) - (info[i].r - info[k].r) * (info[i].r - info[k].r)); // i??????k??? } } } int limit = pow(2, info_index); double minimum = 4000.0; for (int i = 0; i <= info_index; i++) { for (int k = 0; k < info_index; k++) { for (int p = 0; p < limit; p++) dp[i][k][p] = 3000.0; } } int next_state; for (int i = 0; i < info_index; i++) { next_state = pow(2, i); dp[0][i][next_state] = info[i].r; } for (int i = 1; i <= info_index - 1; i++) { for (int k = 0; k < info_index; k++) { for (int state = 0; state < limit; state++) { if (dp[i - 1][k][state] < 2000.0) { for (int a = 0; a < info_index; a++) { if (state & (1 << a)) { // Do nothing } else { next_state = state + pow(2, a); if (i != info_index - 1) { dp[i][a][next_state] = min( dp[i][a][next_state], dp[i - 1][k][state] + distance[k][a]); } else { dp[i][a][next_state] = min(dp[i][a][next_state], dp[i - 1][k][state] + distance[k][a] + info[a].r); } } } } } } } for (int i = 0; i < info_index; i++) { minimum = min(minimum, dp[info_index - 1][i][limit - 1]); } if (minimum < length || fabs(minimum - length) < EPS) { printf("OK\n"); } else { printf("NA\n"); } } int main() { dp = new double **[13]; for (int i = 0; i < 13; i++) { dp[i] = new double *[12]; for (int k = 0; k < 12; k++) { dp[i][k] = new double[4096]; } } char buf[300]; int index; char ch; while (true) { index = 0; ch = getchar(); if (ch == '\n' || ch == '\0' || ch == EOF) break; while (ch != '\n' && ch != '\0' && ch != EOF) { buf[index++] = ch; ch = getchar(); } buf[index] = '\0'; func(buf); } return 0; }
[["-", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 31, 22]]
1
976
2
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define REP(i, a, n) for (int i = (a); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) #define all(x) (x).begin(), (x).end() #define mp make_pair #define pb push_back #define EPS 1e-8 #define DEB 0 int main() { int n, a; string in; vector<int> data; while (getline(cin, in)) { data.clear(); stringstream ss(in); ss >> n; while (ss >> a) { data.push_back(a); } sort(all(data)); int l, r, lr, rr; bool f = false; rep(t, 2) { double sum = 0.0; int sz = 1; int r1, r2, a; if (t == 0) { l = 0, r = data.size() - 1; lr = rr = data[data.size() - 1]; } else { l = data.size() - 1, r = 0; lr = rr = data[0]; } if (data.size() == 1) { if (data[0] <= n) { f = true; break; } } #if DEB printf("t:%d --------------------\n", t); printf("(l,r)=(%d,%d) init_r:%d\n", l, r, rr); #endif for (; sz < data.size();) { r1 = data[l] + lr; r2 = abs(data[l] - lr); a = r1 * r1 - r2 * r2; sum += sqrt((double)a); lr = data[l]; #if DEB printf("sz:%d l:%d lr:%d sum:%lf\n", sz + 1, l, lr, sum); #endif if (t == 0) l++; else l--; sz++; if (sz == data.size()) break; r1 = data[l] + rr; r2 = abs(data[l] - rr); a = r1 * r1 - r2 * r2; sum += sqrt((double)a); rr = data[l]; #if DEB printf("sz:%d l:%d rr:%d sum:%lf\n", sz + 1, l, rr, sum); #endif if (t == 0) l++; else l--; sz++; if (sz == data.size()) break; r1 = data[r] + lr; r2 = abs(data[r] - lr); a = r1 * r1 - r2 * r2; sum += sqrt((double)a); lr = data[r]; #if DEB printf("sz:%d r:%d lr:%d sum:%lf\n", sz + 1, r, lr, sum); #endif if (t == 0) r--; else r++; sz++; if (sz == data.size()) break; r1 = data[r] + rr; r2 = abs(data[r] - rr); a = r1 * r1 - r2 * r2; sum += sqrt((double)a); rr = data[r]; #if DEB printf("sz:%d r:%d rr:%d sum:%lf\n", sz + 1, r, rr, sum); #endif if (t == 0) r--; else r++; sz++; if (sz == data.size()) break; } sum += (double)(lr + rr); #if DEB printf("sum:%lf\n", sum); #endif if (sum <= (double)n) { f = true; } } puts(f ? "OK" : "NA"); } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define REP(i, a, n) for (int i = (a); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) #define all(x) (x).begin(), (x).end() #define mp make_pair #define pb push_back #define EPS 1e-8 #define DEB 0 int main() { int n, a; string in; vector<int> data; while (getline(cin, in)) { data.clear(); stringstream ss(in); ss >> n; while (ss >> a) { data.push_back(a); } sort(all(data)); int l, r, lr, rr; bool f = false; rep(t, 2) { double sum = 0.0; int sz = 1; int r1, r2, a; if (t == 0) { l = 0, r = data.size() - 2; lr = rr = data[data.size() - 1]; } else { l = data.size() - 1, r = 1; lr = rr = data[0]; } if (data.size() == 1) { if (data[0] <= n) { f = true; break; } } #if DEB printf("t:%d --------------------\n", t); printf("(l,r)=(%d,%d) init_r:%d\n", l, r, rr); #endif for (; sz < data.size();) { r1 = data[l] + lr; r2 = abs(data[l] - lr); a = r1 * r1 - r2 * r2; sum += sqrt((double)a); lr = data[l]; #if DEB printf("sz:%d l:%d lr:%d sum:%lf\n", sz + 1, l, lr, sum); #endif if (t == 0) l++; else l--; sz++; if (sz == data.size()) break; r1 = data[l] + rr; r2 = abs(data[l] - rr); a = r1 * r1 - r2 * r2; sum += sqrt((double)a); rr = data[l]; #if DEB printf("sz:%d l:%d rr:%d sum:%lf\n", sz + 1, l, rr, sum); #endif if (t == 0) l++; else l--; sz++; if (sz == data.size()) break; r1 = data[r] + lr; r2 = abs(data[r] - lr); a = r1 * r1 - r2 * r2; sum += sqrt((double)a); lr = data[r]; #if DEB printf("sz:%d r:%d lr:%d sum:%lf\n", sz + 1, r, lr, sum); #endif if (t == 0) r--; else r++; sz++; if (sz == data.size()) break; r1 = data[r] + rr; r2 = abs(data[r] - rr); a = r1 * r1 - r2 * r2; sum += sqrt((double)a); rr = data[r]; #if DEB printf("sz:%d r:%d rr:%d sum:%lf\n", sz + 1, r, rr, sum); #endif if (t == 0) r--; else r++; sz++; if (sz == data.size()) break; } sum += (double)(lr + rr); #if DEB printf("sum:%lf\n", sum); #endif if (sum <= (double)n) { f = true; } } puts(f ? "OK" : "NA"); } return 0; }
[["-", 0, 1, 0, 34, 12, 11, 12, 16, 12, 13], ["+", 0, 1, 0, 34, 12, 11, 12, 16, 12, 13], ["-", 0, 9, 0, 1, 0, 34, 12, 11, 12, 13], ["+", 0, 9, 0, 1, 0, 34, 12, 11, 12, 13]]
1
780
4
#include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; double dp[1 << 12][12]; int n; int c[12]; double d(int back, int next) { return sqrt(pow(c[back] + c[next], 2) - pow(c[back] - c[next], 2)); } double solve(int state, int back) { if (dp[state][back] != -1) return dp[state][back]; if (state == (1 << n) - 1) return dp[state][back] = c[back]; double ret = 1e+20; double dist; for (int i = 0; i < n; i++) { if (!((1 << i) & state)) { if (state == 0) { dist = c[i]; } else { dist = d(back, i); } ret = min(ret, solve(state | 1 << i, i) + dist); } } return dp[state][back] = ret; } int main(void) { string str; while (getline(cin, str)) { istringstream iss(str); double l; double ret = 1e+20; fill((double *)dp, (double *)dp + (1 << 12) * 12, -1); memset(c, 0, sizeof(c)); n = 0; iss >> l; while (iss >> c[n]) n++; ret = min(ret, solve(0, 0)); if (ret <= l) { cout << "OK" << endl; } else { cout << "NG" << endl; } } }
#include <cmath> #include <cstdlib> #include <cstring> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; double dp[1 << 12][12]; int n; int c[12]; double d(int back, int next) { return sqrt(pow(c[back] + c[next], 2) - pow(c[back] - c[next], 2)); } double solve(int state, int back) { if (dp[state][back] != -1) return dp[state][back]; if (state == (1 << n) - 1) return dp[state][back] = c[back]; double ret = 1e+20; double dist; for (int i = 0; i < n; i++) { if (!((1 << i) & state)) { if (state == 0) { dist = c[i]; } else { dist = d(back, i); } ret = min(ret, solve(state | 1 << i, i) + dist); } } return dp[state][back] = ret; } int main(void) { string str; while (getline(cin, str)) { istringstream iss(str); double l; double ret = 1e+20; fill((double *)dp, (double *)dp + (1 << 12) * 12, -1); memset(c, 0, sizeof(c)); n = 0; iss >> l; while (iss >> c[n]) n++; ret = min(ret, solve(0, 0)); if (ret <= l) { cout << "OK" << endl; } else { cout << "NA" << endl; } } }
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
1
371
2