problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p00103
C++
Time Limit Exceeded
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main() { int N, m = 0, rn = 0, pn = 0; string str; cin >> N; for (int i = 0; i < N; i++) { while (1) { cin >> str; if (str == "OUT") { m++; if (m == 3) break; } if (str == "HIT") { rn++; if (rn == 4) { pn++; rn = 3; } } if (str == "HOMERUN") { pn += rn + 1; rn = 0; } } cout << pn << endl; rn = 0; pn = 0; } return 0; }
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main() { int N, m = 0, rn = 0, pn = 0; string str; cin >> N; for (int i = 0; i < N; i++) { while (1) { cin >> str; if (str == "OUT") { m++; if (m == 3) break; } if (str == "HIT") { rn++; if (rn == 4) { pn++; rn = 3; } } if (str == "HOMERUN") { pn += rn + 1; rn = 0; } } cout << pn << endl; rn = 0; pn = 0; m = 0; } return 0; }
insert
32
32
32
33
TLE
p00104
C++
Time Limit Exceeded
#include <iostream> #include <string.h> using namespace std; struct zahyou { int x; int y; }; int main() { int tx, ty, count; char map[100][100]; bool flag; struct zahyou now; while (cin >> tx >> ty, tx && ty) { flag = false; now.x = 0; now.y = 0; memset(map, '\0', sizeof(map)); for (int i = 0; i < ty; i++) { for (int j = 0; j < tx; j++) { cin >> map[i][j]; } } while (1) { if (map[now.y][now.x] == '.') { cout << now.x << " " << now.y << endl; break; } else if (map[now.y][now.x] == '1') { cout << "LOOP" << endl; break; } if (map[now.y][now.x] == '>') { map[now.y][now.x] = '1'; now.x++; } else if (map[now.y][now.x] == '<') { map[now.y][now.x] = '1'; now.x--; } else if (map[now.y][now.x] == 'v') { map[now.y][now.x] = '1'; now.y++; } else if (map[now.y][now.x] == '^') { map[now.y][now.x] = '1'; now.y--; } } } }
#include <iostream> #include <string.h> using namespace std; struct zahyou { int x; int y; }; int main() { int tx, ty, count; char map[100][100]; bool flag; struct zahyou now; while (cin >> tx >> ty, tx && ty) { flag = false; now.x = 0; now.y = 0; memset(map, '\0', sizeof(map)); for (int i = 0; i < tx; i++) { for (int j = 0; j < ty; j++) { cin >> map[i][j]; } } while (1) { if (map[now.y][now.x] == '.') { cout << now.x << " " << now.y << endl; break; } else if (map[now.y][now.x] == '1') { cout << "LOOP" << endl; break; } if (map[now.y][now.x] == '>') { map[now.y][now.x] = '1'; now.x++; } else if (map[now.y][now.x] == '<') { map[now.y][now.x] = '1'; now.x--; } else if (map[now.y][now.x] == 'v') { map[now.y][now.x] = '1'; now.y++; } else if (map[now.y][now.x] == '^') { map[now.y][now.x] = '1'; now.y--; } } } }
replace
23
25
23
25
TLE
p00104
C++
Time Limit Exceeded
#include <cstring> #include <iostream> using namespace std; char a[8000][8000]; int h, w, loop, cx, cy; int main() { while (true) { cin >> h >> w; if (!h) { break; } memset(a, ' ', sizeof(a)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } cx = 0; cy = 0; loop = 0; while (true) { if (loop > 18000000) { cout << "LOOP" << endl; break; } if (a[cy][cx] == '.') { cout << cx << ' ' << cy << endl; break; } if (a[cy][cx] == '<') { cx--; } if (a[cy][cx] == '>') { cx++; } if (a[cy][cx] == '^') { cy--; } if (a[cy][cx] == 'v') { cy++; } loop++; } } }
#include <cstring> #include <iostream> using namespace std; char a[8000][8000]; int h, w, loop, cx, cy; int main() { while (true) { cin >> h >> w; if (!h) { break; } memset(a, ' ', sizeof(a)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } cx = 0; cy = 0; loop = 0; while (true) { if (loop > 15000000) { cout << "LOOP" << endl; break; } if (a[cy][cx] == '.') { cout << cx << ' ' << cy << endl; break; } if (a[cy][cx] == '<') { cx--; } if (a[cy][cx] == '>') { cx++; } if (a[cy][cx] == '^') { cy--; } if (a[cy][cx] == 'v') { cy++; } loop++; } } }
replace
21
22
21
22
TLE
p00104
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int w, h; while (cin >> h >> w, w + h) { char room[10][10]; int t = 0; int now_x = 0, now_y = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> room[i][j]; } } while (1) { if (room[now_y][now_x] == '.') break; char c = room[now_y][now_x]; if (c == '>') now_x++; if (c == '<') now_x--; if (c == 'v') now_y++; if (c == '^') now_y--; t++; if (t > w * h) break; } if (t > w * h) cout << "LOOP" << endl; else cout << now_x << " " << now_y << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int w, h; while (cin >> h >> w, w + h) { char room[101][101]; int t = 0; int now_x = 0, now_y = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> room[i][j]; } } while (1) { if (room[now_y][now_x] == '.') break; char c = room[now_y][now_x]; if (c == '>') now_x++; if (c == '<') now_x--; if (c == 'v') now_y++; if (c == '^') now_y--; t++; if (t > w * h) break; } if (t > w * h) cout << "LOOP" << endl; else cout << now_x << " " << now_y << endl; } }
replace
7
8
7
8
TLE
p00104
C++
Runtime Error
#include <iostream> using namespace std; int main() { int H, W, x, y; while (cin >> H >> W) { if (H == 0 && W == 0) break; char space[H][W], c; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> space[j][i]; } } x = y = 0; while (1) { c = space[x][y]; space[x][y] = '0'; if (c == '>') x++; else if (c == '<') x--; else if (c == 'v') y++; else if (c == '^') y--; else if (c == '.') { cout << x << ' ' << y << endl; break; } else if (c == '0') { cout << "LOOP" << endl; break; } } } return 0; }
#include <iostream> using namespace std; int main() { int H, W, x, y; while (cin >> H >> W) { if (H == 0 && W == 0) break; char space[W][H], c; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> space[j][i]; } } x = y = 0; while (1) { c = space[x][y]; space[x][y] = '0'; if (c == '>') x++; else if (c == '<') x--; else if (c == 'v') y++; else if (c == '^') y--; else if (c == '.') { cout << x << ' ' << y << endl; break; } else if (c == '0') { cout << "LOOP" << endl; break; } } } return 0; }
replace
7
8
7
8
0
p00104
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back string fie[30] = {}; int main() { int h, w; while (cin >> h >> w) { if (w == 0 && h == 0) break; rep(i, h) { cin >> fie[i]; } int y = 0, x = 0; int c = 0; while (c < 1000000) { c++; char ch = fie[y][x]; if (ch == '>') { x++; } else if (ch == '<') { x--; } else if (ch == '^') { y--; } else if (ch == 'v') { y++; } else break; } if (c == 1000000) cout << "LOOP" << endl; else cout << x << " " << y << endl; } }
#include "bits/stdc++.h" using namespace std; typedef vector<int> vi; typedef pair<int, int> pii; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back string fie[200] = {}; int main() { int h, w; while (cin >> h >> w) { if (w == 0 && h == 0) break; rep(i, h) { cin >> fie[i]; } int y = 0, x = 0; int c = 0; while (c < 1000000) { c++; char ch = fie[y][x]; if (ch == '>') { x++; } else if (ch == '<') { x--; } else if (ch == '^') { y--; } else if (ch == 'v') { y++; } else break; } if (c == 1000000) cout << "LOOP" << endl; else cout << x << " " << y << endl; } }
replace
12
13
12
13
0
p00104
C++
Runtime Error
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> P; static const double EPS = 1e-5; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) bool ariv[101][101]; P end(vector<string> grid, int x, int y) { if (ariv[y][x]) return P(-1, -1); else ariv[y][x] = true; if (grid[y][x] == '.') return P(x, y); if (grid[y][x] == '>') return end(grid, x + 1, y); if (grid[y][x] == '<') return end(grid, x - 1, y); if (grid[y][x] == '^') return end(grid, x, y - 1); if (grid[y][x] == 'v') return end(grid, x, y + 1); } void outp(vector<string> grid, int x, int y) { P point = end(grid, x, y); if (point.first == -1) cout << "LOOP" << endl; else printf("%d %d\n", point.first, point.second); } int main(void) { int H, W; while (cin >> H >> W) { vector<string> grid; memset(ariv, 0, sizeof(ariv)); REP(y, H) { string tmp; cin >> tmp; grid.push_back(tmp); } if (grid[0][0] == '.') printf("0 0\n"); else if (grid[0][0] == '>') outp(grid, 1, 0); else if (grid[0][0] == '<') outp(grid, 0, 0); else if (grid[0][0] == '^') outp(grid, 0, 0); else if (grid[0][0] == 'v') outp(grid, 0, 1); } return 0; }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> P; static const double EPS = 1e-5; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) bool ariv[101][101]; P end(vector<string> grid, int x, int y) { if (ariv[y][x]) return P(-1, -1); else ariv[y][x] = true; if (grid[y][x] == '.') return P(x, y); if (grid[y][x] == '>') return end(grid, x + 1, y); if (grid[y][x] == '<') return end(grid, x - 1, y); if (grid[y][x] == '^') return end(grid, x, y - 1); if (grid[y][x] == 'v') return end(grid, x, y + 1); } void outp(vector<string> grid, int x, int y) { P point = end(grid, x, y); if (point.first == -1) cout << "LOOP" << endl; else printf("%d %d\n", point.first, point.second); } int main(void) { int H, W; while (cin >> H >> W) { if (H == 0) break; vector<string> grid; memset(ariv, 0, sizeof(ariv)); REP(y, H) { string tmp; cin >> tmp; grid.push_back(tmp); } if (grid[0][0] == '.') printf("0 0\n"); else if (grid[0][0] == '>') outp(grid, 1, 0); else if (grid[0][0] == '<') outp(grid, 0, 0); else if (grid[0][0] == '^') outp(grid, 0, 0); else if (grid[0][0] == 'v') outp(grid, 0, 1); } return 0; }
insert
55
55
55
57
0
p00104
C++
Runtime Error
#include <iostream> using namespace std; #define MAX 10 int main(void) { int H, W; char tile[MAX][MAX]; while (1) { int x = 0, y = 0; for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { tile[i][j] = '.'; } } cin >> H >> W; if (H == 0 && W == 0) break; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> tile[i][j]; } } int cal = 0, sum = 0; cal = H * W; // for(int i = 0 ; i < H ; i++) { // for(int j = 0 ; j < W ; j++) { while (tile[x][y] != '.') { if (tile[x][y] == '>') y++, sum++; else if (tile[x][y] == '<') y--, sum++; else if (tile[x][y] == '^') x--, sum++; else if (tile[x][y] == 'v') x++, sum++; if (sum > cal) break; // cout << "x = " << x << endl << "y = " << y << endl; } if (sum <= cal) cout << y << ' ' << x << endl; else cout << "LOOP" << endl; } return 0; }
#include <iostream> using namespace std; #define MAX 100 int main(void) { int H, W; char tile[MAX][MAX]; while (1) { int x = 0, y = 0; for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { tile[i][j] = '.'; } } cin >> H >> W; if (H == 0 && W == 0) break; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> tile[i][j]; } } int cal = 0, sum = 0; cal = H * W; // for(int i = 0 ; i < H ; i++) { // for(int j = 0 ; j < W ; j++) { while (tile[x][y] != '.') { if (tile[x][y] == '>') y++, sum++; else if (tile[x][y] == '<') y--, sum++; else if (tile[x][y] == '^') x--, sum++; else if (tile[x][y] == 'v') x++, sum++; if (sum > cal) break; // cout << "x = " << x << endl << "y = " << y << endl; } if (sum <= cal) cout << y << ' ' << x << endl; else cout << "LOOP" << endl; } return 0; }
replace
3
4
3
4
TLE
p00104
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <complex> #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 <typeinfo> #include <utility> #include <vector> using namespace std; char tile[11][11]; void loop(int x, int y) { if (tile[x][y] == '.') { cout << y << ' ' << x << endl; return; } if (tile[x][y] == '#') { cout << "LOOP" << endl; return; } if (tile[x][y] == '>') { tile[x][y] = '#'; loop(x, y + 1); } if (tile[x][y] == '<') { tile[x][y] = '#'; loop(x, y - 1); } if (tile[x][y] == '^') { tile[x][y] = '#'; loop(x - 1, y); } if (tile[x][y] == 'v') { tile[x][y] = '#'; loop(x + 1, y); } } int main(void) { int h, w; int x, y; int a; while (1) { x = 0, y = 0; cin >> h >> w; if (h == 0 && w == 0) break; for (a = 0; a < h; a++) { cin >> tile[a]; } loop(0, 0); } return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <complex> #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 <typeinfo> #include <utility> #include <vector> using namespace std; char tile[101][101]; void loop(int x, int y) { if (tile[x][y] == '.') { cout << y << ' ' << x << endl; return; } if (tile[x][y] == '#') { cout << "LOOP" << endl; return; } if (tile[x][y] == '>') { tile[x][y] = '#'; loop(x, y + 1); } if (tile[x][y] == '<') { tile[x][y] = '#'; loop(x, y - 1); } if (tile[x][y] == '^') { tile[x][y] = '#'; loop(x - 1, y); } if (tile[x][y] == 'v') { tile[x][y] = '#'; loop(x + 1, y); } } int main(void) { int h, w; int x, y; int a; while (1) { x = 0, y = 0; cin >> h >> w; if (h == 0 && w == 0) break; for (a = 0; a < h; a++) { cin >> tile[a]; } loop(0, 0); } return 0; }
replace
28
29
28
29
TLE
p00104
C++
Time Limit Exceeded
#include <iostream> #include <string> #include <vector> using namespace std; int main(void) { while (1) { int w, h; cin >> w >> h; if (w == 0 && h == 0) break; vector<string> data; for (int i = 0; i < h; i++) { string line; cin >> line; data.push_back(line); } bool loop_flag = false; int x = 0; int y = 0; while (1) { int oldx = x; int oldy = y; if (data[y][x] == '.') break; else if (data[y][x] == '>') x++; else if (data[y][x] == 'v') y++; else if (data[y][x] == '^') y--; else if (data[y][x] == '<') x--; else if (data[y][x] == 'x') { loop_flag = true; break; } data[oldy][oldx] = 'x'; } if (loop_flag) cout << "LOOP" << endl; else cout << x << " " << y << endl; } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int main(void) { while (1) { int w, h; cin >> h >> w; if (w == 0 && h == 0) break; vector<string> data; for (int i = 0; i < h; i++) { string line; cin >> line; data.push_back(line); } bool loop_flag = false; int x = 0; int y = 0; while (1) { int oldx = x; int oldy = y; if (data[y][x] == '.') break; else if (data[y][x] == '>') x++; else if (data[y][x] == 'v') y++; else if (data[y][x] == '^') y--; else if (data[y][x] == '<') x--; else if (data[y][x] == 'x') { loop_flag = true; break; } data[oldy][oldx] = 'x'; } if (loop_flag) cout << "LOOP" << endl; else cout << x << " " << y << endl; } return 0; }
replace
7
8
7
8
TLE
p00105
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; pair<string, int> a[1]; int main() { int t = 0, g, i; while (cin >> a[t].first >> a[t].second) t++; sort(a, a + t); string f, b = a[0].first; cout << b << endl << a[0].second; for (i = 1; i < t; i++) { bool n = 0; f = a[i].first, g = a[i].second; if (f == b) n = 1; if (n) cout << " " << g; else cout << endl << f << endl << g; b = f; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; pair<string, int> a[100]; int main() { int t = 0, g, i; while (cin >> a[t].first >> a[t].second) t++; sort(a, a + t); string f, b = a[0].first; cout << b << endl << a[0].second; for (i = 1; i < t; i++) { bool n = 0; f = a[i].first, g = a[i].second; if (f == b) n = 1; if (n) cout << " " << g; else cout << endl << f << endl << g; b = f; } cout << endl; return 0; }
replace
2
3
2
3
-11
p00105
C++
Runtime Error
#define scanf_s scanf // #define sscanf_s sscanf // #define gets_s gets #include <iostream> #include <math.h> #include <stdio.h> #include <string> using namespace std; #define MAX 1001 #define _MAX 100 #define HO 6 int main(void) { char str[MAX]; string word[_MAX]; int pa[_MAX], rank[_MAX], cou, a; for (cou = 0; scanf_s("%s %d", str, MAX, &pa[cou]) != EOF; ++cou) { string wo(str); word[cou] = wo; rank[cou] = cou; } for (int i = 0; i < cou; ++i) { for (int j = cou - 1; j > i; --j) { if (word[rank[j]] <= word[rank[j - 1]]) { if (word[rank[j]] == word[rank[j - 1]]) { if (pa[rank[j]] < pa[rank[j - 1]]) { a = rank[j]; rank[j] = rank[j - 1]; rank[j - 1] = a; } } else { a = rank[j]; rank[j] = rank[j - 1]; rank[j - 1] = a; } } } } for (int i = 0; i < cou; ++i) { if (i == 0 || word[rank[i - 1]] != word[rank[i]]) cout << word[rank[i]] << "\n"; if (i != 0 && word[rank[i - 1]] == word[rank[i]]) printf(" "); printf("%d", pa[rank[i]]); if (i != cou - 1 && word[rank[i + 1]] != word[rank[i]]) printf("\n"); } printf("\n"); }
#define scanf_s scanf // #define sscanf_s sscanf // #define gets_s gets #include <iostream> #include <math.h> #include <stdio.h> #include <string> using namespace std; #define MAX 1001 #define _MAX 100 #define HO 6 int main(void) { char str[MAX]; string word[_MAX]; int pa[_MAX], rank[_MAX], cou, a; for (cou = 0; scanf_s("%s %d", str, &pa[cou]) != EOF; ++cou) { string wo(str); word[cou] = wo; rank[cou] = cou; } for (int i = 0; i < cou; ++i) { for (int j = cou - 1; j > i; --j) { if (word[rank[j]] <= word[rank[j - 1]]) { if (word[rank[j]] == word[rank[j - 1]]) { if (pa[rank[j]] < pa[rank[j - 1]]) { a = rank[j]; rank[j] = rank[j - 1]; rank[j - 1] = a; } } else { a = rank[j]; rank[j] = rank[j - 1]; rank[j - 1] = a; } } } } for (int i = 0; i < cou; ++i) { if (i == 0 || word[rank[i - 1]] != word[rank[i]]) cout << word[rank[i]] << "\n"; if (i != 0 && word[rank[i - 1]] == word[rank[i]]) printf(" "); printf("%d", pa[rank[i]]); if (i != cou - 1 && word[rank[i + 1]] != word[rank[i]]) printf("\n"); } printf("\n"); }
replace
15
16
15
16
-11
p00105
C++
Memory Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #include <vector> int main() { std::string s; int p; std::map<std::string, std::vector<int>> m; while (std::cin >> s >> p, p != 0) { m[s].push_back(p); } std::map<std::string, std::vector<int>>::iterator m_it = m.begin(); while (m_it != m.end()) { std::cout << (*m_it).first << std::endl; std::sort((*m_it).second.begin(), (*m_it).second.end()); std::vector<int>::iterator v_it = (*m_it).second.begin(); while (v_it != (*m_it).second.end()) { std::cout << *v_it; v_it++; (v_it == (*m_it).second.end()) ? std::cout << std::endl : std::cout << " "; } m_it++; } return 0; } /* Sample Input style 12 even 25 introduction 3 easy 9 style 7 document 13 style 21 even 18 Output for the Sample Input document 13 easy 9 even 18 25 introduction 3 style 7 12 21 */
#include <algorithm> #include <iostream> #include <map> #include <vector> int main() { std::string s; int p; std::map<std::string, std::vector<int>> m; while (std::cin >> s >> p, !std::cin.eof()) { m[s].push_back(p); } std::map<std::string, std::vector<int>>::iterator m_it = m.begin(); while (m_it != m.end()) { std::cout << (*m_it).first << std::endl; std::sort((*m_it).second.begin(), (*m_it).second.end()); std::vector<int>::iterator v_it = (*m_it).second.begin(); while (v_it != (*m_it).second.end()) { std::cout << *v_it; v_it++; (v_it == (*m_it).second.end()) ? std::cout << std::endl : std::cout << " "; } m_it++; } return 0; } /* Sample Input style 12 even 25 introduction 3 easy 9 style 7 document 13 style 21 even 18 Output for the Sample Input document 13 easy 9 even 18 25 introduction 3 style 7 12 21 */
replace
9
10
9
10
MLE
p00105
C++
Memory Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #include <vector> int main() { std::string s; int p; std::map<std::string, std::vector<int>> m; while (std::cin >> s >> p, p != 0) { m[s].push_back(p); } std::map<std::string, std::vector<int>>::iterator m_it = m.begin(); while (m_it != m.end()) { std::cout << (*m_it).first << std::endl; std::sort((*m_it).second.begin(), (*m_it).second.end()); std::vector<int>::iterator v_it = (*m_it).second.begin(); while (v_it != (*m_it).second.end()) { std::cout << *v_it; v_it++; (v_it == (*m_it).second.end()) ? std::cout << std::endl : std::cout << " "; } m_it++; } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <vector> int main() { std::string s; int p; std::map<std::string, std::vector<int>> m; while (std::cin >> s >> p, !std::cin.eof()) { m[s].push_back(p); } std::map<std::string, std::vector<int>>::iterator m_it = m.begin(); while (m_it != m.end()) { std::cout << (*m_it).first << std::endl; std::sort((*m_it).second.begin(), (*m_it).second.end()); std::vector<int>::iterator v_it = (*m_it).second.begin(); while (v_it != (*m_it).second.end()) { std::cout << *v_it; v_it++; (v_it == (*m_it).second.end()) ? std::cout << std::endl : std::cout << " "; } m_it++; } return 0; }
replace
9
10
9
10
MLE
p00106
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define iota(i, n, b, s) \ for (int i = int(b); i != int((b) + (s) * (n)); i += (s)) #define range(i, n, m) \ iota(i, (((n) > (m)) ? ((n) - (m) + 1) : ((m) - (n) + 1)), (n), \ ((n) > (m) ? -1 : 1)) #define rep(i, n) iota(i, (n), 0, 1) #define INF (1e9) #define EPS (1e-9) #define cons(a, b) (make_pair(a, b)) #define car(a) (a.first) #define cdr(a) (a.second) #define cadr(a) (car(cdr(a))) #define cddr(a) (cdr(cdr(a))) #define all(a) a.begin(), a.end() #define trace(var) cerr << ">>> " << #var << " = " << var << endl; typedef long long Integer; typedef double Real; template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << '(' << car(p) << ", " << cdr(p) << ')'; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> v) { os << v[0]; for (int i = 1, len = v.size(); i < len; ++i) os << ' ' << v[i]; return os; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int cost_a(int n) { int r = (n % 5) * 380; r += (n - (n % 5)) * 38 * 8; return r; } int cost_b(int n) { int r = (n % 4) * 550; r += (n - (n % 4)) * 55 * 85 / 10; return r; } int cost_c(int n) { int r = (n % 3) * 850; r += (n - (n % 3)) * 88 * 85 / 10; return r; } int main() { for (;;) { int c; cin >> c; if (c == 0) break; c /= 100; trace(c); int mc = INF; rep(i, 1 + c / 2) { rep(j, 1 + c / 3) { rep(k, 1 + c / 5) { if (2 * i + 3 * j + 5 * k == c) { int cc = cost_a(i) + cost_b(j) + cost_c(k); mc = min(mc, cc); } } } } cout << mc << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define iota(i, n, b, s) \ for (int i = int(b); i != int((b) + (s) * (n)); i += (s)) #define range(i, n, m) \ iota(i, (((n) > (m)) ? ((n) - (m) + 1) : ((m) - (n) + 1)), (n), \ ((n) > (m) ? -1 : 1)) #define rep(i, n) iota(i, (n), 0, 1) #define INF (1e9) #define EPS (1e-9) #define cons(a, b) (make_pair(a, b)) #define car(a) (a.first) #define cdr(a) (a.second) #define cadr(a) (car(cdr(a))) #define cddr(a) (cdr(cdr(a))) #define all(a) a.begin(), a.end() #define trace(var) cerr << ">>> " << #var << " = " << var << endl; typedef long long Integer; typedef double Real; template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << '(' << car(p) << ", " << cdr(p) << ')'; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> v) { os << v[0]; for (int i = 1, len = v.size(); i < len; ++i) os << ' ' << v[i]; return os; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; int cost_a(int n) { int r = (n % 5) * 380; r += (n - (n % 5)) * 38 * 8; return r; } int cost_b(int n) { int r = (n % 4) * 550; r += (n - (n % 4)) * 55 * 85 / 10; return r; } int cost_c(int n) { int r = (n % 3) * 850; r += (n - (n % 3)) * 88 * 85 / 10; return r; } int main() { for (;;) { int c; cin >> c; if (c == 0) break; c /= 100; int mc = INF; rep(i, 1 + c / 2) { rep(j, 1 + c / 3) { rep(k, 1 + c / 5) { if (2 * i + 3 * j + 5 * k == c) { int cc = cost_a(i) + cost_b(j) + cost_c(k); mc = min(mc, cc); } } } } cout << mc << endl; } return 0; }
delete
63
64
63
63
0
>>> c = 5 >>> c = 22
p00107
C++
Time Limit Exceeded
#include <math.h> #include <stdio.h> int main() { while (1) { int d, w, h; scanf("%d %d %d", &d, &w, &h); if (d == 0 && w == 0 && h == 0) break; int n; scanf("%d", &n); int max = (int)fmax((double)d, fmax((double)w, (double)h)); double a = sqrt(d * d + w * w + h * h - max * max) / 2; int i; for (i = 0; i < 5; i++) { int b; scanf("%d", &b); // printf("a:%lf b:%d", a, b); if ((double)b <= a) printf("NA\n"); else printf("OK\n"); } } return 0; }
#include <math.h> #include <stdio.h> int main() { while (1) { int d, w, h; scanf("%d %d %d", &d, &w, &h); if (d == 0 && w == 0 && h == 0) break; int n; scanf("%d", &n); int max = (int)fmax((double)d, fmax((double)w, (double)h)); double a = sqrt(d * d + w * w + h * h - max * max) / 2; int i; for (i = 0; i < n; i++) { int b; scanf("%d", &b); // printf("a:%lf b:%d", a, b); if ((double)b <= a) printf("NA\n"); else printf("OK\n"); } } return 0; }
replace
16
17
16
17
TLE
p00108
C++
Runtime Error
#include <iostream> #include <map> using namespace std; int ans[100][2] [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列 int counter = 0, flag = 0; void solve() { int n; cin >> n; if (n == 0) { flag = 1; return; } ans[counter][0][0] = n; int S[12]; // 数列 for (int i = 0; i < n; i++) { cin >> S[i]; } bool check = false; // 操作の有無のフラグ int time = 0; // 操作回数 do { map<int, int> marker; for (int i = 0; i < n; i++) { marker[S[i]]; marker[S[i]]++; } check = false; for (int i = 0; i < n; i++) { if (S[i] != marker[S[i]]) { check = true; S[i] = marker[S[i]]; } } if (check) time++; } while (check); ans[counter][0][1] = time; for (int i = 0; i < n; i++) { ans[counter][1][i] = S[i]; } counter++; } int main() { while (flag == 0) solve(); for (int i = 0; i < counter; i++) { cout << ans[i][0][1] << endl; for (int j = 0; j < ans[i][0][0] - 1; j++) { cout << ans[i][1][j] << " "; } cout << ans[i][1][ans[i][0][0] - 1] << endl; } }
#include <iostream> #include <map> using namespace std; int ans[1000][2] [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列 int counter = 0, flag = 0; void solve() { int n; cin >> n; if (n == 0) { flag = 1; return; } ans[counter][0][0] = n; int S[12]; // 数列 for (int i = 0; i < n; i++) { cin >> S[i]; } bool check = false; // 操作の有無のフラグ int time = 0; // 操作回数 do { map<int, int> marker; for (int i = 0; i < n; i++) { marker[S[i]]; marker[S[i]]++; } check = false; for (int i = 0; i < n; i++) { if (S[i] != marker[S[i]]) { check = true; S[i] = marker[S[i]]; } } if (check) time++; } while (check); ans[counter][0][1] = time; for (int i = 0; i < n; i++) { ans[counter][1][i] = S[i]; } counter++; } int main() { while (flag == 0) solve(); for (int i = 0; i < counter; i++) { cout << ans[i][0][1] << endl; for (int j = 0; j < ans[i][0][0] - 1; j++) { cout << ans[i][1][j] << " "; } cout << ans[i][1][ans[i][0][0] - 1] << endl; } }
replace
4
5
4
5
0
p00108
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main() { int n; while (1) { cin >> n; if (n == 0) break; vector<int> s; int b; for (int i = 0; i < n; i++) { cin >> b; s.push_back(b); } int c = 0; while (1) { vector<int> freq(13, 0); for (int i = 0; i < n; i++) freq[s[i]]++; vector<int> ss = s; for (int i = 0; i < n; i++) ss[i] = freq[ss[i]]; if (ss == s) { cout << c << endl; for (int i = 0; i < s.size(); i++) printf("%d%c", s[i], (i == s.size() - 1) ? '\n' : ' '); break; } s = ss; c++; } } }
#include <iostream> #include <vector> using namespace std; int main() { int n; while (1) { cin >> n; if (n == 0) break; vector<int> s; int b; for (int i = 0; i < n; i++) { cin >> b; s.push_back(b); } int c = 0; while (1) { vector<int> freq(1000, 0); for (int i = 0; i < n; i++) freq[s[i]]++; vector<int> ss = s; for (int i = 0; i < n; i++) ss[i] = freq[ss[i]]; if (ss == s) { cout << c << endl; for (int i = 0; i < s.size(); i++) printf("%d%c", s[i], (i == s.size() - 1) ? '\n' : ' '); break; } s = ss; c++; } } }
replace
18
19
18
19
0
p00108
C++
Memory Limit Exceeded
#include <cstring> #include <iostream> using namespace std; int x[100000][500], n; int main() { while (true) { memset(x, 0, sizeof(x)); cin >> n; if (!n) { break; } for (int i = 0; i < n; i++) { cin >> x[0][i]; } for (int i = 1; i < 30000; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (x[i - 1][k] == x[i - 1][j]) { x[i][j]++; } } } } for (int i = 1; i < 30000; i++) { for (int j = 0; j < n; j++) { if (x[i - 1][j] != x[i][j]) { goto E; } } cout << i - 1 << endl; for (int j = 0; j < n; j++) { if (j) { cout << ' '; } cout << x[i][j]; } cout << endl; goto owata; E:; } owata:; } }
#include <cstring> #include <iostream> using namespace std; int x[32000][500], n; int main() { while (true) { memset(x, 0, sizeof(x)); cin >> n; if (!n) { break; } for (int i = 0; i < n; i++) { cin >> x[0][i]; } for (int i = 1; i < 30000; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (x[i - 1][k] == x[i - 1][j]) { x[i][j]++; } } } } for (int i = 1; i < 30000; i++) { for (int j = 0; j < n; j++) { if (x[i - 1][j] != x[i][j]) { goto E; } } cout << i - 1 << endl; for (int j = 0; j < n; j++) { if (j) { cout << ' '; } cout << x[i][j]; } cout << endl; goto owata; E:; } owata:; } }
replace
3
4
3
4
MLE
p00108
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <iterator> #include <vector> using namespace std; int main() { int n; vector<int> vi, vi2; while (cin >> n) { if (n == 0) break; for (int i = 0; i < n; i++) { int j; cin >> j; vi.push_back(j); } int counter = -1; do { vi2.clear(); copy(vi.begin(), vi.end(), back_inserter(vi2)); for (int i = 0; i < n; i++) { vi[i] = count(vi2.begin(), vi2.end(), vi[i]); } counter++; } while (!equal(vi.begin(), vi.end(), vi2.begin())); cout << counter << endl; for (int i = 0; i < vi.size(); i++) { if (i != 0) cout << " "; cout << vi[i]; } cout << endl; } return 0; }
#include <algorithm> #include <iostream> #include <iterator> #include <vector> using namespace std; int main() { int n; vector<int> vi, vi2; while (cin >> n) { vi.clear(); vi2.clear(); if (n == 0) break; for (int i = 0; i < n; i++) { int j; cin >> j; vi.push_back(j); } int counter = -1; do { vi2.clear(); copy(vi.begin(), vi.end(), back_inserter(vi2)); for (int i = 0; i < n; i++) { vi[i] = count(vi2.begin(), vi2.end(), vi[i]); } counter++; } while (!equal(vi.begin(), vi.end(), vi2.begin())); cout << counter << endl; for (int i = 0; i < vi.size(); i++) { if (i != 0) cout << " "; cout << vi[i]; } cout << endl; } return 0; }
insert
10
10
10
12
TLE
p00108
C++
Runtime Error
#include <iostream> #include <map> using namespace std; int ans[113][2] [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列 int counter = 0, flag = 0; void solve() { int n; cin >> n; if (n == 0) { flag = 1; return; } ans[counter][0][0] = n; int S[12]; // 数列 for (int i = 0; i < n; i++) { cin >> S[i]; } bool check = false; // 操作の有無のフラグ int time = 0; // 操作回数 do { map<int, int> marker; for (int i = 0; i < n; i++) { marker[S[i]]; marker[S[i]]++; } check = false; for (int i = 0; i < n; i++) { if (S[i] != marker[S[i]]) { check = true; S[i] = marker[S[i]]; } } if (check) time++; } while (check); ans[counter][0][1] = time; for (int i = 0; i < n; i++) { ans[counter][1][i] = S[i]; } counter++; } int main() { while (flag == 0) solve(); for (int i = 0; i < counter; i++) { cout << ans[i][0][1] << endl; for (int j = 0; j < ans[i][0][0] - 1; j++) { cout << ans[i][1][j] << " "; } cout << ans[i][1][ans[i][0][0] - 1] << endl; } }
#include <iostream> #include <map> using namespace std; int ans[114][2] [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列 int counter = 0, flag = 0; void solve() { int n; cin >> n; if (n == 0) { flag = 1; return; } ans[counter][0][0] = n; int S[12]; // 数列 for (int i = 0; i < n; i++) { cin >> S[i]; } bool check = false; // 操作の有無のフラグ int time = 0; // 操作回数 do { map<int, int> marker; for (int i = 0; i < n; i++) { marker[S[i]]; marker[S[i]]++; } check = false; for (int i = 0; i < n; i++) { if (S[i] != marker[S[i]]) { check = true; S[i] = marker[S[i]]; } } if (check) time++; } while (check); ans[counter][0][1] = time; for (int i = 0; i < n; i++) { ans[counter][1][i] = S[i]; } counter++; } int main() { while (flag == 0) solve(); for (int i = 0; i < counter; i++) { cout << ans[i][0][1] << endl; for (int j = 0; j < ans[i][0][0] - 1; j++) { cout << ans[i][1][j] << " "; } cout << ans[i][1][ans[i][0][0] - 1] << endl; } }
replace
4
5
4
5
0
p00108
C++
Runtime Error
#include <iostream> #include <map> using namespace std; int ans[110][2] [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列 int counter = 0, flag = 0; void solve() { int n; cin >> n; if (n == 0) { flag = 1; return; } ans[counter][0][0] = n; int S[12]; // 数列 for (int i = 0; i < n; i++) { cin >> S[i]; } bool check = false; // 操作の有無のフラグ int time = 0; // 操作回数 do { map<int, int> marker; for (int i = 0; i < n; i++) { marker[S[i]]; marker[S[i]]++; } check = false; for (int i = 0; i < n; i++) { if (S[i] != marker[S[i]]) { check = true; S[i] = marker[S[i]]; } } if (check) time++; } while (check); ans[counter][0][1] = time; for (int i = 0; i < n; i++) { ans[counter][1][i] = S[i]; } counter++; } int main() { while (flag == 0) solve(); for (int i = 0; i < counter; i++) { cout << ans[i][0][1] << endl; for (int j = 0; j < ans[i][0][0] - 1; j++) { cout << ans[i][1][j] << " "; } cout << ans[i][1][ans[i][0][0] - 1] << endl; } }
#include <iostream> #include <map> using namespace std; int ans[120][2] [12]; // 二つ目の次元の0の0に数列の長さ、0の1に操作回数、1に答えの数列 int counter = 0, flag = 0; void solve() { int n; cin >> n; if (n == 0) { flag = 1; return; } ans[counter][0][0] = n; int S[12]; // 数列 for (int i = 0; i < n; i++) { cin >> S[i]; } bool check = false; // 操作の有無のフラグ int time = 0; // 操作回数 do { map<int, int> marker; for (int i = 0; i < n; i++) { marker[S[i]]; marker[S[i]]++; } check = false; for (int i = 0; i < n; i++) { if (S[i] != marker[S[i]]) { check = true; S[i] = marker[S[i]]; } } if (check) time++; } while (check); ans[counter][0][1] = time; for (int i = 0; i < n; i++) { ans[counter][1][i] = S[i]; } counter++; } int main() { while (flag == 0) solve(); for (int i = 0; i < counter; i++) { cout << ans[i][0][1] << endl; for (int j = 0; j < ans[i][0][0] - 1; j++) { cout << ans[i][1][j] << " "; } cout << ans[i][1][ans[i][0][0] - 1] << endl; } }
replace
4
5
4
5
0
p00108
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; int main() { while (1) { int n, count = 0; long before[13], after[13], c[13]; scanf(" %d", &n); if (n == 0) break; for (int i = 0; i < n; ++i) scanf(" %ld", &before[i]); // loop while (1) { for (int i = 0; i <= 120; ++i) c[i] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (before[i] == before[j]) c[i]++; } } for (int i = 0; i < n; ++i) after[i] = c[i]; bool flag = true; for (int i = 0; i < n; ++i) { if (before[i] != after[i]) { flag = false; break; } } if (flag) break; /* for(int i=0; i<n; ++i){ printf("%ld", after[i]); if(i<n-1) printf(" "); } printf("\n"); */ count++; for (int i = 0; i < n; ++i) before[i] = after[i]; } printf("%d\n", count); for (int i = 0; i < n; ++i) { printf("%ld", after[i]); if (i < n - 1) printf(" "); } printf("\n"); } return 0; }
#include <cstdio> #include <iostream> using namespace std; int main() { while (1) { int n, count = 0; long before[13], after[13], c[13]; scanf(" %d", &n); if (n == 0) break; for (int i = 0; i < n; ++i) scanf(" %ld", &before[i]); // loop while (1) { for (int i = 0; i <= 12; ++i) c[i] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (before[i] == before[j]) c[i]++; } } for (int i = 0; i < n; ++i) after[i] = c[i]; bool flag = true; for (int i = 0; i < n; ++i) { if (before[i] != after[i]) { flag = false; break; } } if (flag) break; /* for(int i=0; i<n; ++i){ printf("%ld", after[i]); if(i<n-1) printf(" "); } printf("\n"); */ count++; for (int i = 0; i < n; ++i) before[i] = after[i]; } printf("%d\n", count); for (int i = 0; i < n; ++i) { printf("%ld", after[i]); if (i < n - 1) printf(" "); } printf("\n"); } return 0; }
replace
18
19
18
19
-6
*** stack smashing detected ***: terminated
p00108
C++
Runtime Error
#include <iostream> #include <map> using namespace std; int main() { int n, c, i; int pre[12], now[12], cnt[12]; while (cin >> n, n) { map<int, int> m; for (i = 0; i < n; i++) { cin >> pre[i]; m[pre[i]] = i; } for (i = 0; i < n; i++) now[i] = m[pre[i]]; for (c = 0;; c++) { for (i = 0; i < n; i++) cnt[i] = 0; for (i = 0; i < n; i++) cnt[now[i] - 1]++; for (i = 0; i < n; i++) now[i] = cnt[now[i] - 1]; for (i = 0; i < n; i++) if (now[i] != pre[i]) break; if (i == n) break; for (i = 0; i < n; i++) pre[i] = now[i]; } cout << c << endl << now[0]; for (i = 1; i < n; i++) cout << " " << now[i]; cout << endl; } return 0; }
#include <iostream> #include <map> using namespace std; int main() { int n, c, i; int pre[12], now[12], cnt[12]; while (cin >> n, n) { map<int, int> m; for (i = 0; i < n; i++) { cin >> pre[i]; m[pre[i]] = i + 1; } for (i = 0; i < n; i++) now[i] = m[pre[i]]; for (c = 0;; c++) { for (i = 0; i < n; i++) cnt[i] = 0; for (i = 0; i < n; i++) cnt[now[i] - 1]++; for (i = 0; i < n; i++) now[i] = cnt[now[i] - 1]; for (i = 0; i < n; i++) if (now[i] != pre[i]) break; if (i == n) break; for (i = 0; i < n; i++) pre[i] = now[i]; } cout << c << endl << now[0]; for (i = 1; i < n; i++) cout << " " << now[i]; cout << endl; } return 0; }
replace
11
12
11
12
0
p00109
C++
Runtime Error
#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; }
replace
37
38
37
38
0
p00109
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> // cin.sync_with_stdio(false); // streambuf using namespace std; typedef long long ll; typedef double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; using vpll = vector<pll>; using ti3 = tuple<int, int, int>; using vti3 = vector<ti3>; template <class T, int s> using va = vector<array<T, s>>; template <class T, class T2> using umap = unordered_map<T, T2>; template <class T> using uset = unordered_set<T>; template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } #define ALL(a) a.begin(), a.end() #define rep(i, a) for (int i = 0; i < a; i++) #define rep1(i, a) for (int i = 1; i <= a; i++) #define rrep(i, a) for (int i = (a)-1; i >= 0; i--) #define rrep1(i, a) for (int i = a; i; i--) #define repi(i, a, b) for (int i = a; i < b; i++) const ll mod = 1000000007; template <class T> using heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>; template <class T> inline void hash_combine(size_t &seed, const T &v) { hash<T> hasher; seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 6) + (seed >> 2); } namespace std { template <typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> &v) const { size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } }; // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; } // namespace std template <class T> int id(vector<T> &a, T b) { return lower_bound(ALL(a), b) - a.begin(); } ll pow(ll base, ll i, ll mod) { ll a = 1; while (i) { if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i /= 2; } return a; } ll gcd(ll a, ll b) { while (b) { ll c = a % b; a = b; b = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } #ifdef _MSC_VER #include <intrin.h> #endif int popcnt(unsigned long long a) { #ifndef _MSC_VER return __builtin_popcountll(a); #elif defined _WIN64 return _mm_popcnt_u64(a); #else return _mm_popcnt_u32(a >> 32) + _mm_popcnt_u32(a); a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555); a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333); a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f); a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff); a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff); return (a & 0xffffffff) + (a >> 32); #endif } int BitScanF(unsigned long long a) { #ifndef _MSC_VER return __builtin_ctzll(a); #elif defined _WIN64 unsigned long ret; _BitScanForward64(&ret, a); return a; #else unsigned long ret; if (!(unsigned long)a) { _BitScanForward(&ret, a); ret += 32; } else _BitScanForward(&ret, (unsigned long)a); return ret; #endif } int BitScanR(unsigned long long a) { #ifndef _MSC_VER return 63 - __builtin_clzll(a); #elif defined _WIN64 unsigned long ret; _BitScanReverse64(&ret, a); return a; #else unsigned long ret; if (a >> 32) { _BitScanReverse(&ret, a); ret += 32; } else _BitScanReverse(&ret, (unsigned long)a); return ret; #endif } template <class T> class matrix { public: vector<valarray<T>> obj; pair<int, int> s; public: matrix(pair<int, int> size, T e = 0) : matrix(size.first, size.second, e) {} matrix(int n, int m = -1, T e = 0) : obj(n, valarray<T>(e, m == -1 ? n : m)), s(n, m == -1 ? n : m) {} static matrix e(int n) { matrix a = (n); for (int i = 0; i < n; i++) a[i][i] = 1; return a; } matrix &operator+=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] += p.obj[i][j]; return *this; } matrix operator+(const matrix &p) { matrix res(*this); return res += p; } matrix &operator-=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] -= p.obj[i][j]; return *this; } matrix operator-(const matrix &p) { matrix res(*this); return res -= p; } matrix &operator*=(T p) { for (auto &a : obj) for (auto &b : a) b *= p; return *this; } matrix operator*(T p) { matrix res(*this); return res *= p; } matrix operator*(const matrix &p) { if (s.second != p.s.first) throw runtime_error("matrix error"); matrix ret(s.first, p.s.second); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) ret[i] += obj[i][j] * p.obj[j]; return ret; } matrix &operator*=(const matrix &p) { return *this = *this * p; } bool operator==(const matrix &p) { if (s != p.s) return 0; rep(i, s.first) rep(j, s.second) if (obj[i][j] != p.obj[i][j]) return 0; return 1; } pair<int, int> size() const { return s; } matrix &mod(T m) { for (auto &a : obj) for (auto &b : a) b %= m; return *this; } valarray<T> &operator[](int t) { return obj[t]; } void gauss() { if (size().first + 1 != size().second) return; rep(i, size().first) { int p = i; repi(j, i, size().first) if (abs(obj[j][i]) > abs(obj[p][i])) p = j; swap(obj[i], obj[p]); if (abs(obj[i][i]) < 1e-8) return; // contniue; repi(j, i + 1, size().second) obj[i][j] /= obj[i][i]; rep(j, size().first) { if (i != j) { repi(k, i + 1, size().second) obj[j][k] -= obj[j][i] * obj[i][k]; } } } } }; template <class T> inline matrix<T> pow(matrix<T> &base, unsigned long long exp) { auto base_(base); if (base_.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) res *= base_; if (res.obj[0].size() > 100) { int a = 0; } if (!(exp /= 2)) break; base_ *= base_; } return res; } template <class T> inline matrix<T> modpow(matrix<T> &base, unsigned long long exp, ll m) { auto base_(base); if (base.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) (res *= base_).mod(m); if (res.obj[0].size() > 100) { int a = 0; } if (!(exp /= 2)) break; (base_ *= base_).mod(m); } return res; } class unionfind { vector<int> par, rank, size_; // 速度ではなくメモリ効率を考えるならrankのかわりにsizeを使う public: unionfind(int n) : par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); } int find(int x) { if (par[x] == x) return x; 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[y] = x; size_[x] += size_[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return size_[find(x)]; } }; // end of lib // template<class S=void,int ptr_num, class T = char>class trie { // umap<T, trie<S, ptr_num, T> next; // public: // S key; // trie<S, ptr_num, T>* ptr[ptr_num] = {}; // trie(S &&data) :key(data) {} // trie(const S &data) :key(data) {} // void add(T x,S data) { // if (!next.find(x))next.insert(x, data); // } // trie& operator[](T x) { // return next[x]; // } // bool find(T x) { // retun next.find(x); // } // }; // template<class T=char>class AhoCorasick { // trie<pair<bool,int>, 2, T> tree; // AhoCorasick(vector<string> p) { // int num = 0; // vector<decltype(&tree)> que(p.size(),&tree); // for (int i = 0;; i++) { // bool end = 1; // int i = 0; // for (auto a : p) { // if (i >= a.size())break; // end = ;0 // que[i] = (*que[i])[a[i]]; // i++; // } // if (end)break; // } // }  // }; template <class T, class Func = function<T(T, T)>> class segtree { vector<T> obj; int offset; Func updater; T e; int bufsize(int num) { int i = 1; for (; num > i; i <<= 1) ; offset = i - 1; return (i << 1) - 1; } T query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return e; if (a <= l && r <= b) return obj[k]; else return updater(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } public: T query(int a, int b) { //[a,b) return query(a, b, 0, 0, offset + 1); } void updateall(int l = 0, int r = -1) { if (r < 0) r = offset + 1; l += offset, r += offset; if (l == 0) return; do { l = l - 1 >> 1, r = r - 1 >> 1; for (int i = l; i < r; i++) obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]); } while (l); } void update(int k, T &a) { k += offset; obj[k] = a; while (k) { k = k - 1 >> 1; obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]); } } segtree(int n, T e, const Func &updater = Func()) : obj(bufsize(n), e), e(e), updater(updater) {} segtree(vector<T> &vec, T e, const Func &updater = Func()) : obj(bufsize(vec.size()), e), e(e), updater(updater) { copy(vec.begin(), vec.end(), obj.begin() + offset); updateall(); } T &operator[](int n) { return obj[n + offset]; } }; template <class T = int> class BIT { // 多次元BITはループをネストすればいいらしい。 vector<T> bit; public: BIT(int n) : bit(n + 1, 0) {} void add(int i, T x) { i++; while (i <= bit.size()) { bit[i - 1] += x; i += i & -i; } } T sum(int i) { T s = 0; i++; while (i) { s += bit[i - 1]; i &= i - 1; } return s; } }; template <class T> class rangeadd { BIT<T> b0, b1; int n; rangeadd(int n) : b0(n), b1(n), n(n) {} void add(int l, int r, T x) { //[l,r) b0.add(l, -x(l - 1)); b1.add(l, x); b0.add(r, x * r); b1.add(r, -x); } T sum(int i) { return b0.sum(i) + b1.sum(i) * i; } }; int g(string &s, int &p); int f(string &s, int &p) { int k = 0; int m = 1; while (s[p] != ')' && s[p] != '=') { switch (s[p]) { // case '(': // p++; // k += m*f(s, p); // p++; // break; case '-': m = -1; p++; k -= g(s, p); break; case '+': m = 1; p++; k += g(s, p); break; // default: // k += m*g(s, p); } } return k; } int g(string &s, int &p) { int k = 1; bool div = 0; while (s[p] != ')' && s[p] != '+' && s[p] != '-' && s[p] != '=') { switch (s[p]) { case '(': p++; if (div) k /= f(s, p); else k *= f(s, p); p++; break; case '*': div = 0; p++; break; case '/': div = 1; p++; break; default: int num = 0; while (isdigit(s[p])) { num = num * 10 + s[p] - '0'; p++; } if (div) k /= num; else k *= num; } } return k; } int main() { int n; cin >> n; while (n--) { string s; cin >> s; int p = 0; cout << f(s, p) << endl; } } // template<class T, class map = std::unordered_map<T,unique_ptr<node>>> /*class AhoCorasick { struct node { map<char,unique_ptr<node>> next; node* fail = nullptr, *match_list = nullptr; std::vector<int> match; }root; int pattern; //template<class string> AhoCorasick(std::vector<string> &vs) :pattern(vs.size()) { root.fail = &root; for (int i = 0; i < vs.size(); i++) { node* now = &root; for (auto c : vs[i]) { if (!now->next[c])now->next[c]=make_unique<node>(); now = now->next[c].get(); } now->match.push_back(i); } std::queue<node*> que; que.push(&root); while (!que.empty()) { auto now = que.front(); que.pop(); for (auto &next : now->next) { if (!next.second)continue; if (now->fail->next.count(next.first))next.second->fail = now->fail->next[next.first].get(); else next.second->fail = now->fail->fail; //next.second->match.insert(next.second->match.end(), next.second->fail->match.begin(), next.second->fail->match.end()); if (next.second->fail->match.empty())next.second->match_list = next.second->fail->match_list; else next.second->match_list = next.second->fail; que.push(next.second.get()); } } } auto match_n(string str) { vector<int> num(pattern); } auto match_list(string str) { vector<pair<int, int>> list; auto now = &root; for (int i = 0; i < str.size(); i++) { if (now->next.count(str[i]))now = now->next[str[i]].get(); else now = now->fail; auto match = now->match_list; do { match } } } };*/
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> // cin.sync_with_stdio(false); // streambuf using namespace std; typedef long long ll; typedef double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; using vpll = vector<pll>; using ti3 = tuple<int, int, int>; using vti3 = vector<ti3>; template <class T, int s> using va = vector<array<T, s>>; template <class T, class T2> using umap = unordered_map<T, T2>; template <class T> using uset = unordered_set<T>; template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } #define ALL(a) a.begin(), a.end() #define rep(i, a) for (int i = 0; i < a; i++) #define rep1(i, a) for (int i = 1; i <= a; i++) #define rrep(i, a) for (int i = (a)-1; i >= 0; i--) #define rrep1(i, a) for (int i = a; i; i--) #define repi(i, a, b) for (int i = a; i < b; i++) const ll mod = 1000000007; template <class T> using heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>; template <class T> inline void hash_combine(size_t &seed, const T &v) { hash<T> hasher; seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 6) + (seed >> 2); } namespace std { template <typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> &v) const { size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } }; // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; } // namespace std template <class T> int id(vector<T> &a, T b) { return lower_bound(ALL(a), b) - a.begin(); } ll pow(ll base, ll i, ll mod) { ll a = 1; while (i) { if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i /= 2; } return a; } ll gcd(ll a, ll b) { while (b) { ll c = a % b; a = b; b = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } #ifdef _MSC_VER #include <intrin.h> #endif int popcnt(unsigned long long a) { #ifndef _MSC_VER return __builtin_popcountll(a); #elif defined _WIN64 return _mm_popcnt_u64(a); #else return _mm_popcnt_u32(a >> 32) + _mm_popcnt_u32(a); a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555); a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333); a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f); a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff); a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff); return (a & 0xffffffff) + (a >> 32); #endif } int BitScanF(unsigned long long a) { #ifndef _MSC_VER return __builtin_ctzll(a); #elif defined _WIN64 unsigned long ret; _BitScanForward64(&ret, a); return a; #else unsigned long ret; if (!(unsigned long)a) { _BitScanForward(&ret, a); ret += 32; } else _BitScanForward(&ret, (unsigned long)a); return ret; #endif } int BitScanR(unsigned long long a) { #ifndef _MSC_VER return 63 - __builtin_clzll(a); #elif defined _WIN64 unsigned long ret; _BitScanReverse64(&ret, a); return a; #else unsigned long ret; if (a >> 32) { _BitScanReverse(&ret, a); ret += 32; } else _BitScanReverse(&ret, (unsigned long)a); return ret; #endif } template <class T> class matrix { public: vector<valarray<T>> obj; pair<int, int> s; public: matrix(pair<int, int> size, T e = 0) : matrix(size.first, size.second, e) {} matrix(int n, int m = -1, T e = 0) : obj(n, valarray<T>(e, m == -1 ? n : m)), s(n, m == -1 ? n : m) {} static matrix e(int n) { matrix a = (n); for (int i = 0; i < n; i++) a[i][i] = 1; return a; } matrix &operator+=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] += p.obj[i][j]; return *this; } matrix operator+(const matrix &p) { matrix res(*this); return res += p; } matrix &operator-=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] -= p.obj[i][j]; return *this; } matrix operator-(const matrix &p) { matrix res(*this); return res -= p; } matrix &operator*=(T p) { for (auto &a : obj) for (auto &b : a) b *= p; return *this; } matrix operator*(T p) { matrix res(*this); return res *= p; } matrix operator*(const matrix &p) { if (s.second != p.s.first) throw runtime_error("matrix error"); matrix ret(s.first, p.s.second); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) ret[i] += obj[i][j] * p.obj[j]; return ret; } matrix &operator*=(const matrix &p) { return *this = *this * p; } bool operator==(const matrix &p) { if (s != p.s) return 0; rep(i, s.first) rep(j, s.second) if (obj[i][j] != p.obj[i][j]) return 0; return 1; } pair<int, int> size() const { return s; } matrix &mod(T m) { for (auto &a : obj) for (auto &b : a) b %= m; return *this; } valarray<T> &operator[](int t) { return obj[t]; } void gauss() { if (size().first + 1 != size().second) return; rep(i, size().first) { int p = i; repi(j, i, size().first) if (abs(obj[j][i]) > abs(obj[p][i])) p = j; swap(obj[i], obj[p]); if (abs(obj[i][i]) < 1e-8) return; // contniue; repi(j, i + 1, size().second) obj[i][j] /= obj[i][i]; rep(j, size().first) { if (i != j) { repi(k, i + 1, size().second) obj[j][k] -= obj[j][i] * obj[i][k]; } } } } }; template <class T> inline matrix<T> pow(matrix<T> &base, unsigned long long exp) { auto base_(base); if (base_.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) res *= base_; if (res.obj[0].size() > 100) { int a = 0; } if (!(exp /= 2)) break; base_ *= base_; } return res; } template <class T> inline matrix<T> modpow(matrix<T> &base, unsigned long long exp, ll m) { auto base_(base); if (base.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) (res *= base_).mod(m); if (res.obj[0].size() > 100) { int a = 0; } if (!(exp /= 2)) break; (base_ *= base_).mod(m); } return res; } class unionfind { vector<int> par, rank, size_; // 速度ではなくメモリ効率を考えるならrankのかわりにsizeを使う public: unionfind(int n) : par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); } int find(int x) { if (par[x] == x) return x; 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[y] = x; size_[x] += size_[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return size_[find(x)]; } }; // end of lib // template<class S=void,int ptr_num, class T = char>class trie { // umap<T, trie<S, ptr_num, T> next; // public: // S key; // trie<S, ptr_num, T>* ptr[ptr_num] = {}; // trie(S &&data) :key(data) {} // trie(const S &data) :key(data) {} // void add(T x,S data) { // if (!next.find(x))next.insert(x, data); // } // trie& operator[](T x) { // return next[x]; // } // bool find(T x) { // retun next.find(x); // } // }; // template<class T=char>class AhoCorasick { // trie<pair<bool,int>, 2, T> tree; // AhoCorasick(vector<string> p) { // int num = 0; // vector<decltype(&tree)> que(p.size(),&tree); // for (int i = 0;; i++) { // bool end = 1; // int i = 0; // for (auto a : p) { // if (i >= a.size())break; // end = ;0 // que[i] = (*que[i])[a[i]]; // i++; // } // if (end)break; // } // }  // }; template <class T, class Func = function<T(T, T)>> class segtree { vector<T> obj; int offset; Func updater; T e; int bufsize(int num) { int i = 1; for (; num > i; i <<= 1) ; offset = i - 1; return (i << 1) - 1; } T query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return e; if (a <= l && r <= b) return obj[k]; else return updater(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } public: T query(int a, int b) { //[a,b) return query(a, b, 0, 0, offset + 1); } void updateall(int l = 0, int r = -1) { if (r < 0) r = offset + 1; l += offset, r += offset; if (l == 0) return; do { l = l - 1 >> 1, r = r - 1 >> 1; for (int i = l; i < r; i++) obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]); } while (l); } void update(int k, T &a) { k += offset; obj[k] = a; while (k) { k = k - 1 >> 1; obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]); } } segtree(int n, T e, const Func &updater = Func()) : obj(bufsize(n), e), e(e), updater(updater) {} segtree(vector<T> &vec, T e, const Func &updater = Func()) : obj(bufsize(vec.size()), e), e(e), updater(updater) { copy(vec.begin(), vec.end(), obj.begin() + offset); updateall(); } T &operator[](int n) { return obj[n + offset]; } }; template <class T = int> class BIT { // 多次元BITはループをネストすればいいらしい。 vector<T> bit; public: BIT(int n) : bit(n + 1, 0) {} void add(int i, T x) { i++; while (i <= bit.size()) { bit[i - 1] += x; i += i & -i; } } T sum(int i) { T s = 0; i++; while (i) { s += bit[i - 1]; i &= i - 1; } return s; } }; template <class T> class rangeadd { BIT<T> b0, b1; int n; rangeadd(int n) : b0(n), b1(n), n(n) {} void add(int l, int r, T x) { //[l,r) b0.add(l, -x(l - 1)); b1.add(l, x); b0.add(r, x * r); b1.add(r, -x); } T sum(int i) { return b0.sum(i) + b1.sum(i) * i; } }; int g(string &s, int &p); int f(string &s, int &p) { int k = 0; int m = 1; while (s[p] != ')' && s[p] != '=') { switch (s[p]) { // case '(': // p++; // k += m*f(s, p); // p++; // break; case '-': m = -1; p++; k -= g(s, p); break; case '+': m = 1; p++; k += g(s, p); break; default: k += m * g(s, p); } } return k; } int g(string &s, int &p) { int k = 1; bool div = 0; while (s[p] != ')' && s[p] != '+' && s[p] != '-' && s[p] != '=') { switch (s[p]) { case '(': p++; if (div) k /= f(s, p); else k *= f(s, p); p++; break; case '*': div = 0; p++; break; case '/': div = 1; p++; break; default: int num = 0; while (isdigit(s[p])) { num = num * 10 + s[p] - '0'; p++; } if (div) k /= num; else k *= num; } } return k; } int main() { int n; cin >> n; while (n--) { string s; cin >> s; int p = 0; cout << f(s, p) << endl; } } // template<class T, class map = std::unordered_map<T,unique_ptr<node>>> /*class AhoCorasick { struct node { map<char,unique_ptr<node>> next; node* fail = nullptr, *match_list = nullptr; std::vector<int> match; }root; int pattern; //template<class string> AhoCorasick(std::vector<string> &vs) :pattern(vs.size()) { root.fail = &root; for (int i = 0; i < vs.size(); i++) { node* now = &root; for (auto c : vs[i]) { if (!now->next[c])now->next[c]=make_unique<node>(); now = now->next[c].get(); } now->match.push_back(i); } std::queue<node*> que; que.push(&root); while (!que.empty()) { auto now = que.front(); que.pop(); for (auto &next : now->next) { if (!next.second)continue; if (now->fail->next.count(next.first))next.second->fail = now->fail->next[next.first].get(); else next.second->fail = now->fail->fail; //next.second->match.insert(next.second->match.end(), next.second->fail->match.begin(), next.second->fail->match.end()); if (next.second->fail->match.empty())next.second->match_list = next.second->fail->match_list; else next.second->match_list = next.second->fail; que.push(next.second.get()); } } } auto match_n(string str) { vector<int> num(pattern); } auto match_list(string str) { vector<pair<int, int>> list; auto now = &root; for (int i = 0; i < str.size(); i++) { if (now->next.count(str[i]))now = now->next[str[i]].get(); else now = now->fail; auto match = now->match_list; do { match } } } };*/
replace
466
468
466
468
TLE
p00109
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define pb(n) push_back(n) #define mp(n, m) make_pair(n, m) #define fi first #define se second #define all(r) (r).begin(), (r).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define repc(i, a, b) for (int i = (a); i < (b); i++) typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int IMAX = ((1 << 30) - 1) * 2 + 1; const double EPS = 1e-10; // int mod=1000000007 string s; int calc(int b, int e) { vector<int> v; vector<char> ope; for (int i = b; i <= e; i++) { if (s[i] == '(') { int o = 1, c = 0; for (int j = i + 1; j <= e; j++) { if (s[j] == '(') o++; if (s[j] == ')') { c++; if (o == c) { int a = calc(i + 1, j + 1); v.pb(a); i = j; break; } } } } else if (s[i] >= '0' && s[i] <= '9') { for (int j = i + 1; j <= e; j++) { if (s[j] < '0' || s[j] > '9') { string t = s.substr(i, j); v.pb(atoi(t.c_str())); i = j - 1; break; } } } else { ope.pb(s[i]); } } for (int i = 0; i < ope.size(); i++) { if (ope[i] == '*') { v[i + 1] = v[i] * v[i + 1]; v[i] = 0; ope[i] = (i == 0 ? '+' : ope[i - 1]); } if (ope[i] == '/') { v[i + 1] = v[i] / v[i + 1]; v[i] = 0; ope[i] = (i == 0 ? '+' : ope[i - 1]); } } for (int i = 0; i < ope.size() - 1; i++) { if (ope[i] == '+') { v[0] += v[i + 1]; } if (ope[i] == '-') { v[0] -= v[i + 1]; } } return v[0]; } int main() { int n; cin >> n; rep(i, n) { cin >> s; cout << calc(0, s.size() - 1) << endl; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define pb(n) push_back(n) #define mp(n, m) make_pair(n, m) #define fi first #define se second #define all(r) (r).begin(), (r).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define repc(i, a, b) for (int i = (a); i < (b); i++) typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int IMAX = ((1 << 30) - 1) * 2 + 1; const double EPS = 1e-10; // int mod=1000000007 string s; int calc(int b, int e) { vector<int> v; vector<char> ope; for (int i = b; i <= e; i++) { if (s[i] == '(') { int o = 1, c = 0; for (int j = i + 1; j <= e; j++) { if (s[j] == '(') o++; if (s[j] == ')') { c++; if (o == c) { int a = calc(i + 1, j); v.pb(a); i = j; break; } } } } else if (s[i] >= '0' && s[i] <= '9') { for (int j = i + 1; j <= e; j++) { if (s[j] < '0' || s[j] > '9') { string t = s.substr(i, j); v.pb(atoi(t.c_str())); i = j - 1; break; } } } else { ope.pb(s[i]); } } for (int i = 0; i < ope.size(); i++) { if (ope[i] == '*') { v[i + 1] = v[i] * v[i + 1]; v[i] = 0; ope[i] = (i == 0 ? '+' : ope[i - 1]); } if (ope[i] == '/') { v[i + 1] = v[i] / v[i + 1]; v[i] = 0; ope[i] = (i == 0 ? '+' : ope[i - 1]); } } for (int i = 0; i < ope.size() - 1; i++) { if (ope[i] == '+') { v[0] += v[i + 1]; } if (ope[i] == '-') { v[0] -= v[i + 1]; } } return v[0]; } int main() { int n; cin >> n; rep(i, n) { cin >> s; cout << calc(0, s.size() - 1) << endl; } }
replace
49
50
49
50
0
p00109
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int calc(char *s, char *end) { /*for (char *p = s; p != end; p++) printf("%c", *p); puts("");*/ bool f = false; vector<int> st; vector<char> op; char *p = s; while (p != end) { if (!f) if (*p == '(') { char *e; for (e = p + 1; *e != ')'; e++) ; st.push_back(calc(p + 1, e)); p = e; } if (f) if (*p == '+' || *p == '-' || *p == '*' || *p == '/') { op.push_back(*p); p++; f ^= 1; continue; } if (!f) if (*p >= '0' && *p <= '9') { int n = atoi(p); st.push_back(n); stringstream ss; ss << n; p += ss.str().size() - 1; } if (!f) if (*p == '-') { int n = atoi(p); st.push_back(n); stringstream ss; ss << n; p += ss.str().size() - 1; // printf("%s\n", ss.str().c_str()); } if (op.size() && (op.back() == '*' || op.back() == '/')) { int b = st.back(); st.pop_back(); int a = st.back(); st.pop_back(); if (op.back() == '*') a *= b; if (op.back() == '/') a /= b; st.push_back(a); op.pop_back(); } p++; f = !f; /*printf("__"); for (auto a : st) printf("%d ", a); puts("");*/ } while (op.size()) { int t = st.front(); st.erase(st.begin()); /*if (st.size() == 0){ if (op.front() == '-') st.push_back(-t); else st.push_back(t); op.erase(op.begin()); break; }*/ if (op.front() == '+') st[0] = t + st[0]; if (op.front() == '-') st[0] = t - st[0]; op.erase(op.begin()); } return st[0]; } int main() { int n; char str[1024]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", str); str[strlen(str) - 1] = '\0'; printf("%d\n", calc(str, str + strlen(str))); } return 0; }
#include <bits/stdc++.h> using namespace std; int calc(char *s, char *end) { /*for (char *p = s; p != end; p++) printf("%c", *p); puts("");*/ bool f = false; vector<int> st; vector<char> op; char *p = s; while (p != end) { if (!f) if (*p == '(') { char *e = p + 1; int cnt = 1; while (true) { if (*e == '(') cnt++; if (*e == ')') cnt--; if (cnt == 0) break; e++; } st.push_back(calc(p + 1, e)); p = e; } if (f) if (*p == '+' || *p == '-' || *p == '*' || *p == '/') { op.push_back(*p); p++; f ^= 1; continue; } if (!f) if (*p >= '0' && *p <= '9') { int n = atoi(p); st.push_back(n); stringstream ss; ss << n; p += ss.str().size() - 1; } if (!f) if (*p == '-') { int n = atoi(p); st.push_back(n); stringstream ss; ss << n; p += ss.str().size() - 1; // printf("%s\n", ss.str().c_str()); } if (op.size() && (op.back() == '*' || op.back() == '/')) { int b = st.back(); st.pop_back(); int a = st.back(); st.pop_back(); if (op.back() == '*') a *= b; if (op.back() == '/') a /= b; st.push_back(a); op.pop_back(); } p++; f = !f; /*printf("__"); for (auto a : st) printf("%d ", a); puts("");*/ } while (op.size()) { int t = st.front(); st.erase(st.begin()); /*if (st.size() == 0){ if (op.front() == '-') st.push_back(-t); else st.push_back(t); op.erase(op.begin()); break; }*/ if (op.front() == '+') st[0] = t + st[0]; if (op.front() == '-') st[0] = t - st[0]; op.erase(op.begin()); } return st[0]; } int main() { int n; char str[1024]; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", str); str[strlen(str) - 1] = '\0'; printf("%d\n", calc(str, str + strlen(str))); } return 0; }
replace
16
19
16
27
0
p00109
C++
Runtime Error
#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; }
insert
55
55
55
56
0
p00110
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> using namespace std; typedef long long ll; using vi = vector<int>; string str; string stra; int id = 0; void pt(vector<int> v) { for (int r = 0; r < v.size(); r++) { cout << v[r]; } cout << endl; } vi number() { vi res; for (; '0' <= stra[id] && stra[id] <= '9'; id++) { res.push_back(stra[id] - '0'); } reverse(res.begin(), res.end()); return res; } int solve() { stra = str; for (int i = 9; i >= 0; i--) { id = 0; for (int j = 0; j < (int)str.size(); j++) { if (str[j] == 'X') { stra[j] = i + '0'; } } vi a = number(); if (stra[id] == '+') { id++; } vi b = number(); // a.resize(max(b.size() + 1,a.size()+1),0); // a.push_back(0); // while(a.size()<=b.size()){ // a.push_back(0); // } vi ab; int q; for (q = 0; q < a.size() && q < b.size(); q++) { ab.push_back(a[q] + b[q]); } for (; q < a.size(); q++) { ab.push_back(a[q]); } for (; q < b.size(); q++) { ab.push_back(b[q]); } ab.push_back(0); // for (int j = 0; j < (int)b.size(); j++) { // a[j] += b[j]; // // if (a[j]>=10) { // // a[j] -= 10; // // a[j+1]++; // // } // } // for (int j = b.size(); j < (int)(a.size()-1); j++) { // if (a[j] >= 10) { // a[j] -= 10; // a[j+1]++; // } // } for (int j = 0; j < (int)ab.size() - 1; j++) { if (ab[j] >= 10) { ab[j] -= 10; ab[j + 1]++; } } while (ab[ab.size() - 1] == 0) { ab.pop_back(); } a = ab; // pt(a); // int j; // int k=0; // int l=0; // vi ab; // for(j=0;j<int(a.size())&&j<int(b.size());j++){ // k=0; // if(a[j]+b[j]+l>=10){ // k++; // ab.push_back(a[j]+b[j]+l-10); // } // else{ // ab.push_back(a[j]+b[j]+l); // } // l=k; // } // for(;j<int(a.size());j++){ // k=0; // if(a[j]+l>=10){ // k++; // ab.push_back(a[j]+l-10); // } // else{ // ab.push_back(a[j]+l); // } // l=k; // } // for(;j<int(b.size());j++){ // k=0; // if(b[j]+l>=10){ // k++; // ab.push_back(b[j]+l-10); // } // else{ // ab.push_back(b[j]+l); // } // l=k; // } // if(l>0){ // ab.push_back(l); // } // a=ab; ///////// if (stra[id] == '=') { id++; } vi c = number(); if (a.size() != c.size()) { continue; } int flag = 0; for (int j = 0; j < (int)a.size(); j++) { if (a[j] == c[j]) { flag++; } } if (flag == (int)a.size()) { return i; } } return -1; } int main() { cin >> str; while (str.size()) { int ans = solve(); if (ans >= 0) { cout << ans << endl; } else { cout << "NA" << endl; } str = ""; cin >> str; } return 0; }
#include <algorithm> #include <cstring> #include <iostream> #include <math.h> #include <stdio.h> #include <string> #include <vector> using namespace std; typedef long long ll; using vi = vector<int>; string str; string stra; int id = 0; void pt(vector<int> v) { for (int r = 0; r < v.size(); r++) { cout << v[r]; } cout << endl; } vi number() { vi res; for (; '0' <= stra[id] && stra[id] <= '9'; id++) { res.push_back(stra[id] - '0'); } reverse(res.begin(), res.end()); return res; } int solve() { stra = str; for (int i = 9; i >= 0; i--) { id = 0; for (int j = 0; j < (int)str.size(); j++) { if (str[j] == 'X') { stra[j] = i + '0'; } } vi a = number(); if (stra[id] == '+') { id++; } vi b = number(); // a.resize(max(b.size() + 1,a.size()+1),0); // a.push_back(0); // while(a.size()<=b.size()){ // a.push_back(0); // } vi ab; int q; for (q = 0; q < a.size() && q < b.size(); q++) { ab.push_back(a[q] + b[q]); } for (; q < a.size(); q++) { ab.push_back(a[q]); } for (; q < b.size(); q++) { ab.push_back(b[q]); } ab.push_back(0); // for (int j = 0; j < (int)b.size(); j++) { // a[j] += b[j]; // // if (a[j]>=10) { // // a[j] -= 10; // // a[j+1]++; // // } // } // for (int j = b.size(); j < (int)(a.size()-1); j++) { // if (a[j] >= 10) { // a[j] -= 10; // a[j+1]++; // } // } for (int j = 0; j < (int)ab.size() - 1; j++) { if (ab[j] >= 10) { ab[j] -= 10; ab[j + 1]++; } } if (ab[ab.size() - 1] == 0) { ab.pop_back(); } a = ab; // pt(a); // int j; // int k=0; // int l=0; // vi ab; // for(j=0;j<int(a.size())&&j<int(b.size());j++){ // k=0; // if(a[j]+b[j]+l>=10){ // k++; // ab.push_back(a[j]+b[j]+l-10); // } // else{ // ab.push_back(a[j]+b[j]+l); // } // l=k; // } // for(;j<int(a.size());j++){ // k=0; // if(a[j]+l>=10){ // k++; // ab.push_back(a[j]+l-10); // } // else{ // ab.push_back(a[j]+l); // } // l=k; // } // for(;j<int(b.size());j++){ // k=0; // if(b[j]+l>=10){ // k++; // ab.push_back(b[j]+l-10); // } // else{ // ab.push_back(b[j]+l); // } // l=k; // } // if(l>0){ // ab.push_back(l); // } // a=ab; ///////// if (stra[id] == '=') { id++; } vi c = number(); if (a.size() != c.size()) { continue; } int flag = 0; for (int j = 0; j < (int)a.size(); j++) { if (a[j] == c[j]) { flag++; } } if (flag == (int)a.size()) { return i; } } return -1; } int main() { cin >> str; while (str.size()) { int ans = solve(); if (ans >= 0) { cout << ans << endl; } else { cout << "NA" << endl; } str = ""; cin >> str; } return 0; }
replace
86
87
86
87
0
p00111
C++
Runtime Error
#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; }
insert
28
28
28
29
0
p00111
C++
Runtime Error
#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++) { 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; }
insert
93
93
93
95
0
p00112
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; int main(void) { while (1) { int n, t[100], w; w = 0; cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> t[i]; } sort(t, t + n); for (int i = 0; i < n; i++) { w += t[i] * (n - 1 - i); } cout << w << endl; } return 0; }
#include <algorithm> #include <iostream> using namespace std; int main(void) { while (1) { int n, t[10005]; long long int w; w = 0; cin >> n; if (n == 0) { break; } for (int i = 0; i < n; i++) { cin >> t[i]; } sort(t, t + n); for (int i = 0; i < n; i++) { w += t[i] * (n - 1 - i); } cout << w << endl; } return 0; }
replace
5
6
5
7
0
p00112
C++
Runtime Error
#include <iostream> using namespace std; int n, m, temp; int t[100]; int sum; int main() { while (1) { cin >> n; if (n == 0) break; m = n; while (m--) { cin >> t[m]; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (t[j] > t[j + 1]) { temp = t[j]; t[j] = t[j + 1]; t[j + 1] = temp; } } } sum = 0; for (int i = 0; i < n - 1; i++) { sum += (i + 1) * t[n - 2 - i]; } cout << sum << endl; } return 0; }
#include <iostream> using namespace std; int n, m, temp; int t[10005]; long long int sum; int main() { while (1) { cin >> n; if (n == 0) break; m = n; while (m--) { cin >> t[m]; } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - 1 - i; j++) { if (t[j] > t[j + 1]) { temp = t[j]; t[j] = t[j + 1]; t[j + 1] = temp; } } } sum = 0; for (int i = 0; i < n - 1; i++) { sum += (i + 1) * t[n - 2 - i]; } cout << sum << endl; } return 0; }
replace
4
6
4
6
0
p00112
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n, n) { vector<int> ts; for (int i = 0; i < n; i++) { cin >> ts[i]; } sort(ts.begin(), ts.end()); long long sum = 0, ans = 0; for (int i = 0; i < n; i++) { ans += sum; sum += ts[i]; } cout << ans << endl; } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n, n) { vector<int> ts(n); for (int i = 0; i < n; i++) { cin >> ts[i]; } sort(ts.begin(), ts.end()); long long sum = 0, ans = 0; for (int i = 0; i < n; i++) { ans += sum; sum += ts[i]; } cout << ans << endl; } }
replace
7
8
7
8
-11
p00112
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n, n) { int need_time[500] = {0}, total_time[500] = {0}, total = 0; for (int i = 0; i < n; i++) { cin >> need_time[i]; } sort(need_time, need_time + n); for (int i = 1; i < n; i++) { total_time[i] = need_time[i - 1] + total_time[i - 1]; total += total_time[i]; } cout << total << endl; } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <vector> using namespace std; int main() { int n; while (cin >> n, n) { long long need_time[10100] = {0}, total_time[10100] = {0}, total = 0; for (int i = 0; i < n; i++) { cin >> need_time[i]; } sort(need_time, need_time + n); for (int i = 1; i < n; i++) { total_time[i] = need_time[i - 1] + total_time[i - 1]; total += total_time[i]; } cout << total << endl; } return 0; }
replace
8
9
8
9
0
p00112
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; int main() { int n, time[1000]; long long ans = 0; while (cin >> n) { if (n == 0) break; int wait = 0; for (int i = 0; i < n; i++) cin >> time[i]; sort(time, time + n); for (int i = 0; i < n; i++) { ans += wait; wait += time[i]; } cout << ans << endl; ans = 0; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int main() { int n, time[10000]; long long ans = 0; while (cin >> n) { if (n == 0) break; int wait = 0; for (int i = 0; i < n; i++) cin >> time[i]; sort(time, time + n); for (int i = 0; i < n; i++) { ans += wait; wait += time[i]; } cout << ans << endl; ans = 0; } return 0; }
replace
6
7
6
7
0
p00114
C++
Time Limit Exceeded
#include <iostream> using namespace std; typedef long long ll; ll GCD(ll a, ll 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]) { break; } int val[3] = {1, 1, 1}, move[3]; int cnt = 0; for (int i = 1;; ++i) { for (int j = 0; j < 3; ++j) { if (move[j]) continue; val[j] = a[j] * val[j] % m[j]; if (val[j] == 1) { move[j] = i; cnt++; } } if (cnt >= 3) 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) { 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]) { break; } int val[3] = {1, 1, 1}, move[3] = {0}; int cnt = 0; for (int i = 1;; ++i) { for (int j = 0; j < 3; ++j) { if (move[j]) continue; val[j] = a[j] * val[j] % m[j]; if (val[j] == 1) { move[j] = i; cnt++; } } if (cnt >= 3) break; } ll ans = LCM(LCM(move[0], move[1]), move[2]); cout << ans << endl; } }
replace
12
13
12
13
TLE
p00114
C++
Runtime Error
// x^t = 1 mod m のとき、 // x^(kt) = 1 mod m. #include <iostream> using namespace std; typedef long long ll; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } int main() { ll a1, m1, a2, m2, a3, m3; while (1) { cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3; ll x = 1, y = 1, z = 1; ll tmp = a1; while (tmp != 1) { x++; tmp = (tmp * a1) % m1; } tmp = a2; while (tmp != 1) { y++; tmp = (tmp * a2) % m2; } tmp = a3; while (tmp != 1) { z++; tmp = (tmp * a3) % m3; } ll ans = lcm(lcm(x, y), z); cout << ans << endl; } return 0; }
// x^t = 1 mod m のとき、 // x^(kt) = 1 mod m. #include <iostream> using namespace std; typedef long long ll; ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } int main() { ll a1, m1, a2, m2, a3, m3; while (1) { cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3; if (a1 + m1 + a2 + m2 + a3 + m3 == 0) break; ll x = 1, y = 1, z = 1; ll tmp = a1; while (tmp != 1) { x++; tmp = (tmp * a1) % m1; } tmp = a2; while (tmp != 1) { y++; tmp = (tmp * a2) % m2; } tmp = a3; while (tmp != 1) { z++; tmp = (tmp * a3) % m3; } ll ans = lcm(lcm(x, y), z); cout << ans << endl; } return 0; }
insert
16
16
16
19
-8
p00114
C++
Time Limit Exceeded
#include <iostream> #define int long long using namespace std; int cou(int a, int m); int ggd(int number1, int number2); int lcm(int number1, int number2); signed main() { int a1, a2, a3, m1, m2, m3; int x, y, z; int count; int countx, county, countz; while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1 || a2 || a3 || m1 || m2 || m3) { x = 1; y = 1; z = 1; count = 0; countx = cou(a1, m1); county = cou(a2, m2); countz = cou(a3, m3); cout << lcm(lcm(countx, county), countz) << endl; } return 0; } int cou(int a, int m) { int x = 1; int count = 0; do { count++; x = (a * x) % m; } while (x != 1); return count; } int ggd(int number1, int number2) { int m = number1; int n = number2; if (number2 > number1) { m = number2; n = number1; } while (m != n) { int temp = n; n = m - n; m = temp; } return m; } int lcm(int number1, int number2) { return number1 * number2 / ggd(number1, number2); }
#include <iostream> #define int long long using namespace std; int cou(int a, int m); int ggd(int number1, int number2); int lcm(int number1, int number2); signed main() { int a1, a2, a3, m1, m2, m3; int x, y, z; int count; int countx, county, countz; while (cin >> a1 >> m1 >> a2 >> m2 >> a3 >> m3, a1 || a2 || a3 || m1 || m2 || m3) { x = 1; y = 1; z = 1; count = 0; countx = cou(a1, m1); county = cou(a2, m2); countz = cou(a3, m3); cout << lcm(lcm(countx, county), countz) << endl; } return 0; } int cou(int a, int m) { int x = 1; int count = 0; do { count++; x = (a * x) % m; } while (x != 1); return count; } int ggd(int number1, int number2) { int m = number1; int n = number2; if (number2 > number1) { m = number2; n = number1; } while (n != 0) { int temp = m % n; m = n; n = temp; } return m; } int lcm(int number1, int number2) { return number1 * number2 / ggd(number1, number2); }
replace
49
53
49
53
TLE
p00114
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); i++) namespace std { template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; } } // namespace std int main() { long long cyc[3], a[3], m[3]; #define in(i) , &a[i], &m[i] #define o(i) | a[i] | m[i] while (scanf("%lld%lld%lld%lld%lld%lld" in(0)in(1)in(2)), 1 o(0) o(1) o(2)) { rep(i, 3) { cyc[i] = 0; int cnt = 1, k = 1; while (1) { k *= a[i]; k %= m[i]; if (k == 1) { cyc[i] = cnt; break; } cnt++; } } cout << lcm(lcm(cyc[0], cyc[1]), cyc[2]) << endl; } }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); i++) namespace std { template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; } } // namespace std int main() { long long cyc[3], a[3], m[3]; #define in(i) , &a[i], &m[i] #define o(i) | a[i] | m[i] while (scanf("%lld%lld%lld%lld%lld%lld" in(0)in(1)in(2)), 0 o(0) o(1) o(2)) { rep(i, 3) { cyc[i] = 0; int cnt = 1, k = 1; while (1) { k *= a[i]; k %= m[i]; if (k == 1) { cyc[i] = cnt; break; } cnt++; } } cout << lcm(lcm(cyc[0], cyc[1]), cyc[2]) << endl; } }
replace
18
19
18
19
-8
p00114
C++
Runtime Error
#include <iostream> using namespace std; long long a[3], m[3], k[3]; long long gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } int main() { while (cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2]) { for (int i = 0; i < 3; i++) { long long cnt = 0, x = 1; while (true) { if (x == 1 && cnt >= 1) { break; } x *= a[i]; x %= m[i]; cnt++; } k[i] = cnt; } int LCM = k[0] * k[1] / gcd(k[0], k[1]); cout << LCM * k[2] / gcd(LCM, k[2]) << endl; } return 0; }
#include <iostream> using namespace std; long long a[3], m[3], k[3]; long long gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } int main() { while (true) { cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2]; if (a[0] == 0 && m[0] == 0 && a[1] == 0 && m[1] == 0 && a[2] == 0 && m[2] == 0) { break; } for (int i = 0; i < 3; i++) { long long cnt = 0, x = 1; while (true) { if (x == 1 && cnt >= 1) { break; } x *= a[i]; x %= m[i]; cnt++; } k[i] = cnt; } int LCM = k[0] * k[1] / gcd(k[0], k[1]); cout << LCM * k[2] / gcd(LCM, k[2]) << endl; } return 0; }
replace
10
11
10
16
-8
p00116
C++
Time Limit Exceeded
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> PII; static const double EPS = 1e-5; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) int main(void) { int H, W; while (cin >> H >> W) { if (H == 0) break; vector<string> grid(H); int sum[501][501] = {0}; REP(i, H) { cin >> grid[i]; } REP(y, H) REP(x, W) { sum[y + 1][x + 1] = sum[y + 1][x] + sum[y][x + 1] - sum[y][x]; sum[y + 1][x + 1] += (grid[y][x] == '*') ? 1 : 0; } int ans = 0; REP(y1, H) REP(x1, W) FOR(y2, y1 + 1, H) FOR(x2, x1 + 1, W) { if (sum[x2 + 1][y2 + 1] - sum[x1][y2 + 1] - sum[x2 + 1][y1] + sum[x1][y1] == 0) { ans = max((x2 - x1 + 1) * (y2 - y1 + 1), ans); } } cout << ans << endl; } return 0; }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long LL; typedef pair<int, int> PII; static const double EPS = 1e-5; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) int main(void) { int H, W; while (cin >> H >> W) { if (H == 0) break; vector<string> grid(H); int sum[501][501] = {0}; REP(i, H) { cin >> grid[i]; } REP(y, H) REP(x, W) { sum[y + 1][x + 1] = sum[y + 1][x] + sum[y][x + 1] - sum[y][x]; sum[y + 1][x + 1] += (grid[y][x] == '*') ? 1 : 0; } int ans = 0; /* REP(y1,H)REP(x1,W)FOR(y2,y1+1,H)FOR(x2,x1+1,W){ if(sum[x2+1][y2+1]-sum[x1][y2+1] -sum[x2+1][y1]+sum[x1][y1]==0){ ans = max((x2-x1+1)*(y2-y1+1),ans); } } */ bool f = false; FOR(dy, 1, H + 1) { FOR(dx, 1, W + 1) { f = false; FOR(y, dy - 1, H) { FOR(x, dx - 1, W) { if (sum[y + 1][x + 1] - sum[y + 1 - dy][x + 1] - sum[y + 1][x + 1 - dx] + sum[y + 1 - dy][x + 1 - dx] == 0) { f = true; break; } } if (f) break; } if (f) ans = max(ans, dx * dy); else break; } } cout << ans << endl; } return 0; }
replace
42
47
42
70
TLE
p00116
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int getS(vector<int> &H) { int N = H.size(); vector<int> L(N), R(N); stack<int> S; for (int i = 0; i < N; i++) { while (!S.empty() && H[i] <= H[S.top()]) S.pop(); L[i] = S.empty() ? 0 : (S.top() + 1); S.push(i); } S = stack<int>(); for (int i = N - 1; i >= 0; i--) { while (!S.empty() && H[i] <= H[S.top()]) S.pop(); R[i] = S.empty() ? N : S.top(); S.push(i); } int ma = 0; for (int i = 0; i < N; i++) ma = max(ma, (R[i] - L[i]) * H[i]); return ma; } int H, W; vector<int> V[300]; void solve() { fill_n(V, H, vector<int>(W, 0)); int ma = 0; for (int i = 0; i < H; i++) { char fld[510]; cin >> fld; for (int j = 0; j < W; j++) { if (fld[j] == '*') continue; V[i][j] = (i ? V[i - 1][j] : 0) + 1; } ma = max(ma, getS(V[i])); } cout << ma << endl; } signed main() { while (cin >> H >> W, H || W) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int getS(vector<int> &H) { int N = H.size(); vector<int> L(N), R(N); stack<int> S; for (int i = 0; i < N; i++) { while (!S.empty() && H[i] <= H[S.top()]) S.pop(); L[i] = S.empty() ? 0 : (S.top() + 1); S.push(i); } S = stack<int>(); for (int i = N - 1; i >= 0; i--) { while (!S.empty() && H[i] <= H[S.top()]) S.pop(); R[i] = S.empty() ? N : S.top(); S.push(i); } int ma = 0; for (int i = 0; i < N; i++) ma = max(ma, (R[i] - L[i]) * H[i]); return ma; } int H, W; vector<int> V[500]; void solve() { fill_n(V, H, vector<int>(W, 0)); int ma = 0; for (int i = 0; i < H; i++) { char fld[510]; cin >> fld; for (int j = 0; j < W; j++) { if (fld[j] == '*') continue; V[i][j] = (i ? V[i - 1][j] : 0) + 1; } ma = max(ma, getS(V[i])); } cout << ma << endl; } signed main() { while (cin >> H >> W, H || W) solve(); return 0; }
replace
28
29
28
29
0
p00116
C++
Memory Limit Exceeded
#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)); WH data[3][502][10000]; int data_max = 0; int height_sum[502][502], width_sum[502][502]; int data_pt[3][502]; WH wh; int main() { while (std::cin >> h >> w, w + h != 0) { data_max = 0; memset(data_pt, 0, sizeof(data_pt)); 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] == '.'); 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_pt[i][l] = 0; 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_pt[YNUM(i - 1)][l]; ++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], data[i][l] + data_pt[i][l], wh) == data[i][l] + data_pt[i][l]) { data[i][l][data_pt[i][l]] = wh, data_pt[i][l]++; data_max = std::max(data_max, wh.first * wh.second); } } for (int m = 0; m < data_pt[i][l - 1]; ++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], data[i][l] + data_pt[i][l], wh) == data[i][l] + data_pt[i][l]) { data[i][l][data_pt[i][l]] = wh, data_pt[i][l]++; data_max = std::max(data_max, wh.first * wh.second); } } } else if (map[n - 1][l]) { for (int m = 0; m < data_pt[YNUM(i - 1)][l]; ++m) { data[i][l][data_pt[i][l]] = (wh = WH(1, data[YNUM(i - 1)][l][m].second + 1)), data_pt[i][l]++; data_max = std::max(data_max, wh.first * wh.second); } } else if (map[n][l - 1]) { for (int m = 0; m < data_pt[i][l - 1]; ++m) { data[i][l][data_pt[i][l]] = (wh = WH(data[i][l - 1][m].first + 1, 1)), data_pt[i][l]++; data_max = std::max(data_max, wh.first * wh.second); } } else data[i][l][data_pt[i][l]] = (wh = WH(1, 1)), data_pt[i][l]++, 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)); WH data[3][502][500]; int data_max = 0; int height_sum[502][502], width_sum[502][502]; int data_pt[3][502]; WH wh; int main() { while (std::cin >> h >> w, w + h != 0) { data_max = 0; memset(data_pt, 0, sizeof(data_pt)); 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] == '.'); 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_pt[i][l] = 0; 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_pt[YNUM(i - 1)][l]; ++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], data[i][l] + data_pt[i][l], wh) == data[i][l] + data_pt[i][l]) { data[i][l][data_pt[i][l]] = wh, data_pt[i][l]++; data_max = std::max(data_max, wh.first * wh.second); } } for (int m = 0; m < data_pt[i][l - 1]; ++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], data[i][l] + data_pt[i][l], wh) == data[i][l] + data_pt[i][l]) { data[i][l][data_pt[i][l]] = wh, data_pt[i][l]++; data_max = std::max(data_max, wh.first * wh.second); } } } else if (map[n - 1][l]) { for (int m = 0; m < data_pt[YNUM(i - 1)][l]; ++m) { data[i][l][data_pt[i][l]] = (wh = WH(1, data[YNUM(i - 1)][l][m].second + 1)), data_pt[i][l]++; data_max = std::max(data_max, wh.first * wh.second); } } else if (map[n][l - 1]) { for (int m = 0; m < data_pt[i][l - 1]; ++m) { data[i][l][data_pt[i][l]] = (wh = WH(data[i][l - 1][m].first + 1, 1)), data_pt[i][l]++; data_max = std::max(data_max, wh.first * wh.second); } } else data[i][l][data_pt[i][l]] = (wh = WH(1, 1)), data_pt[i][l]++, data_max = std::max(data_max, wh.first * wh.second); } } } std::cout << data_max << std::endl; } return 0; }
replace
17
18
17
18
MLE
p00116
C++
Time Limit Exceeded
#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 main() { int h, w; while (std::cin >> h >> w, w + h != 0) { 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; 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, n = 1; n <= h; n++, i = n % 3) { data[i] = std::vector<whvec>(502); for (int l = 1; l <= w; l++) { 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 main() { int h, w; while (std::cin >> h >> w, w + h != 0) { 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; 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) { data[i] = std::vector<whvec>(502); for (int l = 1; l <= w; l++) { 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; }
insert
32
32
32
41
TLE
p00116
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int h, w; while (1) { cin >> h >> w; if (!h && !w) break; vector<string> grid; for (int i = 0; i < h; i++) { string tmp; cin >> tmp; grid.push_back(tmp); } int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (grid[i][j] == '.') { int x = w; int tmp = 0; for (int k = i; k < h; k++) { if (grid[k][j] == '*') break; int l; for (l = j; l < w; l++) { if (grid[k][l] == '*') { x = min(x, l); break; } } tmp = max(tmp, (x - j) * (k - i + 1)); } ans = max(ans, tmp); } } } cout << ans << endl; } }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int h, w; while (1) { cin >> h >> w; if (!h && !w) break; vector<string> grid; for (int i = 0; i < h; i++) { string tmp; cin >> tmp; grid.push_back(tmp); } int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (grid[i][j] == '.') { int x = w; int tmp = 0; for (int k = i; k < h; k++) { if (grid[k][j] == '*') break; int l; for (l = j; l < x; l++) { if (grid[k][l] == '*') { x = min(x, l); break; } } tmp = max(tmp, (x - j) * (k - i + 1)); } ans = max(ans, tmp); } } } cout << ans << endl; } }
replace
30
31
30
31
TLE
p00117
C++
Runtime Error
#include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define INF 1 << 30 typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector<Edge> Edges; typedef vector<Edges> Graph; void shortestPath(const Graph &g, int s, vector<Weight> &dist) { int n = g.size(); dist.assign(n, INF); dist[s] = 0; priority_queue<Edge> Q; // "e < f" <=> "e.weight > f.weight" for (Q.push(Edge(-2, s, 0)); !Q.empty();) { Edge e = Q.top(); Q.pop(); if (dist[e.dst] < e.weight) continue; FOR(f, g[e.dst]) { if (dist[f->dst] > e.weight + f->weight) { dist[f->dst] = e.weight + f->weight; Q.push(Edge(f->src, f->dst, e.weight + f->weight)); } } } } int main() { int n, m; cin >> n >> m; Graph g(n); REP(i, m) { int a, b, c, d; scanf("%d,%d,%d,%d\n", &a, &b, &c, &d); a--; b--; g[a].push_back(Edge(a, b, c)); g[b].push_back(Edge(b, a, d)); } int s, d, y1, y2; scanf("%d,%d,%d,%d\n", &s, &d, &y1, &y2); s--; d--; vector<int> dist; int hoge; shortestPath(g, s, dist); hoge = dist[d]; shortestPath(g, dist[d], dist); hoge += dist[s]; cout << y1 - y2 - hoge << endl; }
#include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define INF 1 << 30 typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector<Edge> Edges; typedef vector<Edges> Graph; void shortestPath(const Graph &g, int s, vector<Weight> &dist) { int n = g.size(); dist.assign(n, INF); dist[s] = 0; priority_queue<Edge> Q; // "e < f" <=> "e.weight > f.weight" for (Q.push(Edge(-2, s, 0)); !Q.empty();) { Edge e = Q.top(); Q.pop(); if (dist[e.dst] < e.weight) continue; FOR(f, g[e.dst]) { if (dist[f->dst] > e.weight + f->weight) { dist[f->dst] = e.weight + f->weight; Q.push(Edge(f->src, f->dst, e.weight + f->weight)); } } } } int main() { int n, m; cin >> n >> m; Graph g(n); REP(i, m) { int a, b, c, d; scanf("%d,%d,%d,%d\n", &a, &b, &c, &d); a--; b--; g[a].push_back(Edge(a, b, c)); g[b].push_back(Edge(b, a, d)); } int s, d, y1, y2; scanf("%d,%d,%d,%d\n", &s, &d, &y1, &y2); s--; d--; vector<int> dist; int hoge; shortestPath(g, s, dist); hoge = dist[d]; shortestPath(g, d, dist); hoge += dist[s]; cout << y1 - y2 - hoge << endl; }
replace
71
72
71
72
0
p00117
C++
Runtime Error
#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; }
replace
33
34
33
34
0
11.0
p00118
C++
Runtime Error
#include <algorithm> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> #define INF 1000000000 #define MOD 1000000000 + 7 using namespace std; int H, W; char fruit[15][15]; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; void dfs(int h, int w, char c) { fruit[h][w] = '.'; for (int i = 0; i < 4; i++) { if (h + dx[i] >= 0 && h + dx[i] <= H && w + dy[i] >= 0 && w + dy[i] <= W && fruit[h + dx[i]][w + dy[i]] == c) { dfs(h + dx[i], w + dy[i], c); } } } int main() { while (cin >> H >> W && H != 0 && W != 0) { string a; for (int i = 0; i < H; i++) { cin >> a; for (int j = 0; j < W; j++) { fruit[i][j] = a[j]; } } int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (fruit[i][j] == '#') dfs(i, j, '#'), ans++; if (fruit[i][j] == '*') dfs(i, j, '*'), ans++; if (fruit[i][j] == '@') dfs(i, j, '@'), ans++; } } cout << ans << endl; } }
#include <algorithm> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> #define INF 1000000000 #define MOD 1000000000 + 7 using namespace std; int H, W; char fruit[200][200]; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; void dfs(int h, int w, char c) { fruit[h][w] = '.'; for (int i = 0; i < 4; i++) { if (h + dx[i] >= 0 && h + dx[i] <= H && w + dy[i] >= 0 && w + dy[i] <= W && fruit[h + dx[i]][w + dy[i]] == c) { dfs(h + dx[i], w + dy[i], c); } } } int main() { while (cin >> H >> W && H != 0 && W != 0) { string a; for (int i = 0; i < H; i++) { cin >> a; for (int j = 0; j < W; j++) { fruit[i][j] = a[j]; } } int ans = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (fruit[i][j] == '#') dfs(i, j, '#'), ans++; if (fruit[i][j] == '*') dfs(i, j, '*'), ans++; if (fruit[i][j] == '@') dfs(i, j, '@'), ans++; } } cout << ans << endl; } }
replace
16
17
16
17
0
p00118
C++
Runtime Error
// // Created by yijiezhu on 12/20/16. // #include <cstring> #include <iostream> using namespace std; const int MAX_N = 21; int m, n; char matrix[MAX_N][MAX_N]; bool visited[MAX_N][MAX_N]; int cnt; void dfs(int i, int j, char ch) { visited[i][j] = true; matrix[i][j] = (char)('a' + cnt - 1); int delta[] = {0, 1, 0, -1, 0}; for (int q = 0; q < 4; q++) { int ni = i + delta[q], nj = j + delta[q + 1]; if (ni < 0 || ni >= m || nj < 0 || nj >= n) continue; if (visited[ni][nj] || matrix[ni][nj] != ch) continue; dfs(ni, nj, ch); } } void solve() { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (!visited[i][j]) { cnt++; dfs(i, j, matrix[i][j]); } } } cout << cnt << endl; } int main() { while (true) { cin >> m >> n; if (m == 0) break; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> matrix[i][j]; } } cnt = 0; memset(visited, false, sizeof(visited)); solve(); } }
// // Created by yijiezhu on 12/20/16. // #include <cstring> #include <iostream> using namespace std; #define MAX_N 101 int m, n; char matrix[MAX_N][MAX_N]; bool visited[MAX_N][MAX_N]; int cnt; void dfs(int i, int j, char ch) { visited[i][j] = true; matrix[i][j] = (char)('a' + cnt - 1); int delta[] = {0, 1, 0, -1, 0}; for (int q = 0; q < 4; q++) { int ni = i + delta[q], nj = j + delta[q + 1]; if (ni < 0 || ni >= m || nj < 0 || nj >= n) continue; if (visited[ni][nj] || matrix[ni][nj] != ch) continue; dfs(ni, nj, ch); } } void solve() { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (!visited[i][j]) { cnt++; dfs(i, j, matrix[i][j]); } } } cout << cnt << endl; } int main() { while (true) { cin >> m >> n; if (m == 0) break; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> matrix[i][j]; } } cnt = 0; memset(visited, false, sizeof(visited)); solve(); } }
replace
8
9
8
9
0
p00118
C++
Runtime Error
#include <cstdio> #include <queue> using namespace std; int main() { const int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1}; while (true) { int h, w; char fld[32][32]; scanf("%d%d", &h, &w); if (h == 0) break; for (int i = 0; i < h; ++i) scanf("%s", fld[i]); int ans = 0; for (int sx = 0; sx < h; ++sx) { for (int sy = 0; sy < w; ++sy) { if (fld[sx][sy] == 'x') continue; char c = fld[sx][sy]; queue<pair<int, int>> q; q.push(make_pair(sx, sy)); while (!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue; if (fld[nx][ny] != c) continue; fld[nx][ny] = 'x'; q.push(make_pair(nx, ny)); } } ans += 1; } } printf("%d\n", ans); } return 0; }
#include <cstdio> #include <queue> using namespace std; int main() { const int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1}; while (true) { int h, w; char fld[128][128]; scanf("%d%d", &h, &w); if (h == 0) break; for (int i = 0; i < h; ++i) scanf("%s", fld[i]); int ans = 0; for (int sx = 0; sx < h; ++sx) { for (int sy = 0; sy < w; ++sy) { if (fld[sx][sy] == 'x') continue; char c = fld[sx][sy]; queue<pair<int, int>> q; q.push(make_pair(sx, sy)); while (!q.empty()) { int x = q.front().first, y = q.front().second; q.pop(); for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue; if (fld[nx][ny] != c) continue; fld[nx][ny] = 'x'; q.push(make_pair(nx, ny)); } } ans += 1; } } printf("%d\n", ans); } return 0; }
replace
8
9
8
9
0
p00118
C++
Runtime Error
#include <iostream> #include <string> using namespace std; #define MAX_N 100 void makemap(int h); void seach(); void setblank(int x, int y, char c); int h, w, ans; string str[MAX_N]; int main() { while (cin >> h >> w) { if (h == 0 && w == 0) break; ans = 0; makemap(h); seach(); cout << ans << endl; } return 0; } void makemap(int h) { for (int i = 0; i < h; i++) { cin >> str[i]; } } void seach() { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (str[i][j] == '#' || str[i][j] == '@' || str[i][j] == '*') { ans++; setblank(j, i, str[i][j]); } } } } void setblank(int x, int y, char c) { int movex[] = {0, 1, 0, -1}, movey[] = {-1, 0, 1, 0}; for (int i = 0; i < 4; i++) { int nowx = x + movex[i], nowy = y + movey[i]; if (nowx > w || nowx < 0 || nowy > h || nowy < 0 || str[nowy][nowx] != c) ; else { str[nowy][nowx] = '!'; setblank(nowx, nowy, c); } } }
#include <iostream> #include <string> using namespace std; #define MAX_N 100 void makemap(int h); void seach(); void setblank(int x, int y, char c); int h, w, ans; string str[MAX_N]; int main() { while (cin >> h >> w) { if (h == 0 && w == 0) break; ans = 0; makemap(h); seach(); cout << ans << endl; } return 0; } void makemap(int h) { for (int i = 0; i < h; i++) { cin >> str[i]; } } void seach() { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (str[i][j] == '#' || str[i][j] == '@' || str[i][j] == '*') { ans++; setblank(j, i, str[i][j]); } } } } void setblank(int x, int y, char c) { int movex[] = {0, 1, 0, -1}, movey[] = {-1, 0, 1, 0}; for (int i = 0; i < 4; i++) { int nowx = x + movex[i], nowy = y + movey[i]; if (nowx >= w || nowx < 0 || nowy >= h || nowy < 0 || str[nowy][nowx] != c) ; else { str[nowy][nowx] = '!'; setblank(nowx, nowy, c); } } }
replace
43
44
43
44
0
p00118
C++
Time Limit Exceeded
#include <iostream> const int MAX_H = 100, MAX_W = 100, v[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; std::string map[MAX_H]; int W, H; // x,y Œ»Ý‚̈ʒu c Á‚·•¶Žš void dfs(int x, int y, char c) { map[y][x] = ' '; for (int i = 0; i < 4; i++) { int nx = x + v[i][0], ny = y + v[i][1]; if (nx >= 0 && nx < W && ny >= 0 && ny < H && map[ny][nx] == c) dfs(nx, ny, c); } return; } int main() { // Input while (std::cin >> W >> H, W || H) { for (int i = 0; i < H; i++) { std::cin >> map[i]; } // Processing int res = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (map[i][j] != ' ') res++, dfs(j, i, map[i][j]); } } std::cout << res << std::endl; } }
#include <iostream> const int MAX_H = 100, MAX_W = 100, v[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; std::string map[MAX_H]; int W, H; // x,y Œ»Ý‚̈ʒu c Á‚·•¶Žš void dfs(int x, int y, char c) { map[y][x] = ' '; for (int i = 0; i < 4; i++) { int nx = x + v[i][0], ny = y + v[i][1]; if (nx >= 0 && nx < W && ny >= 0 && ny < H && map[ny][nx] == c) dfs(nx, ny, c); } return; } int main() { // Input while (std::cin >> H >> W, W || H) { for (int i = 0; i < H; i++) { std::cin >> map[i]; } // Processing int res = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (map[i][j] != ' ') res++, dfs(j, i, map[i][j]); } } std::cout << res << std::endl; } }
replace
21
22
21
22
TLE
p00118
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #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> typedef long long ll; using namespace std; int W, H; char field[30][30]; void dfs(int x, int y, char obj) { field[x][y] = 'T'; for (int dx = -1; dx <= 1; ++dx) { for (int dy = -1; dy <= 1; ++dy) { if (dx != 0 && dy != 0) continue; int nx = x + dx, ny = y + dy; if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == obj) { dfs(nx, ny, obj); } } } } int main(void) { while (1) { cin >> H, cin >> W; if (!W && !H) break; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cin >> field[i][j]; } } int count = 0; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (field[i][j] == '#' || field[i][j] == '@' || field[i][j] == '*') { dfs(i, j, field[i][j]); count++; } } } cout << count << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #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> typedef long long ll; using namespace std; int W, H; char field[100][100]; void dfs(int x, int y, char obj) { field[x][y] = 'T'; for (int dx = -1; dx <= 1; ++dx) { for (int dy = -1; dy <= 1; ++dy) { if (dx != 0 && dy != 0) continue; int nx = x + dx, ny = y + dy; if (0 <= nx && nx < H && 0 <= ny && ny < W && field[nx][ny] == obj) { dfs(nx, ny, obj); } } } } int main(void) { while (1) { cin >> H, cin >> W; if (!W && !H) break; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { cin >> field[i][j]; } } int count = 0; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (field[i][j] == '#' || field[i][j] == '@' || field[i][j] == '*') { dfs(i, j, field[i][j]); count++; } } } cout << count << endl; } return 0; }
replace
28
29
28
29
0
p00118
C++
Runtime Error
#include <iostream> using namespace std; const int N = 22; int w, h, ans; char ch, maze[N][N]; const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1}; void dfs(int x, int y) { maze[x][y] = '-'; for (int i = 0; i < 4; ++i) { int nx = x + cx[i], ny = y + cy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == ch) dfs(nx, ny); } } int main() { while (cin >> h >> w, (h && w)) { for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) cin >> maze[i][j]; ans = 0; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) { if (maze[i][j] != '-') { ch = maze[i][j]; ++ans; dfs(i, j); } } cout << ans << endl; } return 0; }
#include <iostream> using namespace std; const int N = 111; int w, h, ans; char ch, maze[N][N]; const int cx[] = {1, -1, 0, 0}, cy[] = {0, 0, 1, -1}; void dfs(int x, int y) { maze[x][y] = '-'; for (int i = 0; i < 4; ++i) { int nx = x + cx[i], ny = y + cy[i]; if (0 <= nx && nx < h && 0 <= ny && ny < w && maze[nx][ny] == ch) dfs(nx, ny); } } int main() { while (cin >> h >> w, (h && w)) { for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) cin >> maze[i][j]; ans = 0; for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) { if (maze[i][j] != '-') { ch = maze[i][j]; ++ans; dfs(i, j); } } cout << ans << endl; } return 0; }
replace
2
3
2
3
0
p00118
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // common #define BR "\n" #define ALL(c) (c).begin(), (c).end() #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it) #define IN(l, v, r) ((l) <= (v) && (v) < (r)) typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int>> vpii; typedef long long ll; typedef vector<long long> vl; typedef pair<long long, long long> pll; typedef vector<pair<long long, long long>> vpll; typedef vector<string> vs; typedef long double ld; // config #define MODE_DEBUG // #define INF 1<<30 // #define EPS 1e-8 // const ll MOD =100000007; // debug #ifdef MODE_DEBUG #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) \ DUMP(x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl #define CHECK(exp, act) \ if (exp != act) { \ DUMP(exp); \ DEBUG(act); \ } #define STOP(e) \ CHECK(e, true); \ if (!(e)) \ exit(1); #else #define DUMP(x) #define DEBUG(x) #define CHECK(exp, act) #define STOP(e) #endif template <class T> inline string toString(const vector<T> &x) { stringstream ss; REP(i, x.size()) { if (i != 0) ss << " "; ss << x[i]; } return ss.str(); } template <class T> inline string toString(const vector<vector<T>> &map) { stringstream ss; REP(i, map.size()) { if (i != 0) ss << BR; ss << toString(map[i]); } return ss.str(); } template <class K, class V> string toString(map<K, V> &x) { string res; stringstream ss; for (auto &p : x) ss << p.first << ":" << p.second << " "; return ss.str(); } template <typename T, typename V> inline T mod(T v, V MOD) { return (v % MOD + MOD) % MOD; } struct UnionFind { vector<int> par; // 親 vector<int> rank; // 木の深さ int size; UnionFind(int n) { REP(i, n) par.push_back(i); rank = vector<int>(n); size = n; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) par[x] = y; else par[y] = x; if (rank[x] == rank[y]) rank[x]++; size--; } }; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { while (true) { int H, W; cin >> H >> W; if (H == 0 && W == 0) return 0; vector<vector<int>> map(H, vector<int>(W)); REP(y, H) { string str; cin >> str; REP(x, W) map[y][x] = str[x]; } UnionFind uf(H * W); REP(y, H) REP(x, W) { REP(di, 4) { int px = x + dx[di], py = y + dy[di]; if (IN(0, px, W) && IN(0, py, H)) { if (map[y][x] == map[py][px]) { uf.unite(y * H + x, py * H + px); } } } } cout << uf.size << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // common #define BR "\n" #define ALL(c) (c).begin(), (c).end() #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it) #define IN(l, v, r) ((l) <= (v) && (v) < (r)) typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int>> vpii; typedef long long ll; typedef vector<long long> vl; typedef pair<long long, long long> pll; typedef vector<pair<long long, long long>> vpll; typedef vector<string> vs; typedef long double ld; // config #define MODE_DEBUG // #define INF 1<<30 // #define EPS 1e-8 // const ll MOD =100000007; // debug #ifdef MODE_DEBUG #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) \ DUMP(x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl #define CHECK(exp, act) \ if (exp != act) { \ DUMP(exp); \ DEBUG(act); \ } #define STOP(e) \ CHECK(e, true); \ if (!(e)) \ exit(1); #else #define DUMP(x) #define DEBUG(x) #define CHECK(exp, act) #define STOP(e) #endif template <class T> inline string toString(const vector<T> &x) { stringstream ss; REP(i, x.size()) { if (i != 0) ss << " "; ss << x[i]; } return ss.str(); } template <class T> inline string toString(const vector<vector<T>> &map) { stringstream ss; REP(i, map.size()) { if (i != 0) ss << BR; ss << toString(map[i]); } return ss.str(); } template <class K, class V> string toString(map<K, V> &x) { string res; stringstream ss; for (auto &p : x) ss << p.first << ":" << p.second << " "; return ss.str(); } template <typename T, typename V> inline T mod(T v, V MOD) { return (v % MOD + MOD) % MOD; } struct UnionFind { vector<int> par; // 親 vector<int> rank; // 木の深さ int size; UnionFind(int n) { REP(i, n) par.push_back(i); rank = vector<int>(n); size = n; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) par[x] = y; else par[y] = x; if (rank[x] == rank[y]) rank[x]++; size--; } }; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main() { while (true) { int H, W; cin >> H >> W; if (H == 0 && W == 0) return 0; vector<vector<int>> map(H, vector<int>(W)); REP(y, H) { string str; cin >> str; REP(x, W) map[y][x] = str[x]; } UnionFind uf(H * W); REP(y, H) REP(x, W) { REP(di, 4) { int px = x + dx[di], py = y + dy[di]; if (IN(0, px, W) && IN(0, py, H)) { if (map[y][x] == map[py][px]) { uf.unite(y * W + x, py * W + px); } } } } cout << uf.size << endl; } return 0; }
replace
145
146
145
146
0
p00118
C++
Runtime Error
#include <iostream> using namespace std; typedef long long ll; #define INF 2147483647 int w, h; char a[22][22]; int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; int ans = 0; void dfs(int x, int y, char s) { if (x < 0 || x >= h || y < 0 || y >= w || a[x][y] != s) return; char t = a[x][y]; a[x][y] = 'o'; for (int i = 0; i < 4; i++) { dfs(x + dir[i][0], y + dir[i][1], t); } } int main() { while (cin >> h >> w) { if (w == 0 && h == 0) break; ans = 0; int sx, sy; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (a[i][j] != 'o') { ans++; dfs(i, j, a[i][j]); } } } cout << ans << endl; } return 0; }
#include <iostream> using namespace std; typedef long long ll; #define INF 2147483647 int w, h; char a[102][102]; int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; int ans = 0; void dfs(int x, int y, char s) { if (x < 0 || x >= h || y < 0 || y >= w || a[x][y] != s) return; char t = a[x][y]; a[x][y] = 'o'; for (int i = 0; i < 4; i++) { dfs(x + dir[i][0], y + dir[i][1], t); } } int main() { while (cin >> h >> w) { if (w == 0 && h == 0) break; ans = 0; int sx, sy; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (a[i][j] != 'o') { ans++; dfs(i, j, a[i][j]); } } } cout << ans << endl; } return 0; }
replace
6
7
6
7
0
p00118
C++
Runtime Error
#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); } } } }
replace
4
5
4
5
0
p00118
C++
Runtime Error
#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(); } }
replace
35
36
35
36
-11
p00118
C++
Time Limit Exceeded
#include <algorithm> #include <cstring> #include <iostream> #include <string> using namespace std; int W, H; string field[100]; bool visit[100][100]; void dfs(int y, int x) { visit[y][x] = true; static int dy[4] = {0, 1, 0, -1}; static int dx[4] = {1, 0, -1, 0}; char c = field[y][x]; for (int i = 0; i < 4; i++) { int ny = y + dy[i]; int nx = x + dx[i]; if (0 <= ny && ny < H && 0 <= nx && nx < W && visit[ny][nx] == false && c == field[ny][nx]) { dfs(ny, nx); } } } void pv() { cerr << endl; cerr << "-----" << endl; for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { cerr << visit[y][x]; } cerr << endl; } cerr << "-----" << endl; } int main() { for (; cin >> W >> H, W || H;) { for (int y = 0; y < H; y++) cin >> field[y]; memset(visit, 0, sizeof visit); int ans = 0; for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { if (visit[y][x] == false) { ans++; dfs(y, x); } } } cout << ans << endl; } }
#include <algorithm> #include <cstring> #include <iostream> #include <string> using namespace std; int W, H; string field[100]; bool visit[100][100]; void dfs(int y, int x) { visit[y][x] = true; static int dy[4] = {0, 1, 0, -1}; static int dx[4] = {1, 0, -1, 0}; char c = field[y][x]; for (int i = 0; i < 4; i++) { int ny = y + dy[i]; int nx = x + dx[i]; if (0 <= ny && ny < H && 0 <= nx && nx < W && visit[ny][nx] == false && c == field[ny][nx]) { dfs(ny, nx); } } } void pv() { cerr << endl; cerr << "-----" << endl; for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { cerr << visit[y][x]; } cerr << endl; } cerr << "-----" << endl; } int main() { for (; cin >> H >> W, W || H;) { for (int y = 0; y < H; y++) cin >> field[y]; memset(visit, 0, sizeof visit); int ans = 0; for (int y = 0; y < H; y++) { for (int x = 0; x < W; x++) { if (visit[y][x] == false) { ans++; dfs(y, x); } } } cout << ans << endl; } }
replace
42
43
42
43
TLE
p00118
C++
Runtime Error
/* * Filename: aoj0118.cpp * Desciption: DFS * Created: 2016-03-02 * Author: JIngwei Xu [mail:[email protected]] * */ #include <algorithm> #include <cstring> #include <iostream> #include <stdio.h> #define INT_MAX 2 << 30 using namespace std; // typedef long long ll; int n, m, ans; int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1}; char maze[30][30]; void dfs(int x, int y, char s) { int tx, ty; maze[x][y] = 0; for (int i = 0; i < 4; i += 1) { tx = x + dx[i]; ty = y + dy[i]; if (tx < 0 || ty < 0 || tx >= n || ty >= m) continue; if (maze[tx][ty] == s) dfs(tx, ty, s); } } int main() { while (scanf("%d%d", &n, &m) && n && m) { for (int i = 0; i < n; i += 1) { scanf("%s", maze[i]); } ans = 0; for (int i = 0; i < n; i += 1) { for (int j = 0; j < m; j += 1) { if (maze[i][j] != 0) { dfs(i, j, maze[i][j]); ans++; } } } printf("%d\n", ans); } return 0; }
/* * Filename: aoj0118.cpp * Desciption: DFS * Created: 2016-03-02 * Author: JIngwei Xu [mail:[email protected]] * */ #include <algorithm> #include <cstring> #include <iostream> #include <stdio.h> #define INT_MAX 2 << 30 using namespace std; // typedef long long ll; int n, m, ans; int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1}; char maze[105][105]; void dfs(int x, int y, char s) { int tx, ty; maze[x][y] = 0; for (int i = 0; i < 4; i += 1) { tx = x + dx[i]; ty = y + dy[i]; if (tx < 0 || ty < 0 || tx >= n || ty >= m) continue; if (maze[tx][ty] == s) dfs(tx, ty, s); } } int main() { while (scanf("%d%d", &n, &m) && n && m) { for (int i = 0; i < n; i += 1) { scanf("%s", maze[i]); } ans = 0; for (int i = 0; i < n; i += 1) { for (int j = 0; j < m; j += 1) { if (maze[i][j] != 0) { dfs(i, j, maze[i][j]); ans++; } } } printf("%d\n", ans); } return 0; }
replace
16
17
16
17
0
p00118
C++
Time Limit Exceeded
#include <iostream> #include <string> #include <vector> using namespace std; int H, W; vector<string> map; const vector<int> dx = {0, 1, 0, -1}; const vector<int> dy = {1, 0, -1, 0}; void rec(int x, int y, char c) { map[y][x] = '.'; for (int i = 0; i < 4; i++) { if (0 <= x + dx[i] && x + dx[i] < W && 0 <= y + dy[i] && y + dy[i] < H) { if (map[y + dy[i]][x + dx[i]] == c) { rec(x + dx[i], y + dy[i], c); } } } } int solve() { int ret = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (map[i][j] != '.') { rec(j, i, map[i][j]); ret++; } } } return ret; } int main() { while (true) { cin >> H >> W; map = vector<string>(H); for (int i = 0; i < H; i++) { cin >> map[i]; } cout << solve() << endl; } return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; int H, W; vector<string> map; const vector<int> dx = {0, 1, 0, -1}; const vector<int> dy = {1, 0, -1, 0}; void rec(int x, int y, char c) { map[y][x] = '.'; for (int i = 0; i < 4; i++) { if (0 <= x + dx[i] && x + dx[i] < W && 0 <= y + dy[i] && y + dy[i] < H) { if (map[y + dy[i]][x + dx[i]] == c) { rec(x + dx[i], y + dy[i], c); } } } } int solve() { int ret = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (map[i][j] != '.') { rec(j, i, map[i][j]); ret++; } } } return ret; } int main() { while (true) { cin >> H >> W; if (H == 0 && W == 0) { break; } map = vector<string>(H); for (int i = 0; i < H; i++) { cin >> map[i]; } cout << solve() << endl; } return 0; }
insert
44
44
44
48
TLE
p00118
C++
Runtime Error
#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; }
replace
33
34
33
34
-11
p00118
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; char map[30][30]; pair<int, int> s; int w, h; int cnt; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; class Node { public: int x, y; Node(int _x, int _y) : x(_x), y(_y) {} }; int sx, sy; void show() { for (int i = 0; i < h; i++) { cout << map[i] << endl; } cout << endl; } void bfs(int x, int y, char c) { // cout<<x<<","<<y<<endl; queue<Node> q; q.push(Node(x, y)); map[y][x] = '.'; while (!q.empty()) { Node cur = q.front(); q.pop(); // show(); for (int i = 0; i < 4; i++) { int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if (nx < 0 || nx >= w || ny < 0 || ny >= h || map[ny][nx] == '.' || map[ny][nx] != c) continue; map[ny][nx] = '.'; q.push(Node(nx, ny)); } } } void solve() { cnt = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (map[i][j] != '.') { cnt++; bfs(j, i, map[i][j]); } } } cout << cnt << endl; } int main() { while (cin >> h >> w && (w || h)) { // for(int i=0;i<h;i++)for(int j=0;j<w;j++)map[i][j]='.'; for (int i = 0; i < h; i++) { cin >> map[i]; } solve(); } return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <queue> #include <utility> #include <vector> using namespace std; char map[101][101]; pair<int, int> s; int w, h; int cnt; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; class Node { public: int x, y; Node(int _x, int _y) : x(_x), y(_y) {} }; int sx, sy; void show() { for (int i = 0; i < h; i++) { cout << map[i] << endl; } cout << endl; } void bfs(int x, int y, char c) { // cout<<x<<","<<y<<endl; queue<Node> q; q.push(Node(x, y)); map[y][x] = '.'; while (!q.empty()) { Node cur = q.front(); q.pop(); // show(); for (int i = 0; i < 4; i++) { int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if (nx < 0 || nx >= w || ny < 0 || ny >= h || map[ny][nx] == '.' || map[ny][nx] != c) continue; map[ny][nx] = '.'; q.push(Node(nx, ny)); } } } void solve() { cnt = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (map[i][j] != '.') { cnt++; bfs(j, i, map[i][j]); } } } cout << cnt << endl; } int main() { while (cin >> h >> w && (w || h)) { // for(int i=0;i<h;i++)for(int j=0;j<w;j++)map[i][j]='.'; for (int i = 0; i < h; i++) { cin >> map[i]; } solve(); } return 0; }
replace
10
11
10
11
0
p00118
C++
Memory Limit Exceeded
#include <algorithm> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; int H, W, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; char c[100][100], sc; int main() { while (cin >> H >> W) { if (!H) return 0; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cin >> c[i][j]; queue<P> q; int ans = 0; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { if (c[i][j] != '.') { q.push(P(i, j)); sc = c[i][j]; ans++; } while (!q.empty()) { int sy = q.front().first, sx = q.front().second; q.pop(); c[sy][sx] = '.'; // cout<<"("<<sy<<","<<sx<<")"<<sc<<endl; for (int i = 0; i < 4; i++) { int ty = sy + dy[i], tx = sx + dx[i]; if (ty < 0 || ty >= H || tx < 0 || tx >= W || c[ty][tx] != sc) continue; q.push(P(ty, tx)); } /*for(int i=0;i<H;i++){ for(int j=0;j<W;j++)cout<<c[i][j];cout<<endl; }*/ } } cout << ans << endl; } }
#include <algorithm> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; int H, W, dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; char c[100][100], sc; int main() { while (cin >> H >> W) { if (!H) return 0; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cin >> c[i][j]; queue<P> q; int ans = 0; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) { if (c[i][j] != '.') { q.push(P(i, j)); sc = c[i][j]; ans++; } while (!q.empty()) { int sy = q.front().first, sx = q.front().second; q.pop(); c[sy][sx] = '.'; // cout<<"("<<sy<<","<<sx<<")"<<sc<<endl; for (int i = 0; i < 4; i++) { int ty = sy + dy[i], tx = sx + dx[i]; if (ty < 0 || ty >= H || tx < 0 || tx >= W || c[ty][tx] != sc) continue; q.push(P(ty, tx)); c[ty][tx] = '.'; } /*for(int i=0;i<H;i++){ for(int j=0;j<W;j++)cout<<c[i][j];cout<<endl; }*/ } } cout << ans << endl; } }
insert
46
46
46
47
MLE
p00118
C++
Runtime Error
#include <cmath> #include <cstring> #include <iostream> using namespace std; const int MAX = 20; int N, M; char garden[MAX][MAX + 1]; int num; void dfs(int x, int y, char ch) { garden[x][y] = '.'; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (abs(dx) + abs(dy) > 1) continue; int nx = x + dx, ny = y + dy; if (nx >= 0 && nx < N && ny >= 0 && ny < M && garden[nx][ny] == ch) dfs(nx, ny, ch); } } return; } int main() { for (;;) { cin >> N >> M; if (N == 0) break; memset(garden, '\0', sizeof(garden)); for (int i = 0; i < N; i++) cin >> garden[i]; num = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (garden[i][j] != '.') { dfs(i, j, garden[i][j]); num++; } } } cout << num << endl; } return 0; }
#include <cmath> #include <cstring> #include <iostream> using namespace std; const int MAX = 100; int N, M; char garden[MAX][MAX + 1]; int num; void dfs(int x, int y, char ch) { garden[x][y] = '.'; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (abs(dx) + abs(dy) > 1) continue; int nx = x + dx, ny = y + dy; if (nx >= 0 && nx < N && ny >= 0 && ny < M && garden[nx][ny] == ch) dfs(nx, ny, ch); } } return; } int main() { for (;;) { cin >> N >> M; if (N == 0) break; memset(garden, '\0', sizeof(garden)); for (int i = 0; i < N; i++) cin >> garden[i]; num = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (garden[i][j] != '.') { dfs(i, j, garden[i][j]); num++; } } } cout << num << endl; } return 0; }
replace
5
6
5
6
0
p00118
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) begin(a), end(a) #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> P; typedef long long ll; const ll INF = 114514810; const ll MOD = 1000000007; const double EPS = 1e-8; const double PI = acos(-1.0); struct edge { int from, to, cost; bool operator<(const edge &e) const { return cost < e.cost; } bool operator>(const edge &e) const { return cost > e.cost; } }; int dx[] = {-1, 0, 0, 1}; int dy[] = {0, -1, 1, 0}; bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h && y < w); } ///*************************************************************************************/// ///*************************************************************************************/// ///*************************************************************************************/// struct union_find { vector<int> par; union_find(int n) : par(n, -1) {} int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (par[y] < par[x]) swap(x, y); if (par[x] == par[y]) par[x]--; par[y] = x; if (find(x) != find(y)) par[find(y)] = x; } bool same(int x, int y) { return find(x) == find(y); }; int count() { int cnt = 0; REP(i, par.size()) if (par[i] < 0) cnt++; return cnt; } }; int main() { int h, w; while (cin >> h >> w, h) { union_find uft(h * w); vs fld(h); REP(i, h) cin >> fld[i]; REP(i, h) REP(j, w) { REP(k, 4) { int nx = i + dx[k]; int ny = j + dy[k]; if (valid(nx, ny, h, w) && fld[i][j] == fld[nx][ny] && !uft.same(i * h + j, nx * h + ny)) { uft.unite(i * h + j, nx * h + ny); } } } cout << uft.count() << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) begin(a), end(a) #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> P; typedef long long ll; const ll INF = 114514810; const ll MOD = 1000000007; const double EPS = 1e-8; const double PI = acos(-1.0); struct edge { int from, to, cost; bool operator<(const edge &e) const { return cost < e.cost; } bool operator>(const edge &e) const { return cost > e.cost; } }; int dx[] = {-1, 0, 0, 1}; int dy[] = {0, -1, 1, 0}; bool valid(int x, int y, int h, int w) { return (x >= 0 && y >= 0 && x < h && y < w); } ///*************************************************************************************/// ///*************************************************************************************/// ///*************************************************************************************/// struct union_find { vector<int> par; union_find(int n) : par(n, -1) {} int find(int x) { return par[x] < 0 ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (par[y] < par[x]) swap(x, y); if (par[x] == par[y]) par[x]--; par[y] = x; if (find(x) != find(y)) par[find(y)] = x; } bool same(int x, int y) { return find(x) == find(y); }; int count() { int cnt = 0; REP(i, par.size()) if (par[i] < 0) cnt++; return cnt; } }; int main() { int h, w; while (cin >> h >> w, h) { union_find uft(h * w); vs fld(h); REP(i, h) cin >> fld[i]; REP(i, h) REP(j, w) { REP(k, 4) { int nx = i + dx[k]; int ny = j + dy[k]; if (valid(nx, ny, h, w) && fld[i][j] == fld[nx][ny]) { uft.unite(i * w + j, nx * w + ny); } } } cout << uft.count() << endl; } return 0; }
replace
64
67
64
66
0
p00118
C++
Time Limit Exceeded
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; struct UnionFind { vector<int> data; UnionFind(int size) : data(size, -1) {} bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; int main() { while (1) { int H, W; cin >> H >> W; UnionFind uf(H * W); vector<string> field(H); for (int i = 0; i < H; ++i) { cin >> field[i]; } for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (j != W - 1 && field[i][j] == field[i][j + 1]) { uf.unionSet(i * W + j, i * W + j + 1); } if (i != H - 1 && field[i][j] == field[i + 1][j]) { uf.unionSet(i * W + j, (i + 1) * W + j); } } } set<int> aset; for (int i = 0; i < H * W; ++i) { aset.emplace(uf.root(i)); } cout << aset.size() << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; struct UnionFind { vector<int> data; UnionFind(int size) : data(size, -1) {} bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; int main() { while (1) { int H, W; cin >> H >> W; if (!H) break; UnionFind uf(H * W); vector<string> field(H); for (int i = 0; i < H; ++i) { cin >> field[i]; } for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (j != W - 1 && field[i][j] == field[i][j + 1]) { uf.unionSet(i * W + j, i * W + j + 1); } if (i != H - 1 && field[i][j] == field[i + 1][j]) { uf.unionSet(i * W + j, (i + 1) * W + j); } } } set<int> aset; for (int i = 0; i < H * W; ++i) { aset.emplace(uf.root(i)); } cout << aset.size() << endl; } return 0; }
insert
31
31
31
33
TLE
p00118
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdlib> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <stack> #include <string> #include <vector> #define INF 100000000 #define _INF -100000000 #define Loop(i, n) for (int i = 0; i < (int)n; i++) #define Loop1(i, n) for (int i = 1; i <= (int)n; i++) #define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--) #define Loopr1(i, n) for (int i = (int)n; i >= 1; i--) using namespace std; typedef long long int ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef pair<int, int> P; char field[100][101]; int h, w; queue<P> que; int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; int ret; void solve() { Loop(i, h) { Loop(j, w) { if (field[i][j] != '.') { que.push(P(i, j)); char c = field[i][j]; field[i][j] = '.'; while (que.size() > 0) { P p = que.front(); que.pop(); Loop(k, 4) { int a = p.first + dx[k], b = p.second + dy[k]; if (field[a][b] == c) { field[a][b] = '.'; que.push(P(a, b)); } } } ret++; } } } } int main() { while (cin >> h >> w, h || w) { Loop(i, h) { Loop(j, w) { cin >> field[i][j]; } } ret = 0; solve(); cout << ret << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdlib> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <stack> #include <string> #include <vector> #define INF 100000000 #define _INF -100000000 #define Loop(i, n) for (int i = 0; i < (int)n; i++) #define Loop1(i, n) for (int i = 1; i <= (int)n; i++) #define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--) #define Loopr1(i, n) for (int i = (int)n; i >= 1; i--) using namespace std; typedef long long int ll; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef pair<int, int> P; char field[100][101]; int h, w; queue<P> que; int dx[4] = {0, 0, -1, 1}, dy[4] = {-1, 1, 0, 0}; int ret; void solve() { Loop(i, h) { Loop(j, w) { if (field[i][j] != '.') { que.push(P(i, j)); char c = field[i][j]; field[i][j] = '.'; while (que.size() > 0) { P p = que.front(); que.pop(); Loop(k, 4) { int a = p.first + dx[k], b = p.second + dy[k]; if (a >= 0 && a <= h - 1 && b >= 0 && b <= w - 1 && field[a][b] == c) { field[a][b] = '.'; que.push(P(a, b)); } } } ret++; } } } } int main() { while (cin >> h >> w, h || w) { Loop(i, h) { Loop(j, w) { cin >> field[i][j]; } } ret = 0; solve(); cout << ret << endl; } return 0; }
replace
44
45
44
46
0
p00119
C++
Runtime Error
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual ///Studio 2015\Projects\programing_contest_c++\Debug\b.answer" long long int memo[1 << 20]; long long int getans(bitset<20> &bs, vector<int> &nums, vector<vector<int>> &edges) { if (memo[bs.to_ulong()] != -1) return memo[bs.to_ulong()]; if (bs.count()) { long long int ans = 0; for (int i = 0; i < nums.size(); ++i) { if (!nums[i] && !bs[i]) { bs[i] = true; for (auto e : edges[i]) { nums[e]--; } ans += getans(bs, nums, edges); bs[i] = false; for (auto e : edges[i]) { nums[e]++; } } } return memo[bs.to_ulong()] = ans; } else { return memo[bs.to_ulong()] = 1; } } vector<int> topo_sort(const vector<vector<int>> &edges) { int size_ = edges.size(); vector<int> nums(size_); for (const auto &es : edges) { for (auto e : es) { nums[e]++; } } queue<int> que; for (int i = 0; i < size_; ++i) { if (!nums[i]) { que.push(i); } } vector<int> topo; while (!que.empty()) { int ver(que.front()); topo.push_back(ver); que.pop(); for (auto e : edges[ver]) { nums[e]--; if (!nums[e]) que.push(e); } } if (topo.size() == size_) return topo; else return vector<int>(); } int main() { for (int i = 0; i < (1 << 20); ++i) { memo[i] = -1; } int M, N; cin >> M >> N; vector<vector<int>> edges(M); vector<int> nums(N); for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; x--; y--; edges[x].push_back(y); nums[y]++; } bitset<20> bs((1 << M) - 1); long long int ans = getans(bs, nums, edges); auto v = topo_sort(edges); for (int i = 0; i < v.size(); ++i) { cout << v[i] + 1 << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual ///Studio 2015\Projects\programing_contest_c++\Debug\b.answer" long long int memo[1 << 20]; long long int getans(bitset<20> &bs, vector<int> &nums, vector<vector<int>> &edges) { if (memo[bs.to_ulong()] != -1) return memo[bs.to_ulong()]; if (bs.count()) { long long int ans = 0; for (int i = 0; i < nums.size(); ++i) { if (!nums[i] && !bs[i]) { bs[i] = true; for (auto e : edges[i]) { nums[e]--; } ans += getans(bs, nums, edges); bs[i] = false; for (auto e : edges[i]) { nums[e]++; } } } return memo[bs.to_ulong()] = ans; } else { return memo[bs.to_ulong()] = 1; } } vector<int> topo_sort(const vector<vector<int>> &edges) { int size_ = edges.size(); vector<int> nums(size_); for (const auto &es : edges) { for (auto e : es) { nums[e]++; } } queue<int> que; for (int i = 0; i < size_; ++i) { if (!nums[i]) { que.push(i); } } vector<int> topo; while (!que.empty()) { int ver(que.front()); topo.push_back(ver); que.pop(); for (auto e : edges[ver]) { nums[e]--; if (!nums[e]) que.push(e); } } if (topo.size() == size_) return topo; else return vector<int>(); } int main() { for (int i = 0; i < (1 << 20); ++i) { memo[i] = -1; } int M, N; cin >> M >> N; vector<vector<int>> edges(M); vector<int> nums(M); for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; x--; y--; edges[x].push_back(y); nums[y]++; } bitset<20> bs((1 << M) - 1); long long int ans = getans(bs, nums, edges); auto v = topo_sort(edges); for (int i = 0; i < v.size(); ++i) { cout << v[i] + 1 << endl; } return 0; }
replace
76
77
76
77
-11
p00119
C++
Runtime Error
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual ///Studio 2015\Projects\programing_contest_c++\Debug\b.answer" long long int memo[1 << 20]; long long int getans(bitset<20> &bs, vector<int> &nums, vector<vector<int>> &edges) { if (memo[bs.to_ulong()] != -1) return memo[bs.to_ulong()]; if (bs.count()) { long long int ans = 0; for (int i = 0; i < nums.size(); ++i) { if (!nums[i] && !bs[i]) { bs[i] = true; for (auto e : edges[i]) { nums[e]--; } ans += getans(bs, nums, edges); bs[i] = false; for (auto e : edges[i]) { nums[e]++; } } } return memo[bs.to_ulong()] = ans; } else { return memo[bs.to_ulong()] = 1; } } vector<int> topo_sort(const vector<vector<int>> &edges) { int size_ = edges.size(); vector<int> nums(size_); for (const auto &es : edges) { for (auto e : es) { nums[e]++; } } queue<int> que; for (int i = 0; i < size_; ++i) { if (!nums[i]) { que.push(i); } } vector<int> topo; while (!que.empty()) { int ver(que.front()); topo.push_back(ver); que.pop(); for (auto e : edges[ver]) { nums[e]--; if (!nums[e]) que.push(e); } } if (topo.size() == size_) return topo; else return vector<int>(); } int main() { for (int i = 0; i < (1 << 20); ++i) { memo[i] = -1; } int M, N; cin >> M >> N; vector<vector<int>> edges(M); vector<int> nums(N); for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; x--; y--; edges[x].push_back(y); nums[y]++; } bitset<20> bs((1 << N) - 1); long long int ans = getans(bs, nums, edges); auto v = topo_sort(edges); for (int i = 0; i < v.size(); ++i) { cout << v[i] + 1 << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual ///Studio 2015\Projects\programing_contest_c++\Debug\b.answer" long long int memo[1 << 20]; long long int getans(bitset<20> &bs, vector<int> &nums, vector<vector<int>> &edges) { if (memo[bs.to_ulong()] != -1) return memo[bs.to_ulong()]; if (bs.count()) { long long int ans = 0; for (int i = 0; i < nums.size(); ++i) { if (!nums[i] && !bs[i]) { bs[i] = true; for (auto e : edges[i]) { nums[e]--; } ans += getans(bs, nums, edges); bs[i] = false; for (auto e : edges[i]) { nums[e]++; } } } return memo[bs.to_ulong()] = ans; } else { return memo[bs.to_ulong()] = 1; } } vector<int> topo_sort(const vector<vector<int>> &edges) { int size_ = edges.size(); vector<int> nums(size_); for (const auto &es : edges) { for (auto e : es) { nums[e]++; } } queue<int> que; for (int i = 0; i < size_; ++i) { if (!nums[i]) { que.push(i); } } vector<int> topo; while (!que.empty()) { int ver(que.front()); topo.push_back(ver); que.pop(); for (auto e : edges[ver]) { nums[e]--; if (!nums[e]) que.push(e); } } if (topo.size() == size_) return topo; else return vector<int>(); } int main() { for (int i = 0; i < (1 << 20); ++i) { memo[i] = -1; } int M, N; cin >> M >> N; vector<vector<int>> edges(M); vector<int> nums(N); for (int i = 0; i < N; ++i) { int x, y; cin >> x >> y; x--; y--; edges[x].push_back(y); nums[y]++; } bitset<20> bs((1 << N) - 1); // long long int ans = getans(bs, nums, edges); auto v = topo_sort(edges); for (int i = 0; i < v.size(); ++i) { cout << v[i] + 1 << endl; } return 0; }
replace
86
87
86
87
0
p00119
C++
Runtime Error
#include <algorithm> #include <functional> #include <iostream> #include <vector> #define mp(x, y) make_pair(x, y) #define X first #define Y second using namespace std; typedef pair<int, int> pii; int main(void) { int m; while (cin >> m) { vector<pii> v(m); for (int i = 0; i < m; ++i) { v[i].X = 0; v[i].Y = i + 1; } int n; cin >> n; vector<pii> t(n); for (int i = 0; i < n; ++i) { cin >> t[i].X >> t[i].Y; --t[i].X; --t[i].Y; } bool hit = true; while (hit) { hit = false; for (int i = 0; i < m; ++i) { pii p = t[i]; if (v[p.X].X < v[p.Y].X + 1) { v[p.X].X = v[p.Y].X + 1; hit = true; } } } sort(v.begin(), v.end(), greater<pii>()); for (int i = 0; i < m; ++i) { cout << v.at(i).Y << endl; } } return 0; }
#include <algorithm> #include <functional> #include <iostream> #include <vector> #define mp(x, y) make_pair(x, y) #define X first #define Y second using namespace std; typedef pair<int, int> pii; int main(void) { int m; while (cin >> m) { vector<pii> v(m); for (int i = 0; i < m; ++i) { v[i].X = 0; v[i].Y = i + 1; } int n; cin >> n; vector<pii> t(n); for (int i = 0; i < n; ++i) { cin >> t[i].X >> t[i].Y; --t[i].X; --t[i].Y; } bool hit = true; while (hit) { hit = false; for (int i = 0; i < n; ++i) { pii p = t[i]; if (v[p.X].X < v[p.Y].X + 1) { v[p.X].X = v[p.Y].X + 1; hit = true; } } } sort(v.begin(), v.end(), greater<pii>()); for (int i = 0; i < m; ++i) { cout << v.at(i).Y << endl; } } return 0; }
replace
41
42
41
42
0
p00120
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <sstream> #include <string> #include <vector> using namespace std; int main(void) { int N; string s; while (getline(cin, s)) { long long int D = 100000; vector<double> A, B; istringstream ss(s); int t; ss >> N; while (ss >> t) A.push_back(t); // for(int i = 0;i < A.size();i++)cout<<A[i]<<endl; double best = 1000000; double ans = 1000000; while (D--) { vector<double> V1, V2; srand(time(NULL) + D); V1 = A; double sum = 0; int x = rand() % A.size(); int y = rand() % A.size(); swap(V1[x], V1[y]); for (int i = 0; i < V1.size() - 1; i++) { double R1 = V1[i] + V1[i + 1]; double R2 = V1[i] - V1[i + 1]; sum += sqrt((R1 * R1) - (R2 * R2)); } sum += V1[0] + V1[V1.size() - 1]; double e = pow((sum / ans), 75); double z = (double)((int)(rand() % 1000000)) / 1000000; if (z >= (1 - (1 / e))) ans = sum, A = V1; /* if(D%10000 == 0){ for(int i = 0;i < A.size();i++){ cout<<A[i]<<" "; } cout<<" : "; for(int i = 0;i < B.size();i++){ cout<<B[i]<<" "; } cout<<endl; }*/ if (sum < best) best = sum, B = V1; } int siz = 0; for (int i = 0; i < 1000; i++) { if (best <= (double)i) { siz = i; // cout<<siz<<endl; break; } } if (siz <= N) cout << "OK" << endl; else cout << "NA" << endl; } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <sstream> #include <string> #include <vector> using namespace std; int main(void) { int N; string s; while (getline(cin, s)) { long long int D = 10000; vector<double> A, B; istringstream ss(s); int t; ss >> N; while (ss >> t) A.push_back(t); // for(int i = 0;i < A.size();i++)cout<<A[i]<<endl; double best = 1000000; double ans = 1000000; while (D--) { vector<double> V1, V2; srand(time(NULL) + D); V1 = A; double sum = 0; int x = rand() % A.size(); int y = rand() % A.size(); swap(V1[x], V1[y]); for (int i = 0; i < V1.size() - 1; i++) { double R1 = V1[i] + V1[i + 1]; double R2 = V1[i] - V1[i + 1]; sum += sqrt((R1 * R1) - (R2 * R2)); } sum += V1[0] + V1[V1.size() - 1]; double e = pow((sum / ans), 75); double z = (double)((int)(rand() % 1000000)) / 1000000; if (z >= (1 - (1 / e))) ans = sum, A = V1; /* if(D%10000 == 0){ for(int i = 0;i < A.size();i++){ cout<<A[i]<<" "; } cout<<" : "; for(int i = 0;i < B.size();i++){ cout<<B[i]<<" "; } cout<<endl; }*/ if (sum < best) best = sum, B = V1; } int siz = 0; for (int i = 0; i < 1000; i++) { if (best <= (double)i) { siz = i; // cout<<siz<<endl; break; } } if (siz <= N) cout << "OK" << endl; else cout << "NA" << endl; } return 0; }
replace
19
20
19
20
TLE
p00120
C++
Time Limit Exceeded
#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 <iterator> #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; #define FOR(i, n) for (int i = 0; i < (n); i++) #define sz(c) ((int)(c).size()) template <class T> void splitstr(const string &s, vector<T> &out) { istringstream in(s); out.clear(); copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); } vector<int> a; double memo[12][1 << 12]; double solve(int start, int flag, int rest) { if (rest == 0) return memo[start][flag] = a[start]; double ret = INF; FOR(i, sz(a)) { if ((1 << i) & flag) continue; double add = solve(i, flag | (1 << i), rest - 1) + sqrt((double)a[start] * a[i]) * 2; ret = min(ret, add); } return memo[start][flag] = ret; } int main() { string s; while (getline(cin, s)) { splitstr(s, a); int n = a[0]; a.erase(a.begin()); int m = sz(a); double mn = INF; FOR(j, m) fill(memo[j], memo[j] + (1 << m), -1.0); FOR(i, m) { mn = min(mn, solve(i, 1 << i, m - 1) + a[i]); } cout << (n >= mn ? "OK" : "NA") << 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 <iterator> #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; #define FOR(i, n) for (int i = 0; i < (n); i++) #define sz(c) ((int)(c).size()) template <class T> void splitstr(const string &s, vector<T> &out) { istringstream in(s); out.clear(); copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); } vector<int> a; double memo[12][1 << 12]; double solve(int start, int flag, int rest) { if (memo[start][flag] > 0) return memo[start][flag]; if (rest == 0) return memo[start][flag] = a[start]; double ret = INF; FOR(i, sz(a)) { if ((1 << i) & flag) continue; double add = solve(i, flag | (1 << i), rest - 1) + sqrt((double)a[start] * a[i]) * 2; ret = min(ret, add); } return memo[start][flag] = ret; } int main() { string s; while (getline(cin, s)) { splitstr(s, a); int n = a[0]; a.erase(a.begin()); int m = sz(a); double mn = INF; FOR(j, m) fill(memo[j], memo[j] + (1 << m), -1.0); FOR(i, m) { mn = min(mn, solve(i, 1 << i, m - 1) + a[i]); } cout << (n >= mn ? "OK" : "NA") << endl; } return 0; }
insert
50
50
50
52
TLE
p00120
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <list> #include <map> #include <string> #include <vector> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define SUPrl 13 #define UNDEFCHAN 1145 int rl; int rs[SUPrl]; int L; double memf[1 << SUPrl][SUPrl]; void resetf() { FOR(s, 0, 1 << rl) FOR(ri, 0, rl) memf[s][ri] = UNDEFCHAN; } double f(int s, int ri) { // min_({a0,a1,..ri}=s) (sqrt(a0)-sqrt(a1))^2 + // (sqrt(a1)-sqrt(a2))^2 + ..ri))^2 if (memf[s][ri] != UNDEFCHAN) return memf[s][ri]; double re = 0; int a = 0; FOR(ari, 0, rl) if ((s >> ari & 1) == 1) a++; if (a == 1) re = 0; else { FOR(bri, 0, rl) { if ((s >> bri & 1) == 0) continue; if (bri == ri) continue; re = max(re, f(s - (1 << ri), bri) + pow(sqrt(rs[bri]) - sqrt(rs[ri]), 2)); } } memf[s][ri] = re; return re; } void showf() { FOR(s, 0, 1 << rl) FOR(ri, 0, rl) cout << s << " " << ri << " " << memf[s][ri] << endl; } double solve() { double re = 0; FOR(ri, 0, rl) re = max(re, f((1 << rl) - 1, ri)); re = -re; FOR(ri, 0, rl) re += 2 * rs[ri]; return re; } int main() { while (!cin.eof()) { cin >> L; // if(!L) return 0; rl = 0; while (cin.peek() != '\n') { cin >> rs[rl++]; } resetf(); double re = solve(); // showf(); if (re <= L) cout << "OK" << endl; else cout << "NA" << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <list> #include <map> #include <string> #include <vector> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define SUPrl 13 #define UNDEFCHAN 1145 int rl; int rs[SUPrl]; int L; double memf[1 << SUPrl][SUPrl]; void resetf() { FOR(s, 0, 1 << rl) FOR(ri, 0, rl) memf[s][ri] = UNDEFCHAN; } double f(int s, int ri) { // min_({a0,a1,..ri}=s) (sqrt(a0)-sqrt(a1))^2 + // (sqrt(a1)-sqrt(a2))^2 + ..ri))^2 if (memf[s][ri] != UNDEFCHAN) return memf[s][ri]; double re = 0; int a = 0; FOR(ari, 0, rl) if ((s >> ari & 1) == 1) a++; if (a == 1) re = 0; else { FOR(bri, 0, rl) { if ((s >> bri & 1) == 0) continue; if (bri == ri) continue; re = max(re, f(s - (1 << ri), bri) + pow(sqrt(rs[bri]) - sqrt(rs[ri]), 2)); } } memf[s][ri] = re; return re; } void showf() { FOR(s, 0, 1 << rl) FOR(ri, 0, rl) cout << s << " " << ri << " " << memf[s][ri] << endl; } double solve() { double re = 0; FOR(ri, 0, rl) re = max(re, f((1 << rl) - 1, ri)); re = -re; FOR(ri, 0, rl) re += 2 * rs[ri]; return re; } int main() { while (cin >> L) { // if(!L) return 0; rl = 0; while (cin.peek() != '\n') { cin >> rs[rl++]; } resetf(); double re = solve(); // showf(); if (re <= L) cout << "OK" << endl; else cout << "NA" << endl; } return 0; }
replace
63
65
63
64
TLE
p00121
C++
Time Limit Exceeded
#define _USE_MATH_DEFINES #include <algorithm> #include <cctype> #include <cfloat> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef pair<double, double> P; int b[19][19]; int main() { int dx[] = {1, -1, -4, 4}; string s = "01234567"; queue<string> que; map<string, int> ans; que.push(s); ans[s] = 0; while (que.size()) { string q = que.front(); que.pop(); int x = q.find("0"); for (int i = 0; i < 4; i++) { int next = x + dx[i]; if (next >= 0 && next <= 7 && !(x == 3 && next == 4) && !(x == 4 && next == 3)) { string nextq = q; swap(nextq[x], nextq[next]); if (ans.find(nextq) == ans.end()) { que.push(nextq); ans[nextq] = ans[q] + 1; } } } } while (1) { string input = ""; char temp; for (int i = 0; i < 8; i++) { if (cin >> temp) input.push_back(temp); } cout << ans[input] << endl; } }
#define _USE_MATH_DEFINES #include <algorithm> #include <cctype> #include <cfloat> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef pair<double, double> P; int b[19][19]; int main() { int dx[] = {1, -1, -4, 4}; string s = "01234567"; queue<string> que; map<string, int> ans; que.push(s); ans[s] = 0; while (que.size()) { string q = que.front(); que.pop(); int x = q.find("0"); for (int i = 0; i < 4; i++) { int next = x + dx[i]; if (next >= 0 && next <= 7 && !(x == 3 && next == 4) && !(x == 4 && next == 3)) { string nextq = q; swap(nextq[x], nextq[next]); if (ans.find(nextq) == ans.end()) { que.push(nextq); ans[nextq] = ans[q] + 1; } } } } string input = ""; while (getline(cin, input)) { input.erase(remove(input.begin(), input.end(), ' '), input.end()); cout << ans[input] << endl; } }
replace
53
60
53
56
TLE
p00121
C++
Memory Limit Exceeded
#include <cstdio> #include <queue> #include <vector> using namespace std; int ans[1 << 24]; int adj[] = {-6, -1, 1, 6}; int pos[] = {7, 8, 9, 10, 13, 14, 15, 16}; int encode(const vector<int> &v) { int r = 0; for (int i = 0; i < 8; ++i) { r |= v[pos[i]] << (3 * i); } return r; } void decode(int r, vector<int> &ret) { ret.assign(24, -1); for (int i = 0; i < 8; ++i) { ret[pos[i]] = r & 7; r >>= 3; } } int main() { for (int i = 0; i < (1 << 24); ++i) { ans[i] = -1; } vector<int> v(24, -1), w; for (int i = 0; i < 8; ++i) { v[pos[i]] = i; } int c = encode(v); ans[c] = 0; queue<int> q; q.push(c); q.push(-1); int tm = 1; while (q.size() > 1) { c = q.front(); q.pop(); if (c == -1) { ++tm; q.push(-1); } else { decode(c, v); int x; for (x = 7; v[x]; ++x) ; for (int i = 0; i < 4; ++i) { int y = x + adj[i]; if (v[y] > 0) { swap(v[x], v[y]); c = encode(v); if (ans[c] == -1) { ans[c] = tm; q.push(c); } swap(v[x], v[y]); } } } } while (1) { for (int i = 0; i < 8; ++i) { if (scanf("%d", &v[pos[i]]) == EOF) { return 0; } } printf("%d\n", ans[encode(v)]); } }
#include <cstdio> #include <queue> #include <vector> using namespace std; short ans[1 << 24]; int adj[] = {-6, -1, 1, 6}; int pos[] = {7, 8, 9, 10, 13, 14, 15, 16}; int encode(const vector<int> &v) { int r = 0; for (int i = 0; i < 8; ++i) { r |= v[pos[i]] << (3 * i); } return r; } void decode(int r, vector<int> &ret) { ret.assign(24, -1); for (int i = 0; i < 8; ++i) { ret[pos[i]] = r & 7; r >>= 3; } } int main() { for (int i = 0; i < (1 << 24); ++i) { ans[i] = -1; } vector<int> v(24, -1), w; for (int i = 0; i < 8; ++i) { v[pos[i]] = i; } int c = encode(v); ans[c] = 0; queue<int> q; q.push(c); q.push(-1); int tm = 1; while (q.size() > 1) { c = q.front(); q.pop(); if (c == -1) { ++tm; q.push(-1); } else { decode(c, v); int x; for (x = 7; v[x]; ++x) ; for (int i = 0; i < 4; ++i) { int y = x + adj[i]; if (v[y] > 0) { swap(v[x], v[y]); c = encode(v); if (ans[c] == -1) { ans[c] = tm; q.push(c); } swap(v[x], v[y]); } } } } while (1) { for (int i = 0; i < 8; ++i) { if (scanf("%d", &v[pos[i]]) == EOF) { return 0; } } printf("%d\n", ans[encode(v)]); } }
replace
5
6
5
6
MLE
p00121
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, a) for (int i = 0; i < (a); i++) #define reps(i, a, b) for (int i = (a); i < (b); i++) #define INF 1e9 typedef vector<int> vi; typedef vector<vi> vii; typedef pair<int, int> pii; int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; int memo[16434825]; class Data { public: vii status; pii zero; int cnt; Data() {} Data(vii status, pii zero, int cnt) : status(status), zero(zero), cnt(cnt) {} }; int to_s(vector<vi> s) { int ret = 0; int r = 1; rep(i, 8) { ret += s[i / 4][i % 4] * r; r *= 8; } return ret; } void solve() { memset(memo, -1, sizeof(memo)); vii ans = {{0, 1, 2, 3}, {4, 5, 6, 7}}; queue<Data> Q; Q.push(Data(ans, make_pair(0, 0), 0)); while (!Q.empty()) { auto d = Q.front(); Q.pop(); int tmp = to_s(d.status); if (memo[tmp] != -1) continue; memo[tmp] = d.cnt; auto z = d.zero; rep(i, 4) { int nx = dx[i] + z.first, ny = dy[i] + z.second; if (nx < 0 || nx >= 4 || ny < 0 || ny >= 2) continue; auto p = d.status; swap(p[z.second][z.first], p[ny][nx]); Q.push(Data(p, make_pair(nx, ny), d.cnt + 1)); } } } int main() { vii v = vii(2, vi(4, 0)); solve(); while (1) { cin >> v[0][0]; if (cin.eof()) break; reps(i, 1, 8) cin >> v[i / 4][i % 4]; printf("%d\n", memo[to_s(v)]); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, a) for (int i = 0; i < (a); i++) #define reps(i, a, b) for (int i = (a); i < (b); i++) #define INF 1e9 typedef vector<int> vi; typedef vector<vi> vii; typedef pair<int, int> pii; int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; char memo[16434825]; class Data { public: vii status; pii zero; int cnt; Data() {} Data(vii status, pii zero, int cnt) : status(status), zero(zero), cnt(cnt) {} }; int to_s(vector<vi> s) { int ret = 0; int r = 1; rep(i, 8) { ret += s[i / 4][i % 4] * r; r *= 8; } return ret; } void solve() { memset(memo, -1, sizeof(memo)); vii ans = {{0, 1, 2, 3}, {4, 5, 6, 7}}; queue<Data> Q; Q.push(Data(ans, make_pair(0, 0), 0)); while (!Q.empty()) { auto d = Q.front(); Q.pop(); int tmp = to_s(d.status); if (memo[tmp] != -1) continue; memo[tmp] = d.cnt; auto z = d.zero; rep(i, 4) { int nx = dx[i] + z.first, ny = dy[i] + z.second; if (nx < 0 || nx >= 4 || ny < 0 || ny >= 2) continue; auto p = d.status; swap(p[z.second][z.first], p[ny][nx]); Q.push(Data(p, make_pair(nx, ny), d.cnt + 1)); } } } int main() { vii v = vii(2, vi(4, 0)); solve(); while (1) { cin >> v[0][0]; if (cin.eof()) break; reps(i, 1, 8) cin >> v[i / 4][i % 4]; printf("%d\n", memo[to_s(v)]); } return 0; }
replace
9
10
9
10
MLE
p00121
C++
Memory Limit Exceeded
#include <algorithm> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define INF 100000000 typedef long long int lli; typedef pair<int, int> P; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool f[8][8][8][8][8][8][8][8]; int cnt[8][8][8][8][8][8][8][8]; int r[2][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}}; struct puzzle { int card[2][4]; int cnt; }; int main() { puzzle sp; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 4; ++j) { sp.card[i][j] = r[i][j]; } } memset(f, false, sizeof(f)); memset(cnt, 0, sizeof(cnt)); sp.cnt = 0; queue<puzzle> que; que.push(sp); while (que.size()) { puzzle pz = que.front(); que.pop(); cnt[pz.card[0][0]][pz.card[0][1]][pz.card[0][2]][pz.card[0][3]] [pz.card[1][0]][pz.card[1][1]][pz.card[1][2]][pz.card[1][3]] = pz.cnt; f[pz.card[0][0]][pz.card[0][1]][pz.card[0][2]][pz.card[0][3]][pz.card[1][0]] [pz.card[1][1]][pz.card[1][2]][pz.card[1][3]] = true; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 4; ++j) { if (pz.card[i][j] == 0) { if (0 <= i - 1) { puzzle np = pz; np.cnt = pz.cnt + 1; swap(np.card[i - 1][j], np.card[i][j]); if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]] [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]]) que.push(np); } if (i + 1 < 2) { puzzle np = pz; np.cnt = pz.cnt + 1; swap(np.card[i + 1][j], np.card[i][j]); if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]] [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]]) que.push(np); } if (0 <= j - 1) { puzzle np = pz; np.cnt = pz.cnt + 1; swap(np.card[i][j - 1], np.card[i][j]); if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]] [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]]) que.push(np); } if (j + 1 < 4) { puzzle np = pz; np.cnt = pz.cnt + 1; swap(np.card[i][j + 1], np.card[i][j]); if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]] [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]]) que.push(np); } } } } } int n[8]; int ans; while (cin >> n[0] >> n[1] >> n[2] >> n[3] >> n[4] >> n[5] >> n[6] >> n[7]) { ans = cnt[n[0]][n[1]][n[2]][n[3]][n[4]][n[5]][n[6]][n[7]]; cout << ans << "\n"; } return 0; }
#include <algorithm> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define INF 100000000 typedef long long int lli; typedef pair<int, int> P; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool f[8][8][8][8][8][8][8][8]; short cnt[8][8][8][8][8][8][8][8]; int r[2][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}}; struct puzzle { int card[2][4]; int cnt; }; int main() { puzzle sp; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 4; ++j) { sp.card[i][j] = r[i][j]; } } memset(f, false, sizeof(f)); memset(cnt, 0, sizeof(cnt)); sp.cnt = 0; queue<puzzle> que; que.push(sp); while (que.size()) { puzzle pz = que.front(); que.pop(); cnt[pz.card[0][0]][pz.card[0][1]][pz.card[0][2]][pz.card[0][3]] [pz.card[1][0]][pz.card[1][1]][pz.card[1][2]][pz.card[1][3]] = pz.cnt; f[pz.card[0][0]][pz.card[0][1]][pz.card[0][2]][pz.card[0][3]][pz.card[1][0]] [pz.card[1][1]][pz.card[1][2]][pz.card[1][3]] = true; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 4; ++j) { if (pz.card[i][j] == 0) { if (0 <= i - 1) { puzzle np = pz; np.cnt = pz.cnt + 1; swap(np.card[i - 1][j], np.card[i][j]); if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]] [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]]) que.push(np); } if (i + 1 < 2) { puzzle np = pz; np.cnt = pz.cnt + 1; swap(np.card[i + 1][j], np.card[i][j]); if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]] [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]]) que.push(np); } if (0 <= j - 1) { puzzle np = pz; np.cnt = pz.cnt + 1; swap(np.card[i][j - 1], np.card[i][j]); if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]] [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]]) que.push(np); } if (j + 1 < 4) { puzzle np = pz; np.cnt = pz.cnt + 1; swap(np.card[i][j + 1], np.card[i][j]); if (!f[np.card[0][0]][np.card[0][1]][np.card[0][2]][np.card[0][3]] [np.card[1][0]][np.card[1][1]][np.card[1][2]][np.card[1][3]]) que.push(np); } } } } } int n[8]; int ans; while (cin >> n[0] >> n[1] >> n[2] >> n[3] >> n[4] >> n[5] >> n[6] >> n[7]) { ans = cnt[n[0]][n[1]][n[2]][n[3]][n[4]][n[5]][n[6]][n[7]]; cout << ans << "\n"; } return 0; }
replace
22
23
22
23
MLE
p00121
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; typedef vector<int> V; typedef queue<V> Q; typedef map<V, int> M; class Solver { private: M min_count; public: Solver() { V p0(8); Q q; int zi; for (int i = 0; i < 8; ++i) { p0[i] = i; } q.push(p0); min_count[p0] = 0; while (!q.empty()) { V p = q.front(); int n = min_count[p]; q.pop(); zi = 0; while (p[zi] != 0) { ++zi; } if (zi != 0 && zi != 4) { V p2 = p; swap(p2[zi], p2[zi - 1]); if (min_count.find(p2) == min_count.end()) { min_count[p2] = n + 1; q.push(p2); } } if (zi != 3 && zi != 7) { V p2 = p; swap(p2[zi], p2[zi + 1]); if (min_count.find(p2) == min_count.end()) { min_count[p2] = n + 1; q.push(p2); } } if (zi < 4) { V p2 = p; swap(p2[zi], p2[zi + 4]); if (min_count.find(p2) == min_count.end()) { min_count[p2] = n + 1; q.push(p2); } } else { V p2 = p; swap(p2[zi], p2[zi - 4]); if (min_count.find(p2) == min_count.end()) { min_count[p2] = n + 1; q.push(p2); } } } } int solve(int puzzle[]) { V p(8); for (int i = 0; i < 8; ++i) { p[i] = puzzle[i]; } return min_count[p]; } }; int main() { int puzzle[8]; Solver s; while (true) { for (int i = 0; i < 8; ++i) { cin >> puzzle[i]; } cout << s.solve(puzzle) << endl; } }
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; typedef vector<int> V; typedef queue<V> Q; typedef map<V, int> M; class Solver { private: M min_count; public: Solver() { V p0(8); Q q; int zi; for (int i = 0; i < 8; ++i) { p0[i] = i; } q.push(p0); min_count[p0] = 0; while (!q.empty()) { V p = q.front(); int n = min_count[p]; q.pop(); zi = 0; while (p[zi] != 0) { ++zi; } if (zi != 0 && zi != 4) { V p2 = p; swap(p2[zi], p2[zi - 1]); if (min_count.find(p2) == min_count.end()) { min_count[p2] = n + 1; q.push(p2); } } if (zi != 3 && zi != 7) { V p2 = p; swap(p2[zi], p2[zi + 1]); if (min_count.find(p2) == min_count.end()) { min_count[p2] = n + 1; q.push(p2); } } if (zi < 4) { V p2 = p; swap(p2[zi], p2[zi + 4]); if (min_count.find(p2) == min_count.end()) { min_count[p2] = n + 1; q.push(p2); } } else { V p2 = p; swap(p2[zi], p2[zi - 4]); if (min_count.find(p2) == min_count.end()) { min_count[p2] = n + 1; q.push(p2); } } } } int solve(int puzzle[]) { V p(8); for (int i = 0; i < 8; ++i) { p[i] = puzzle[i]; } return min_count[p]; } }; int main() { int puzzle[8]; Solver s; while (true) { for (int i = 0; i < 8; ++i) { cin >> puzzle[i]; } if (cin.eof()) { return 0; } cout << s.solve(puzzle) << endl; } }
insert
83
83
83
86
TLE
p00121
C++
Memory Limit Exceeded
#include <algorithm> #include <cstring> #include <fstream> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; int visited[0xffffff]; inline int is_visited(unsigned s) { if (visited[s & 0xffffff] == -1) return 0; else return 1; } inline void set_visited(unsigned s, int n) { visited[s & 0xffffff] = n; } inline unsigned exchange(unsigned s, unsigned p1, unsigned p2, unsigned n) { unsigned x = ((s >> p1) ^ (s >> p2)) & ((1U << n) - 1); unsigned ns = s ^ ((x << p1) | (x << p2)); return ns; } int main() { cin.sync_with_stdio(false); unsigned istate = 0; for (int i = 0; i < 8; ++i) istate |= (i << (3 * i)); queue<unsigned> q; q.push(istate); memset(visited, -1, sizeof(visited)); set_visited(istate, 0); for (int n = 0;; ++n) { queue<unsigned> nq; while (!q.empty()) { unsigned p = q.front(); q.pop(); unsigned state = (p & 0xffffff); unsigned pos = (p >> 24); int bit_pos = 3 * pos; unsigned next_state; if (pos != 0 && pos != 4) { next_state = exchange(state, bit_pos, bit_pos - 3, 3); next_state |= ((pos - 1) << 24); if (!is_visited(next_state)) { set_visited(next_state, n + 1); nq.push(next_state); } } if (pos != 3 && pos != 7) { next_state = exchange(state, bit_pos, bit_pos + 3, 3); next_state |= ((pos + 1) << 24); if (!is_visited(next_state)) { set_visited(next_state, n + 1); nq.push(next_state); } } if (pos <= 3) { next_state = exchange(state, bit_pos, bit_pos + 12, 3); next_state |= ((pos + 4) << 24); if (!is_visited(next_state)) { set_visited(next_state, n + 1); nq.push(next_state); } } if (pos >= 4) { next_state = exchange(state, bit_pos, bit_pos - 12, 3); next_state |= ((pos - 4) << 24); if (!is_visited(next_state)) { set_visited(next_state, n + 1); nq.push(next_state); } } } if (nq.empty()) break; q = nq; } unsigned int a[8]; while (cin >> a[0]) { for (int i = 1; i < 8; ++i) cin >> a[i]; unsigned state = 0; for (int i = 0; i < 8; ++i) state |= (a[i] << (3 * i)); cout << visited[state] << endl; } return 0; }
#include <algorithm> #include <cstring> #include <fstream> #include <iostream> #include <queue> #include <string> #include <vector> using namespace std; short visited[0xffffff]; inline int is_visited(unsigned s) { if (visited[s & 0xffffff] == -1) return 0; else return 1; } inline void set_visited(unsigned s, int n) { visited[s & 0xffffff] = n; } inline unsigned exchange(unsigned s, unsigned p1, unsigned p2, unsigned n) { unsigned x = ((s >> p1) ^ (s >> p2)) & ((1U << n) - 1); unsigned ns = s ^ ((x << p1) | (x << p2)); return ns; } int main() { cin.sync_with_stdio(false); unsigned istate = 0; for (int i = 0; i < 8; ++i) istate |= (i << (3 * i)); queue<unsigned> q; q.push(istate); memset(visited, -1, sizeof(visited)); set_visited(istate, 0); for (int n = 0;; ++n) { queue<unsigned> nq; while (!q.empty()) { unsigned p = q.front(); q.pop(); unsigned state = (p & 0xffffff); unsigned pos = (p >> 24); int bit_pos = 3 * pos; unsigned next_state; if (pos != 0 && pos != 4) { next_state = exchange(state, bit_pos, bit_pos - 3, 3); next_state |= ((pos - 1) << 24); if (!is_visited(next_state)) { set_visited(next_state, n + 1); nq.push(next_state); } } if (pos != 3 && pos != 7) { next_state = exchange(state, bit_pos, bit_pos + 3, 3); next_state |= ((pos + 1) << 24); if (!is_visited(next_state)) { set_visited(next_state, n + 1); nq.push(next_state); } } if (pos <= 3) { next_state = exchange(state, bit_pos, bit_pos + 12, 3); next_state |= ((pos + 4) << 24); if (!is_visited(next_state)) { set_visited(next_state, n + 1); nq.push(next_state); } } if (pos >= 4) { next_state = exchange(state, bit_pos, bit_pos - 12, 3); next_state |= ((pos - 4) << 24); if (!is_visited(next_state)) { set_visited(next_state, n + 1); nq.push(next_state); } } } if (nq.empty()) break; q = nq; } unsigned int a[8]; while (cin >> a[0]) { for (int i = 1; i < 8; ++i) cin >> a[i]; unsigned state = 0; for (int i = 0; i < 8; ++i) state |= (a[i] << (3 * i)); cout << visited[state] << endl; } return 0; }
replace
10
11
10
11
MLE
p00121
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int nx; int cnt = 0; int px[4] = {1, -1, 4, -4}; string str = " "; map<string, int> m; queue<int> q; queue<string> sq; q.push(0); sq.push("01234567"); while (!q.empty()) { int siz = q.size(); for (int nsiz = 0; nsiz < siz; nsiz++) { nx = q.front(); q.pop(); str = sq.front(); sq.pop(); if (m.count(str) == 0) { m[str] = cnt; for (int i = 0; i < 4; i++) { if (i < 2 && nx + px[i] >= 0 && nx + px[i] < 8 && nx / 4 == (nx + px[i]) / 4) { swap(str[nx], str[nx + px[i]]); q.push(nx + px[i]); sq.push(str); swap(str[nx], str[nx + px[i]]); } if (i > 1 && nx + px[i] >= 0 && nx + px[i] < 8) { swap(str[nx], str[nx + px[i]]); q.push(nx + px[i]); sq.push(str); swap(str[nx], str[nx + px[i]]); } } } } cnt++; } while (1) { for (int i = 0; i < 8; i++) { cin >> str[i]; if (str[i] == EOF) return (0); } cout << m[str] << endl; } return (0); }
#include <bits/stdc++.h> using namespace std; int main() { int nx; int cnt = 0; int px[4] = {1, -1, 4, -4}; string str = " "; map<string, int> m; queue<int> q; queue<string> sq; q.push(0); sq.push("01234567"); while (!q.empty()) { int siz = q.size(); for (int nsiz = 0; nsiz < siz; nsiz++) { nx = q.front(); q.pop(); str = sq.front(); sq.pop(); if (m.count(str) == 0) { m[str] = cnt; for (int i = 0; i < 4; i++) { if (i < 2 && nx + px[i] >= 0 && nx + px[i] < 8 && nx / 4 == (nx + px[i]) / 4) { swap(str[nx], str[nx + px[i]]); q.push(nx + px[i]); sq.push(str); swap(str[nx], str[nx + px[i]]); } if (i > 1 && nx + px[i] >= 0 && nx + px[i] < 8) { swap(str[nx], str[nx + px[i]]); q.push(nx + px[i]); sq.push(str); swap(str[nx], str[nx + px[i]]); } } } } cnt++; } while (1) { for (int i = 0; i < 8; i++) { if (!(cin >> str[i])) return 0; } cout << m[str] << endl; } return (0); }
replace
48
51
48
50
TLE
p00121
C++
Time Limit Exceeded
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <ostream> #include <queue> #include <stack> #include <string> #include <vector> #define INF 1000000000 #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rev(i, a, b) for (int i = (a)-1; i >= b; i--) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef queue<int> qi; typedef queue<pair<int, int>> qpii; typedef vector<int> vi; typedef vector<int, int> vii; typedef vector<string> vs; typedef vector<pair<int, int>> vpii; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; map<string, int> d; void bfs() { int dn[4] = {4, -4, -1, 1}; queue<string> qs; qs.push("01234567"); d["01234567"] = 0; while (qs.size()) { string s = qs.front(); qs.pop(); int n = s.find("0"); rep(i, 0, 4) { int ne = n + dn[i]; if (0 <= ne && ne < 8) { if ((n == 4 && ne == 3) || (n == 3 && ne == 4)) continue; string ss = s; char tmp = ss[n]; ss[n] = ss[ne]; ss[ne] = tmp; if (d.find(ss) == d.end()) { qs.push(ss); d[ss] = d[s] + 1; } } } } } int main() { cin.tie(0); ios::sync_with_stdio(false); bfs(); while (1) { string in = ""; char num; rep(i, 0, 8) { if (cin >> num) in += num; else break; } cout << d[in] << endl; } return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <ostream> #include <queue> #include <stack> #include <string> #include <vector> #define INF 1000000000 #define rep(i, a, b) for (int i = (a); i < (b); i++) #define rev(i, a, b) for (int i = (a)-1; i >= b; i--) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef queue<int> qi; typedef queue<pair<int, int>> qpii; typedef vector<int> vi; typedef vector<int, int> vii; typedef vector<string> vs; typedef vector<pair<int, int>> vpii; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; map<string, int> d; void bfs() { int dn[4] = {4, -4, -1, 1}; queue<string> qs; qs.push("01234567"); d["01234567"] = 0; while (qs.size()) { string s = qs.front(); qs.pop(); int n = s.find("0"); rep(i, 0, 4) { int ne = n + dn[i]; if (0 <= ne && ne < 8) { if ((n == 4 && ne == 3) || (n == 3 && ne == 4)) continue; string ss = s; char tmp = ss[n]; ss[n] = ss[ne]; ss[ne] = tmp; if (d.find(ss) == d.end()) { qs.push(ss); d[ss] = d[s] + 1; } } } } } int main() { cin.tie(0); ios::sync_with_stdio(false); bfs(); while (1) { string in = ""; char num; rep(i, 0, 8) { if (cin >> num) in += num; else return 0; } cout << d[in] << endl; } return 0; }
replace
75
76
75
76
TLE
p00122
C++
Memory Limit Exceeded
#include <iostream> #include <queue> #include <utility> #define P pair<int, int> using namespace std; int main(void) { std::ios_base::sync_with_stdio(); P st; int dx[] = {2, 2, 2, 1, 1, 0, 0, -1, -1, -2, -2, -2}; int dy[] = {0, 1, -1, 2, -2, 2, -2, 2, -2, 0, 1, -1}; int kx[] = {1, 1, 1, 0, 0, 0, -1, -1, -1}; int ky[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; int nx, ny; int be, af; int n; int cnt; int map[10][10][10]; bool flag; int sql[2][10]; /*0...x 1...y*/ queue<P> now; while (1) { cin >> st.first >> st.second; if (st.first == 0 && st.second == 0) break; cin >> n; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { map[i][j][k] = -1; } } } for (int i = 0; i < n; i++) cin >> sql[0][i] >> sql[1][i]; now.push(P(st.first, st.second)); flag = false; cnt = 0; be = 1; af = 0; while (!now.empty() && !flag) { nx = now.front().first; ny = now.front().second; map[cnt][nx][ny] = 1; for (int i = 0; i < 12; i++) { if (nx + dx[i] >= 0 && nx + dx[i] < 10 && ny + dy[i] >= 0 && ny + dy[i] < 10) { for (int j = 0; j < 9; j++) { if (sql[0][cnt] + kx[j] >= 0 && sql[0][cnt] + kx[j] < 10 && sql[1][cnt] + ky[j] >= 0 && sql[1][cnt] + ky[j] < 10) { if (nx + dx[i] == sql[0][cnt] + kx[j] && ny + dy[i] == sql[1][cnt] + ky[j]) { if (map[(cnt + 1) % n][nx + dx[i]][ny + dy[i]] == 1) flag = true; else if (map[(cnt + 1) % n][nx + dx[i]][ny + dy[i]] == -1) { af++; now.push(P(nx + dx[i], ny + dy[i])); } } } } } } be--; if (be == 0) { be = af; af = 0; cnt++; if (cnt == n) cnt = 0; } now.pop(); } if (flag) cout << "OK" << endl; else cout << "NA" << endl; } return 0; }
#include <iostream> #include <queue> #include <utility> #define P pair<int, int> using namespace std; int main(void) { std::ios_base::sync_with_stdio(); P st; int dx[] = {2, 2, 2, 1, 1, 0, 0, -1, -1, -2, -2, -2}; int dy[] = {0, 1, -1, 2, -2, 2, -2, 2, -2, 0, 1, -1}; int kx[] = {1, 1, 1, 0, 0, 0, -1, -1, -1}; int ky[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; int nx, ny; int be, af; int n; int cnt; int map[10][10][10]; bool flag; int sql[2][10]; /*0...x 1...y*/ queue<P> now; while (1) { cin >> st.first >> st.second; if (st.first == 0 && st.second == 0) break; cin >> n; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { map[i][j][k] = -1; } } } for (int i = 0; i < n; i++) cin >> sql[0][i] >> sql[1][i]; now.push(P(st.first, st.second)); flag = false; cnt = 0; be = 1; af = 0; while (!now.empty() && !flag) { nx = now.front().first; ny = now.front().second; map[cnt][nx][ny] = 1; for (int i = 0; i < 12; i++) { if (nx + dx[i] >= 0 && nx + dx[i] < 10 && ny + dy[i] >= 0 && ny + dy[i] < 10) { for (int j = 0; j < 9; j++) { if (sql[0][cnt] + kx[j] >= 0 && sql[0][cnt] + kx[j] < 10 && sql[1][cnt] + ky[j] >= 0 && sql[1][cnt] + ky[j] < 10) { if (nx + dx[i] == sql[0][cnt] + kx[j] && ny + dy[i] == sql[1][cnt] + ky[j]) { if (map[(cnt + 1) % n][nx + dx[i]][ny + dy[i]] == 1) flag = true; else if (map[(cnt + 1) % n][nx + dx[i]][ny + dy[i]] == -1) { af++; map[(cnt + 1) % n][nx + dx[i]][ny + dy[i]] = 0; now.push(P(nx + dx[i], ny + dy[i])); } } } } } } be--; if (be == 0) { be = af; af = 0; cnt++; if (cnt == n) cnt = 0; } now.pop(); } if (flag) cout << "OK" << endl; else cout << "NA" << endl; } return 0; }
insert
60
60
60
61
MLE
p00123
C++
Runtime Error
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { double t1, t2; while (scanf("%lf %lf\n", &t1, &t2) != EOF) { printf("%lf %lf\n", t1, t2); const double bound_1[7] = {35.5, 37.5, 40, 43, 50, 55, 70}; const double bound_2[7] = {71, 77, 83, 89, 105, 116, 148}; const string c[7] = {"AAA", "AA", "A", "B", "C", "D", "E"}; int i = 0, num; while (1) { if (t1 < bound_1[i] && t2 < bound_2[i]) { cout << c[i] << endl; break; } if (i > 7) { printf("NA\n"); break; } i++; } /* for( int i = 0; i < 7; i++ ) { if( t1 > bound_1[i] ) { num1 = i; } if( t2 > bound_2[i] ) { num2 = i; } } */ } return 0; }
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { double t1, t2; while (scanf("%lf %lf\n", &t1, &t2) != EOF) { const double bound_1[100] = {35.5, 37.5, 40, 43, 50, 55, 70}; const double bound_2[100] = {71, 77, 83, 89, 105, 116, 148}; const string c[100] = {"AAA", "AA", "A", "B", "C", "D", "E"}; int i = 0, num; while (1) { if (t1 < bound_1[i] && t2 < bound_2[i]) { cout << c[i] << endl; break; } if (i > 7) { printf("NA\n"); break; } i++; } /* for( int i = 0; i < 7; i++ ) { if( t1 > bound_1[i] ) { num1 = i; } if( t2 > bound_2[i] ) { num2 = i; } } */ } return 0; }
replace
9
13
9
12
0
p00123
C++
Time Limit Exceeded
#include <stdio.h> int main(void) { double a, b; while (scanf("%lf%lf", &a, &b)) { 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"); } return 0; }
#include <stdio.h> int main(void) { double a, b; while (scanf("%lf%lf", &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"); } return 0; }
replace
4
5
4
5
TLE
p00123
C++
Time Limit Exceeded
#include <stdio.h> int main() { float hideo, ruio; while (scanf("%f %f", &hideo, &ruio)) { if (hideo < 35.50 && ruio < 71.00) { printf("AAA\n"); } else if (hideo < 37.50 && ruio < 77.00) { printf("AA\n"); } else if (hideo < 40.00 && ruio < 83.00) { printf("A\n"); } else if (hideo < 43.00 && ruio < 89.00) { printf("B\n"); } else if (hideo < 50.00 && ruio < 105.00) { printf("C\n"); } else if (hideo < 55.00 && ruio < 116.00) { printf("D\n"); } else if (hideo < 70.00 && ruio < 148.00) { printf("E\n"); } else { printf("NA\n"); } } return 0; }
#include <stdio.h> int main() { float hideo, ruio; while (scanf("%f %f", &hideo, &ruio) != EOF) { if (hideo < 35.50 && ruio < 71.00) { printf("AAA\n"); } else if (hideo < 37.50 && ruio < 77.00) { printf("AA\n"); } else if (hideo < 40.00 && ruio < 83.00) { printf("A\n"); } else if (hideo < 43.00 && ruio < 89.00) { printf("B\n"); } else if (hideo < 50.00 && ruio < 105.00) { printf("C\n"); } else if (hideo < 55.00 && ruio < 116.00) { printf("D\n"); } else if (hideo < 70.00 && ruio < 148.00) { printf("E\n"); } else { printf("NA\n"); } } return 0; }
replace
3
4
3
4
TLE
p00123
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) #define ll long long #define INF 114514 using namespace std; int main() { double fh, om; while (scanf("%lf %lf", &fh, &om)) { if (fh < 35.5 && om < 71.0) { puts("AAA"); } else if (fh < 37.5 && om < 77.0) { puts("AA"); } else if (fh < 40.0 && om < 83.0) { puts("A"); } else if (fh < 43.0 && om < 89.0) { puts("B"); } else if (fh < 50.0 && om < 105.0) { puts("C"); } else if (fh < 55.0 && om < 116.0) { puts("D"); } else if (fh < 70.0 && om < 148.0) { puts("E"); } else { puts("NA"); } } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) #define ll long long #define INF 114514 using namespace std; int main() { double fh, om; while (cin >> fh >> om) { if (fh < 35.5 && om < 71.0) { puts("AAA"); } else if (fh < 37.5 && om < 77.0) { puts("AA"); } else if (fh < 40.0 && om < 83.0) { puts("A"); } else if (fh < 43.0 && om < 89.0) { puts("B"); } else if (fh < 50.0 && om < 105.0) { puts("C"); } else if (fh < 55.0 && om < 116.0) { puts("D"); } else if (fh < 70.0 && om < 148.0) { puts("E"); } else { puts("NA"); } } return 0; }
replace
22
23
22
23
TLE
p00123
C++
Time Limit Exceeded
#include <iostream> using namespace std; int result500 = 0, result1000 = 0; void time500m(double time) { if (time < 35.50) { result500 = 0; } else if (time >= 35.50 && time < 37.50) { result500 = 1; } else if (time >= 37.50 && time < 40) { result500 = 2; } else if (time >= 40 && time < 43) { result500 = 3; } else if (time >= 43 && time < 50) { result500 = 4; } else if (time >= 50 && time < 55) { result500 = 5; } else if (time >= 55 && time < 70) { result500 = 6; } else if (time >= 70) { result500 = 7; } } void time1000m(double time) { if (time < 71) { result1000 = 0; } else if (time >= 71 && time < 77) { result1000 = 1; } else if (time >= 77 && time < 83) { result1000 = 2; } else if (time >= 83 && time < 89) { result1000 = 3; } else if (time >= 89 && time < 105) { result1000 = 4; } else if (time >= 105 && time < 116) { result1000 = 5; } else if (time >= 116 && time < 148) { result1000 = 6; } else if (time >= 148) { result1000 = 7; } } int main() { int result; double time500, time1000; while (1) { cin >> time500 >> time1000; time500m(time500); time1000m(time1000); if (result500 > result1000) { result = result500; } else { result = result1000; } switch (result) { case 0: cout << "AAA" << endl; break; case 1: cout << "AA" << endl; break; case 2: cout << "A" << endl; break; case 3: cout << "B" << endl; break; case 4: cout << "C" << endl; break; case 5: cout << "D" << endl; break; case 6: cout << "E" << endl; break; case 7: cout << "NA" << endl; break; } } return 0; }
#include <iostream> using namespace std; int result500 = 0, result1000 = 0; void time500m(double time) { if (time < 35.50) { result500 = 0; } else if (time >= 35.50 && time < 37.50) { result500 = 1; } else if (time >= 37.50 && time < 40) { result500 = 2; } else if (time >= 40 && time < 43) { result500 = 3; } else if (time >= 43 && time < 50) { result500 = 4; } else if (time >= 50 && time < 55) { result500 = 5; } else if (time >= 55 && time < 70) { result500 = 6; } else if (time >= 70) { result500 = 7; } } void time1000m(double time) { if (time < 71) { result1000 = 0; } else if (time >= 71 && time < 77) { result1000 = 1; } else if (time >= 77 && time < 83) { result1000 = 2; } else if (time >= 83 && time < 89) { result1000 = 3; } else if (time >= 89 && time < 105) { result1000 = 4; } else if (time >= 105 && time < 116) { result1000 = 5; } else if (time >= 116 && time < 148) { result1000 = 6; } else if (time >= 148) { result1000 = 7; } } int main() { int result; double time500, time1000; while (cin >> time500 >> time1000) { time500m(time500); time1000m(time1000); if (result500 > result1000) { result = result500; } else { result = result1000; } switch (result) { case 0: cout << "AAA" << endl; break; case 1: cout << "AA" << endl; break; case 2: cout << "A" << endl; break; case 3: cout << "B" << endl; break; case 4: cout << "C" << endl; break; case 5: cout << "D" << endl; break; case 6: cout << "E" << endl; break; case 7: cout << "NA" << endl; break; } } return 0; }
replace
51
53
51
52
TLE
p00124
C++
Time Limit Exceeded
#include <iostream> #include <string> using namespace std; string a[10]; int p[10]; int main() { int n, b, z, c; bool first = true; while (true) { cin >> n; if (!first) cout << endl; first = false; for (int i = 0; i < n; i++) { cin >> a[i] >> b >> z >> c; p[i] = b * 3 + c * 1; } for (int i = 0; i < n - 1; i++) { for (int j = n - 1; j > i; j--) { if (p[j] > p[j - 1]) { string tmps = a[j]; a[j] = a[j - 1]; a[j - 1] = tmps; int tmp = p[j]; p[j] = p[j - 1]; p[j - 1] = tmp; } } } for (int i = 0; i < n; i++) cout << a[i] << "," << p[i] << endl; } return 0; }
#include <iostream> #include <string> using namespace std; string a[10]; int p[10]; int main() { int n, b, z, c; bool first = true; while (true) { cin >> n; if (n == 0) break; if (!first) cout << endl; first = false; for (int i = 0; i < n; i++) { cin >> a[i] >> b >> z >> c; p[i] = b * 3 + c * 1; } for (int i = 0; i < n - 1; i++) { for (int j = n - 1; j > i; j--) { if (p[j] > p[j - 1]) { string tmps = a[j]; a[j] = a[j - 1]; a[j - 1] = tmps; int tmp = p[j]; p[j] = p[j - 1]; p[j - 1] = tmp; } } } for (int i = 0; i < n; i++) cout << a[i] << "," << p[i] << endl; } return 0; }
insert
10
10
10
12
TLE
p00127
C++
Runtime Error
#include <iostream> #include <string> using namespace std; int main() { string s, r; int i; const char *a[7] = { "afkpuz", "bglqv.", "chmrw?", "dinsx!", "ejoty ", }; while (cin >> s) { r = ""; for (i = 0; i + 1 < s.size(); i += 2) { if (s[i] > '6' || s[i + 1] > '5') break; r += a[s[i + 1] - '1'][s[i] - '1']; } if (i != s.size()) cout << "NA" << endl; else cout << r << endl; } return 0; }
#include <iostream> #include <string> using namespace std; int main() { string s, r; int i; const char *a[7] = { "afkpuz", "bglqv.", "chmrw?", "dinsx!", "ejoty ", }; while (cin >> s) { r = ""; for (i = 0; i + 1 < s.size(); i += 2) { if (s[i] > '6' || s[i + 1] > '5' || s[i] < '1' || s[i + 1] < '1') break; r += a[s[i + 1] - '1'][s[i] - '1']; } if (i != s.size()) cout << "NA" << endl; else cout << r << endl; } return 0; }
replace
12
13
12
13
0
p00127
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef unsigned int UI; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef istringstream ISS; typedef ostringstream OSS; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define EACH(v, c) for (auto &v : c) #define ITER(c) __typeof((c).begin()) #define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it) #define ALL(c) (c).begin(), (c).end() #define PB(n) push_back(n) #define MP(a, b) make_pair((a), (b)) #define EXIST(c, e) ((c).find(e) != (c).end()) #define fst first #define snd second #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) \ cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl const string table[] = { "abcde", "fghij", "klmno", "pqrst", "uvwxy", "z.?! ", }; string solve(string src) { if (src.size() % 2) { return "NA"; } string res; for (int i = 0; i < src.size(); i += 2) { int idx1 = src[i] - '1', idx2 = src[i + 1] - '1'; if (6 <= idx1 || 5 <= idx2) { return "NA"; } res += table[idx1][idx2]; } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); string str; while (cin >> str) { cout << solve(str) << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef unsigned int UI; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef istringstream ISS; typedef ostringstream OSS; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define EACH(v, c) for (auto &v : c) #define ITER(c) __typeof((c).begin()) #define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it) #define ALL(c) (c).begin(), (c).end() #define PB(n) push_back(n) #define MP(a, b) make_pair((a), (b)) #define EXIST(c, e) ((c).find(e) != (c).end()) #define fst first #define snd second #define DUMP(x) cerr << #x << " = " << (x) << endl #define DEBUG(x) \ cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl const string table[] = { "abcde", "fghij", "klmno", "pqrst", "uvwxy", "z.?! ", }; string solve(string src) { if (src.size() % 2) { return "NA"; } string res; for (int i = 0; i < src.size(); i += 2) { int idx1 = src[i] - '1', idx2 = src[i + 1] - '1'; if (!(0 <= idx1 && idx1 < 6 && 0 <= idx2 && idx2 < 5)) { return "NA"; } res += table[idx1][idx2]; } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); string str; while (cin >> str) { cout << solve(str) << endl; } return 0; }
replace
64
65
64
65
0
p00127
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; int main(void) { string str; string hyo[] = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z.?! "}; while (cin >> str) { if (str.size() % 2) { cout << "NA" << endl; continue; } string out = ""; for (int i = 0, len = str.size(); i < len; i += 2) { int x, y; y = str[i] - '1'; x = str[i + 1] - '1'; out += hyo[y][x]; } cout << out << endl; } return 0; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int main(void) { string str; string hyo[] = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z.?! "}; while (cin >> str) { if (str.size() % 2) { cout << "NA" << endl; continue; } string out = ""; for (int i = 0, len = str.size(); i < len; i += 2) { int x, y; y = str[i] - '1'; x = str[i + 1] - '1'; if (0 > x || x > 4 || 0 > y || y > 5) { out = "NA"; break; } out += hyo[y][x]; } cout << out << endl; } return 0; }
insert
25
25
25
30
0
p00127
C++
Runtime Error
#include <cstdio> #include <iostream> using namespace std; const string hyou[] = {"afkpuz", "bglqv.", "chmrw?", "dinsx!", "ejoty "}; int main() { string in; while (cin >> in) { cin.ignore(); if (in.size() % 2 == 1) { cout << "NA" << endl; continue; } bool flag = true; string ans = ""; for (int i = 0; i < in.size() - 1; i += 2) { if (!('1' <= in[i + 1] && in[i + 1] <= '6') || !('1' <= in[i] && in[i] <= '5')) { flag = false; break; } ans += hyou[in[i + 1] - '0' - 1][in[i] - '0' - 1]; } if (flag) cout << ans << endl; else cout << "NA" << endl; } return 0; }
#include <cstdio> #include <iostream> using namespace std; const string hyou[] = {"afkpuz", "bglqv.", "chmrw?", "dinsx!", "ejoty "}; int main() { string in; while (cin >> in) { cin.ignore(); if (in.size() % 2 == 1) { cout << "NA" << endl; continue; } bool flag = true; string ans = ""; for (int i = 0; i < in.size() - 1; i += 2) { if (!('1' <= in[i + 1] && in[i + 1] <= '5') || !('1' <= in[i] && in[i] <= '6')) { flag = false; break; } ans += hyou[in[i + 1] - '0' - 1][in[i] - '0' - 1]; } if (flag) cout << ans << endl; else cout << "NA" << endl; } return 0; }
replace
16
18
16
18
0
p00127
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string str; vector<string> vs = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z.?! "}; while (cin >> str) { if (str.size() % 2) { cout << "NA" << endl; continue; } string ans; for (int i = 0; i < str.size(); i += 2) { int a = str[i] - '0' - 1; int b = str[i + 1] - '0' - 1; if (a < 6 && b < 5) { ans += vs[a][b]; } else { ans = "NA"; break; } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; vector<string> vs = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z.?! "}; while (cin >> str) { if (str.size() % 2) { cout << "NA" << endl; continue; } string ans; for (int i = 0; i < str.size(); i += 2) { int a = str[i] - '0' - 1; int b = str[i + 1] - '0' - 1; if (0 <= a && a < 6 && 0 <= b && b < 5) { ans += vs[a][b]; } else { ans = "NA"; break; } } cout << ans << endl; } return 0; }
replace
16
17
16
17
0
p00127
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cmath> #include <ctype.h> #include <exception> #include <fstream> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) using namespace std; typedef long long int lint; vector<string> keys; vector<char> vals; map<string, char> encoder; bool AOJ0127() { string str, ret; cin >> str; if (str.size() % 2) { cout << "NA" << endl; return true; } REP(i, str.size() / 2) { string buf = str.substr(2 * i, 2); if (encoder.find(buf) == encoder.end()) { cout << "NA" << endl; return true; } ret.push_back(encoder[buf]); } cout << ret << endl; return true; } int main() { FOR(i, 1, 7) { FOR(j, 1, 6) { string key; key.push_back('0' + i); key.push_back('0' + j); keys.push_back(key); } } for (char c = 'a'; c <= 'z'; c++) { vals.push_back(c); } vals.push_back('.'); vals.push_back('?'); vals.push_back('!'); vals.push_back(' '); REP(i, keys.size()) { encoder[keys[i]] = vals[i]; } while (AOJ0127()) { } return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cmath> #include <ctype.h> #include <exception> #include <fstream> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define RREP(i, n) for (int i = (n)-1; i >= 0; i--) using namespace std; typedef long long int lint; vector<string> keys; vector<char> vals; map<string, char> encoder; bool AOJ0127() { string str, ret; if (!(cin >> str)) return false; if (str.size() % 2) { cout << "NA" << endl; return true; } REP(i, str.size() / 2) { string buf = str.substr(2 * i, 2); if (encoder.find(buf) == encoder.end()) { cout << "NA" << endl; return true; } ret.push_back(encoder[buf]); } cout << ret << endl; return true; } int main() { FOR(i, 1, 7) { FOR(j, 1, 6) { string key; key.push_back('0' + i); key.push_back('0' + j); keys.push_back(key); } } for (char c = 'a'; c <= 'z'; c++) { vals.push_back(c); } vals.push_back('.'); vals.push_back('?'); vals.push_back('!'); vals.push_back(' '); REP(i, keys.size()) { encoder[keys[i]] = vals[i]; } while (AOJ0127()) { } return 0; }
replace
35
36
35
37
TLE
p00127
C++
Runtime Error
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string map[6]; map[0] = "abcde"; map[1] = "fghij"; map[2] = "klmno"; map[3] = "pqrst"; map[4] = "uvwxy"; map[5] = "z.?! "; string str; while (cin >> str) { int num[1000] = {0}; int len = str.size(); if (len % 2 == 1) { cout << "NA" << endl; continue; } for (int i = 0; i < len; i += 2) { for (int j = i; j < i + 2; j++) { string tmp = ""; tmp += str[j]; istringstream ss(tmp); ss >> num[j]; // cout << num[j] << endl; } } for (int i = 0; i < len; i += 2) { cout << map[num[i] - 1][num[i + 1] - 1]; } cout << endl; } }
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string map[6]; map[0] = "abcde"; map[1] = "fghij"; map[2] = "klmno"; map[3] = "pqrst"; map[4] = "uvwxy"; map[5] = "z.?! "; string str; while (cin >> str) { int num[1000] = {0}; int len = str.size(); bool flag = false; for (int i = 0; i < len; i += 2) { if (str[i] <= '0' || str[i] > '6') { flag = true; break; } else if (str[i + 1] <= '0' || str[i + 1] > '5') { flag = true; break; } } if (len % 2 == 1 || flag) { cout << "NA" << endl; continue; } for (int i = 0; i < len; i += 2) { for (int j = i; j < i + 2; j++) { string tmp = ""; tmp += str[j]; istringstream ss(tmp); ss >> num[j]; // cout << num[j] << endl; } } for (int i = 0; i < len; i += 2) { cout << map[num[i] - 1][num[i + 1] - 1]; } cout << endl; } }
replace
17
18
17
28
0
p00128
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <string> #include <vector> using namespace std; int main() { string s; string soro[10] = {"* = ****", "* =* ***", "* =** **", "* =*** *", "* =**** ", " *= ****", " *=* ***", " *=** **", " *=*** *", " *=**** "}; bool flag = true; while (cin >> s) { vector<char> c; for (int i = 0; i < s.size(); i++) { c.push_back('0'); } for (int i = 0; i < s.size(); i++) { c.push_back(s[i]); } if (flag) { flag = false; } else { cout << endl; } for (int i = 0; i < 8; i++) { for (int j = 0; j < 5; j++) { cout << soro[c[j] - '0'][i]; } cout << endl; } } return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <math.h> #include <string> #include <vector> using namespace std; int main() { string s; string soro[10] = {"* = ****", "* =* ***", "* =** **", "* =*** *", "* =**** ", " *= ****", " *=* ***", " *=** **", " *=*** *", " *=**** "}; bool flag = true; while (cin >> s) { vector<char> c; for (int i = 0; i < 5 - s.size(); i++) { c.push_back('0'); } for (int i = 0; i < s.size(); i++) { c.push_back(s[i]); } if (flag) { flag = false; } else { cout << endl; } for (int i = 0; i < 8; i++) { for (int j = 0; j < 5; j++) { cout << soro[c[j] - '0'][i]; } cout << endl; } } return 0; }
replace
16
17
16
17
0
p00128
C++
Runtime Error
#include <bits/stdc++.h> #define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v) #define rep(v, n) reps(v, 0, n) #define lep(v, n) reps(v, 1, n + 1) #define rreps(v, f, l) for (int v = (l), v##_ = (f); v >= v##_; --v) #define rrep(v, n) rreps(v, 0, n - 1) #define rlep(v, n) rreps(v, 1, n) #define show_a(a, size) \ rep(a##_it, size) std::cout << a[a##_it] << " \n"[a##_it == a##_it_ - 1]; #define show_v(v) show_a(v, v.size()) #define debug(x) std::cerr << #x << " = " << (x) << "\n"; using namespace std; typedef long long int lint; static inline int in() { int x; scanf("%d", &x); return x; } static inline lint inl() { lint x; scanf("%lld", &x); return x; } int main() { bool is_first = true; int n; while (~scanf("%d", &n)) { if (is_first) is_first = false; else puts(""); char snum[8]; sprintf(snum, "%05d", n); debug(snum); rep(i, 5) printf("%c", " *"[snum[i] < '5']); puts(""); rep(i, 5) printf("%c", "* "[snum[i] < '5']); puts(""); puts("====="); rep(i, 5) { rep(j, 5) { printf("%c", " *"[i != (snum[j] - '0') % 5]); } puts(""); } } return (0); }
#include <bits/stdc++.h> #define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v) #define rep(v, n) reps(v, 0, n) #define lep(v, n) reps(v, 1, n + 1) #define rreps(v, f, l) for (int v = (l), v##_ = (f); v >= v##_; --v) #define rrep(v, n) rreps(v, 0, n - 1) #define rlep(v, n) rreps(v, 1, n) #define show_a(a, size) \ rep(a##_it, size) std::cout << a[a##_it] << " \n"[a##_it == a##_it_ - 1]; #define show_v(v) show_a(v, v.size()) #define debug(x) std::cerr << #x << " = " << (x) << "\n"; using namespace std; typedef long long int lint; static inline int in() { int x; scanf("%d", &x); return x; } static inline lint inl() { lint x; scanf("%lld", &x); return x; } int main() { bool is_first = true; int n; while (~scanf("%d", &n)) { if (is_first) is_first = false; else puts(""); char snum[8]; sprintf(snum, "%05d", n); rep(i, 5) printf("%c", " *"[snum[i] < '5']); puts(""); rep(i, 5) printf("%c", "* "[snum[i] < '5']); puts(""); puts("====="); rep(i, 5) { rep(j, 5) { printf("%c", " *"[i != (snum[j] - '0') % 5]); } puts(""); } } return (0); }
delete
38
39
38
38
0
snum = 02006 snum = 01111