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
p02942
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 205, M = 20005, inf = 0x3f3f3f3f, INF = 0x7fffffff; int read() { int f = 1, g = 0; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) g = g * 10 + ch - '0'; return f * g; } int n, m, cnt, S, T, ans, a[N][N], b[N], col[N * N]; int fir[N], firr[N], siz; struct edge { int u, v, w, nex; } e[M]; void add(int u, int v, int w) { e[++siz] = (edge){u, v, w, fir[u]}; fir[u] = siz; e[++siz] = (edge){v, u, 0, fir[v]}; fir[v] = siz; } int dist[N], que[N]; void dec(int x, int fl) { e[x].w -= fl; e[x ^ 1].w += fl; } bool bfs(int S, int T) { memset(dist, 0x3f, 4 * (cnt + 1)); int l = 1, r = 1; que[l] = S; dist[S] = 0; while (l <= r) { int x = que[l]; for (int i = fir[x]; i; i = e[i].nex) if ((e[i].w) && (dist[e[i].v] > dist[x] + 1)) { que[++r] = e[i].v; dist[e[i].v] = dist[x] + 1; } l++; } return dist[T] != inf; } int dfs(int x, int flow, int &T) { if (x == T) return flow; int fl = 0, t = 0; for (int i = firr[x]; i; i = e[i].nex) if (e[i].w && (dist[e[i].v] > dist[x])) { t = dfs(e[i].v, min(flow - fl, e[i].w), T); dec(i, t); fl += t; if (fl == flow) { firr[x] = i; return fl; } } firr[x] = 0; return fl; } ll dinic() { ll flow = 0; while (bfs(S, T)) { for (int i = 0; i <= cnt; i++) firr[i] = fir[i]; flow += dfs(S, 2147483647, T); cerr << flow << endl; } return flow; } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) a[i][j] = read(), col[++cnt] = i; for (int k = 1; k <= m; k++) { S = 0; T = cnt = n * 2 + 1; siz = 1; memset(fir, 0, sizeof(fir)); for (int i = 1; i <= n; i++) { add(S, i, 1); add(i + n, T, 1); for (int j = k; j <= m; j++) add(i, n + col[a[i][j]], 1); } assert(dinic() == n); for (int i = 2; i < siz; i += 2) if ((e[i].u <= n) && (e[i].v > n) && e[i ^ 1].w) b[e[i].u] = e[i].v - n; for (int i = 1; i <= n; i++) for (int j = k; j <= m; j++) if (col[a[i][j]] == b[i]) swap(a[i][j], a[i][k]); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) printf("%d ", a[i][j]); printf("\n"); } for (int j = 1; j <= m; j++) { for (int i = 1; i <= n; i++) b[col[a[i][j]]] = a[i][j]; for (int i = 1; i <= n; i++) a[i][j] = b[i]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) printf("%d ", a[i][j]); printf("\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 205, M = 21005, inf = 0x3f3f3f3f, INF = 0x7fffffff; int read() { int f = 1, g = 0; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) g = g * 10 + ch - '0'; return f * g; } int n, m, cnt, S, T, ans, a[N][N], b[N], col[N * N]; int fir[N], firr[N], siz; struct edge { int u, v, w, nex; } e[M]; void add(int u, int v, int w) { e[++siz] = (edge){u, v, w, fir[u]}; fir[u] = siz; e[++siz] = (edge){v, u, 0, fir[v]}; fir[v] = siz; } int dist[N], que[N]; void dec(int x, int fl) { e[x].w -= fl; e[x ^ 1].w += fl; } bool bfs(int S, int T) { memset(dist, 0x3f, 4 * (cnt + 1)); int l = 1, r = 1; que[l] = S; dist[S] = 0; while (l <= r) { int x = que[l]; for (int i = fir[x]; i; i = e[i].nex) if ((e[i].w) && (dist[e[i].v] > dist[x] + 1)) { que[++r] = e[i].v; dist[e[i].v] = dist[x] + 1; } l++; } return dist[T] != inf; } int dfs(int x, int flow, int &T) { if (x == T) return flow; int fl = 0, t = 0; for (int i = firr[x]; i; i = e[i].nex) if (e[i].w && (dist[e[i].v] > dist[x])) { t = dfs(e[i].v, min(flow - fl, e[i].w), T); dec(i, t); fl += t; if (fl == flow) { firr[x] = i; return fl; } } firr[x] = 0; return fl; } ll dinic() { ll flow = 0; while (bfs(S, T)) { for (int i = 0; i <= cnt; i++) firr[i] = fir[i]; flow += dfs(S, 2147483647, T); cerr << flow << endl; } return flow; } int main() { n = read(); m = read(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) a[i][j] = read(), col[++cnt] = i; for (int k = 1; k <= m; k++) { S = 0; T = cnt = n * 2 + 1; siz = 1; memset(fir, 0, sizeof(fir)); for (int i = 1; i <= n; i++) { add(S, i, 1); add(i + n, T, 1); for (int j = k; j <= m; j++) add(i, n + col[a[i][j]], 1); } assert(dinic() == n); for (int i = 2; i < siz; i += 2) if ((e[i].u <= n) && (e[i].v > n) && e[i ^ 1].w) b[e[i].u] = e[i].v - n; for (int i = 1; i <= n; i++) for (int j = k; j <= m; j++) if (col[a[i][j]] == b[i]) swap(a[i][j], a[i][k]); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) printf("%d ", a[i][j]); printf("\n"); } for (int j = 1; j <= m; j++) { for (int i = 1; i <= n; i++) b[col[a[i][j]]] = a[i][j]; for (int i = 1; i <= n; i++) a[i][j] = b[i]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) printf("%d ", a[i][j]); printf("\n"); } return 0; }
replace
3
4
3
4
0
3 3
p02942
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back const int N = 105; int a[N][N], n, m; bool was[N][N]; void rot(int x) { int tmp = a[x][1]; for (int i = 2; i <= m; i++) a[x][i - 1] = a[x][i]; a[x][m] = tmp; } void SolveRow(int x) { sort(a[x] + 1, a[x] + 1 + m); int cnt = 0; while (1) { bool ok = 1; for (int i = 1; i <= m; i++) if (was[i][(a[x][i] - 1) / m]) { ok = 0; break; } if (ok) break; rot(x); cnt++; assert(cnt != m); } for (int i = 1; i <= m; i++) was[i][(a[x][i] - 1) / m] = 1; } void SortCol(int y) { vector<int> tmp; for (int i = 1; i <= n; i++) tmp.pb(a[i][y]); sort(tmp.begin(), tmp.end()); for (int i = 1; i <= n; i++) a[i][y] = tmp[i - 1]; } int cnt[N][N]; vector<int> my[N][N]; bool use[N], cws[N]; struct Edge { int u, v, c; Edge() {} Edge(int a, int b, int d) : u(a), v(b), c(d) {} }; vector<Edge> edges; int s, t; const int M = 2 * N; vector<int> G[M]; void AddEdge(int u, int v) { G[u].pb(edges.size()); edges.pb(Edge(u, v, 1)); G[v].pb(edges.size()); edges.pb(Edge(v, u, 0)); } int go[M]; bool bfs_was[N]; bool BFS() { for (int i = s; i <= t; i++) bfs_was[i] = 0; bfs_was[s] = 1; queue<int> q; q.push(s); while (q.size()) { int u = q.front(); q.pop(); for (int e : G[u]) { int v = edges[e].v; int c = edges[e].c; if (c && !bfs_was[v]) { bfs_was[v] = 1; q.push(v); go[v] = e; } } } return bfs_was[t]; } vector<int> Matching() { int Flow = 0; while (BFS()) { for (int i = t; i != s; i = edges[go[i]].u) { edges[go[i]].c--; edges[go[i] ^ 1].c++; } Flow++; } if (Flow != n) { while (clock() < 15 * CLOCKS_PER_SEC) n++; } vector<int> ans(n, 0); for (Edge E : edges) { if (E.u == s || E.v == t) continue; if (E.u <= n && E.v > n && E.c == 0) ans[E.u - 1] = E.v - n - 1; } return ans; } void Clear() { for (int i = s; i <= t; i++) G[i].clear(); edges.clear(); } void Solve() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cnt[i][(a[i][j] - 1) / m]++; my[i][(a[i][j] - 1) / m].pb(a[i][j]); } } s = 0; t = 2 * n + 1; for (int j = 1; j <= m; j++) { // for(int i=0;i<=n;i++) use[i]=0,cws[i]=0; Clear(); for (int k = 0; k < n; k++) { AddEdge(n + k + 1, t); AddEdge(s, k + 1); for (int i = 1; i <= n; i++) if (cnt[i][k] > 0) AddEdge(i, n + k + 1); /*int mx=-1; for(int i=1;i<=n;i++) if(!cws[i]) { if(mx==-1 || cnt[i][k]>cnt[mx][k]) mx=i; } assert(cnt[mx][k]!=0); a[mx][j]=my[mx][k].back(); my[mx][k].pop_back(); cnt[mx][k]--; cws[mx]=1; printf("a[%i][%i]=%i\n",mx,j,a[mx][j]);*/ } vector<int> ans = Matching(); for (int i = 1; i <= n; i++) { a[i][j] = my[i][ans[i - 1]].back(); my[i][ans[i - 1]].pop_back(); cnt[i][ans[i - 1]]--; // printf("a[%i][%i]=%i\n",i,j,a[i][j]); } /*for(int t=1;t<=n;t++) { pair<int,pair<int,int>> mx={0,{0,0}}; for(int i=1;i<=n;i++) if(!cws[i]) { for(int k=0;k<n;k++) if(!use[k]) { mx=max(mx,{cnt[i][k],{i,k}}); } } printf("%i %i %i\n",mx.second.first,j,mx.second.second); assert(mx.first!=0); cws[mx.second.first]=1; use[mx.second.second]=1; cnt[mx.second.first][mx.second.second]--; a[mx.second.first][j]=my[mx.second.first][mx.second.second].back(); my[mx.second.first][mx.second.second].pop_back(); printf("a[%i][%i]=%i\n",mx.second.first,j,a[mx.second.first][j]); }*/ } } int main() { scanf("%i %i", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%i", &a[i][j]); // for(int i=1;i<=n;i++) SolveRow(i); Solve(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) printf("%i%c", a[i][j], j == m ? '\n' : ' '); for (int i = 1; i <= m; i++) SortCol(i); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) printf("%i%c", a[i][j], j == m ? '\n' : ' '); return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back const int N = 105; int a[N][N], n, m; bool was[N][N]; void rot(int x) { int tmp = a[x][1]; for (int i = 2; i <= m; i++) a[x][i - 1] = a[x][i]; a[x][m] = tmp; } void SolveRow(int x) { sort(a[x] + 1, a[x] + 1 + m); int cnt = 0; while (1) { bool ok = 1; for (int i = 1; i <= m; i++) if (was[i][(a[x][i] - 1) / m]) { ok = 0; break; } if (ok) break; rot(x); cnt++; assert(cnt != m); } for (int i = 1; i <= m; i++) was[i][(a[x][i] - 1) / m] = 1; } void SortCol(int y) { vector<int> tmp; for (int i = 1; i <= n; i++) tmp.pb(a[i][y]); sort(tmp.begin(), tmp.end()); for (int i = 1; i <= n; i++) a[i][y] = tmp[i - 1]; } int cnt[N][N]; vector<int> my[N][N]; bool use[N], cws[N]; struct Edge { int u, v, c; Edge() {} Edge(int a, int b, int d) : u(a), v(b), c(d) {} }; vector<Edge> edges; int s, t; const int M = 2 * N; vector<int> G[M]; void AddEdge(int u, int v) { G[u].pb(edges.size()); edges.pb(Edge(u, v, 1)); G[v].pb(edges.size()); edges.pb(Edge(v, u, 0)); } int go[M]; bool bfs_was[M]; bool BFS() { for (int i = s; i <= t; i++) bfs_was[i] = 0; bfs_was[s] = 1; queue<int> q; q.push(s); while (q.size()) { int u = q.front(); q.pop(); for (int e : G[u]) { int v = edges[e].v; int c = edges[e].c; if (c && !bfs_was[v]) { bfs_was[v] = 1; q.push(v); go[v] = e; } } } return bfs_was[t]; } vector<int> Matching() { int Flow = 0; while (BFS()) { for (int i = t; i != s; i = edges[go[i]].u) { edges[go[i]].c--; edges[go[i] ^ 1].c++; } Flow++; } if (Flow != n) { while (clock() < 15 * CLOCKS_PER_SEC) n++; } vector<int> ans(n, 0); for (Edge E : edges) { if (E.u == s || E.v == t) continue; if (E.u <= n && E.v > n && E.c == 0) ans[E.u - 1] = E.v - n - 1; } return ans; } void Clear() { for (int i = s; i <= t; i++) G[i].clear(); edges.clear(); } void Solve() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cnt[i][(a[i][j] - 1) / m]++; my[i][(a[i][j] - 1) / m].pb(a[i][j]); } } s = 0; t = 2 * n + 1; for (int j = 1; j <= m; j++) { // for(int i=0;i<=n;i++) use[i]=0,cws[i]=0; Clear(); for (int k = 0; k < n; k++) { AddEdge(n + k + 1, t); AddEdge(s, k + 1); for (int i = 1; i <= n; i++) if (cnt[i][k] > 0) AddEdge(i, n + k + 1); /*int mx=-1; for(int i=1;i<=n;i++) if(!cws[i]) { if(mx==-1 || cnt[i][k]>cnt[mx][k]) mx=i; } assert(cnt[mx][k]!=0); a[mx][j]=my[mx][k].back(); my[mx][k].pop_back(); cnt[mx][k]--; cws[mx]=1; printf("a[%i][%i]=%i\n",mx,j,a[mx][j]);*/ } vector<int> ans = Matching(); for (int i = 1; i <= n; i++) { a[i][j] = my[i][ans[i - 1]].back(); my[i][ans[i - 1]].pop_back(); cnt[i][ans[i - 1]]--; // printf("a[%i][%i]=%i\n",i,j,a[i][j]); } /*for(int t=1;t<=n;t++) { pair<int,pair<int,int>> mx={0,{0,0}}; for(int i=1;i<=n;i++) if(!cws[i]) { for(int k=0;k<n;k++) if(!use[k]) { mx=max(mx,{cnt[i][k],{i,k}}); } } printf("%i %i %i\n",mx.second.first,j,mx.second.second); assert(mx.first!=0); cws[mx.second.first]=1; use[mx.second.second]=1; cnt[mx.second.first][mx.second.second]--; a[mx.second.first][j]=my[mx.second.first][mx.second.second].back(); my[mx.second.first][mx.second.second].pop_back(); printf("a[%i][%i]=%i\n",mx.second.first,j,a[mx.second.first][j]); }*/ } } int main() { scanf("%i %i", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%i", &a[i][j]); // for(int i=1;i<=n;i++) SolveRow(i); Solve(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) printf("%i%c", a[i][j], j == m ? '\n' : ' '); for (int i = 1; i <= m; i++) SortCol(i); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) printf("%i%c", a[i][j], j == m ? '\n' : ' '); return 0; }
replace
58
59
58
59
0
p02942
C++
Runtime Error
#include <bits/stdc++.h> #define XX first #define YY second #define pb emplace_back #define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i)) #define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i)) #define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X)) #define REP rep #define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X)) #define all(X) (X).begin(), (X).end() #define eb emplace_back using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef ll LL; typedef pii PII; typedef pll PLL; const ll MOD = 1e9 + 7; #define rall(X) (X).rbegin(), (X).rend() #define UNIQUE(X) (X).erase(unique(all(X)), (X).end()) #define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X)) #define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X)) template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } int N; int M; int A[114][114]; int vec[114][114]; set<int> unused[214]; using Tup = tuple<int, int, int>; vector<Tup> eds; map<int, pii> op[114]; void Invert(int v, int uc, int vc) { assert(op[v].count(uc)); int w = v; vector<int> vs(1, w); vector<int> as; while (op[w].count(uc)) { int nw, a; tie(nw, a) = op[w][uc]; vs.eb(nw); as.eb(a); swap(uc, vc); w = nw; } int m = as.size(); assert(unused[vs.back()].count(uc)); assert(!op[vs.back()].count(uc)); unused[vs.back()].insert(vc); op[vs.back()].erase(vc); rrep(i, m) { int a = as[i]; int u = vs[i]; int v = vs[i + 1]; op[u][uc] = pii(v, a); op[v][uc] = pii(u, a); unused[u].erase(uc); unused[v].erase(uc); swap(uc, vc); } assert(op[vs.front()].count(uc) && op[vs.front()].count(vc)); op[vs.front()].erase(uc); unused[vs.front()].insert(uc); } signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); cin >> N >> M; rep(i, N) { rep(j, M) { cin >> A[i][j]; int r = N + (A[i][j] - 1) / M; eds.eb(Tup(i, r, A[i][j])); } } rep(i, N) { rep(j, M) { unused[i].insert(j); unused[N + i].insert(j); } } for (Tup &p : eds) { int u, v, a; tie(u, v, a) = p; int uc = *unused[u].begin(); if (!unused[v].count(uc)) { assert(unused[v].size()); int vc = *unused[v].begin(); Invert(v, uc, vc); assert(unused[u].count(uc)); assert(unused[v].count(uc)); assert(op[v].count(vc)); assert(!unused[v].count(vc)); } unused[u].erase(uc); unused[v].erase(uc); op[u][uc] = pii(v, a); op[v][uc] = pii(u, a); continue; } rep(i, N) { rep(j, M) { if (j) cout << " "; assert(op[i].count(j)); int a = op[i][j].YY; cout << a; vec[j][i] = a; } cout << endl; } rep(j, M) { sort(vec[j], vec[j] + N); } rep(i, N) { rep(j, M) { if (j) cout << " "; cout << vec[j][i]; } cout << endl; } }
#include <bits/stdc++.h> #define XX first #define YY second #define pb emplace_back #define FOR(i, a, b) for (int(i) = (a); i < (b); ++(i)) #define EFOR(i, a, b) for (int(i) = (a); i <= (b); ++(i)) #define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X)) #define REP rep #define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X)) #define all(X) (X).begin(), (X).end() #define eb emplace_back using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef ll LL; typedef pii PII; typedef pll PLL; const ll MOD = 1e9 + 7; #define rall(X) (X).rbegin(), (X).rend() #define UNIQUE(X) (X).erase(unique(all(X)), (X).end()) #define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X)) #define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X)) template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } int N; int M; int A[114][114]; int vec[114][114]; set<int> unused[214]; using Tup = tuple<int, int, int>; vector<Tup> eds; map<int, pii> op[214]; void Invert(int v, int uc, int vc) { assert(op[v].count(uc)); int w = v; vector<int> vs(1, w); vector<int> as; while (op[w].count(uc)) { int nw, a; tie(nw, a) = op[w][uc]; vs.eb(nw); as.eb(a); swap(uc, vc); w = nw; } int m = as.size(); assert(unused[vs.back()].count(uc)); assert(!op[vs.back()].count(uc)); unused[vs.back()].insert(vc); op[vs.back()].erase(vc); rrep(i, m) { int a = as[i]; int u = vs[i]; int v = vs[i + 1]; op[u][uc] = pii(v, a); op[v][uc] = pii(u, a); unused[u].erase(uc); unused[v].erase(uc); swap(uc, vc); } assert(op[vs.front()].count(uc) && op[vs.front()].count(vc)); op[vs.front()].erase(uc); unused[vs.front()].insert(uc); } signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); cin >> N >> M; rep(i, N) { rep(j, M) { cin >> A[i][j]; int r = N + (A[i][j] - 1) / M; eds.eb(Tup(i, r, A[i][j])); } } rep(i, N) { rep(j, M) { unused[i].insert(j); unused[N + i].insert(j); } } for (Tup &p : eds) { int u, v, a; tie(u, v, a) = p; int uc = *unused[u].begin(); if (!unused[v].count(uc)) { assert(unused[v].size()); int vc = *unused[v].begin(); Invert(v, uc, vc); assert(unused[u].count(uc)); assert(unused[v].count(uc)); assert(op[v].count(vc)); assert(!unused[v].count(vc)); } unused[u].erase(uc); unused[v].erase(uc); op[u][uc] = pii(v, a); op[v][uc] = pii(u, a); continue; } rep(i, N) { rep(j, M) { if (j) cout << " "; assert(op[i].count(j)); int a = op[i][j].YY; cout << a; vec[j][i] = a; } cout << endl; } rep(j, M) { sort(vec[j], vec[j] + N); } rep(i, N) { rep(j, M) { if (j) cout << " "; cout << vec[j][i]; } cout << endl; } }
replace
39
40
39
40
0
p02943
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s; int n, k, st = 0, ed; cin >> n >> k >> s; if (k > 30 || (1 << (k - 1)) >= n) { cout << string(n, *min_element(s.begin(), s.end())) << "\n"; return 0; } string S = s; reverse(S.begin(), S.end()); string T = s + S; for (int i = 1; i < n; i++) if (T.substr(i, n) < T.substr(st, n)) st = i; for (ed = st; ed - st + 1 < n && T[st] == T[ed]; ed++) ; for (int i = 1; i <= ((ed - st) << (k - 1)); i++) cout << T[st], n--; while (n--) cout << T[ed++]; cout << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; int n, k, st = 0, ed; cin >> n >> k >> s; if (k > 30 || (1 << (k - 1)) >= n) { cout << string(n, *min_element(s.begin(), s.end())) << "\n"; return 0; } string S = s; reverse(S.begin(), S.end()); string T = s + S; for (int i = 1; i < n; i++) if (T.substr(i, n) < T.substr(st, n)) st = i; for (ed = st; ed - st + 1 < n && T[st] == T[ed]; ed++) ; for (int i = 1; i <= ((ed - st) << (k - 1)) && n; i++) cout << T[st], n--; while (n--) cout << T[ed++]; cout << "\n"; return 0; }
replace
18
19
18
19
0
p02943
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long #define lld long double #define MOD 998244353LL #define CAP 10000000 #define inf 1000000000000000000LL #define pii pair<ll, ll> #define f first #define s second #define pb push_back #define mp make_pair #define endl '\n' #define sz(v) v.size() #define all(v) v.begin(), v.end() #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); using namespace std; ll power(ll x, ll y, ll md = MOD) { ll res = 1; x %= md; while (y > 0) { if (y & 1) res = (res * x) % md; x = (x * x) % md; y = y >> 1; } return res % md; } #define dbg(x) cout << #x << ":" << x << endl; #define int ll ll min1() { return inf; } ll max1() { return -inf; } template <typename... Args> int min1(int a, Args... args) { return min(a, min1(args...)); } template <typename... Args> int max1(int a, Args... args) { return max(a, max1(args...)); } struct node { std::vector<ll> v, w; }; string ans; char mn; int len; string mmn; int ff = 0; void dfs(string s, int c, int k, int mul) { if (ff) return; if (c == k) { if (ans > s) ans = s; return; } int n = s.size(); string t = s; reverse(t.begin(), t.end()); string u = s + t; int st = 0; int end = n; if (c != k - 1 && c > 1) { for (int i = n - 1; i > -1; i--) { if (s[i] == mn) { int num = (n - i) * 2; st = i + num - n; if (st < 0) st = 0; end = st; } else break; } } for (int i = st; i <= end; i++) { string temp = u.substr(i, n); if (c == k - 1) { int cnt = 0; for (int i = 0; i < n; i++) { if (mn == temp[i]) cnt++; else break; } if (cnt >= mul) dfs(temp, c + 1, k, mul * 2); continue; } int cnt = 0; for (int i = n - 1; i > -1; i--) { if (mn == temp[i]) cnt++; else break; } int pp = cnt; for (int i = 0; i < k - 2 - c; i++) { pp *= 2; } if (pp >= n) { string temp = ""; for (int i = 0; i < n; i++) temp += mn; ans = mn; ff = 1; return; } if (cnt >= mul) dfs(temp, c + 1, k, mul * 2); } } signed main() { fast; int n, k; cin >> n >> k; string s; cin >> s; mn = s[0]; len = 1; int cur = 1; for (int i = 1; i < n; i++) { if (s[i] == s[i - 1]) { cur++; } else { if (s[i - 1] < mn) { mn = s[i - 1]; len = cur; } else if (s[i - 1] == mn) { len = max(len, cur); } cur = 1; } } int f = 0; if (s[n - 1] < mn) { mn = s[n - 1]; len = cur; } else if (s[n - 1] == mn) { if (cur > len) { len = cur; f = 1; } } if (k + f > 14 || len == n) { for (int i = 0; i < n; i++) cout << mn; return 0; } else { int pp = len; for (int i = 0; i < k - 1 + f; i++) { pp *= 2; if (pp >= n) { for (int i = 0; i < n; i++) cout << mn; return 0; } } } mmn = s; ans = s; dfs(s, 0, k, len * (1 + f)); cout << ans; }
#include <bits/stdc++.h> #define ll long long #define lld long double #define MOD 998244353LL #define CAP 10000000 #define inf 1000000000000000000LL #define pii pair<ll, ll> #define f first #define s second #define pb push_back #define mp make_pair #define endl '\n' #define sz(v) v.size() #define all(v) v.begin(), v.end() #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); using namespace std; ll power(ll x, ll y, ll md = MOD) { ll res = 1; x %= md; while (y > 0) { if (y & 1) res = (res * x) % md; x = (x * x) % md; y = y >> 1; } return res % md; } #define dbg(x) cout << #x << ":" << x << endl; #define int ll ll min1() { return inf; } ll max1() { return -inf; } template <typename... Args> int min1(int a, Args... args) { return min(a, min1(args...)); } template <typename... Args> int max1(int a, Args... args) { return max(a, max1(args...)); } struct node { std::vector<ll> v, w; }; string ans; char mn; int len; string mmn; int ff = 0; void dfs(string s, int c, int k, int mul) { if (ff) return; if (c == k) { if (ans > s) ans = s; return; } int n = s.size(); string t = s; reverse(t.begin(), t.end()); string u = s + t; int st = 0; int end = n; if (c != k - 1 && c != 0) { for (int i = n - 1; i > -1; i--) { if (s[i] == mn) { int num = (n - i) * 2; st = i + num - n; if (st < 0) st = 0; end = st; } else break; } } for (int i = st; i <= end; i++) { string temp = u.substr(i, n); if (c == k - 1) { int cnt = 0; for (int i = 0; i < n; i++) { if (mn == temp[i]) cnt++; else break; } if (cnt >= mul) dfs(temp, c + 1, k, mul * 2); continue; } int cnt = 0; for (int i = n - 1; i > -1; i--) { if (mn == temp[i]) cnt++; else break; } int pp = cnt; for (int i = 0; i < k - 2 - c; i++) { pp *= 2; } if (pp >= n) { string temp = ""; for (int i = 0; i < n; i++) temp += mn; ans = mn; ff = 1; return; } if (cnt >= mul) dfs(temp, c + 1, k, mul * 2); } } signed main() { fast; int n, k; cin >> n >> k; string s; cin >> s; mn = s[0]; len = 1; int cur = 1; for (int i = 1; i < n; i++) { if (s[i] == s[i - 1]) { cur++; } else { if (s[i - 1] < mn) { mn = s[i - 1]; len = cur; } else if (s[i - 1] == mn) { len = max(len, cur); } cur = 1; } } int f = 0; if (s[n - 1] < mn) { mn = s[n - 1]; len = cur; } else if (s[n - 1] == mn) { if (cur > len) { len = cur; f = 1; } } if (k + f > 14 || len == n) { for (int i = 0; i < n; i++) cout << mn; return 0; } else { int pp = len; for (int i = 0; i < k - 1 + f; i++) { pp *= 2; if (pp >= n) { for (int i = 0; i < n; i++) cout << mn; return 0; } } } mmn = s; ans = s; dfs(s, 0, k, len * (1 + f)); cout << ans; }
replace
70
71
70
71
TLE
p02943
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) // const ll mod = 1000000007; string ans; bool checker(ll N, ll K) { ll now = 1; for (int i = 3; i < K; i++) { now *= 2; if (now >= N) { return true; } } return false; } void f(int i, string S, int K, int len) { string T = S; int N = S.size(); reverse(T.begin(), T.end()); S += T; /* if(len == 0 && K == 1) { string ret = S.substr(i, N); cerr << ret << endl; chmin(ans, ret); return; } */ if (K == 1) { string ret = S.substr(max(0, N - len), N); cerr << "ans: " << ret << endl; chmin(ans, ret); return; } if (i != -1) { string ret = S.substr(2 * N - i - N, N); /// cerr << i << " " << len << " " << ret << endl; if (i + len != N) f(-1, ret, K - 1, len); if (i + len == N) f(-1, ret, K - 1, min(N, len * 2)); return; } // cerr << "now:" << S << endl; string ret = S.substr(min(len, N), N); cerr << ret << endl; f(-1, ret, K - 1, min(N, len * 2)); } string solve(int N, int K, string S) { if (K == 0) return S; string ans = "z"; ans[0]++; string T = S; reverse(T.begin(), T.end()); S += T; for (int i = 0; i <= N; i++) { string tmp = S.substr(i, N); string ret = solve(N, K - 1, tmp); chmin(ans, ret); } return ans; } int main() { // cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); int N, K; string S; cin >> N >> K >> S; if (K <= 2) { cout << solve(N, K, S) << endl; return 0; } if (checker(N, K)) { char mini = 'z'; for (int i = 0; i < S.size(); i++) { chmin(mini, S[i]); } for (int i = 0; i < S.size(); i++) { cout << mini; } cout << endl; return 0; } char mini = 'z'; for (int i = 0; i < S.size(); i++) { chmin(mini, S[i]); } ans = "z"; ans[0]++; // cerr << ans << endl; /* for(int i = 0; i < S.size(); i++) { cerr << "----" << i << "----" << endl; f(i, S, K, 0); } */ int l = 0; for (int r = 0; r < S.size(); r++) { if (S[r] == mini) continue; if (l != r) { cerr << l << " " << r << endl; f(l, S, K, r - l); } l = r + 1; } if (l != S.size()) { cerr << l << " " << S.size() - l << endl; f(l, S, K, S.size() - l); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) // const ll mod = 1000000007; string ans; bool checker(ll N, ll K) { ll now = 1; for (int i = 3; i < K; i++) { now *= 2; if (now >= N) { return true; } } return false; } void f(int i, string S, int K, int len) { string T = S; int N = S.size(); reverse(T.begin(), T.end()); S += T; /* if(len == 0 && K == 1) { string ret = S.substr(i, N); cerr << ret << endl; chmin(ans, ret); return; } */ if (K == 1) { string ret = S.substr(max(0, N - len), N); cerr << "ans: " << ret << endl; chmin(ans, ret); return; } if (i != -1) { string ret = S.substr(2 * N - i - N, N); /// cerr << i << " " << len << " " << ret << endl; if (i + len != N) f(-1, ret, K - 1, len); if (i + len == N) f(-1, ret, K - 1, min(N, len * 2)); return; } // cerr << "now:" << S << endl; string ret = S.substr(min(len, N), N); cerr << ret << endl; f(-1, ret, K - 1, min(N, len * 2)); } string solve(int N, int K, string S) { if (K == 0) return S; string ans = "z"; ans[0]++; string T = S; reverse(T.begin(), T.end()); S += T; for (int i = 0; i <= N; i++) { string tmp = S.substr(i, N); string ret = solve(N, K - 1, tmp); chmin(ans, ret); } return ans; } int main() { // cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); int N, K; string S; cin >> N >> K >> S; if (K <= 1) { cout << solve(N, K, S) << endl; return 0; } if (checker(N, K)) { char mini = 'z'; for (int i = 0; i < S.size(); i++) { chmin(mini, S[i]); } for (int i = 0; i < S.size(); i++) { cout << mini; } cout << endl; return 0; } char mini = 'z'; for (int i = 0; i < S.size(); i++) { chmin(mini, S[i]); } ans = "z"; ans[0]++; // cerr << ans << endl; /* for(int i = 0; i < S.size(); i++) { cerr << "----" << i << "----" << endl; f(i, S, K, 0); } */ int l = 0; for (int r = 0; r < S.size(); r++) { if (S[r] == mini) continue; if (l != r) { cerr << l << " " << r << endl; f(l, S, K, r - l); } l = r + 1; } if (l != S.size()) { cerr << l << " " << S.size() - l << endl; f(l, S, K, S.size() - l); } cout << ans << endl; return 0; }
replace
94
95
94
95
TLE
p02943
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #define _USE_MATH_DEFINES #include <complex> #include <functional> #include <math.h> using namespace std; #define rep(i, x) for (ll i = 0; i < x; i++) #define repn(i, x) for (ll i = 1; i <= x; i++) typedef long long ll; const ll INF = 1e17; const ll MOD = 998244353; const ll MAX = 4000001; const long double eps = 1E-14; ll max(ll a, ll b) { if (a > b) { return a; } return b; } ll min(ll a, ll b) { if (a > b) { return b; } return a; } ll gcd(ll a, ll b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } struct edge { ll ind; ll fr; ll to; ll d; }; class mint { long long x; public: mint(long long x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint &a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint &a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint &a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint &a) const { mint res(*this); return res += a; } mint operator-(const mint &a) const { mint res(*this); return res -= a; } mint operator*(const mint &a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint &operator/=(const mint &a) { return (*this) *= a.inv(); } mint operator/(const mint &a) const { mint res(*this); return res /= a; } friend ostream &operator<<(ostream &os, const mint &m) { os << m.x; return os; } }; mint pw(mint a, ll b) { if (b == 0) { return 1; } mint ret = pw(a, b >> 1); ret *= ret; if (b & 1) { ret *= a; } return ret; } typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<vector<vector<ll>>> vvvll; typedef vector<mint> vmint; typedef vector<vector<mint>> vvmint; typedef vector<vector<vector<mint>>> vvvmint; ////////////////////////////////////// string op(string s, ll x) { ll N = s.size(); string t(N, '.'); rep(i, N - x) t[i] = s[i + x]; rep(i, x) t[N - x + i] = s[N - 1 - i]; // cout << t << endl; return t; } int main() { ll N, K; string S; cin >> N >> K >> S; char top = 'z'; rep(i, N) top = min(top, S[i]); string ans = S; rep(i, N + 1) { string T = op(S, i); rep(k, K - 1) { ll x = 0; for (ll j = N; j >= 1; j--) { if (T[j - 1] != top) { x = j; break; } } // cout << x << endl; if (x == 0) { break; } T = op(T, N - x); } reverse(T.begin(), T.end()); ans = min(ans, T); } cout << ans << endl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #define _USE_MATH_DEFINES #include <complex> #include <functional> #include <math.h> using namespace std; #define rep(i, x) for (ll i = 0; i < x; i++) #define repn(i, x) for (ll i = 1; i <= x; i++) typedef long long ll; const ll INF = 1e17; const ll MOD = 998244353; const ll MAX = 4000001; const long double eps = 1E-14; ll max(ll a, ll b) { if (a > b) { return a; } return b; } ll min(ll a, ll b) { if (a > b) { return b; } return a; } ll gcd(ll a, ll b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } struct edge { ll ind; ll fr; ll to; ll d; }; class mint { long long x; public: mint(long long x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint &a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint &operator-=(const mint &a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint &operator*=(const mint &a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint &a) const { mint res(*this); return res += a; } mint operator-(const mint &a) const { mint res(*this); return res -= a; } mint operator*(const mint &a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint &operator/=(const mint &a) { return (*this) *= a.inv(); } mint operator/(const mint &a) const { mint res(*this); return res /= a; } friend ostream &operator<<(ostream &os, const mint &m) { os << m.x; return os; } }; mint pw(mint a, ll b) { if (b == 0) { return 1; } mint ret = pw(a, b >> 1); ret *= ret; if (b & 1) { ret *= a; } return ret; } typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<vector<vector<ll>>> vvvll; typedef vector<mint> vmint; typedef vector<vector<mint>> vvmint; typedef vector<vector<vector<mint>>> vvvmint; ////////////////////////////////////// string op(string s, ll x) { ll N = s.size(); string t(N, '.'); rep(i, N - x) t[i] = s[i + x]; rep(i, x) t[N - x + i] = s[N - 1 - i]; // cout << t << endl; return t; } int main() { ll N, K; string S; cin >> N >> K >> S; char top = 'z'; rep(i, N) top = min(top, S[i]); string ans = S; rep(i, N + 1) { string T = op(S, i); rep(k, min(K, 30) - 1) { ll x = 0; for (ll j = N; j >= 1; j--) { if (T[j - 1] != top) { x = j; break; } } // cout << x << endl; if (x == 0) { break; } T = op(T, N - x); } reverse(T.begin(), T.end()); ans = min(ans, T); } cout << ans << endl; }
replace
159
160
159
160
TLE
p02943
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using VI = vector<int>; using VL = vector<ll>; using VS = vector<string>; template <class T> using PQ = priority_queue<T, vector<T>, greater<T>>; #define FOR(i, a, n) for (int(i) = (a); (i) < (n); ++(i)) #define eFOR(i, a, n) for (int(i) = (a); (i) <= (n); ++(i)) #define rFOR(i, a, n) for (int(i) = (n)-1; (i) >= (a); --(i)) #define erFOR(i, a, n) for (int(i) = (n); (i) >= (a); --(i)) #define each(i, a) for (auto &i : a) #define SORT(i) sort((i).begin(), (i).end()) #define rSORT(i, a) sort((i).begin(), (i).end(), (a)) #define all(i) (i).begin(), (i).end() #define out(y, x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x)) #define line cout << "-----------------------------\n" #define ENDL(i, n) ((i) == (n)-1 ? "\n" : " ") constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; constexpr ll mod = 1000000007; constexpr ll MOD = 998244353; constexpr ld eps = 1e-10; constexpr ld pi = 3.1415926535897932; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template <class T> inline istream &operator>>(istream &is, vector<T> &v) { for (auto &a : v) is >> a; return is; } template <class T, class U> inline istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <class T> inline vector<T> vec(size_t a) { return vector<T>(a); } template <class T> inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); } template <class T, class... Ts> inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); } template <class T, class... Ts> inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); } inline void print() { cout << "\n"; } template <class T, class... Ts> inline void print(const T &a, const Ts &...ts) { cout << a << " "; print(ts...); } template <class T> inline void print(const vector<T> &v) { for (int i = 0; i < v.size(); ++i) cout << v[i] << (i == v.size() - 1 ? "\n" : " "); } template <class T> inline void print(const vector<vector<T>> &v) { for (auto &a : v) print(a); } int main() { init(); int n, k; cin >> n >> k; k = min(k - 1, n); string s; cin >> s; string u = s; reverse(all(u)); u = s + u; char p = *min_element(all(s)); eFOR(i, 1, n) { string t = u.substr(i, n); if (0 < k) { FOR(c, 0, k - 1) { string y = t; reverse(all(y)); t += y; int idx = -1, cnt = 0, cur_idx = 0, cur_cnt = 0; rFOR(j, 0, n * 2) { if (t[j] == p) { if (cur_cnt == 0) cur_idx = j; ++cur_cnt; } else { if (chmax(cnt, cur_cnt)) idx = cur_idx; cur_cnt = 0; } } if (chmax(cnt, cur_cnt)) idx = cur_idx; t = t.substr(idx - n + 1, n); } { string y = t; reverse(all(y)); t += y; int idx = -1, cnt = 0, cur_idx = 0, cur_cnt = 0; FOR(j, 0, n * 2) { if (t[j] == p) { if (cur_cnt == 0) cur_idx = j; ++cur_cnt; } else { if (chmax(cnt, cur_cnt)) idx = cur_idx; cur_cnt = 0; } } if (chmax(cnt, cur_cnt)) idx = cur_idx; t = t.substr(idx, n); } } chmin(s, t); } print(s); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using VI = vector<int>; using VL = vector<ll>; using VS = vector<string>; template <class T> using PQ = priority_queue<T, vector<T>, greater<T>>; #define FOR(i, a, n) for (int(i) = (a); (i) < (n); ++(i)) #define eFOR(i, a, n) for (int(i) = (a); (i) <= (n); ++(i)) #define rFOR(i, a, n) for (int(i) = (n)-1; (i) >= (a); --(i)) #define erFOR(i, a, n) for (int(i) = (n); (i) >= (a); --(i)) #define each(i, a) for (auto &i : a) #define SORT(i) sort((i).begin(), (i).end()) #define rSORT(i, a) sort((i).begin(), (i).end(), (a)) #define all(i) (i).begin(), (i).end() #define out(y, x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x)) #define line cout << "-----------------------------\n" #define ENDL(i, n) ((i) == (n)-1 ? "\n" : " ") constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; constexpr ll mod = 1000000007; constexpr ll MOD = 998244353; constexpr ld eps = 1e-10; constexpr ld pi = 3.1415926535897932; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template <class T> inline istream &operator>>(istream &is, vector<T> &v) { for (auto &a : v) is >> a; return is; } template <class T, class U> inline istream &operator>>(istream &is, pair<T, U> &p) { is >> p.first >> p.second; return is; } template <class T> inline vector<T> vec(size_t a) { return vector<T>(a); } template <class T> inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); } template <class T, class... Ts> inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); } template <class T, class... Ts> inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); } inline void print() { cout << "\n"; } template <class T, class... Ts> inline void print(const T &a, const Ts &...ts) { cout << a << " "; print(ts...); } template <class T> inline void print(const vector<T> &v) { for (int i = 0; i < v.size(); ++i) cout << v[i] << (i == v.size() - 1 ? "\n" : " "); } template <class T> inline void print(const vector<vector<T>> &v) { for (auto &a : v) print(a); } int main() { init(); int n, k; cin >> n >> k; k = min(k - 1, n); string s; cin >> s; string u = s; reverse(all(u)); u = s + u; char p = *min_element(all(s)); eFOR(i, 1, n) { string t = u.substr(i, n); if (0 < k) { FOR(c, 0, k - 1) { string y = t; reverse(all(y)); t += y; int idx = -1, cnt = 0, cur_idx = 0, cur_cnt = 0; rFOR(j, 0, n * 2) { if (t[j] == p) { if (cur_cnt == 0) cur_idx = j; ++cur_cnt; } else { if (chmax(cnt, cur_cnt)) idx = cur_idx; cur_cnt = 0; } } if (chmax(cnt, cur_cnt)) idx = cur_idx; t = t.substr(idx - n + 1, n); if (cnt >= n) break; } { string y = t; reverse(all(y)); t += y; int idx = -1, cnt = 0, cur_idx = 0, cur_cnt = 0; FOR(j, 0, n * 2) { if (t[j] == p) { if (cur_cnt == 0) cur_idx = j; ++cur_cnt; } else { if (chmax(cnt, cur_cnt)) idx = cur_idx; cur_cnt = 0; } } if (chmax(cnt, cur_cnt)) idx = cur_idx; t = t.substr(idx, n); } } chmin(s, t); } print(s); }
insert
114
114
114
116
TLE
p02943
C++
Runtime Error
//////////////////////////////////////////// _LeMur_ // #pragma GCC optimize("Ofast,no-stack-protector") // #pragma GCC // target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; const int N = 5005; const int inf = 1000 * 1000 * 1000; const int mod = 998244353; int n, k; string s; pair<pair<int, int>, int> p[N]; int R[N]; int main() { mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count()); cin >> n >> k; cin >> s; k = min(k, 16); while (k--) { string t = s; reverse(t.begin(), t.end()); string S = s + t; for (int i = 0; i <= 16; i++) { for (int j = 0; j < (int)S.size(); j++) { if (i == 0) { p[j].first = make_pair(S[j] - 'a', 0); p[j].second = j; } else { if (j + (1 << (i - 1)) >= (int)S.size()) { p[j].first = make_pair(R[j], -1); p[j].second = j; } else { p[j].first = make_pair(R[j], R[j + (1 << (i - 1))]); p[j].second = j; } } } sort(p, p + (int)S.size()); int it = 0; for (int j = 0; j < (int)S.size(); j++) { ++it; pair<int, int> x = p[j].first; while (j < (int)S.size() && p[j].first == x) { R[p[j].second] = it; ++j; } --j; } } int mn = inf; int id; for (int i = 0; i < n; i++) { if (R[i] < mn) { mn = R[i]; id = i; } } if (k == 0) { int it = 0; while (it < n) { s[it++] = S[id++]; } } else { int it = n - 1; id = 2 * n - 1 - id; while (it >= 0) { s[it--] = S[id--]; } } } cout << s << endl; return 0; }
//////////////////////////////////////////// _LeMur_ // #pragma GCC optimize("Ofast,no-stack-protector") // #pragma GCC // target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <functional> #include <iostream> #include <list> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; const int N = 20005; const int inf = 1000 * 1000 * 1000; const int mod = 998244353; int n, k; string s; pair<pair<int, int>, int> p[N]; int R[N]; int main() { mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count()); cin >> n >> k; cin >> s; k = min(k, 16); while (k--) { string t = s; reverse(t.begin(), t.end()); string S = s + t; for (int i = 0; i <= 16; i++) { for (int j = 0; j < (int)S.size(); j++) { if (i == 0) { p[j].first = make_pair(S[j] - 'a', 0); p[j].second = j; } else { if (j + (1 << (i - 1)) >= (int)S.size()) { p[j].first = make_pair(R[j], -1); p[j].second = j; } else { p[j].first = make_pair(R[j], R[j + (1 << (i - 1))]); p[j].second = j; } } } sort(p, p + (int)S.size()); int it = 0; for (int j = 0; j < (int)S.size(); j++) { ++it; pair<int, int> x = p[j].first; while (j < (int)S.size() && p[j].first == x) { R[p[j].second] = it; ++j; } --j; } } int mn = inf; int id; for (int i = 0; i < n; i++) { if (R[i] < mn) { mn = R[i]; id = i; } } if (k == 0) { int it = 0; while (it < n) { s[it++] = S[id++]; } } else { int it = n - 1; id = 2 * n - 1 - id; while (it >= 0) { s[it--] = S[id--]; } } } cout << s << endl; return 0; }
replace
28
29
28
29
0
p02943
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; string s, ss, s2, t, ans, ff; vector<pii> v; int n, k, i, z, l; char mn = 127; int main() { freopen("1", "r", stdin); ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> s; ss = s; reverse(ss.begin(), ss.end()); ss = s + ss; for (char c : s) mn = min(mn, c); for (i = 0; i < n; ++i) if (s[i] == mn) v.push_back(pii(++z, i)); else z = 0; sort(v.begin(), v.end()); if (k > 15 || (v.back().first << k - 1) >= n) { for (i = 0; i < n; ++i) cout << mn; return 0; } if (z) { if ((z << k) >= n) { for (i = 0; i < n; ++i) cout << mn; return 0; } l = n - (z << k); ff = string(n - l, mn) + ss.substr(n + z, l); } else ff = s; l = n - (v.back().first << k - 1); for (pii u : v) if (u.first == v.back().first) { t = ss.substr(n * 2 - 1 - u.second - l, l); reverse(t.begin(), t.end()); if (s2.empty() || t < s2) s2 = t; } ans = min(string(n - l, mn) + s2, min(s, ff)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; string s, ss, s2, t, ans, ff; vector<pii> v; int n, k, i, z, l; char mn = 127; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> s; ss = s; reverse(ss.begin(), ss.end()); ss = s + ss; for (char c : s) mn = min(mn, c); for (i = 0; i < n; ++i) if (s[i] == mn) v.push_back(pii(++z, i)); else z = 0; sort(v.begin(), v.end()); if (k > 15 || (v.back().first << k - 1) >= n) { for (i = 0; i < n; ++i) cout << mn; return 0; } if (z) { if ((z << k) >= n) { for (i = 0; i < n; ++i) cout << mn; return 0; } l = n - (z << k); ff = string(n - l, mn) + ss.substr(n + z, l); } else ff = s; l = n - (v.back().first << k - 1); for (pii u : v) if (u.first == v.back().first) { t = ss.substr(n * 2 - 1 - u.second - l, l); reverse(t.begin(), t.end()); if (s2.empty() || t < s2) s2 = t; } ans = min(string(n - l, mn) + s2, min(s, ff)); cout << ans << endl; return 0; }
delete
8
9
8
8
-11
p02943
C++
Runtime Error
#include <bits/stdc++.h> #define LL long long #define pii pair<int, int> using namespace std; const int inf = 2147483647; const int N = 20001; int _max(int x, int y) { return x > y ? x : y; } int _min(int x, int y) { return x < y ? x : y; } inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); return x * f; } void put(int x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) put(x / 10); putchar(x % 10 + '0'); } int n, k; char ss[5001], hh[5001]; bool cmp(int x, int y) { for (int i = 1; i <= n; i++) { if (ss[x + i - 1] > ss[y + i - 1]) return 1; else if (ss[x + i - 1] < ss[y + i - 1]) return 0; } return 0; } int main() { n = read(), k = read(); scanf("%s", ss + 1); int gg = 'z'; for (int i = 1; i <= n; i++) gg = _min(gg, ss[i]); if (k > 30) { for (int i = 1; i <= n; i++) putchar(gg); return 0; } else { for (int i = n + 1; i <= 2 * n; i++) ss[i] = ss[n - (i - n) + 1]; int now = 1; for (int i = 2; i <= n + 1; i++) { if (cmp(now, i)) now = i; } for (int i = now; i <= now + n - 1; i++) hh[i - now + 1] = ss[i]; k--; int s = 0; while (hh[s + 1] == hh[1]) s++; while (k--) { for (int i = 1; i <= n; i++) ss[i] = hh[i]; for (int i = 1; i <= _min(n, s * 2); i++) hh[i] = ss[1]; for (int i = _min(n, s * 2) + 1, t = 1; i <= n; i++, ++t) hh[i] = ss[s + t]; s = _min(s * 2, n); if (s == n) break; } printf("%s", hh + 1); } return 0; }
#include <bits/stdc++.h> #define LL long long #define pii pair<int, int> using namespace std; const int inf = 2147483647; const int N = 20001; int _max(int x, int y) { return x > y ? x : y; } int _min(int x, int y) { return x < y ? x : y; } inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); return x * f; } void put(int x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) put(x / 10); putchar(x % 10 + '0'); } int n, k; char ss[N], hh[N]; bool cmp(int x, int y) { for (int i = 1; i <= n; i++) { if (ss[x + i - 1] > ss[y + i - 1]) return 1; else if (ss[x + i - 1] < ss[y + i - 1]) return 0; } return 0; } int main() { n = read(), k = read(); scanf("%s", ss + 1); int gg = 'z'; for (int i = 1; i <= n; i++) gg = _min(gg, ss[i]); if (k > 30) { for (int i = 1; i <= n; i++) putchar(gg); return 0; } else { for (int i = n + 1; i <= 2 * n; i++) ss[i] = ss[n - (i - n) + 1]; int now = 1; for (int i = 2; i <= n + 1; i++) { if (cmp(now, i)) now = i; } for (int i = now; i <= now + n - 1; i++) hh[i - now + 1] = ss[i]; k--; int s = 0; while (hh[s + 1] == hh[1]) s++; while (k--) { for (int i = 1; i <= n; i++) ss[i] = hh[i]; for (int i = 1; i <= _min(n, s * 2); i++) hh[i] = ss[1]; for (int i = _min(n, s * 2) + 1, t = 1; i <= n; i++, ++t) hh[i] = ss[s + t]; s = _min(s * 2, n); if (s == n) break; } printf("%s", hh + 1); } return 0; }
replace
31
32
31
32
0
p02943
C++
Time Limit Exceeded
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> constexpr long long INF = 1LL << 60; constexpr long long MOD = 1000000007; double PI = acos(-1.0); #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep1(i, n) for (ll i = 1; i <= (n); ++i) #define rrep(i, n) for (ll i = (n - 1); i >= 0; --i) #define perm(c) \ sort(ALL(c)); \ for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c))) #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define pb push_back #define to_s to_string #define len(v) (ll) v.size() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define print(x) cout << (x) << '\n' #define drop(x) cout << (x) << '\n', exit(0) #define debug(x) cout << #x << ": " << (x) << '\n' using namespace std; using ll = long long; typedef pair<ll, ll> P; typedef vector<ll> vec; typedef vector<vector<ll>> vec2; typedef vector<vector<vector<ll>>> vec3; template <class S, class T> inline bool chmax(S &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class S, class T> inline bool chmin(S &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); } inline ll devc(ll x, ll y) { return (x + y - 1) / y; } inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } inline ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); } struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t); return os; } template <typename T1, typename T2, typename T3> istream &operator>>(istream &is, tuple<T1, T2, T3> &t) { is >> get<0>(t) >> get<1>(t) >> get<2>(t); return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < (int)v.size(); ++i) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { int ct = 0; for (auto &s : st) cout << s << (++ct != st.size() ? " " : ""); return os; } template <typename T> constexpr set<T> &operator|=(set<T> &st1, const set<T> &st2) { for (auto &s : st2) st1.insert(s); return st1; } template <typename T> constexpr set<T> &operator-=(set<T> &st1, const set<T> &st2) { for (auto &s : st2) if (st1.count(s)) st1.erase(s); return st1; } template <typename T> constexpr set<T> &operator&=(set<T> &st1, const set<T> &st2) { auto itr = st1.begin(); while (itr != st1.end()) { if (!st2.count(*itr)) itr = st1.erase(itr); else ++itr; } return st1; } template <typename T> constexpr set<T> operator|(const set<T> &st1, const set<T> &st2) { set<T> res = st1; res |= st2; return res; } template <typename T> constexpr set<T> operator-(const set<T> &st1, const set<T> &st2) { set<T> res = st1; res -= st2; return res; } template <typename T> constexpr set<T> operator&(const set<T> &st1, const set<T> &st2) { set<T> res = st1; res &= st2; return res; } /*--------------------------------- Tools * ------------------------------------------*/ template <typename T> vector<T> cumsum(const vector<T> &X) { vector<T> res(X.size() + 1, 0); for (int i = 0; i < X.size(); ++i) res[i + 1] += res[i] + X[i]; return res; } template <typename S, typename T, typename F> pair<T, T> bisearch(S left, T right, F f) { while (abs(right - left) > 1) { T mid = (right + left) / 2; if (f(mid)) right = mid; else left = mid; } return {left, right}; } template <typename S, typename T, typename F> double trisearch(S left, T right, F f, int maxLoop = 90) { double low = left, high = right; while (maxLoop--) { double mid_left = high / 3 + low * 2 / 3; double mid_right = high * 2 / 3 + low / 3; if (f(mid_left) >= f(mid_right)) low = mid_left; else high = mid_right; } return (low + high) * 0.5; } template <typename F> ll ternarySearch(ll L, ll R, F f) { //[L, R) ll lo = L - 1, hi = R - 1; while (lo + 1 != hi) { ll mi = (lo + hi) / 2; if (f(mi) <= f(mi + 1)) hi = mi; else lo = mi; } return hi; } /*------------------------------- Main Code Here * -----------------------------------------*/ int main() { ll N, K; cin >> N >> K; string S; cin >> S; string now = S; chmin(K, 2 * N); while (K--) { string tmp = now; reverse(ALL(now)); tmp += now; if (K) reverse(ALL(tmp)); string comp(N, 'z'); rep(i, 2 * N) if (i + N < 2 * N) { string x = tmp.substr(i, N); chmin(comp, x); } if (K) reverse(ALL(comp)); now = comp; } print(now); return 0; }
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> constexpr long long INF = 1LL << 60; constexpr long long MOD = 1000000007; double PI = acos(-1.0); #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep1(i, n) for (ll i = 1; i <= (n); ++i) #define rrep(i, n) for (ll i = (n - 1); i >= 0; --i) #define perm(c) \ sort(ALL(c)); \ for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c))) #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define pb push_back #define to_s to_string #define len(v) (ll) v.size() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define print(x) cout << (x) << '\n' #define drop(x) cout << (x) << '\n', exit(0) #define debug(x) cout << #x << ": " << (x) << '\n' using namespace std; using ll = long long; typedef pair<ll, ll> P; typedef vector<ll> vec; typedef vector<vector<ll>> vec2; typedef vector<vector<vector<ll>>> vec3; template <class S, class T> inline bool chmax(S &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class S, class T> inline bool chmin(S &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } inline ll msb(ll v) { return 1 << (31 - __builtin_clzll(v)); } inline ll devc(ll x, ll y) { return (x + y - 1) / y; } inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } inline ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); } struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t); return os; } template <typename T1, typename T2, typename T3> istream &operator>>(istream &is, tuple<T1, T2, T3> &t) { is >> get<0>(t) >> get<1>(t) >> get<2>(t); return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < (int)v.size(); ++i) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { int ct = 0; for (auto &s : st) cout << s << (++ct != st.size() ? " " : ""); return os; } template <typename T> constexpr set<T> &operator|=(set<T> &st1, const set<T> &st2) { for (auto &s : st2) st1.insert(s); return st1; } template <typename T> constexpr set<T> &operator-=(set<T> &st1, const set<T> &st2) { for (auto &s : st2) if (st1.count(s)) st1.erase(s); return st1; } template <typename T> constexpr set<T> &operator&=(set<T> &st1, const set<T> &st2) { auto itr = st1.begin(); while (itr != st1.end()) { if (!st2.count(*itr)) itr = st1.erase(itr); else ++itr; } return st1; } template <typename T> constexpr set<T> operator|(const set<T> &st1, const set<T> &st2) { set<T> res = st1; res |= st2; return res; } template <typename T> constexpr set<T> operator-(const set<T> &st1, const set<T> &st2) { set<T> res = st1; res -= st2; return res; } template <typename T> constexpr set<T> operator&(const set<T> &st1, const set<T> &st2) { set<T> res = st1; res &= st2; return res; } /*--------------------------------- Tools * ------------------------------------------*/ template <typename T> vector<T> cumsum(const vector<T> &X) { vector<T> res(X.size() + 1, 0); for (int i = 0; i < X.size(); ++i) res[i + 1] += res[i] + X[i]; return res; } template <typename S, typename T, typename F> pair<T, T> bisearch(S left, T right, F f) { while (abs(right - left) > 1) { T mid = (right + left) / 2; if (f(mid)) right = mid; else left = mid; } return {left, right}; } template <typename S, typename T, typename F> double trisearch(S left, T right, F f, int maxLoop = 90) { double low = left, high = right; while (maxLoop--) { double mid_left = high / 3 + low * 2 / 3; double mid_right = high * 2 / 3 + low / 3; if (f(mid_left) >= f(mid_right)) low = mid_left; else high = mid_right; } return (low + high) * 0.5; } template <typename F> ll ternarySearch(ll L, ll R, F f) { //[L, R) ll lo = L - 1, hi = R - 1; while (lo + 1 != hi) { ll mi = (lo + hi) / 2; if (f(mi) <= f(mi + 1)) hi = mi; else lo = mi; } return hi; } /*------------------------------- Main Code Here * -----------------------------------------*/ int main() { ll N, K; cin >> N >> K; string S; cin >> S; string now = S; chmin(K, 10 + log2(N)); while (K--) { string tmp = now; reverse(ALL(now)); tmp += now; if (K) reverse(ALL(tmp)); string comp(N, 'z'); rep(i, 2 * N) if (i + N < 2 * N) { string x = tmp.substr(i, N); chmin(comp, x); } if (K) reverse(ALL(comp)); now = comp; } print(now); return 0; }
replace
205
206
205
206
TLE
p02943
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define R cin >> #define Z class #define ll long long #define ln cout << '\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n", a) #define mem(a) memset(a, 0, sizeof(a)) #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--) #define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++) #define rep(i, n) REP(i, 0, n) #define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++) template <Z A> void pr(A a) { cout << a; ln; } template <Z A, Z B> void pr(A a, B b) { cout << a << ' '; pr(b); } template <Z A, Z B, Z C> void pr(A a, B b, C c) { cout << a << ' '; pr(b, c); } template <Z A, Z B, Z C, Z D> void pr(A a, B b, C c, D d) { cout << a << ' '; pr(b, c, d); } template <Z A> void PR(A a, ll n) { rep(i, n) { if (i) cout << ' '; cout << a[i]; } ln; } ll check(ll n, ll m, ll x, ll y) { return x >= 0 && x < n && y >= 0 && y < m; } const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; typedef pair<ll, ll> P; void sa_is_countingAlphabets(const int *s, int n, int size, vector<int> &b, bool f = false) { if (f || (int)b.size() / 2 >= size) { vector<int>::iterator a = f ? b.begin() : b.begin() + size; fill(a, a + size, 0); for (int i = 0; i < n; i++) a[(int)s[i]]++; } } void sa_is_getBucketOffsets(const int *s, int n, bool dir, int size, vector<int> &b) { vector<int>::iterator a; if ((int)b.size() / 2 < size) { sa_is_countingAlphabets(s, n, size, b, true); a = b.begin(); } else a = b.begin() + size; int cumsum = 1; if (dir) { for (int i = 0; i < size; i++) { cumsum += a[i]; b[i] = cumsum; } } else { for (int i = 0; i < size; i++) { int x = a[i]; b[i] = cumsum; cumsum += x; } } } void sa_is_inducedSort(const int *s, int n, int size, const vector<bool> &types, int *sa, vector<int> &b) { sa_is_getBucketOffsets(s, n, false, size, b); for (int i = 0; i < n; i++) { int j = sa[i] - 1; if (j >= 0 && !types[j]) sa[b[(int)s[j]]++] = j; } sa_is_getBucketOffsets(s, n, true, size, b); for (int i = n; i >= 1; i--) { int j = sa[i] - 1; if (j >= 0 && types[j]) sa[--b[(int)s[j]]] = j; } } void sa_is_rec(const int *s, int n, int size, int *sa, vector<int> &b) { vector<bool> types(n + 1); types[n - 1] = 0; types[n] = 1; for (int i = n - 2; i >= 0; i--) types[i] = s[i] < s[i + 1] || (s[i] == s[i + 1] && types[i + 1]); sa_is_countingAlphabets(s, n, size, b); sa_is_getBucketOffsets(s, n, true, size, b); fill(sa, sa + n + 1, -1); for (int i = 1; i < n; i++) if (types[i] && !types[i - 1]) sa[--b[(int)s[i]]] = i; sa[0] = n; sa_is_inducedSort(s, n, size, types, sa, b); int n1 = 0; for (int i = 0; i <= n; i++) { int j = sa[i]; if (j > 0 && types[j] && !types[j - 1]) sa[n1++] = j; } int *buffer = sa + n1; fill(buffer, sa + n + 1, -1); int c = 0, ppos = -1; buffer[sa[0] / 2] = c++; for (int i = 1; i < n1; i++) { int pos = sa[i]; bool diff = false; if (ppos == -1) diff = true; else { for (int j = pos, k = ppos;; j++, k++) { if (s[j] != s[k] || types[j] != types[k]) { diff = true; break; } else if (j != pos && ((types[j] && !types[j - 1]) || (types[k] && !types[k - 1]))) break; } } if (diff) { c++; ppos = pos; } buffer[pos / 2] = c - 1; } for (int i = n, j = n; i >= n1; i--) if (sa[i] >= 0) sa[j--] = sa[i]; int *sa1 = sa, *s1 = sa + n + 1 - n1; if (c == n1) for (int i = 0; i < n1; i++) sa1[s1[i]] = i; else sa_is_rec(s1, n1 - 1, c, sa1, b); sa_is_countingAlphabets(s, n, size, b); sa_is_getBucketOffsets(s, n, true, size, b); for (int i = 1, j = 0; i <= n; i++) if (types[i] && !types[i - 1]) s1[j++] = i; for (int i = 0; i < n1; i++) sa1[i] = s1[sa1[i]]; fill(sa + n1, sa + n + 1, -1); for (int i = n1 - 1; i >= 1; i--) { int j = sa[i]; sa[i] = -1; sa[--b[(int)s[j]]] = j; } sa_is_inducedSort(s, n, size, types, sa, b); } void buildSuffixArray(const int *s, int n, int size, int *sa) { if (n == 0) sa[0] = 0; else { vector<int> b(max(size, (n + 1) / 2) + 1); sa_is_rec(s, n, size, &sa[0], b); } } void buildSuffixArray(const string s, int n, int *sa) { int a[n]; for (int i = 0; i < n; i++) a[i] = (int)s[i]; int maxElem = *max_element(a, a + n); buildSuffixArray(a, n, maxElem + 1, sa); } const int MAXN = 10010; int sa[MAXN]; string ans = "{"; ll n; void dfs(string s, ll m) { if (!m) { ans = min(ans, s); return; } string t = s; reverse(all(t)); s += t; string s1, s2; buildSuffixArray(s, s.size(), sa); rep(i, s.size()) { if (sa[i] < n) { s1 = s.substr(sa[i], n); break; } } reverse(all(s)); buildSuffixArray(s, s.size(), sa); rep(i, s.size()) { if (sa[i] < n) { s2 = s.substr(sa[i], n); reverse(all(s2)); break; } } if (s1 != s2) { dfs(s1, m - 1); dfs(s2, m - 1); } else dfs(s1, m - 1); } void Main() { ll m; string s; cin >> n >> m >> s; if (m < 12) dfs(s, m); else { char c = '{'; rep(i, n) c = min(c, s[i]); ans = ""; rep(i, n) ans += c; } pr(ans); } int main() { ios::sync_with_stdio(0); cin.tie(0); Main(); return 0; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define R cin >> #define Z class #define ll long long #define ln cout << '\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n", a) #define mem(a) memset(a, 0, sizeof(a)) #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--) #define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++) #define rep(i, n) REP(i, 0, n) #define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++) template <Z A> void pr(A a) { cout << a; ln; } template <Z A, Z B> void pr(A a, B b) { cout << a << ' '; pr(b); } template <Z A, Z B, Z C> void pr(A a, B b, C c) { cout << a << ' '; pr(b, c); } template <Z A, Z B, Z C, Z D> void pr(A a, B b, C c, D d) { cout << a << ' '; pr(b, c, d); } template <Z A> void PR(A a, ll n) { rep(i, n) { if (i) cout << ' '; cout << a[i]; } ln; } ll check(ll n, ll m, ll x, ll y) { return x >= 0 && x < n && y >= 0 && y < m; } const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; typedef pair<ll, ll> P; void sa_is_countingAlphabets(const int *s, int n, int size, vector<int> &b, bool f = false) { if (f || (int)b.size() / 2 >= size) { vector<int>::iterator a = f ? b.begin() : b.begin() + size; fill(a, a + size, 0); for (int i = 0; i < n; i++) a[(int)s[i]]++; } } void sa_is_getBucketOffsets(const int *s, int n, bool dir, int size, vector<int> &b) { vector<int>::iterator a; if ((int)b.size() / 2 < size) { sa_is_countingAlphabets(s, n, size, b, true); a = b.begin(); } else a = b.begin() + size; int cumsum = 1; if (dir) { for (int i = 0; i < size; i++) { cumsum += a[i]; b[i] = cumsum; } } else { for (int i = 0; i < size; i++) { int x = a[i]; b[i] = cumsum; cumsum += x; } } } void sa_is_inducedSort(const int *s, int n, int size, const vector<bool> &types, int *sa, vector<int> &b) { sa_is_getBucketOffsets(s, n, false, size, b); for (int i = 0; i < n; i++) { int j = sa[i] - 1; if (j >= 0 && !types[j]) sa[b[(int)s[j]]++] = j; } sa_is_getBucketOffsets(s, n, true, size, b); for (int i = n; i >= 1; i--) { int j = sa[i] - 1; if (j >= 0 && types[j]) sa[--b[(int)s[j]]] = j; } } void sa_is_rec(const int *s, int n, int size, int *sa, vector<int> &b) { vector<bool> types(n + 1); types[n - 1] = 0; types[n] = 1; for (int i = n - 2; i >= 0; i--) types[i] = s[i] < s[i + 1] || (s[i] == s[i + 1] && types[i + 1]); sa_is_countingAlphabets(s, n, size, b); sa_is_getBucketOffsets(s, n, true, size, b); fill(sa, sa + n + 1, -1); for (int i = 1; i < n; i++) if (types[i] && !types[i - 1]) sa[--b[(int)s[i]]] = i; sa[0] = n; sa_is_inducedSort(s, n, size, types, sa, b); int n1 = 0; for (int i = 0; i <= n; i++) { int j = sa[i]; if (j > 0 && types[j] && !types[j - 1]) sa[n1++] = j; } int *buffer = sa + n1; fill(buffer, sa + n + 1, -1); int c = 0, ppos = -1; buffer[sa[0] / 2] = c++; for (int i = 1; i < n1; i++) { int pos = sa[i]; bool diff = false; if (ppos == -1) diff = true; else { for (int j = pos, k = ppos;; j++, k++) { if (s[j] != s[k] || types[j] != types[k]) { diff = true; break; } else if (j != pos && ((types[j] && !types[j - 1]) || (types[k] && !types[k - 1]))) break; } } if (diff) { c++; ppos = pos; } buffer[pos / 2] = c - 1; } for (int i = n, j = n; i >= n1; i--) if (sa[i] >= 0) sa[j--] = sa[i]; int *sa1 = sa, *s1 = sa + n + 1 - n1; if (c == n1) for (int i = 0; i < n1; i++) sa1[s1[i]] = i; else sa_is_rec(s1, n1 - 1, c, sa1, b); sa_is_countingAlphabets(s, n, size, b); sa_is_getBucketOffsets(s, n, true, size, b); for (int i = 1, j = 0; i <= n; i++) if (types[i] && !types[i - 1]) s1[j++] = i; for (int i = 0; i < n1; i++) sa1[i] = s1[sa1[i]]; fill(sa + n1, sa + n + 1, -1); for (int i = n1 - 1; i >= 1; i--) { int j = sa[i]; sa[i] = -1; sa[--b[(int)s[j]]] = j; } sa_is_inducedSort(s, n, size, types, sa, b); } void buildSuffixArray(const int *s, int n, int size, int *sa) { if (n == 0) sa[0] = 0; else { vector<int> b(max(size, (n + 1) / 2) + 1); sa_is_rec(s, n, size, &sa[0], b); } } void buildSuffixArray(const string s, int n, int *sa) { int a[n]; for (int i = 0; i < n; i++) a[i] = (int)s[i]; int maxElem = *max_element(a, a + n); buildSuffixArray(a, n, maxElem + 1, sa); } const int MAXN = 10010; int sa[MAXN]; string ans = "{"; ll n; void dfs(string s, ll m) { if (!m) { ans = min(ans, s); return; } string t = s; reverse(all(t)); s += t; string s1, s2; buildSuffixArray(s, s.size(), sa); rep(i, s.size()) { if (sa[i] < n) { s1 = s.substr(sa[i], n); break; } } reverse(all(s)); buildSuffixArray(s, s.size(), sa); rep(i, s.size()) { if (sa[i] < n) { s2 = s.substr(sa[i], n); reverse(all(s2)); break; } } if (s1 != s2) { dfs(s1, m - 1); dfs(s2, m - 1); } else dfs(s1, m - 1); } void Main() { ll m; string s; cin >> n >> m >> s; if (m < 11) dfs(s, m); else { char c = '{'; rep(i, n) c = min(c, s[i]); ans = ""; rep(i, n) ans += c; } pr(ans); } int main() { ios::sync_with_stdio(0); cin.tie(0); Main(); return 0; }
replace
225
226
225
226
TLE
p02943
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; char S[5555]; int n, m, k; int main() { int i, j; scanf("%d%d%s", &n, &k, S); for (i = 0; i < n; i++) { S[n + i] = S[n - i - 1]; } for (i = 0, m = 0; i <= n; i++) { for (j = 0; j < n; j++) { if (S[i + j] != S[m + j]) { if (S[i + j] < S[m + j]) m = i; break; } } } for (i = 0; S[m + i] == S[m] && i < n; i++) ; m += i; for (; --k && i < n; i <<= 1) ; i = min(n, i); S[m + n - i] = '\0'; for (; i--;) printf("%c", S[m - 1]); printf("%s\n", S + m); return 0; }
#include <bits/stdc++.h> using namespace std; char S[10101]; int n, m, k; int main() { int i, j; scanf("%d%d%s", &n, &k, S); for (i = 0; i < n; i++) { S[n + i] = S[n - i - 1]; } for (i = 0, m = 0; i <= n; i++) { for (j = 0; j < n; j++) { if (S[i + j] != S[m + j]) { if (S[i + j] < S[m + j]) m = i; break; } } } for (i = 0; S[m + i] == S[m] && i < n; i++) ; m += i; for (; --k && i < n; i <<= 1) ; i = min(n, i); S[m + n - i] = '\0'; for (; i--;) printf("%c", S[m - 1]); printf("%s\n", S + m); return 0; }
replace
4
5
4
5
0
p02943
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string s; cin >> s; s.resize(2 * s.size()); for (int i = 0; i < n; i++) { s[2 * n - 1 - i] = s[i]; } string mi, m; mi.resize(1e6); m.resize(1e6); for (int i = 0; i < 1e6; i++) { mi[i] = 'z'; m[i] = 'z'; } if (k == 1) { for (int i = 0; i <= n; i++) { mi = min(mi, s.substr(i, n)); } cout << mi << endl; } else { reverse(s.begin(), s.end()); for (int i = 0; i <= n; i++) { mi = min(mi, s.substr(i, n)); } reverse(mi.begin(), mi.end()); // cout << mi << endl; for (int i = k - 1; i >= max(2, i - 30); i--) { mi.resize(2 * n); for (int i = 0; i < n; i++) { mi[2 * n - 1 - i] = mi[i]; } int pos; for (int j = n; j < 2 * n; j++) { if (mi[j] != mi[n]) break; pos = j; } pos++; mi = mi.substr(pos - n, n); // cout << mi << endl; } mi.resize(2 * n); for (int i = 0; i < n; i++) { mi[2 * n - 1 - i] = mi[i]; } for (int i = 0; i <= n; i++) { m = min(m, mi.substr(i, n)); } cout << m << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string s; cin >> s; s.resize(2 * s.size()); for (int i = 0; i < n; i++) { s[2 * n - 1 - i] = s[i]; } string mi, m; mi.resize(1e6); m.resize(1e6); for (int i = 0; i < 1e6; i++) { mi[i] = 'z'; m[i] = 'z'; } if (k == 1) { for (int i = 0; i <= n; i++) { mi = min(mi, s.substr(i, n)); } cout << mi << endl; } else { reverse(s.begin(), s.end()); for (int i = 0; i <= n; i++) { mi = min(mi, s.substr(i, n)); } reverse(mi.begin(), mi.end()); // cout << mi << endl; for (int i = k - 1; i >= max(2, k - 30); i--) { mi.resize(2 * n); for (int i = 0; i < n; i++) { mi[2 * n - 1 - i] = mi[i]; } int pos; for (int j = n; j < 2 * n; j++) { if (mi[j] != mi[n]) break; pos = j; } pos++; mi = mi.substr(pos - n, n); // cout << mi << endl; } mi.resize(2 * n); for (int i = 0; i < n; i++) { mi[2 * n - 1 - i] = mi[i]; } for (int i = 0; i <= n; i++) { m = min(m, mi.substr(i, n)); } cout << m << endl; } }
replace
39
40
39
40
TLE
p02943
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int maxn = 5e3 + 10; char s[maxn], res[maxn]; char c; int n, k, len[maxn], mx; bool check(int p) { for (int i = 1; i <= n; i++) if (s[i + p - 1] > res[i]) return false; else if (s[i + p - 1] < res[i]) return true; return false; } int main() { c = 'z' + 1; scanf("%d%d", &n, &k); scanf("%s", s + 1); for (int i = 1; i <= n; i++) c = min(c, s[i]), s[n * 2 - i + 1] = s[i]; for (int i = 1; i <= n; i++) res[i] = 'z' + 1; if (k >= 15) { for (int i = 1; i <= n; i++) printf("%c", c); printf("\n"); return 0; } if (k == 1) { for (int i = 1; i <= n + 1; i++) if (check(i)) { for (int j = 1; j <= n; j++) res[j] = s[i + j - 1]; } printf("%s", res + 1); return 0; } for (int i = 1; i <= 2 * n; i++) len[i] = s[i] == c ? len[i - 1] + 1 : 0, mx = max(mx, len[i]); k--; if ((mx << k) >= n) { for (int i = 1; i <= n; i++) printf("%c", c); printf("\n"); return 0; } int now = mx << k; for (int i = 1; i <= n + mx; i++) if (len[i] == mx && check(i + 1)) for (int j = 1; j <= n; j++) res[j] = s[i + j]; for (int i = 1; i <= now; i++) printf("%c", c); for (int i = now + 1; i <= n; i++) printf("%c", res[i - now]); printf("\n"); }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 10; char s[maxn], res[maxn]; char c; int n, k, len[maxn], mx; bool check(int p) { for (int i = 1; i <= n; i++) if (s[i + p - 1] > res[i]) return false; else if (s[i + p - 1] < res[i]) return true; return false; } int main() { c = 'z' + 1; scanf("%d%d", &n, &k); scanf("%s", s + 1); for (int i = 1; i <= n; i++) c = min(c, s[i]), s[n * 2 - i + 1] = s[i]; for (int i = 1; i <= n; i++) res[i] = 'z' + 1; if (k >= 15) { for (int i = 1; i <= n; i++) printf("%c", c); printf("\n"); return 0; } if (k == 1) { for (int i = 1; i <= n + 1; i++) if (check(i)) { for (int j = 1; j <= n; j++) res[j] = s[i + j - 1]; } printf("%s", res + 1); return 0; } for (int i = 1; i <= 2 * n; i++) len[i] = s[i] == c ? len[i - 1] + 1 : 0, mx = max(mx, len[i]); k--; if ((mx << k) >= n) { for (int i = 1; i <= n; i++) printf("%c", c); printf("\n"); return 0; } int now = mx << k; for (int i = 1; i <= n + mx; i++) if (len[i] == mx && check(i + 1)) for (int j = 1; j <= n; j++) res[j] = s[i + j]; for (int i = 1; i <= now; i++) printf("%c", c); for (int i = now + 1; i <= n; i++) printf("%c", res[i - now]); printf("\n"); }
replace
2
3
2
3
0
p02943
C++
Time Limit Exceeded
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author majk */ #ifndef MAJK_LIB #define MAJK_LIB #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define x first #define y second typedef std::pair<int, int> pii; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<ui, ui> puu; template <typename T, typename U> std::istream &operator>>(std::istream &i, pair<T, U> &p) { i >> p.x >> p.y; return i; } template <typename T> std::istream &operator>>(std::istream &i, vector<T> &t) { for (auto &v : t) { i >> v; } return i; } template <typename T, typename U> std::ostream &operator<<(std::ostream &o, const pair<T, U> &p) { o << p.x << ' ' << p.y; return o; } template <typename T> std::ostream &operator<<(std::ostream &o, const vector<T> &t) { if (t.empty()) o << '\n'; for (size_t i = 0; i < t.size(); ++i) { o << t[i] << " \n"[i == t.size() - 1]; } return o; } template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>; ui logceil(ll x) { return x ? 8 * sizeof(ll) - __builtin_clzll(x) : 0; } namespace std { template <typename T, typename U> struct hash<pair<T, U>> { hash<T> t; hash<U> u; size_t operator()(const pair<T, U> &p) const { return t(p.x) ^ (u(p.y) << 7); } }; } // namespace std template <typename T, typename F> T bsh(T l, T h, const F &f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { l = m + 1; r = m; } else { h = m - 1; } } return r; } template <typename F> double bshd(double l, double h, const F &f, double p = 1e-9) { ui r = 3 + (ui)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { l = m; } else { h = m; } } return (l + h) / 2; } template <typename T, typename F> T bsl(T l, T h, const F &f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { h = m - 1; r = m; } else { l = m + 1; } } return r; } template <typename F> double bsld(double l, double h, const F &f, double p = 1e-9) { ui r = 3 + (ui)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { h = m; } else { l = m; } } return (l + h) / 2; } template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <typename T> class vector2 : public vector<vector<T>> { public: vector2() {} vector2(size_t a, size_t b, T t = T()) : vector<vector<T>>(a, vector<T>(b, t)) {} }; template <typename T> class vector3 : public vector<vector2<T>> { public: vector3() {} vector3(size_t a, size_t b, size_t c, T t = T()) : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} }; template <typename T> class vector4 : public vector<vector3<T>> { public: vector4() {} vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} }; template <typename T> class vector5 : public vector<vector4<T>> { public: vector5() {} vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} }; #endif #include <type_traits> // ordinary suffix array with optional LCP and LCP RMQ. look elsewhere template <typename Index, bool PrecomputeLCP = false, bool PrecomputeRMQ = false> class SuffixArray { public: static_assert(PrecomputeLCP || !PrecomputeRMQ, "Must have RMQ for LCP"); template <typename T> explicit SuffixArray(T t) : N(t.size()), S(N), I(N), LCP(PrecomputeLCP ? N : 0) { typedef typename std::remove_reference<decltype(t[0])>::type Item; vector<pair<Item, Index>> TR(N); for (Index i = 0; i < N; ++i) { TR[i] = {t[i], i}; } sort(TR.begin(), TR.end()); vector<Index> R(N); Index r = R[TR[0].y] = S[TR[0].y] = 0; for (Index i = 1; i < N; ++i) { R[TR[i].y] = r += (TR[i - 1].x != TR[i].x); S[TR[i].y] = i; } vector<Index> RA(N), SA(N), C(N); for (Index k = 1; k < N; k <<= 1) { counting_sort(R, C, SA, k, r + 1); counting_sort(R, C, SA, 0, r + 1); RA[S[0]] = r = 0; for (Index i = 1; i < N; ++i) { RA[S[i]] = r += (R[S[i]] != R[S[i - 1]] || S[i] + k >= N || S[i - 1] + k >= N || R[S[i] + k] != R[S[i - 1] + k]); } swap(RA, R); if (R[S[N - 1]] == N - 1) break; } for (Index i = 0; i < N; ++i) { I[S[i]] = i; } if (PrecomputeLCP) { Index k = 0; for (Index i = 0; i < N; ++i) { if (I[i] == N - 1) { LCP[I[i]] = k = 0; continue; } while (i + k < N && S[I[i] + 1] + k < N && t[i + k] == t[S[I[i] + 1] + k]) { ++k; } LCP[I[i]] = k; if (k > 0) { --k; } } } if (PrecomputeRMQ) { RMQ.push_back(LCP); for (int p = 0; (1 << p) < N; ++p) { RMQ.push_back(RMQ[p]); for (int i = 0; i < N - (1 << p); ++i) { RMQ[p + 1][i] = min(RMQ[p + 1][i], RMQ[p][i + (1 << p)]); } } } } Index next(Index i) { return (I[i] == N - 1) ? -1 : S[I[i] + 1]; } template <typename = std::enable_if<PrecomputeLCP>> Index lcp(Index i) const { return LCP[I[i]]; } template <typename = std::enable_if<PrecomputeRMQ>> Index lcp(Index i, Index j) const { i = I[i]; j = I[j]; if (i > j) { swap(i, j); } if (i == j - 1) return LCP[i]; Index p = 0; while ((1 << p) < j - i) { ++p; } --p; return min(RMQ[p][i], RMQ[p][j - (1 << p)]); } inline void counting_sort(const vector<int> &R, vector<int> &C, vector<int> &SA, int k, int r) { Index i; fill(C.begin(), C.begin() + r, 0); for (i = k; i < N; i++) C[R[i]]++; C[0] += k; for (Index sum = i = 0; i < r; i++) { C[i] = (sum += C[i], sum - C[i]); } if (k) for (i = 0; i < N; i++) { SA[C[S[i] + k < N ? R[S[i] + k] : 0]++] = S[i]; } else for (i = 0; i < N; i++) { SA[C[R[S[i]]]++] = S[i]; } swap(SA, S); } Index N; vector<Index> S, I, LCP; vector<vector<Index>> RMQ; }; #ifndef MAJK_RANDOM_H #define MAJK_RANDOM_H #include <chrono> #include <random> auto seed = chrono::high_resolution_clock::now().time_since_epoch().count(); std::mt19937 rng(seed); using random_int = std::uniform_int_distribution<int>; using random_ll = std::uniform_int_distribution<ll>; struct TimeLimit { TimeLimit(double seconds) : s(seconds - 0.2), last(clock()), mx(0) {} operator bool() const { clock_t cur = clock(); mx = max(mx, cur - last); last = cur; return clock() < s * CLOCKS_PER_SEC - 1.5 * mx; } double s; mutable clock_t last, mx; }; #endif // MAJK_RANDOM_H class EReversingAndConcatenating { public: void solve(istream &cin, ostream &cout) { int N; cin >> N; int K; cin >> K; string S; cin >> S; string Ans(N, 'z'); char smallest = *min_element(S.begin(), S.end()); int longest = 0; vector<int> I(N + 1); for (int i = 0; i <= N; ++i) I[i] = i; shuffle(I.begin(), I.end(), rng); int suffixes = 0; for (int i : I) { string A = S; string T = A; reverse(T.begin(), T.end()); A += T; A = A.substr(i, N); if (A[0] != smallest) continue; Ans = min(Ans, A); if (K >= 2) { string T = A; reverse(T.begin(), T.end()); A = T + A; int cur = 0, best = 0; for (int a : A) { if (a == smallest) { cur++; best = max(best, cur); } else { cur = 0; } } best = min(best, N); if (best < longest || longest == N) continue; longest = max(longest, best); suffixes++; if (suffixes > 1300) continue; A += '{'; SuffixArray<int> SA(A); int k = SA.S[0]; A = A.substr(SA.S[0], N); int w = 0; while (w < N && A[0] == A[w]) ++w; int z = w; for (int g = 2; g < K; ++g) { z *= 2; if (z > N) { z = N; break; } } string Q = string(z, A[0]) + A.substr(w); Ans = min(Ans, Q.substr(0, N)); if (z == N) break; } // for (int j = 1; j < K; ++j) { // string T = A; // reverse(T.begin(),T.end()); // A = T + A; // A += '{'; // // SuffixArray<int> SA(A); // int k = SA.S[0]; // A = A.substr(SA.S[0], N); // if (count(A.begin(),A.end(),A[0]) == N) { // break; // } // } // Ans = min(Ans, A); } cout << Ans << endl; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); EReversingAndConcatenating solver; std::istream &in(std::cin); std::ostream &out(std::cout); solver.solve(in, out); return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author majk */ #ifndef MAJK_LIB #define MAJK_LIB #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define x first #define y second typedef std::pair<int, int> pii; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<ui, ui> puu; template <typename T, typename U> std::istream &operator>>(std::istream &i, pair<T, U> &p) { i >> p.x >> p.y; return i; } template <typename T> std::istream &operator>>(std::istream &i, vector<T> &t) { for (auto &v : t) { i >> v; } return i; } template <typename T, typename U> std::ostream &operator<<(std::ostream &o, const pair<T, U> &p) { o << p.x << ' ' << p.y; return o; } template <typename T> std::ostream &operator<<(std::ostream &o, const vector<T> &t) { if (t.empty()) o << '\n'; for (size_t i = 0; i < t.size(); ++i) { o << t[i] << " \n"[i == t.size() - 1]; } return o; } template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>; ui logceil(ll x) { return x ? 8 * sizeof(ll) - __builtin_clzll(x) : 0; } namespace std { template <typename T, typename U> struct hash<pair<T, U>> { hash<T> t; hash<U> u; size_t operator()(const pair<T, U> &p) const { return t(p.x) ^ (u(p.y) << 7); } }; } // namespace std template <typename T, typename F> T bsh(T l, T h, const F &f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { l = m + 1; r = m; } else { h = m - 1; } } return r; } template <typename F> double bshd(double l, double h, const F &f, double p = 1e-9) { ui r = 3 + (ui)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { l = m; } else { h = m; } } return (l + h) / 2; } template <typename T, typename F> T bsl(T l, T h, const F &f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { h = m - 1; r = m; } else { l = m + 1; } } return r; } template <typename F> double bsld(double l, double h, const F &f, double p = 1e-9) { ui r = 3 + (ui)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { h = m; } else { l = m; } } return (l + h) / 2; } template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <typename T> class vector2 : public vector<vector<T>> { public: vector2() {} vector2(size_t a, size_t b, T t = T()) : vector<vector<T>>(a, vector<T>(b, t)) {} }; template <typename T> class vector3 : public vector<vector2<T>> { public: vector3() {} vector3(size_t a, size_t b, size_t c, T t = T()) : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} }; template <typename T> class vector4 : public vector<vector3<T>> { public: vector4() {} vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} }; template <typename T> class vector5 : public vector<vector4<T>> { public: vector5() {} vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} }; #endif #include <type_traits> // ordinary suffix array with optional LCP and LCP RMQ. look elsewhere template <typename Index, bool PrecomputeLCP = false, bool PrecomputeRMQ = false> class SuffixArray { public: static_assert(PrecomputeLCP || !PrecomputeRMQ, "Must have RMQ for LCP"); template <typename T> explicit SuffixArray(T t) : N(t.size()), S(N), I(N), LCP(PrecomputeLCP ? N : 0) { typedef typename std::remove_reference<decltype(t[0])>::type Item; vector<pair<Item, Index>> TR(N); for (Index i = 0; i < N; ++i) { TR[i] = {t[i], i}; } sort(TR.begin(), TR.end()); vector<Index> R(N); Index r = R[TR[0].y] = S[TR[0].y] = 0; for (Index i = 1; i < N; ++i) { R[TR[i].y] = r += (TR[i - 1].x != TR[i].x); S[TR[i].y] = i; } vector<Index> RA(N), SA(N), C(N); for (Index k = 1; k < N; k <<= 1) { counting_sort(R, C, SA, k, r + 1); counting_sort(R, C, SA, 0, r + 1); RA[S[0]] = r = 0; for (Index i = 1; i < N; ++i) { RA[S[i]] = r += (R[S[i]] != R[S[i - 1]] || S[i] + k >= N || S[i - 1] + k >= N || R[S[i] + k] != R[S[i - 1] + k]); } swap(RA, R); if (R[S[N - 1]] == N - 1) break; } for (Index i = 0; i < N; ++i) { I[S[i]] = i; } if (PrecomputeLCP) { Index k = 0; for (Index i = 0; i < N; ++i) { if (I[i] == N - 1) { LCP[I[i]] = k = 0; continue; } while (i + k < N && S[I[i] + 1] + k < N && t[i + k] == t[S[I[i] + 1] + k]) { ++k; } LCP[I[i]] = k; if (k > 0) { --k; } } } if (PrecomputeRMQ) { RMQ.push_back(LCP); for (int p = 0; (1 << p) < N; ++p) { RMQ.push_back(RMQ[p]); for (int i = 0; i < N - (1 << p); ++i) { RMQ[p + 1][i] = min(RMQ[p + 1][i], RMQ[p][i + (1 << p)]); } } } } Index next(Index i) { return (I[i] == N - 1) ? -1 : S[I[i] + 1]; } template <typename = std::enable_if<PrecomputeLCP>> Index lcp(Index i) const { return LCP[I[i]]; } template <typename = std::enable_if<PrecomputeRMQ>> Index lcp(Index i, Index j) const { i = I[i]; j = I[j]; if (i > j) { swap(i, j); } if (i == j - 1) return LCP[i]; Index p = 0; while ((1 << p) < j - i) { ++p; } --p; return min(RMQ[p][i], RMQ[p][j - (1 << p)]); } inline void counting_sort(const vector<int> &R, vector<int> &C, vector<int> &SA, int k, int r) { Index i; fill(C.begin(), C.begin() + r, 0); for (i = k; i < N; i++) C[R[i]]++; C[0] += k; for (Index sum = i = 0; i < r; i++) { C[i] = (sum += C[i], sum - C[i]); } if (k) for (i = 0; i < N; i++) { SA[C[S[i] + k < N ? R[S[i] + k] : 0]++] = S[i]; } else for (i = 0; i < N; i++) { SA[C[R[S[i]]]++] = S[i]; } swap(SA, S); } Index N; vector<Index> S, I, LCP; vector<vector<Index>> RMQ; }; #ifndef MAJK_RANDOM_H #define MAJK_RANDOM_H #include <chrono> #include <random> auto seed = chrono::high_resolution_clock::now().time_since_epoch().count(); std::mt19937 rng(seed); using random_int = std::uniform_int_distribution<int>; using random_ll = std::uniform_int_distribution<ll>; struct TimeLimit { TimeLimit(double seconds) : s(seconds - 0.2), last(clock()), mx(0) {} operator bool() const { clock_t cur = clock(); mx = max(mx, cur - last); last = cur; return clock() < s * CLOCKS_PER_SEC - 1.5 * mx; } double s; mutable clock_t last, mx; }; #endif // MAJK_RANDOM_H class EReversingAndConcatenating { public: void solve(istream &cin, ostream &cout) { int N; cin >> N; int K; cin >> K; string S; cin >> S; string Ans(N, 'z'); char smallest = *min_element(S.begin(), S.end()); int longest = 0; vector<int> I(N + 1); for (int i = 0; i <= N; ++i) I[i] = i; shuffle(I.begin(), I.end(), rng); int suffixes = 0; for (int i : I) { string A = S; string T = A; reverse(T.begin(), T.end()); A += T; A = A.substr(i, N); if (A[0] != smallest) continue; Ans = min(Ans, A); if (K >= 2) { string T = A; reverse(T.begin(), T.end()); A = T + A; int cur = 0, best = 0; for (int a : A) { if (a == smallest) { cur++; best = max(best, cur); } else { cur = 0; } } best = min(best, N); if (best < longest || longest == N) continue; longest = max(longest, best); suffixes++; if (suffixes > 1200) continue; A += '{'; SuffixArray<int> SA(A); int k = SA.S[0]; A = A.substr(SA.S[0], N); int w = 0; while (w < N && A[0] == A[w]) ++w; int z = w; for (int g = 2; g < K; ++g) { z *= 2; if (z > N) { z = N; break; } } string Q = string(z, A[0]) + A.substr(w); Ans = min(Ans, Q.substr(0, N)); if (z == N) break; } // for (int j = 1; j < K; ++j) { // string T = A; // reverse(T.begin(),T.end()); // A = T + A; // A += '{'; // // SuffixArray<int> SA(A); // int k = SA.S[0]; // A = A.substr(SA.S[0], N); // if (count(A.begin(),A.end(),A[0]) == N) { // break; // } // } // Ans = min(Ans, A); } cout << Ans << endl; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); EReversingAndConcatenating solver; std::istream &in(std::cin); std::ostream &out(std::cout); solver.solve(in, out); return 0; }
replace
344
345
344
345
TLE
p02943
C++
Runtime Error
// minamoto #include <bits/stdc++.h> #define R register #define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i) #define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i) #define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v) template <class T> inline bool cmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <class T> inline bool cmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } using namespace std; const int N = 5005; char s[N], t[N], ans[N], mn, mx; int n, k; bool cmp(char *s, char *t) { fp(i, 1, n) if (s[i] != t[i]) return s[i] < t[i]; return false; } void solve(char *s, int k) { R int now = n, len = 0; while (now && s[now] == mn) --now, ++len; while (k && len < n) len <<= 1, --k; if (len >= n) { fp(i, 1, n) ans[i] = mn; printf("%s\n", ans + 1); exit(0); } fp(i, 1, len) t[i] = mn; fp(i, len + 1, n) t[i] = s[now--]; if (cmp(t, ans)) fp(i, 1, n) ans[i] = t[i]; } int main() { scanf("%d%d%s", &n, &k, s + 1); mn = 'z', mx = 'a'; fp(i, 1, n) cmin(mn, s[i]), cmax(mx, s[i]), ans[i] = 'z', s[(n << 1) - i + 1] = s[i]; if (mn == mx) return printf("%s\n", s + 1), 0; if (s[n] == mn) solve(s, k); fp(i, n, n << 1) if (s[i] == mn) solve(s + i - n, k - 1); printf("%s\n", ans + 1); return 0; }
// minamoto #include <bits/stdc++.h> #define R register #define fp(i, a, b) for (R int i = (a), I = (b) + 1; i < I; ++i) #define fd(i, a, b) for (R int i = (a), I = (b)-1; i > I; --i) #define go(u) for (int i = head[u], v = e[i].v; i; i = e[i].nx, v = e[i].v) template <class T> inline bool cmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <class T> inline bool cmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } using namespace std; const int N = 10005; char s[N], t[N], ans[N], mn, mx; int n, k; bool cmp(char *s, char *t) { fp(i, 1, n) if (s[i] != t[i]) return s[i] < t[i]; return false; } void solve(char *s, int k) { R int now = n, len = 0; while (now && s[now] == mn) --now, ++len; while (k && len < n) len <<= 1, --k; if (len >= n) { fp(i, 1, n) ans[i] = mn; printf("%s\n", ans + 1); exit(0); } fp(i, 1, len) t[i] = mn; fp(i, len + 1, n) t[i] = s[now--]; if (cmp(t, ans)) fp(i, 1, n) ans[i] = t[i]; } int main() { scanf("%d%d%s", &n, &k, s + 1); mn = 'z', mx = 'a'; fp(i, 1, n) cmin(mn, s[i]), cmax(mx, s[i]), ans[i] = 'z', s[(n << 1) - i + 1] = s[i]; if (mn == mx) return printf("%s\n", s + 1), 0; if (s[n] == mn) solve(s, k); fp(i, n, n << 1) if (s[i] == mn) solve(s + i - n, k - 1); printf("%s\n", ans + 1); return 0; }
replace
13
14
13
14
0
p02943
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int N, K; cin >> N >> K; string S; cin >> S; string ans = S; string T(S.rbegin(), S.rend()); string U = S + T; char a = *min_element(S.begin(), S.end()); if (K == 1) { for (int i = 0; i <= N; i++) { ans = min(ans, string(U.begin() + i, U.begin() + i + N)); } cout << ans << '\n'; return 0; } else { for (int i = 0; i <= N; i++) { string cur = string(U.begin() + i, U.begin() + i + N); int cnt = 0; while (!cur.empty() && cur.back() == a) { cur.pop_back(); cnt++; } for (int z = 0; cnt < N && z < K - 1; z++) { cnt = min(cnt * 2, N); } string cnd(cnt, a); cnd += string(cur.rbegin(), cur.rend()); ans = min(ans, string(cnd.begin(), cnd.begin() + N)); } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int N, K; cin >> N >> K; string S; cin >> S; string ans = S; string T(S.rbegin(), S.rend()); string U = S + T; char a = *min_element(S.begin(), S.end()); if (K == 1) { for (int i = 0; i <= N; i++) { ans = min(ans, string(U.begin() + i, U.begin() + i + N)); } cout << ans << '\n'; return 0; } else { for (int i = 0; i <= N; i++) { string cur = string(U.begin() + i, U.begin() + i + N); int cnt = 0; while (!cur.empty() && cur.back() == a) { cur.pop_back(); cnt++; } if (cnt == 0) continue; for (int z = 0; cnt < N && z < K - 1; z++) { cnt = min(cnt * 2, N); } string cnd(cnt, a); cnd += string(cur.rbegin(), cur.rend()); ans = min(ans, string(cnd.begin(), cnd.begin() + N)); } cout << ans << '\n'; } return 0; }
insert
29
29
29
31
TLE
p02943
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; string s; cin >> s; for (int i = 0; i < k; ++i) { string t = s; reverse(t.begin(), t.end()); string x = s + t; vector<string> tmp; for (int i = 0; i < n; ++i) { tmp.emplace_back(x.substr(i, n)); } sort(tmp.begin(), tmp.end()); s = *min_element(tmp.begin(), tmp.end()); reverse(s.begin(), s.end()); } reverse(s.begin(), s.end()); cout << s << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; string s; cin >> s; for (int i = 0; i < k; ++i) { if (*min_element(s.begin(), s.end()) == *max_element(s.begin(), s.end())) { break; } string t = s; reverse(t.begin(), t.end()); string x = s + t; vector<string> tmp; for (int i = 0; i < n; ++i) { tmp.emplace_back(x.substr(i, n)); } sort(tmp.begin(), tmp.end()); s = *min_element(tmp.begin(), tmp.end()); reverse(s.begin(), s.end()); } reverse(s.begin(), s.end()); cout << s << '\n'; return 0; }
insert
15
15
15
18
TLE
p02943
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; const int N = 5005; int n, k; string s; int main() { // freopen("data.in","r",stdin); // freopen("data.out","w",stdout); cin >> n >> k; cin >> s; char c = 'z' + 1; int L = 0, R = 0; string tmp; for (int i = 0; i <= n; i++) tmp += 'z'; for (int i = 0, j = 0; i < n; i = j) { j = i; while (j < n && s[i] == s[j]) j++; if (s[i] < c) c = s[i], L = i, R = j - 1, tmp = s.substr(j); else if (s[i] == c && j - i > R - L + 1) L = i, R = j - 1, tmp = s.substr(j); else if (s[i] == c && j - i == R - L + 1 && s.substr(j) < tmp) L = i, R = j - 1, tmp = s.substr(j); } if (k == 1) { if (s[n - 1] != c) { string t = s; reverse(t.begin(), t.end()); s += t; s = s.substr(L, n); cout << s; } else { int cnt = 0, now = n - 1; while (now >= 0 && s[now] == c) now--, cnt++; string t = s; reverse(t.begin(), t.end()); string st = s + t; st = st.substr(now + 1, n); t = s; reverse(t.begin(), t.end()); s += t; s = s.substr(L, n); cout << min(st, s); } return 0; } // cerr<<"caht"<<c<<"\n"; // cerr<<"now"<<L<<" "<<R<<"\n"; // cerr<<"char"<<s[R+1]<<"\n"; // cerr<<tmp<<endl; // cerr<<s<<"\n"; if (R != n - 1) { int cnt = 0, now = n - 1; while (now >= 0 && s[now] == c) now--, cnt++; string t = s.substr(0, now + 1); reverse(t.begin(), t.end()); if (cnt * 2 < R - L + 1 || (cnt * 2 == R - L + 1 && tmp < t)) { string t = s; reverse(t.begin(), t.end()); s += t; int r = (n - 1 - L) + n, l = r - n + 1; // cerr<<"ssssss"<<s[r]<<" "<<s[l]<<"\n"; // exit(1); s = s.substr(l, n); k--; } } // cerr<<s<<"\n"; // exit(1); int pre = n - 1; while (pre >= 0 && s[pre] == c) pre--; pre++; // cerr<<"pre"<<pre<<"\n"; for (int i = 1; i < k; i++) { string t = s; reverse(t.begin(), t.end()); s += t; int r = (n - 1 - pre) + n, l = r - n + 1; s = s.substr(l, n); pre = n - 1; while (pre >= 0 && s[pre] == c) pre--; pre++; } string t = s; reverse(t.begin(), t.end()); s += t; int l = pre, r = l + n - 1; s = s.substr(l, n); cout << s; return 0; }
#include <algorithm> #include <cstdio> #include <iostream> using namespace std; const int N = 5005; int n, k; string s; int main() { // freopen("data.in","r",stdin); // freopen("data.out","w",stdout); cin >> n >> k; cin >> s; char c = 'z' + 1; int L = 0, R = 0; string tmp; for (int i = 0; i <= n; i++) tmp += 'z'; for (int i = 0, j = 0; i < n; i = j) { j = i; while (j < n && s[i] == s[j]) j++; if (s[i] < c) c = s[i], L = i, R = j - 1, tmp = s.substr(j); else if (s[i] == c && j - i > R - L + 1) L = i, R = j - 1, tmp = s.substr(j); else if (s[i] == c && j - i == R - L + 1 && s.substr(j) < tmp) L = i, R = j - 1, tmp = s.substr(j); } if (k == 1) { if (s[n - 1] != c) { string t = s; reverse(t.begin(), t.end()); s += t; s = s.substr(L, n); cout << s; } else { int cnt = 0, now = n - 1; while (now >= 0 && s[now] == c) now--, cnt++; string t = s; reverse(t.begin(), t.end()); string st = s + t; st = st.substr(now + 1, n); t = s; reverse(t.begin(), t.end()); s += t; s = s.substr(L, n); cout << min(st, s); } return 0; } // cerr<<"caht"<<c<<"\n"; // cerr<<"now"<<L<<" "<<R<<"\n"; // cerr<<"char"<<s[R+1]<<"\n"; // cerr<<tmp<<endl; // cerr<<s<<"\n"; if (R != n - 1) { int cnt = 0, now = n - 1; while (now >= 0 && s[now] == c) now--, cnt++; string t = s.substr(0, now + 1); reverse(t.begin(), t.end()); if (cnt * 2 < R - L + 1 || (cnt * 2 == R - L + 1 && tmp < t)) { string t = s; reverse(t.begin(), t.end()); s += t; int r = (n - 1 - L) + n, l = r - n + 1; // cerr<<"ssssss"<<s[r]<<" "<<s[l]<<"\n"; // exit(1); s = s.substr(l, n); k--; } } // cerr<<s<<"\n"; // exit(1); int pre = n - 1; while (pre >= 0 && s[pre] == c) pre--; pre++; // cerr<<"pre"<<pre<<"\n"; for (int i = 1; i < k; i++) { string t = s; reverse(t.begin(), t.end()); s += t; int r = (n - 1 - pre) + n, l = r - n + 1; s = s.substr(l, n); pre = n - 1; while (pre >= 0 && s[pre] == c) pre--; pre++; if (pre == 0) break; } string t = s; reverse(t.begin(), t.end()); s += t; int l = pre, r = l + n - 1; s = s.substr(l, n); cout << s; return 0; }
insert
90
90
90
92
TLE
p02943
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 5005; char s[N]; int p[N]; signed main() { int n, K; long long res = 1; cin >> n >> K; for (int i = 2; i <= K; ++i) { res *= 2; if (res >= n) { res = n; break; } } cin >> (s + 1); for (int i = n + 1; i <= n + n; ++i) s[i] = s[i - n]; reverse(s + n + 1, s + n + n + 1); char ch = *min_element(s + 1, s + n + 1); int cnt = (s[1] == ch); for (int i = 2; i <= n + n; ++i) if (s[i] == ch && s[i] == s[i - 1]) ++cnt; else p[i - 1] = cnt, cnt = (s[i] == ch); p[n + n] = cnt; int mx = *max_element(p + n + 1, p + n + n + 1); res *= mx; if (res >= n) { for (int i = 1; i <= n; ++i) cout << ch; return 0; } string S = "", tmp; for (int i = n + 1; i <= n + n; ++i) if (p[i] == mx) { tmp = ""; for (int j = 1; j <= res; ++j) tmp += ch; for (int j = i - p[i]; j > i - p[i] - (n - res); --j) tmp += s[j]; if (S == "") S = tmp; else S = min(S, tmp); } cout << S; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 10005; char s[N]; int p[N]; signed main() { int n, K; long long res = 1; cin >> n >> K; for (int i = 2; i <= K; ++i) { res *= 2; if (res >= n) { res = n; break; } } cin >> (s + 1); for (int i = n + 1; i <= n + n; ++i) s[i] = s[i - n]; reverse(s + n + 1, s + n + n + 1); char ch = *min_element(s + 1, s + n + 1); int cnt = (s[1] == ch); for (int i = 2; i <= n + n; ++i) if (s[i] == ch && s[i] == s[i - 1]) ++cnt; else p[i - 1] = cnt, cnt = (s[i] == ch); p[n + n] = cnt; int mx = *max_element(p + n + 1, p + n + n + 1); res *= mx; if (res >= n) { for (int i = 1; i <= n; ++i) cout << ch; return 0; } string S = "", tmp; for (int i = n + 1; i <= n + n; ++i) if (p[i] == mx) { tmp = ""; for (int j = 1; j <= res; ++j) tmp += ch; for (int j = i - p[i]; j > i - p[i] - (n - res); --j) tmp += s[j]; if (S == "") S = tmp; else S = min(S, tmp); } cout << S; return 0; }
replace
2
3
2
3
0
p02943
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int max_n = 5555, inf = 1000111222; int n, k; string s; string getr(const string &s, int pos, int mx) { if (mx + (mx << k) >= n) { return ""; } // cout << "$" << mx << endl; return {s.rbegin() + s.length() - 1 - pos + mx, s.rbegin() + s.length() - 1 - pos + n - (mx << k)}; } string one_step(const string &s) { string rs = s; reverse(rs.begin(), rs.end()); string t = s + rs; string res = s; for (int i = 1; i <= n; ++i) { res = min(res, t.substr(i, n)); } return res; } int main() { freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n >> k >> s; --k; char mn = s[0]; for (char c : s) { mn = min(mn, c); } if (k) { k = min(k, 20); --k; string rs = s; reverse(rs.begin(), rs.end()); int mx = 0, pos = 0; string t = s + rs; for (int i = 0; i < t.length(); ++i) { int st = i; while (i < t.length() && t[i] == mn) { ++i; } int cnt = i - st; if (cnt == 0) { continue; } if (i < n) { continue; } if (cnt == 1) { // cout << i << ": " << getr(t, i - 1, cnt) << endl; } if (i >= n && (mx < cnt || mx == cnt && getr(t, pos, mx) > getr(t, i - 1, mx))) { pos = i - 1; mx = cnt; } } // cout << t << endl; // cout << mx << " " << pos << " " << getr(t, pos) << endl; s = t.substr(pos - n + 1, n); // reverse(s.begin(), s.end()); // cout << "$" << s << " " << k << endl; mx = min(mx, n); while (k--) { string rs = s; reverse(rs.begin(), rs.end()); string t = s + rs; s = t.substr(s.length() + mx - n, n); mx *= 2; mx = min(mx, n); if (mx == n) { break; } } // cout << s << endl; } cout << one_step(s) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int max_n = 5555, inf = 1000111222; int n, k; string s; string getr(const string &s, int pos, int mx) { if (mx + (mx << k) >= n) { return ""; } // cout << "$" << mx << endl; return {s.rbegin() + s.length() - 1 - pos + mx, s.rbegin() + s.length() - 1 - pos + n - (mx << k)}; } string one_step(const string &s) { string rs = s; reverse(rs.begin(), rs.end()); string t = s + rs; string res = s; for (int i = 1; i <= n; ++i) { res = min(res, t.substr(i, n)); } return res; } int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n >> k >> s; --k; char mn = s[0]; for (char c : s) { mn = min(mn, c); } if (k) { k = min(k, 20); --k; string rs = s; reverse(rs.begin(), rs.end()); int mx = 0, pos = 0; string t = s + rs; for (int i = 0; i < t.length(); ++i) { int st = i; while (i < t.length() && t[i] == mn) { ++i; } int cnt = i - st; if (cnt == 0) { continue; } if (i < n) { continue; } if (cnt == 1) { // cout << i << ": " << getr(t, i - 1, cnt) << endl; } if (i >= n && (mx < cnt || mx == cnt && getr(t, pos, mx) > getr(t, i - 1, mx))) { pos = i - 1; mx = cnt; } } // cout << t << endl; // cout << mx << " " << pos << " " << getr(t, pos) << endl; s = t.substr(pos - n + 1, n); // reverse(s.begin(), s.end()); // cout << "$" << s << " " << k << endl; mx = min(mx, n); while (k--) { string rs = s; reverse(rs.begin(), rs.end()); string t = s + rs; s = t.substr(s.length() + mx - n, n); mx *= 2; mx = min(mx, n); if (mx == n) { break; } } // cout << s << endl; } cout << one_step(s) << endl; return 0; }
replace
30
31
30
31
-6
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr: __pos (which is 1) > this->size() (which is 0)
p02943
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 5e3 + 10; int n, k, L[N]; string s; int main() { cin >> n >> k >> s; char mn = *min_element(s.begin(), s.end()); string t = s; reverse(t.begin(), t.end()); t = s + t; for (int i = 0; i < n + n; i++) { if (i && t[i] == mn) L[i] = L[i - 1] + 1; else L[i] = 0; } int mx = *max_element(L, L + n + n), len = mx; while (k > 1 && len < n) --k, len <<= 1; if (len >= n) cout << string(n, mn); else { string ans; for (int i = n - 1; i < n + n; i++) if (L[i] == mx) { string tmp = string(len, mn) + t.substr(n + n - 1 - i + mx, n - len); if (ans.empty() || ans > tmp) ans = tmp; } cout << ans; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e4 + 10; int n, k, L[N]; string s; int main() { cin >> n >> k >> s; char mn = *min_element(s.begin(), s.end()); string t = s; reverse(t.begin(), t.end()); t = s + t; for (int i = 0; i < n + n; i++) { if (i && t[i] == mn) L[i] = L[i - 1] + 1; else L[i] = 0; } int mx = *max_element(L, L + n + n), len = mx; while (k > 1 && len < n) --k, len <<= 1; if (len >= n) cout << string(n, mn); else { string ans; for (int i = n - 1; i < n + n; i++) if (L[i] == mx) { string tmp = string(len, mn) + t.substr(n + n - 1 - i + mx, n - len); if (ans.empty() || ans > tmp) ans = tmp; } cout << ans; } return 0; }
replace
2
3
2
3
0
p02943
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for (int64 i = 0; i < (n); i++) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define all(x) x.begin(), x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) { for (auto &e : u) fill_v<T>(e, v...); } class SA_IS { private: std::string str; std::vector<int> sa; bool built = false; static inline bool is_lms(const std::vector<int> &is_l, size_t idx) { return idx > 0 && is_l[idx - 1] && !is_l[idx]; } void create_bin(const std::vector<int> &v, std::vector<int> &bin) { std::fill(bin.begin(), bin.end(), 0); for (auto &x : v) bin[x + 1]++; for (int i = 1; i < bin.size(); i++) bin[i] += bin[i - 1]; } void set_lms(const std::vector<int> &v, std::vector<int> &sa, std::vector<int> &bin, const std::vector<int> &is_l) { for (int i = 0; i < v.size(); i++) { if (is_lms(is_l, i)) { sa[--bin[v[i] + 1]] = i; } } for (int i = 0; i < v.size(); i++) { if (is_lms(is_l, i)) { bin[v[i] + 1]++; } } } void induced_sort(const std::vector<int> &v, std::vector<int> &sa, size_t kind_num, std::vector<int> &bin, const std::vector<int> &is_l) { std::vector<int> cnt(kind_num + 2, 0); for (int i = 0; i < v.size(); i++) { if (sa[i] <= 0 || !is_l[sa[i] - 1]) continue; int kind = v[sa[i] - 1]; sa[bin[kind] + cnt[kind]++] = sa[i] - 1; } } void reverse_induced_sort(const std::vector<int> &v, std::vector<int> &sa, std::vector<int> &bin, const std::vector<int> &is_l) { for (int i = v.size() - 1; i > 0; i--) { if (sa[i] <= 0 || is_l[sa[i] - 1]) continue; int kind = v[sa[i] - 1]; sa[--bin[kind + 1]] = sa[i] - 1; } } public: SA_IS() : built(false) {} SA_IS(const std::string &str_) : str(str_) { build(); } SA_IS(std::string &&str_) : str(str_) { build(); } std::vector<int> build(const std::vector<int> &v, int kinds) { if (v.size() == 1) return std::vector<int>(1, 0); std::vector<int> bin(kinds + 2, 0), is_l(v.size(), 0); std::vector<int> sa(v.size(), -1); for (int i = v.size() - 2; i >= 0; i--) { is_l[i] = v[i] == v[i + 1] ? is_l[i + 1] : v[i] > v[i + 1]; } create_bin(v, bin); set_lms(v, sa, bin, is_l); induced_sort(v, sa, kinds, bin, is_l); reverse_induced_sort(v, sa, bin, is_l); std::vector<int> order(v.size()); int cur = 0, prev = -1; for (int i = 0; i < v.size(); i++) if (is_lms(is_l, i)) order[i] = cur++; std::vector<int> next_v(cur); cur = -1; for (int i = 0; i < v.size(); i++) { if (!is_lms(is_l, sa[i])) continue; bool diff = false; for (int d = 0; d < v.size(); d++) { if (prev == -1 || v[sa[i] + d] != v[prev + d] || is_l[sa[i] + d] != is_l[prev + d]) { diff = true; break; } if (d > 0 && is_lms(is_l, sa[i] + d)) break; } if (diff) { cur++; prev = sa[i]; } next_v[order[sa[i]]] = cur; } std::vector<int> re_order(next_v.size()); std::vector<int> next_sa = build(next_v, cur); std::fill(sa.begin(), sa.end(), -1); for (int i = 0; i < v.size(); i++) if (is_lms(is_l, i)) re_order[order[i]] = i; create_bin(v, bin); for (int i = next_sa.size() - 1; i >= 0; i--) sa[--bin[v[re_order[next_sa[i]]] + 1]] = re_order[next_sa[i]]; create_bin(v, bin); induced_sort(v, sa, kinds, bin, is_l); reverse_induced_sort(v, sa, bin, is_l); return sa; } void build() { str += '$'; std::vector<int> v(str.size()); for (int i = 0; i < str.size(); i++) v[i] = str[i]; sa = build(v, *std::max_element(v.begin(), v.end())); sa = std::vector<int>(sa.begin() + 1, sa.end()); built = true; str = str.substr(0, str.size() - 1); } const std::vector<int> &get_sa() { assert(built); return sa; } bool is_upper(const std::string &t, int s_idx) { // t > sa[si] int idx = 0; s_idx = sa[s_idx]; while (s_idx + idx < sa.size() && idx < t.size()) { if (t[idx] > str[s_idx + idx]) return true; if (t[idx] < str[s_idx + idx]) return false; idx++; } return (idx < t.size() && s_idx + idx >= str.size()); } int lower_bound(const std::string &t) { int l = -1, r = sa.size(), m; while (r - l > 1) { m = (l + r) >> 1; if (is_upper(t, m)) l = m; else r = m; } return r; } ::std::pair<int, int> bounds(std::string &t) { int l = lower_bound(t); t.back()++; int r = lower_bound(t); t.back()--; return ::std::pair<int, int>(l, r); } const int &operator[](const size_t &k) { return sa[k]; } const size_t size() { return str.size() - 1; } }; int main(void) { cin.tie(0); ios::sync_with_stdio(false); int64 N, K; string s; cin >> N >> K >> s; REP(i, min(N, K - 1)) { string t = s; reverse(all(t)); t = s + t; reverse(all(t)); SA_IS sa(t); sa.build(); REP(j, sa.size()) { if (sa.size() - sa[j] >= N) { s = t.substr(sa[j], N); break; } } reverse(all(s)); } string t = s; reverse(all(t)); t = s + t; SA_IS sa(t); sa.build(); REP(j, sa.size()) { if (sa.size() - sa[j] >= N) { s = t.substr(sa[j], N); cout << s << endl; break; } } }
#include <bits/stdc++.h> using namespace std; #define INF_LL (int64)1e18 #define INF (int32)1e9 #define REP(i, n) for (int64 i = 0; i < (n); i++) #define FOR(i, a, b) for (int64 i = (a); i < (b); i++) #define all(x) x.begin(), x.end() #define fs first #define sc second using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); } template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) { for (auto &e : u) fill_v<T>(e, v...); } class SA_IS { private: std::string str; std::vector<int> sa; bool built = false; static inline bool is_lms(const std::vector<int> &is_l, size_t idx) { return idx > 0 && is_l[idx - 1] && !is_l[idx]; } void create_bin(const std::vector<int> &v, std::vector<int> &bin) { std::fill(bin.begin(), bin.end(), 0); for (auto &x : v) bin[x + 1]++; for (int i = 1; i < bin.size(); i++) bin[i] += bin[i - 1]; } void set_lms(const std::vector<int> &v, std::vector<int> &sa, std::vector<int> &bin, const std::vector<int> &is_l) { for (int i = 0; i < v.size(); i++) { if (is_lms(is_l, i)) { sa[--bin[v[i] + 1]] = i; } } for (int i = 0; i < v.size(); i++) { if (is_lms(is_l, i)) { bin[v[i] + 1]++; } } } void induced_sort(const std::vector<int> &v, std::vector<int> &sa, size_t kind_num, std::vector<int> &bin, const std::vector<int> &is_l) { std::vector<int> cnt(kind_num + 2, 0); for (int i = 0; i < v.size(); i++) { if (sa[i] <= 0 || !is_l[sa[i] - 1]) continue; int kind = v[sa[i] - 1]; sa[bin[kind] + cnt[kind]++] = sa[i] - 1; } } void reverse_induced_sort(const std::vector<int> &v, std::vector<int> &sa, std::vector<int> &bin, const std::vector<int> &is_l) { for (int i = v.size() - 1; i > 0; i--) { if (sa[i] <= 0 || is_l[sa[i] - 1]) continue; int kind = v[sa[i] - 1]; sa[--bin[kind + 1]] = sa[i] - 1; } } public: SA_IS() : built(false) {} SA_IS(const std::string &str_) : str(str_) { build(); } SA_IS(std::string &&str_) : str(str_) { build(); } std::vector<int> build(const std::vector<int> &v, int kinds) { if (v.size() == 1) return std::vector<int>(1, 0); std::vector<int> bin(kinds + 2, 0), is_l(v.size(), 0); std::vector<int> sa(v.size(), -1); for (int i = v.size() - 2; i >= 0; i--) { is_l[i] = v[i] == v[i + 1] ? is_l[i + 1] : v[i] > v[i + 1]; } create_bin(v, bin); set_lms(v, sa, bin, is_l); induced_sort(v, sa, kinds, bin, is_l); reverse_induced_sort(v, sa, bin, is_l); std::vector<int> order(v.size()); int cur = 0, prev = -1; for (int i = 0; i < v.size(); i++) if (is_lms(is_l, i)) order[i] = cur++; std::vector<int> next_v(cur); cur = -1; for (int i = 0; i < v.size(); i++) { if (!is_lms(is_l, sa[i])) continue; bool diff = false; for (int d = 0; d < v.size(); d++) { if (prev == -1 || v[sa[i] + d] != v[prev + d] || is_l[sa[i] + d] != is_l[prev + d]) { diff = true; break; } if (d > 0 && is_lms(is_l, sa[i] + d)) break; } if (diff) { cur++; prev = sa[i]; } next_v[order[sa[i]]] = cur; } std::vector<int> re_order(next_v.size()); std::vector<int> next_sa = build(next_v, cur); std::fill(sa.begin(), sa.end(), -1); for (int i = 0; i < v.size(); i++) if (is_lms(is_l, i)) re_order[order[i]] = i; create_bin(v, bin); for (int i = next_sa.size() - 1; i >= 0; i--) sa[--bin[v[re_order[next_sa[i]]] + 1]] = re_order[next_sa[i]]; create_bin(v, bin); induced_sort(v, sa, kinds, bin, is_l); reverse_induced_sort(v, sa, bin, is_l); return sa; } void build() { str += '$'; std::vector<int> v(str.size()); for (int i = 0; i < str.size(); i++) v[i] = str[i]; sa = build(v, *std::max_element(v.begin(), v.end())); sa = std::vector<int>(sa.begin() + 1, sa.end()); built = true; str = str.substr(0, str.size() - 1); } const std::vector<int> &get_sa() { assert(built); return sa; } bool is_upper(const std::string &t, int s_idx) { // t > sa[si] int idx = 0; s_idx = sa[s_idx]; while (s_idx + idx < sa.size() && idx < t.size()) { if (t[idx] > str[s_idx + idx]) return true; if (t[idx] < str[s_idx + idx]) return false; idx++; } return (idx < t.size() && s_idx + idx >= str.size()); } int lower_bound(const std::string &t) { int l = -1, r = sa.size(), m; while (r - l > 1) { m = (l + r) >> 1; if (is_upper(t, m)) l = m; else r = m; } return r; } ::std::pair<int, int> bounds(std::string &t) { int l = lower_bound(t); t.back()++; int r = lower_bound(t); t.back()--; return ::std::pair<int, int>(l, r); } const int &operator[](const size_t &k) { return sa[k]; } const size_t size() { return str.size() - 1; } }; int main(void) { cin.tie(0); ios::sync_with_stdio(false); int64 N, K; string s; cin >> N >> K >> s; REP(i, min((int64)20, min(N, K - 1))) { string t = s; reverse(all(t)); t = s + t; reverse(all(t)); SA_IS sa(t); sa.build(); REP(j, sa.size()) { if (sa.size() - sa[j] >= N) { s = t.substr(sa[j], N); break; } } reverse(all(s)); } string t = s; reverse(all(t)); t = s + t; SA_IS sa(t); sa.build(); REP(j, sa.size()) { if (sa.size() - sa[j] >= N) { s = t.substr(sa[j], N); cout << s << endl; break; } } }
replace
225
226
225
226
TLE
p02943
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <time.h> #include <vector> #define int long long #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) #define ggr \ getchar(); \ getchar(); \ return 0; #define prique priority_queue constexpr auto mod = 1000000007; #define inf 1e15 #define key 1e9 using namespace std; typedef pair<int, int> P; template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); } template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); } // combination(Nが小さい時はこれを使う) const int MAX = 330000; int fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } int COMB(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % mod) % mod; } bool prime(int n) { int cnt = 0; for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) cnt++; } if (cnt != 1) return false; else return n != 1; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } // 繰り返し二乗法(Nが大きい時の場合のcombination) int mod_pow(int x, int y, int m) { int res = 1; while (y) { if (y & 1) { res = res * x % m; } x = x * x % m; y >>= 1; } return res; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= (i % mod); res %= mod; } return res; } int comb(int x, int y) { if (y > x) return 0; return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod; } // UnionFind class UnionFind { protected: int *par, *rank, *size; public: UnionFind(unsigned int size) { par = new int[size]; rank = new int[size]; this->size = new int[size]; rep(i, size) { par[i] = i; rank[i] = 0; this->size[i] = 1; } } int find(int n) { if (par[n] == n) return n; return par[n] = find(par[n]); } void unite(int n, int m) { n = find(n); m = find(m); if (n == m) return; if (rank[n] < rank[m]) { par[n] = m; size[m] += size[n]; } else { par[m] = n; size[n] += size[m]; if (rank[n] == rank[m]) rank[n]++; } } bool same(int n, int m) { return find(n) == find(m); } int getsize(int n) { return size[find(n)]; } }; int dight(int n) { int ans = 1; while (n >= 10) { n /= 10; ans++; } return ans; } int dight_sum(int n) { int sum = 0; rep(i, 20) sum += (n % (int)pow(10, i + 1)) / (pow(10, i)); return sum; } int n, k; string s; signed main() { cin >> n >> k; cin >> s; rep(i, min(n, k)) { string memo = s; reverse(memo.begin(), memo.end()); s += memo; string p = s; for (int j = 0; j <= p.size() - n; j++) { memo = min(memo, p.substr(j, n)); } reverse(memo.begin(), memo.end()); s = memo; } reverse(s.begin(), s.end()); cout << s << endl; ggr }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <time.h> #include <vector> #define int long long #define rep(i, n) for (int i = 0; i < n; i++) #define REP(i, n) for (int i = 1; i <= n; i++) #define ggr \ getchar(); \ getchar(); \ return 0; #define prique priority_queue constexpr auto mod = 1000000007; #define inf 1e15 #define key 1e9 using namespace std; typedef pair<int, int> P; template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); } template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); } // combination(Nが小さい時はこれを使う) const int MAX = 330000; int fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - inv[mod % i] * (mod / i) % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } int COMB(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % mod) % mod; } bool prime(int n) { int cnt = 0; for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) cnt++; } if (cnt != 1) return false; else return n != 1; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } // 繰り返し二乗法(Nが大きい時の場合のcombination) int mod_pow(int x, int y, int m) { int res = 1; while (y) { if (y & 1) { res = res * x % m; } x = x * x % m; y >>= 1; } return res; } int kai(int x, int y) { int res = 1; for (int i = x - y + 1; i <= x; i++) { res *= (i % mod); res %= mod; } return res; } int comb(int x, int y) { if (y > x) return 0; return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod; } // UnionFind class UnionFind { protected: int *par, *rank, *size; public: UnionFind(unsigned int size) { par = new int[size]; rank = new int[size]; this->size = new int[size]; rep(i, size) { par[i] = i; rank[i] = 0; this->size[i] = 1; } } int find(int n) { if (par[n] == n) return n; return par[n] = find(par[n]); } void unite(int n, int m) { n = find(n); m = find(m); if (n == m) return; if (rank[n] < rank[m]) { par[n] = m; size[m] += size[n]; } else { par[m] = n; size[n] += size[m]; if (rank[n] == rank[m]) rank[n]++; } } bool same(int n, int m) { return find(n) == find(m); } int getsize(int n) { return size[find(n)]; } }; int dight(int n) { int ans = 1; while (n >= 10) { n /= 10; ans++; } return ans; } int dight_sum(int n) { int sum = 0; rep(i, 20) sum += (n % (int)pow(10, i + 1)) / (pow(10, i)); return sum; } int n, k; string s; signed main() { cin >> n >> k; cin >> s; rep(i, min(k, (int)20)) { string memo = s; reverse(memo.begin(), memo.end()); s += memo; string p = s; for (int j = 0; j <= p.size() - n; j++) { memo = min(memo, p.substr(j, n)); } reverse(memo.begin(), memo.end()); s = memo; } reverse(s.begin(), s.end()); cout << s << endl; ggr }
replace
146
147
146
147
TLE
p02944
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld double #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define pb emplace_back #define X first #define Y second const int N = 2e5 + 5; typedef pair<int, int> ii; int a[N], t[N]; int f1[N], f2[N]; int s1[N], s2[N]; int pre[N]; int nxt[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int L; cin >> L; priority_queue<ii, vector<ii>, greater<ii>> pq; for (int i = 1; i <= n; ++i) { cin >> a[i]; f1[i] = f2[i] = 1; pre[i] = i - 1; nxt[i] = i + 1; pq.push(ii(a[i], i)); } nxt[n] = 0; int tot = n; ll ans = 0; while (pq.size()) { int p = pq.top().Y; int v = pq.top().X; pq.pop(); if (t[p]) continue; int l = p; int r = p; while (a[pre[l]] == a[l]) l = pre[l]; while (a[nxt[r]] == a[r]) r = nxt[r]; int cnt = 0; int prv = 0; for (int i = l; i != nxt[r]; t[i] = 1, i = nxt[i]) { cnt++; if (i <= n) prv = cnt; s1[cnt] = s1[cnt - 1] + f1[i]; s2[cnt] = s2[cnt - 1] + f2[i]; if (prv && cnt >= L) ans += 1ll * f2[i] * s1[min(prv, cnt - L + 1)]; } if (cnt < L) { nxt[pre[l]] = 0; pre[nxt[r]] = 0; continue; } int S = cnt / L; for (int i = 1; i < S; ++i) { nxt[tot + i] = 1 + tot + i; pre[tot + i + 1] = tot + i; } for (int i = 1; i <= S; ++i) a[tot + i] = a[p] + 1; nxt[pre[l]] = tot + 1; pre[tot + 1] = pre[l]; pre[nxt[r]] = tot + S; nxt[tot + S] = nxt[r]; for (int i = 1; i <= S; ++i) { int j = S - i + 1; f2[tot + i] = s2[min((i + 1) * L - 1, cnt)] - s2[i * L - 1]; f1[tot + i] = s1[cnt - j * L + 1] - s1[max(cnt - j * L - L + 1, 0)]; } pq.push(ii(a[tot + 1], tot + 1)); tot += S; } cout << ans + n << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld double #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define pb emplace_back #define X first #define Y second const int N = 5e6 + 5; typedef pair<int, int> ii; int a[N], t[N]; int f1[N], f2[N]; int s1[N], s2[N]; int pre[N]; int nxt[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int L; cin >> L; priority_queue<ii, vector<ii>, greater<ii>> pq; for (int i = 1; i <= n; ++i) { cin >> a[i]; f1[i] = f2[i] = 1; pre[i] = i - 1; nxt[i] = i + 1; pq.push(ii(a[i], i)); } nxt[n] = 0; int tot = n; ll ans = 0; while (pq.size()) { int p = pq.top().Y; int v = pq.top().X; pq.pop(); if (t[p]) continue; int l = p; int r = p; while (a[pre[l]] == a[l]) l = pre[l]; while (a[nxt[r]] == a[r]) r = nxt[r]; int cnt = 0; int prv = 0; for (int i = l; i != nxt[r]; t[i] = 1, i = nxt[i]) { cnt++; if (i <= n) prv = cnt; s1[cnt] = s1[cnt - 1] + f1[i]; s2[cnt] = s2[cnt - 1] + f2[i]; if (prv && cnt >= L) ans += 1ll * f2[i] * s1[min(prv, cnt - L + 1)]; } if (cnt < L) { nxt[pre[l]] = 0; pre[nxt[r]] = 0; continue; } int S = cnt / L; for (int i = 1; i < S; ++i) { nxt[tot + i] = 1 + tot + i; pre[tot + i + 1] = tot + i; } for (int i = 1; i <= S; ++i) a[tot + i] = a[p] + 1; nxt[pre[l]] = tot + 1; pre[tot + 1] = pre[l]; pre[nxt[r]] = tot + S; nxt[tot + S] = nxt[r]; for (int i = 1; i <= S; ++i) { int j = S - i + 1; f2[tot + i] = s2[min((i + 1) * L - 1, cnt)] - s2[i * L - 1]; f1[tot + i] = s1[cnt - j * L + 1] - s1[max(cnt - j * L - L + 1, 0)]; } pq.push(ii(a[tot + 1], tot + 1)); tot += S; } cout << ans + n << endl; }
replace
14
15
14
15
0
p02945
Python
Runtime Error
A, B = int(input().split()) list = [] list.append(A + B) list.append(A - B) list.append(A * B) print(max(list))
A, B = map(int, input().split()) list = [] list.append(A + B) list.append(A - B) list.append(A * B) print(max(list))
replace
0
1
0
1
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02945/Python/s147643407.py", line 1, in <module> A, B = int(input().split()) TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
p02945
Python
Runtime Error
a, b = input("").split("") seki = int(a) * int(b) sa = int(a) - int(b) wa = int(a) + int(b) if wa >= seki and wa >= sa: print(wa) elif sa >= wa and sa >= seki: print(sa) else: print(seki)
a, b = input().split() seki = int(a) * int(b) sa = int(a) - int(b) wa = int(a) + int(b) if wa >= seki and wa >= sa: print(wa) elif sa >= wa and sa >= seki: print(sa) else: print(seki)
replace
0
1
0
1
ValueError: empty separator
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02945/Python/s989854046.py", line 1, in <module> a, b = input(""). split("") ValueError: empty separator
p02945
Python
Runtime Error
A = int(input()) B = int(input()) c = A + B d = A - B e = A * B f = [c, d, e] maxvalue = c for i in range(len(f)): if f[i] > maxvalue: maxvalue = f[i] print(maxvalue)
A, B = map(int, input().split()) c = A + B d = A - B e = A * B f = [c, d, e] maxvalue = c for i in range(len(f)): if f[i] > maxvalue: maxvalue = f[i] print(maxvalue)
replace
0
2
0
1
ValueError: invalid literal for int() with base 10: '-13 3'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02945/Python/s027556545.py", line 1, in <module> A = int(input()) ValueError: invalid literal for int() with base 10: '-13 3'
p02945
Python
Runtime Error
A = int(input()) B = input() answer = max( A + B, A - B, A * B, ) print(answer)
data = input() data = data.split(" ") A = int(data[0]) B = int(data[1]) answer = max( A + B, A - B, A * B, ) print(answer)
replace
0
2
0
5
ValueError: invalid literal for int() with base 10: '-13 3'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02945/Python/s283924824.py", line 1, in <module> A = int(input()) ValueError: invalid literal for int() with base 10: '-13 3'
p02945
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> typedef int ll; using namespace __gnu_pbds; using namespace std; #define pb push_back #define mp make_pair #define ff first #define ss second #define len(v) ((int)v.size()) #define all(v) v.begin(), v.end() #define oset \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define orz \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define inp \ freopen("input.txt", "r", stdin); \ freopen("out.txt", "w", stdout); ll mod1 = 1e9 + 7; void io() { ios_base::sync_with_stdio(false); cin.tie(NULL); // cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif } int main() { orz; inp; int a, b; cin >> a >> b; cout << max(max(a + b, a - b), a * b); }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> typedef int ll; using namespace __gnu_pbds; using namespace std; #define pb push_back #define mp make_pair #define ff first #define ss second #define len(v) ((int)v.size()) #define all(v) v.begin(), v.end() #define oset \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define orz \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define inp \ freopen("input.txt", "r", stdin); \ freopen("out.txt", "w", stdout); ll mod1 = 1e9 + 7; void io() { ios_base::sync_with_stdio(false); cin.tie(NULL); // cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif } int main() { orz; // inp; int a, b; cin >> a >> b; cout << max(max(a + b, a - b), a * b); }
replace
35
36
35
36
0
p02945
C++
Runtime Error
// Garg's Code #include <bits/stdc++.h> using namespace std; #define set set<ll> #define ll long long #define ldb long double; #define pa pair<ll, ll> #define map map<ll, ll> #define vec vector<ll> #define pb push_back #define po pop_back #define mp make_pair #define mt make_tuple #define F first #define S second #define f(i, x, n) for (ll i = x; i < n; i++) #define unique_sort(x) \ sort(all(x)), x.resize(distance(x.begin(), unique(all(x)))) #define all(c) c.begin(), c.end() #define str string #define edl "\n" #define add insert #define cot continue #define fast() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) const int MOD = 1e9 + 7, INF = INT_MAX, N = 1e5 + 10; #define llong 1e9 + 7 #define lsmall -1e9 + 7 const double PI = acos(-1); const ll LINF = LLONG_MAX; int main() { fast(); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll n, m; cin >> n >> m; cout << max(n + m, max(n - m, m * n)); return 0; }
// Garg's Code #include <bits/stdc++.h> using namespace std; #define set set<ll> #define ll long long #define ldb long double; #define pa pair<ll, ll> #define map map<ll, ll> #define vec vector<ll> #define pb push_back #define po pop_back #define mp make_pair #define mt make_tuple #define F first #define S second #define f(i, x, n) for (ll i = x; i < n; i++) #define unique_sort(x) \ sort(all(x)), x.resize(distance(x.begin(), unique(all(x)))) #define all(c) c.begin(), c.end() #define str string #define edl "\n" #define add insert #define cot continue #define fast() \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) const int MOD = 1e9 + 7, INF = INT_MAX, N = 1e5 + 10; #define llong 1e9 + 7 #define lsmall -1e9 + 7 const double PI = acos(-1); const ll LINF = LLONG_MAX; int main() { fast(); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt","w",stdout); // #endif ll n, m; cin >> n >> m; cout << max(n + m, max(n - m, m * n)); return 0; }
replace
36
40
36
40
0
p02945
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fastIO \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define asc(s) sort(s.begin(), s.end()) #define des(s) sort(s.rbegin(), s.rend()) #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) #define rev(v) reverse(v.begin(), v.end()) #define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower); #define upper(s) transform(s.begin(), s.end(), s.begin(), ::toupper); #define precision(x, p) fixed << setprecision(p) << x #define set_bits(n) __builtin_popcount(n); #define mod 1000000007 #define PI 3.14159265358979 typedef long long ll; #ifndef ONLINE_JUDGE #define debug(x) cout << x << endl; #else #define debug(x) #endif template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a * b / gcd(a, b); }; vector<string> split(string s) { istringstream buf(s); istream_iterator<string> beg(buf), end; vector<string> list(beg, end); return list; } bool isvowel(char c) { string s = "aeiou"; if (find(s.begin(), s.end(), c) != s.end()) return true; return false; } bool cmp(pair<int, int> &a, pair<int, int> &b) { return a.second > b.second || (a.second == b.second && a.first > b.first); } bool isprime(ll n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true && (n != 1); } void solve() { int a, b; cin >> a >> b; cout << max(a + b, max(a - b, a * b)); } int main() { fastIO #ifndef ONLINE_JUDGE freopen("in.txt", "rt", stdin); freopen("out.txt", "wt", stdout); #endif int t = 1; // cin>>t; for (int test_case = 1; test_case <= t; test_case++) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define fastIO \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define asc(s) sort(s.begin(), s.end()) #define des(s) sort(s.rbegin(), s.rend()) #define pb(x) push_back(x) #define mp(x, y) make_pair(x, y) #define rev(v) reverse(v.begin(), v.end()) #define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower); #define upper(s) transform(s.begin(), s.end(), s.begin(), ::toupper); #define precision(x, p) fixed << setprecision(p) << x #define set_bits(n) __builtin_popcount(n); #define mod 1000000007 #define PI 3.14159265358979 typedef long long ll; #ifndef ONLINE_JUDGE #define debug(x) cout << x << endl; #else #define debug(x) #endif template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a * b / gcd(a, b); }; vector<string> split(string s) { istringstream buf(s); istream_iterator<string> beg(buf), end; vector<string> list(beg, end); return list; } bool isvowel(char c) { string s = "aeiou"; if (find(s.begin(), s.end(), c) != s.end()) return true; return false; } bool cmp(pair<int, int> &a, pair<int, int> &b) { return a.second > b.second || (a.second == b.second && a.first > b.first); } bool isprime(ll n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true && (n != 1); } void solve() { int a, b; cin >> a >> b; cout << max(a + b, max(a - b, a * b)); } int main() { fastIO int t = 1; // cin>>t; for (int test_case = 1; test_case <= t; test_case++) { solve(); } return 0; }
replace
65
71
65
66
0
p02945
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; int main(void) { int A, B; cin >> A >> B; int max = -20000; if (A + B > max) max = A + B; if (A - B > max) max = A - B; if (A * B > max) max = A * B; return max; }
#include <iostream> #include <vector> using namespace std; int main(void) { int A, B; cin >> A >> B; int max = -20000; if (A + B > max) max = A + B; if (A - B > max) max = A - B; if (A * B > max) max = A * B; cout << max << endl; return 0; }
replace
15
16
15
18
246
p02945
C++
Time Limit Exceeded
#include <stdio.h> int sum(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int mul(int x, int y) { return x * y; } int main() { int a, b, sm, sb, ml, gr; scanf("%d %d", &a, &b); sm = sum(a, b); sb = sub(a, b); ml = mul(a, b); if (sm > sb && sm > ml) gr = sm; else if (sb > sm && sb > ml) gr = sb; else if (ml > sm && ml > sb) gr = ml; printf("%d\n", gr); main(); return 0; }
#include <stdio.h> int sum(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int mul(int x, int y) { return x * y; } int main() { int a, b, sm, sb, ml, gr; scanf("%d %d", &a, &b); sm = sum(a, b); sb = sub(a, b); ml = mul(a, b); if (sm > sb && sm > ml) gr = sm; else if (sb > sm && sb > ml) gr = sb; else if (ml > sm && ml > sb) gr = ml; printf("%d\n", gr); return 0; }
delete
18
19
18
18
TLE
p02945
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; const ll mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); freopen("INP.txt", "r", stdin); freopen("OUT.txt", "w", stdout); ll a, b; cin >> a >> b; cout << max(a + b, max(a - b, a * b)) << '\n'; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const ll mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); // freopen("INP.txt", "r", stdin); // freopen("OUT.txt", "w", stdout); ll a, b; cin >> a >> b; cout << max(a + b, max(a - b, a * b)) << '\n'; return 0; }
replace
9
11
9
11
0
p02945
Python
Runtime Error
N = int(input()) s = [list(input()) for _ in range(N)] S = [] for i in range(N): S.append("".join(sorted(s[i]))) print(S) cnt = 0 for i in range(N): cnt += S.count(S[i]) - 1 print(int(cnt / 2))
A, B = map(int, input().split()) print(max(A + B, A - B, A * B))
replace
0
11
0
2
ValueError: invalid literal for int() with base 10: '-13 3'
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02945/Python/s567458637.py", line 1, in <module> N = int(input()) ValueError: invalid literal for int() with base 10: '-13 3'
p02945
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; vector<int> V; V.at(0) = A + B; V.at(1) = A - B; V.at(2) = A * B; sort(V.begin(), V.end()); cout << V.at(2) << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; vector<int> V(3); V.at(0) = A + B; V.at(1) = A - B; V.at(2) = A * B; sort(V.begin(), V.end()); cout << V.at(2) << endl; }
replace
6
7
6
7
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
p02945
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdlib> using namespace std; int max(int x, int y) { return (x > y ? x : y); } int main() { int a, b; cin >> a >> b; int ans = max(a + b, a - b); ans = max(ans, a * b); return ans; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdlib> using namespace std; int max(int x, int y) { return (x > y ? x : y); } int main() { int a, b; cin >> a >> b; int ans = max(a + b, a - b); ans = max(ans, a * b); cout << ans; }
replace
12
13
12
13
246
p02945
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int a, b; void solve() { cin >> a >> b; cout << max(max(a + b, a - b), a * b) << endl; } signed main() { while (1) solve(); }
#include <bits/stdc++.h> using namespace std; int a, b; void solve() { cin >> a >> b; cout << max(max(a + b, a - b), a * b) << endl; } signed main() { // while(1) solve(); }
replace
11
13
11
13
TLE
p02945
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int a, b; cin >> a >> b; cout << max(a - b, max(a + b, a * b)); }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); // #ifndef ONLINE_JUDGE // freopen("input.txt" , "r" , stdin); // freopen("output.txt" , "w" , stdout); // #endif int a, b; cin >> a >> b; cout << max(a - b, max(a + b, a * b)); }
replace
10
14
10
14
0
p02945
C++
Time Limit Exceeded
// #pragma GCC optimize("O3", "unroll-loops") // #pragma GCC target("avx2") #include <bits/stdc++.h> #define FAST \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define FIXED cout << fixed << setprecision(12) #define RANDOM srand(94385) #define all(a) a.begin(), a.end() #define shuffle(a) \ for (int i = 0; i < sz(a); ++i) \ swap(a[i], a[rand() % sz(a)]); #define enter puts("") #define space putchar(' ') #define Mem(a, x) memset(a, x, sizeof(a)) using namespace std; typedef long long ll; typedef pair<int, int> pii; void debug_out() { cerr << '\n'; } template <typename T, typename... R> void debug_out(const T &f, const R &...r) { cerr << f << " "; debug_out(r...); } #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__); inline ll read() { ll x = 0; int f = 0; char ch = getchar(); while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return x = f ? -x : x; } inline void write(ll x) { if (x == 0) { putchar('0'); return; } if (x < 0) { putchar('-'); x = -x; } static char s[23]; int l = 0; while (x != 0) s[l++] = x % 10 + 48, x /= 10; while (l) putchar(s[--l]); } int lowbit(int x) { return x & (-x); } template <class T> T big(const T &a1, const T &a2) { return a1 > a2 ? a1 : a2; } template <typename T, typename... R> T big(const T &f, const R &...r) { return big(f, big(r...)); } template <class T> T sml(const T &a1, const T &a2) { return a1 < a2 ? a1 : a2; } template <typename T, typename... R> T sml(const T &f, const R &...r) { return sml(f, sml(r...)); } template <class T, class U> inline void checkMin(T &x, U y) { if (y < x) x = y; } template <class T, class U> inline void checkMax(T &x, U y) { if (y > x) x = y; } template <class T, class U> inline bool ifMax(T &x, U y) { if (y > x) return x = y, true; return false; } template <class T, class U> inline bool ifMin(T &x, U y) { if (y < x) return x = y, true; return false; } const int M = 212345; // 2e5+5 const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1000000007; // 1e9+7 // HAVE FREE OPEN IN MAIN FUNCTION void init() { int a = read(), b = read(); write(big(a + b, a * b, a - b)); enter; } void solve() {} int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif init(); solve(); return 0; }
// #pragma GCC optimize("O3", "unroll-loops") // #pragma GCC target("avx2") #include <bits/stdc++.h> #define FAST \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define FIXED cout << fixed << setprecision(12) #define RANDOM srand(94385) #define all(a) a.begin(), a.end() #define shuffle(a) \ for (int i = 0; i < sz(a); ++i) \ swap(a[i], a[rand() % sz(a)]); #define enter puts("") #define space putchar(' ') #define Mem(a, x) memset(a, x, sizeof(a)) using namespace std; typedef long long ll; typedef pair<int, int> pii; void debug_out() { cerr << '\n'; } template <typename T, typename... R> void debug_out(const T &f, const R &...r) { cerr << f << " "; debug_out(r...); } #define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__); inline ll read() { ll x = 0; int f = 0; char ch = getchar(); while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return x = f ? -x : x; } inline void write(ll x) { if (x == 0) { putchar('0'); return; } if (x < 0) { putchar('-'); x = -x; } static char s[23]; int l = 0; while (x != 0) s[l++] = x % 10 + 48, x /= 10; while (l) putchar(s[--l]); } int lowbit(int x) { return x & (-x); } template <class T> T big(const T &a1, const T &a2) { return a1 > a2 ? a1 : a2; } template <typename T, typename... R> T big(const T &f, const R &...r) { return big(f, big(r...)); } template <class T> T sml(const T &a1, const T &a2) { return a1 < a2 ? a1 : a2; } template <typename T, typename... R> T sml(const T &f, const R &...r) { return sml(f, sml(r...)); } template <class T, class U> inline void checkMin(T &x, U y) { if (y < x) x = y; } template <class T, class U> inline void checkMax(T &x, U y) { if (y > x) x = y; } template <class T, class U> inline bool ifMax(T &x, U y) { if (y > x) return x = y, true; return false; } template <class T, class U> inline bool ifMin(T &x, U y) { if (y < x) return x = y, true; return false; } const int M = 212345; // 2e5+5 const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1000000007; // 1e9+7 // HAVE FREE OPEN IN MAIN FUNCTION void init() { int a = read(), b = read(); write(big(a + b, a * b, a - b)); enter; } void solve() {} int main() { init(); solve(); return 0; }
delete
97
101
97
97
TLE
p02945
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define show(x) cout << (#x) << " : " << x << endl; #define ll long long #define ld long double #define fill(a, val) memset(a, val, sizeof(a)) #define mp make_pair #define ff first #define ss second #define pii pair<ll, ll> #define sq(x) ((x) * (x)) #define all(v) v.begin(),v.end()] #define endl "\n" const ll MOD = 1000 * 1000 * 1000 + 7; const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7; const ll MOD2 = 998244353; const ll N = 1000 * 100 + 5; const ll N2 = 1000 * 1000; const double PI = 3.14159265; ll power(ll x, ll y, ll p) { ll res = 1; x %= p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int main() { // comment for all except cc/cf #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif fastio(); int a, b; cin >> a >> b; int s[3]; s[0] = a + b; s[1] = a - b; s[2] = a * b; sort(s, s + 3); cout << s[2]; }
#include <bits/stdc++.h> using namespace std; #define fastio() \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define pb push_back #define show(x) cout << (#x) << " : " << x << endl; #define ll long long #define ld long double #define fill(a, val) memset(a, val, sizeof(a)) #define mp make_pair #define ff first #define ss second #define pii pair<ll, ll> #define sq(x) ((x) * (x)) #define all(v) v.begin(),v.end()] #define endl "\n" const ll MOD = 1000 * 1000 * 1000 + 7; const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7; const ll MOD2 = 998244353; const ll N = 1000 * 100 + 5; const ll N2 = 1000 * 1000; const double PI = 3.14159265; ll power(ll x, ll y, ll p) { ll res = 1; x %= p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int main() { // comment for all except cc/cf /* #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("out.txt","w",stdout); #endif*/ fastio(); int a, b; cin >> a >> b; int s[3]; s[0] = a + b; s[1] = a - b; s[2] = a * b; sort(s, s + 3); cout << s[2]; }
replace
36
41
36
42
0
p02945
C++
Runtime Error
#include <stdio.h> int main() { int a, b; scanf("%d%d", a, b); int c, d, e; c = a + b; d = a - b; e = a * b; if (c >= d && c >= e) { printf("%d", c); } else if (d > c && d >= e) { printf("%d", d); } else { printf("%d", e); } return 0; }
#include <stdio.h> int main() { int a, b; scanf("%d%d", &a, &b); int c, d, e; c = a + b; d = a - b; e = a * b; if (c >= d && c >= e) { printf("%d", c); } else if (d > c && d >= e) { printf("%d", d); } else { printf("%d", e); } return 0; }
replace
3
4
3
4
-11
p02945
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main() { int A, B, sum, sub, mult; cin >> A >> B; while (A <= 100 && A >= -100) { if (B <= 100 && B >= -100) continue; sum = A + B; sub = A - B; mult = A * B; if (sum > sub && sum > mult) { cout << sum; break; } else if (sub > sum && sub > mult) { cout << sub; break; } else { cout << mult; break; } } }
#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; cout << max(a + b, max(a - b, a * b)); return 0; }
replace
4
23
4
8
TLE
p02945
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; int X = A + B; int Y = A - B; int Z = A * B; vector<int> vec; vec = {X, Y, Z}; sort(vec.begin(), vec.end()); cout << vec.at(3) << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; int X = A + B; int Y = A - B; int Z = A * B; vector<int> vec; vec = {X, Y, Z}; sort(vec.begin(), vec.end()); cout << vec.at(2) << endl; }
replace
16
17
16
17
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)
p02945
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #define mod 1000000007 #define eps 1e-9 #define PI 3.14159265358979323846 #define F first #define S second #define ll long long #define pb push_back #define mp make_pair #define Fr(i, a, b) for (int i = a; i <= b; i++) #define RF(i, a, b) for (int i = a; i >= b; i--) #define pii pair<ll, int> #define vi vector<double> #define vvi vector<vector<double>> #define vpii vector<pii> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("text.txt", "r", stdin); #endif int t = 1; // cin>>t; while (t--) { int a, b; cin >> a >> b; int u = max((a + b), max(a - b, a * b)); cout << u << endl; } return 0; }
#include <bits/stdc++.h> #include <iostream> #define mod 1000000007 #define eps 1e-9 #define PI 3.14159265358979323846 #define F first #define S second #define ll long long #define pb push_back #define mp make_pair #define Fr(i, a, b) for (int i = a; i <= b; i++) #define RF(i, a, b) for (int i = a; i >= b; i--) #define pii pair<ll, int> #define vi vector<double> #define vvi vector<vector<double>> #define vpii vector<pii> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin>>t; while (t--) { int a, b; cin >> a >> b; int u = max((a + b), max(a - b, a * b)); cout << u << endl; } return 0; }
delete
22
25
22
22
0
p02945
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define all(x) (x).begin(), (x).end() #define SZ(x) (x).size() #define fir first #define sec second void scan() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } using namespace std; const int maxn = 2e5 + 7; int a[maxn], pre[maxn]; int main() { scan(); ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int a, b, c; cin >> a >> b; cout << max(a + b, max(a - b, a * b)) << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define all(x) (x).begin(), (x).end() #define SZ(x) (x).size() #define fir first #define sec second void scan() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } using namespace std; const int maxn = 2e5 + 7; int a[maxn], pre[maxn]; int main() { // scan(); ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int a, b, c; cin >> a >> b; cout << max(a + b, max(a - b, a * b)) << endl; return 0; }
replace
20
21
20
21
0
p02946
C++
Runtime Error
#include <iostream> using namespace std; int main(void) { // Your code here! int K, X; cin >> K >> X; int c[K]; if (X + K <= 1000000 && X - K >= -1000000) { for (int i = 0; i < K + K - 1; i++) { c[i] = X - K + i + 1; cout << c[i]; } } return 0; }
#include <iostream> using namespace std; int main(void) { // Your code here! int K, X; cin >> K >> X; int c[100000]; for (int i = 0; i < 2 * K - 1; i++) { c[i] = X - K + i + 1; cout << c[i] << " "; } return 0; }
replace
6
13
6
10
0
p02946
C++
Runtime Error
#include <iostream> using namespace std; int k; int x; int main() { cin >> k >> x; int a[k]; for (int i = 0; i < (2 * k - 1); ++i) { a[i] = x - k + 1 + i; } for (int j = 0; j < (2 * k - 1); ++j) { cout << a[j] << " "; } cout << endl; }
#include <iostream> using namespace std; int k; int x; int main() { cin >> k >> x; int a[2 * k - 1]; for (int i = 0; i < (2 * k - 1); ++i) { a[i] = x - k + 1 + i; } for (int j = 0; j < (2 * k - 1); ++j) { cout << a[j] << " "; } cout << endl; }
replace
9
10
9
10
0
p02946
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { int k, x; while (cin >> k >> x) { int s = 0; if (x - k <= 0) s = x + k; int a[200] = {0}; for (int i = x - k + 1 + s; i <= k + s; i++) a[i] = 1; for (int i = k + 1 + s; i < k + x + s; i++) a[i] = 1; for (int i = x - k + 1 + s; i < k + x + s; i++) { if (a[i] == 1) cout << i - s; if (i == k + x - 1 + s) cout << endl; else cout << " "; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int k, x; while (cin >> k >> x) { int s = 0; if (x - k + 1 < 0) s = -(x - k + 1); int a[200] = {0}; for (int i = x - k + 1 + s; i <= k + s; i++) a[i] = 1; for (int i = k + 1 + s; i < k + x + s; i++) a[i] = 1; for (int i = x - k + 1 + s; i < k + x + s; i++) { if (a[i] == 1) cout << i - s; if (i == k + x - 1 + s) cout << endl; else cout << " "; } } return 0; }
replace
6
8
6
8
0
p02946
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, N) for (int i = 0; i < (N); ++i) #define ALL(x) (x).begin(), (x).end() #define MOD 1000000007 #define PB push_back #define MP make_pair using namespace std; typedef long long ll; ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } int main() { int K, X; cin >> K >> X; vector<int> ans(2 * X - 1); REP(i, 2 * X - 1) { ans[i] = X - K + i; } REP(i, 2 * X - 1) cout << ans[i]; cout << endl; }
#include <bits/stdc++.h> #define REP(i, N) for (int i = 0; i < (N); ++i) #define ALL(x) (x).begin(), (x).end() #define MOD 1000000007 #define PB push_back #define MP make_pair using namespace std; typedef long long ll; ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } int main() { int K, X; cin >> K >> X; vector<int> ans(2 * K - 1); REP(i, 2 * K - 1) { ans[i] = X - K + 1 + i; } REP(i, 2 * K - 1) cout << ans[i] << ' '; }
replace
13
17
13
16
0
p02946
Python
Runtime Error
import numpy as np K, X = map(int, input().split()) tmp = X + K tmp2 = X - K + 1 ans = np.arange(tmp2, tmp) for i, v in enumerate(ans): print(f" {v}", end="") if i > 0 else print(f"{v}", end="") print("")
import numpy as np K, X = map(int, input().split()) tmp = X + K tmp2 = X - K + 1 ans = np.arange(tmp2, tmp) for i, v in enumerate(ans): print(" {}".format(v), end="") if i > 0 else print("{}".format(v), end="") print("")
replace
9
10
9
10
TLE
p02946
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const long long inf = 1e15; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pii; void faster() { ios_base::sync_with_stdio(false); cin.tie(NULL); } const ll MAXN = 1e6 + 10; const ll N = 4000 + 10; const ll MN = 200 + 10; const ll mod = 1e6; ll a[MAXN * 2]; bool mark[3 * MAXN]; int main() { faster(); ll x, k, ans = 0; cin >> k >> x; for (int i = -1e6; i <= 1e6; i++) { if (abs(x - i) < k) mark[i] = true; } for (int i = -1e6; i <= 1e6; i++) { if (mark[i]) cout << i << " "; } cout << endl; }
#include <bits/stdc++.h> using namespace std; const long long inf = 1e15; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pii; void faster() { ios_base::sync_with_stdio(false); cin.tie(NULL); } const ll MAXN = 1e6 + 10; const ll N = 4000 + 10; const ll MN = 200 + 10; const ll mod = 1e6; ll a[MAXN * 2]; bool mark[3 * MAXN]; int main() { faster(); ll x, k, ans = 0; cin >> k >> x; for (int i = -1e6; i <= 1e6; i++) { if (abs(x - i) < k) cout << i << " "; } cout << endl; }
delete
26
30
26
26
0
p02946
C++
Runtime Error
#include <iostream> using namespace std; int main() { int a, b, c, d, i, e[110]; cin >> a >> b; c = b - a + 1; d = b + a; for (i = 0; i < 2 * a; i++) { e[i] = c; c++; if (c >= d) break; } for (i = 0; i < 2 * a - 1; i++) { cout << e[i] << " "; } return 0; }
#include <iostream> using namespace std; int main() { int a, b, c, d, i, e[1000]; cin >> a >> b; c = b - a + 1; d = b + a; for (i = 0; i < 2 * a; i++) { e[i] = c; c++; if (c >= d) break; } for (i = 0; i < 2 * a - 1; i++) { cout << e[i] << " "; } return 0; }
replace
3
4
3
4
0
p02946
C++
Runtime Error
#include <iostream> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int main() { int k, x; int m, p; int ans[105]; cin >> k >> x; m = x - k + 1; rep(i, k * 2 - 1) { ans[i] += m + i; cout << ans[i] << " "; } cout << endl; return 0; }
#include <iostream> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int main() { int k, x; int m, p; int ans[105]; cin >> k >> x; m = x - k + 1; rep(i, k * 2 - 1) { ans[i] = m + i; cout << ans[i] << " "; } cout << endl; return 0; }
replace
12
13
12
13
0
p02946
Python
Runtime Error
k, x = map(int, input().split()) print(" ".join([x for x in range(x - k + 1, x + k)]))
k, x = map(int, input().split()) print(" ".join([str(i) for i in range(x - k + 1, x + k)]))
replace
1
2
1
2
TypeError: sequence item 0: expected str instance, int found
Traceback (most recent call last): File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02946/Python/s436123405.py", line 2, in <module> print(' '.join([x for x in range(x - k + 1, x + k)])) TypeError: sequence item 0: expected str instance, int found
p02946
C++
Runtime Error
#include <stdio.h> int main() { int k, x; scanf("%d %d", k, x); for (int i = x - k + 1; i <= x + k - 1; i++) { printf("%d ", i); } printf("\n"); return 0; }
#include <stdio.h> int main() { int k, x; scanf("%d %d", &k, &x); for (int i = x - k + 1; i <= x + k - 1; i++) { printf("%d ", i); } printf("\n"); return 0; }
replace
4
5
4
5
-11
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll N, count = 0; cin >> N; vector<string> s(N); for (int i = 0; i < N; i++) { cin >> s[i]; sort(s[i].begin(), s[i].end()); } for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (s[i] == s[j]) { count++; } } } cout << count << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll N, count = 0; cin >> N; vector<string> s(N); for (int i = 0; i < N; i++) { cin >> s[i]; sort(s[i].begin(), s[i].end()); } sort(s.begin(), s.end()); ll huga = 0; for (int i = 0; i < N - 1; i++) { if (s[i] == s[i + 1]) { count = count + huga + 1; huga++; } else { huga = 0; } } cout << count << endl; }
replace
12
17
12
20
TLE
p02947
C++
Runtime Error
#include <iostream> #include <algorithm> #include <string> #include <vector> using namespace std; int main() { int N; cin >> N; vector<string> S(N + 1); for (int i = 0; i < N; i++) { cin >> S[i]; sort(S[i].begin(), S[i].end()); } long long ans = 0; S[N] = ("zzzzzzzzzzz"); sort(S.begin(), S.end()); long long count = 0; for (int i = 0; i < N + 1; i++) { // cout<<S[i]<<endl; if (S[i] == S[i + 1]) { count++; } else { ans += count * (count + 1) / 2; count = 0; } } cout << ans << endl; return 0; }
#include <iostream> #include <algorithm> #include <string> #include <vector> using namespace std; int main() { int N; cin >> N; vector<string> S(N + 1); for (int i = 0; i < N; i++) { cin >> S[i]; sort(S[i].begin(), S[i].end()); } long long ans = 0; S[N] = ("zzzzzzzzzzz"); sort(S.begin(), S.end()); long long count = 0; for (int i = 0; i < N; i++) { // cout<<S[i]<<endl; if (S[i] == S[i + 1]) { count++; } else { ans += count * (count + 1) / 2; count = 0; } } cout << ans << endl; return 0; }
replace
21
22
21
22
0
p02947
C++
Runtime Error
#include <bits/stdc++.h> // #pragma GCC optimize "-O3" typedef unsigned long long ull; typedef unsigned int ui; typedef unsigned short us; typedef long long ll; typedef long double ld; using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll infll = (1ull << 63) - 1, mod = 1e9 + 7; const int inf = (1ull << 31) - 1; int n; ll ans = 0; string a; unordered_map<string, int> mp; int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(18); cin >> n; for (int i = 0; i < n; i++) { cin >> a; sort(a.begin(), a.end()); mp[a]++; a.clear(); } for (auto t : mp) { ll r = t.second; ans += (((r - 1) * r) >> 1); } cout << ans; return 0; }
#include <bits/stdc++.h> // #pragma GCC optimize "-O3" typedef unsigned long long ull; typedef unsigned int ui; typedef unsigned short us; typedef long long ll; typedef long double ld; using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll infll = (1ull << 63) - 1, mod = 1e9 + 7; const int inf = (1ull << 31) - 1; int n; ll ans = 0; string a; unordered_map<string, int> mp; int main() { // freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(18); cin >> n; for (int i = 0; i < n; i++) { cin >> a; sort(a.begin(), a.end()); mp[a]++; a.clear(); } for (auto t : mp) { ll r = t.second; ans += (((r - 1) * r) >> 1); } cout << ans; return 0; }
replace
18
20
18
19
0
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; #define FOR(i, a, b) for (ll(i) = a; (i) < (b); ++(i)) #define RFOR(i, a, b) for (ll(i) = a; (i) >= (b); --(i)) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, n, 0) #define ALL(v) v.begin(), v.end() #define UNIQ(v) \ sort(ALL(v)); \ v.erase(unique(ALL(v)), v.end()) #define BIT(n) (1LL << (n)) #define DEBUG(a) cout << #a << " = " << a << endl const ll inf = 1e15; const ll mod = 1e9 + 7; int dy[] = {0, 0, 1, -1}; int dx[] = {1, -1, 0, 0}; int N; int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101}; ull s; unordered_map<ull, ll> num; ull convert(string input) { ull ret = 1; for (char &c : input) { ret *= prime[c - 'a']; } // cout << ret << endl; return ret; } int main() { cin >> N; string input; for (int i = 0; i < N; i++) { cin >> input; s = convert(input); bool exist = false; for (auto n : num) { if (n.first == s) { num[n.first] = n.second + 1; exist = true; break; } } if (exist == false) { num[s] = 1; } } ll result = 0; for (auto n : num) { // cout << n.first << "=" << n.second << endl; result += n.second * (n.second - 1) / 2; } cout << result << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; #define FOR(i, a, b) for (ll(i) = a; (i) < (b); ++(i)) #define RFOR(i, a, b) for (ll(i) = a; (i) >= (b); --(i)) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, n, 0) #define ALL(v) v.begin(), v.end() #define UNIQ(v) \ sort(ALL(v)); \ v.erase(unique(ALL(v)), v.end()) #define BIT(n) (1LL << (n)) #define DEBUG(a) cout << #a << " = " << a << endl const ll inf = 1e15; const ll mod = 1e9 + 7; int dy[] = {0, 0, 1, -1}; int dx[] = {1, -1, 0, 0}; int N; int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101}; ull s; unordered_map<ull, ll> num; ull convert(string input) { ull ret = 1; for (char &c : input) { ret *= prime[c - 'a']; } // cout << ret << endl; return ret; } int main() { cin >> N; string input; for (int i = 0; i < N; i++) { cin >> input; s = convert(input); num[s]++; } ll result = 0; for (auto n : num) { // cout << n.first << "=" << n.second << endl; result += n.second * (n.second - 1) / 2; } cout << result << endl; }
replace
43
54
43
44
TLE
p02947
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define be begin #define en end #define pb push_back #define eb emplace_back #define mp make_pair #define ALL(a) (a).be(), (a).en() #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) // repeat n times #define REP2(i, s, n) for (int(i) = (s); (i) < (n); (i)++) // repeat from s to n #define REPD(i, n) for (int(i) = (n); (i) >= 0; (i)--) // repeat from n to 0 #define REPD2(i, s, e) \ for (int(i) = (s); (i) >= (e); (i)--) // repeat from s to e #define RANGE(i, v) for (auto &(i) : v) // repeat range #define ASIZE(a) (sizeof(a) / sizeof(a[0])) // array size using LL = long long; template <typename T> using V = vector<T>; using Vi = V<int>; using Vll = V<LL>; using Vs = V<string>; template <typename T1, typename T2> using M = map<T1, T2>; using Mii = M<int, int>; using Msll = M<string, LL>; LL factorial(int x, int m = 0) { LL a = 1; if (m == 0) { REPD2(i, x, 1) { a *= i; } } else { REPD2(i, x, 1) { a = (a * i) % m; } } return a; } int main() { int n; cin >> n; Vs s(n); REP(i, n) { cin >> s[i]; sort(ALL(s[i])); } sort(ALL(s)); // cout<<endl; // REP(i,n){ // cout<<s[i]<<endl; // } Msll s_num; REP(i, n) { s_num[s[i]]++; } LL cnt = 0; for (auto m : s_num) { // cout<<m.fi<<" : "<<m.se<<endl; if (m.se == 1) continue; LL fact = factorial(m.se); LL m_2 = factorial(m.se - 2); // cout<<fact<<" : "<<m_2<<endl; cnt += fact / (m_2 * 2); } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define be begin #define en end #define pb push_back #define eb emplace_back #define mp make_pair #define ALL(a) (a).be(), (a).en() #define REP(i, n) for (int(i) = 0; (i) < (n); (i)++) // repeat n times #define REP2(i, s, n) for (int(i) = (s); (i) < (n); (i)++) // repeat from s to n #define REPD(i, n) for (int(i) = (n); (i) >= 0; (i)--) // repeat from n to 0 #define REPD2(i, s, e) \ for (int(i) = (s); (i) >= (e); (i)--) // repeat from s to e #define RANGE(i, v) for (auto &(i) : v) // repeat range #define ASIZE(a) (sizeof(a) / sizeof(a[0])) // array size using LL = long long; template <typename T> using V = vector<T>; using Vi = V<int>; using Vll = V<LL>; using Vs = V<string>; template <typename T1, typename T2> using M = map<T1, T2>; using Mii = M<int, int>; using Msll = M<string, LL>; LL factorial(int x, int m = 0) { LL a = 1; if (m == 0) { REPD2(i, x, 1) { a *= i; } } else { REPD2(i, x, 1) { a = (a * i) % m; } } return a; } int main() { int n; cin >> n; Vs s(n); REP(i, n) { cin >> s[i]; sort(ALL(s[i])); } sort(ALL(s)); // cout<<endl; // REP(i,n){ // cout<<s[i]<<endl; // } Msll s_num; REP(i, n) { s_num[s[i]]++; } LL cnt = 0; for (auto m : s_num) { // cout<<m.fi<<" : "<<m.se<<endl; if (m.se == 1) continue; cnt += m.se * (m.se - 1) / 2; } cout << cnt << endl; return 0; }
replace
64
68
64
65
0
p02947
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> using namespace std; int main() { const int MAX_N = 1e3 + 10; int N; string S[MAX_N]; cin >> N; for (int i = 0; i < N; ++i) { string s; cin >> s; int a[10]; for (int j = 0; j < 10; ++j) { a[j] = s[j] - 0; } sort(a, a + 10); for (int j = 0; j < 10; ++j) { s[j] = a[j]; } S[i] = s; } sort(S, S + N); long long ans = 0; long long sum = 1; string n, p = S[0]; for (int i = 1; i < N; ++i) { n = S[i]; if (p == n) { sum += 1; } else { ans += sum * (sum - 1) / 2; sum = 1; } p = n; } ans += sum * (sum - 1) / 2; cout << ans << endl; }
#include <algorithm> #include <iostream> #include <string> using namespace std; int main() { const int MAX_N = 1e5 + 10; int N; string S[MAX_N]; cin >> N; for (int i = 0; i < N; ++i) { string s; cin >> s; int a[10]; for (int j = 0; j < 10; ++j) { a[j] = s[j] - 0; } sort(a, a + 10); for (int j = 0; j < 10; ++j) { s[j] = a[j]; } S[i] = s; } sort(S, S + N); long long ans = 0; long long sum = 1; string n, p = S[0]; for (int i = 1; i < N; ++i) { n = S[i]; if (p == n) { sum += 1; } else { ans += sum * (sum - 1) / 2; sum = 1; } p = n; } ans += sum * (sum - 1) / 2; cout << ans << endl; }
replace
6
7
6
7
0
p02947
C++
Runtime Error
#include <bits/stdc++.h> #pragma GCC optimize("unroll-loops,no-stack-protector") #pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define watch(x) cout << (#x) << " is " << (x) << endl typedef long long ll; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; int i = 0; ll cnt = 0; cin >> n; vector<string> s(n); for (int j = 0; j < n; j++) { cin >> s[j]; sort(s[j].begin(), s[j].end()); } sort(s.begin(), s.end()); while (i < n) { ll tempcnt = 0; while (i < n && s[i] != s[i - 1]) { i++; } while (i < n && s[i] == s[i - 1]) { tempcnt++; cnt += tempcnt; i++; } } cout << cnt; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("unroll-loops,no-stack-protector") #pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define watch(x) cout << (#x) << " is " << (x) << endl typedef long long ll; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; int i = 1; ll cnt = 0; cin >> n; vector<string> s(n); for (int j = 0; j < n; j++) { cin >> s[j]; sort(s[j].begin(), s[j].end()); } sort(s.begin(), s.end()); while (i < n) { ll tempcnt = 0; while (i < n && s[i] != s[i - 1]) { i++; } while (i < n && s[i] == s[i - 1]) { tempcnt++; cnt += tempcnt; i++; } } cout << cnt; return 0; }
replace
12
13
12
13
0
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main(void) { int N; cin >> N; vector<string> S(N); for (int i = 0; i < N; i++) { cin >> S[i]; sort(S[i].begin(), S[i].end()); } long long cnt = 0; for (int i = 0; i < N - 1; i++) { for (int j = i + 1; j < N; j++) { if (S[i] == S[j]) cnt++; } } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int N; cin >> N; vector<string> S(N); for (int i = 0; i < N; i++) { cin >> S[i]; sort(S[i].begin(), S[i].end()); } long long cnt = 0; unordered_map<string, int> arr{ {"non", 0}, }; for (int i = 0; i < N; i++) { if (arr.find(S[i]) != arr.find("non")) { cnt += arr[S[i]]; arr[S[i]]++; } else arr.emplace(S[i], 1); } cout << cnt << endl; return 0; }
replace
13
18
13
23
TLE
p02947
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <set> #include <string> #include <utility> #include <vector> typedef long long ll; #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; #define MX 1000000000 int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; vector<string> vec(N); set<string> st; for (int i = 0; i < N; ++i) { cin >> vec[i]; sort(vec[i].begin(), vec[i].end()); } int ans = 0; rep(i, N) { for (int j = i + 1; j < N; ++j) if (vec[i] == vec[j]) ans++; } cout << ans << endl; }
#include <algorithm> #include <bits/stdc++.h> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <set> #include <string> #include <utility> #include <vector> typedef long long ll; #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; #define MX 1000000000 int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; map<string, int> mp; ll ans = 0; for (int i = 0; i < n; ++i) { string s; cin >> s; sort(s.begin(), s.end()); if (mp[s] == 0) mp[s] = 1; else ans += mp[s]++; } cout << ans << endl; }
replace
18
31
18
30
TLE
p02947
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; // typedef long double ld; typedef map<int, int> mii; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef set<int> si; typedef set<ll> sll; typedef vector<int> vi; typedef vector<pair<int, int>> vii; typedef vector<ll> vll; typedef vector<double> vd; typedef vector<vi> vvi; typedef vector<vii> vvii; typedef vector<vll> vvll; constexpr ll MOD = 1e9 + 7; constexpr int INF = 1 << 30; constexpr ll LINF = 1LL << 60; #define Sort(x) sort(x.begin(), x.end()) #define Reverse(x) reverse(x.begin(), x.end()) #define ABS(a, b) ((a) < (b) ? (b) - (a) : (a) - (b)) #define nbit(n) (1ll << (n)) // sort(begin(x), end(x), greater<ll>()); // lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置 // upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置 // Returns minimum of a and b. // If a is less b, a is set to b. template <typename T> T chmin(T &a, T b) { if (a > b) { a = b; } return a; } // Returns maximum of a and b. // If a is less b, a is set to b. template <typename T> T chmax(T &a, T b) { if (a < b) { a = b; } return a; } // a = (a+b)%MOD; void addmod(ll &a, ll b) { a = (a + b) % MOD; } template <typename T> void dumpContents(const T &v, const string &msg) { cerr << "### " << msg << " ###\n"; for (const auto &x : v) { cerr << x << " "; } cerr << endl; } struct before_main_function { before_main_function() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20) << fixed; #define endl "\n" } } before_main_function; // ========== end of template ========== // x! ll factorial_r(ll x) { assert(x >= 0); ll ret = 1; while (x > 1) { ret *= x; x--; } return ret; } // xCy ll combi_r(ll x, ll y) { assert(x >= 0); assert(y >= 0); assert(x >= y); return factorial_r(x) / factorial_r(x - y) / factorial_r(y); } int main(int argc, char **argv) { int N; cin >> N; vector<vector<char>> S(N, vector<char>(11)); map<string, int> counts; for (int i = 0; i < N; i++) { for (int j = 0; j < 10; j++) { cin >> S[i][j]; } S[i][10] = '\0'; sort(begin(S[i]), begin(S[i]) + 10); string s(&S[i][0]); counts[s] += 1; } ll ret = 0; for (auto kv : counts) { ll v = kv.second; if (v < 2) continue; ret += combi_r(v, 2); } cout << ret << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; // typedef long double ld; typedef map<int, int> mii; typedef pair<int, int> pii; typedef pair<double, double> pdd; typedef set<int> si; typedef set<ll> sll; typedef vector<int> vi; typedef vector<pair<int, int>> vii; typedef vector<ll> vll; typedef vector<double> vd; typedef vector<vi> vvi; typedef vector<vii> vvii; typedef vector<vll> vvll; constexpr ll MOD = 1e9 + 7; constexpr int INF = 1 << 30; constexpr ll LINF = 1LL << 60; #define Sort(x) sort(x.begin(), x.end()) #define Reverse(x) reverse(x.begin(), x.end()) #define ABS(a, b) ((a) < (b) ? (b) - (a) : (a) - (b)) #define nbit(n) (1ll << (n)) // sort(begin(x), end(x), greater<ll>()); // lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置 // upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置 // Returns minimum of a and b. // If a is less b, a is set to b. template <typename T> T chmin(T &a, T b) { if (a > b) { a = b; } return a; } // Returns maximum of a and b. // If a is less b, a is set to b. template <typename T> T chmax(T &a, T b) { if (a < b) { a = b; } return a; } // a = (a+b)%MOD; void addmod(ll &a, ll b) { a = (a + b) % MOD; } template <typename T> void dumpContents(const T &v, const string &msg) { cerr << "### " << msg << " ###\n"; for (const auto &x : v) { cerr << x << " "; } cerr << endl; } struct before_main_function { before_main_function() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20) << fixed; #define endl "\n" } } before_main_function; // ========== end of template ========== // x! ll factorial_r(ll x) { assert(x >= 0); ll ret = 1; while (x > 1) { ret *= x; x--; } return ret; } // xCy ll combi_r(ll x, ll y) { assert(x >= 0); assert(y >= 0); assert(x >= y); ll ret = 1; for (int i = 0; i < y; i++) { ret *= (x - i); } return ret / factorial_r(y); } int main(int argc, char **argv) { int N; cin >> N; vector<vector<char>> S(N, vector<char>(11)); map<string, int> counts; for (int i = 0; i < N; i++) { for (int j = 0; j < 10; j++) { cin >> S[i][j]; } S[i][10] = '\0'; sort(begin(S[i]), begin(S[i]) + 10); string s(&S[i][0]); counts[s] += 1; } ll ret = 0; for (auto kv : counts) { ll v = kv.second; if (v < 2) continue; ret += combi_r(v, 2); } cout << ret << endl; return 0; }
replace
88
89
88
93
0
p02947
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, a, n) for (long long i = (a); i < (long long)(n); ++i) #define REPC(i, a, n) for (long long i = (a); i <= (long long)(n); ++i) #define ALL(t) t.begin(), t.end() #define RALL(t) t.rbegin(), t.rend() #define MATINIT(type, row, col, init) \ vector<vector<type>>(row, vector<type>(col, init)); #define Yes(cond) cout << (cond ? "Yes" : "No") << endl; #define YES(cond) cout << (cond ? "YES" : "NO") << endl; using namespace std; using LL = long long; using ULL = unsigned long long; template <class T> using VEC = std::vector<T>; template <class T> using MAT = std::vector<std::vector<T>>; void DUMP() { cerr << endl; } template <class Head, class... Tail> void DUMP(Head &&head, Tail &&...tail) { cerr << head << ", "; DUMP(std::move(tail)...); } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &s) { os << "{"; for (auto p : s) os << p << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, map<T1, T2> &m) { os << "{"; for (auto p : m) os << p << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "[" << p.first << " " << p.second << "]"; return os; } int main() { int N; cin >> N; VEC<string> S(N); REP(i, 0, N) { cin >> S[i]; sort(ALL(S[i])); } sort(ALL(S)); // DUMP(S); LL cnt = 0; REP(l, 0, N) { int r = l + 1; while (S[l] == S[r]) { r++; } LL k = r - l; cnt += k * (k - 1) / 2; l = r - 1; } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> #define REP(i, a, n) for (long long i = (a); i < (long long)(n); ++i) #define REPC(i, a, n) for (long long i = (a); i <= (long long)(n); ++i) #define ALL(t) t.begin(), t.end() #define RALL(t) t.rbegin(), t.rend() #define MATINIT(type, row, col, init) \ vector<vector<type>>(row, vector<type>(col, init)); #define Yes(cond) cout << (cond ? "Yes" : "No") << endl; #define YES(cond) cout << (cond ? "YES" : "NO") << endl; using namespace std; using LL = long long; using ULL = unsigned long long; template <class T> using VEC = std::vector<T>; template <class T> using MAT = std::vector<std::vector<T>>; void DUMP() { cerr << endl; } template <class Head, class... Tail> void DUMP(Head &&head, Tail &&...tail) { cerr << head << ", "; DUMP(std::move(tail)...); } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &s) { os << "{"; for (auto p : s) os << p << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, map<T1, T2> &m) { os << "{"; for (auto p : m) os << p << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "[" << p.first << " " << p.second << "]"; return os; } int main() { int N; cin >> N; VEC<string> S(N); REP(i, 0, N) { cin >> S[i]; sort(ALL(S[i])); } S.push_back("zzzzzzzzzzzz"); sort(ALL(S)); // DUMP(S); LL cnt = 0; REP(l, 0, N) { int r = l + 1; while (S[l] == S[r]) { r++; } LL k = r - l; cnt += k * (k - 1) / 2; l = r - 1; } cout << cnt << endl; return 0; }
insert
55
55
55
56
0
p02947
C++
Runtime Error
#pragma GCC optimize("O3") #pragma GCC target("sse4") #pragma GCC target("avx2,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define __V vector #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define oit ostream_iterator #define mod 1000000007ll using namespace std; using namespace __gnu_pbds; void doin() { cin.tie(); cout.tie(); ios::sync_with_stdio(0); #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif } template <typename X, typename Y> istream &operator>>(istream &in, pair<X, Y> &a) { in >> a.first >> a.second; return in; } template <typename T> void getv(T &i) { cin >> i; } template <typename T, typename... Ns> void getv(vector<T> &v, int n, Ns... ns) { v.resize(n); for (auto &i : v) getv(i, ns...); } template <typename T> void getv1(T &i) { cin >> i; } template <typename T, typename... Ns> void getv1(vector<T> &v, int n, Ns... ns) { v.resize(n + 1); for (int i = 1; i <= n; i++) getv1(v[i], ns...); } #ifdef _WIN32 #define getchar_unlocked() _getchar_nolock() #define _CRT_DISABLE_PERFCRIT_LOCKS #endif inline void getch(char &x) { while (x = getchar_unlocked(), x < 33) { ; } } inline void getstr(string &str) { str.clear(); char cur; while (cur = getchar_unlocked(), cur < 33) { ; } while (cur > 32) { str += cur; cur = getchar_unlocked(); } } template <typename T> inline bool sc(T &num) { bool neg = 0; int c; num = 0; while (c = getchar_unlocked(), c < 33) { if (c == EOF) return false; } if (c == '-') { neg = 1; c = getchar_unlocked(); } for (; c > 47; c = getchar_unlocked()) num = num * 10 + c - 48; if (neg) num *= -1; return true; } template <typename a, typename b> void minq(a &x, b y) { if (x > y) x = y; } template <typename a, typename b> void maxq(a &x, b y) { if (x < y) x = y; } typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef ll _I; typedef pair<_I, _I> pi; typedef pair<ld, ld> pd; typedef map<_I, _I> mii; typedef __V<_I> vi; typedef __V<char> vc; typedef __V<string> vs; typedef __V<ld> vd; typedef __V<vd> vvd; typedef __V<pi> vpi; typedef __V<__V<_I>> vvi; typedef __V<__V<char>> vvc; typedef __V<__V<pi>> vvpi; using AntonTsypko = void; using arsijo = AntonTsypko; using god = arsijo; // store differences, not the elements for rsq in fenwick or array(for // write-only) Sum Over Subsets + inclusion-exclusion is a thing! (Solved div1E // (383E) using it!) SQRT-heuristic divide items into groups: >sqrt and <=sqrt // and do something according to group(eg. trie for one, z-func for other) - // 1202E uniform_real_distribution<double> double_dist(0, 1); // WHEN DOING MODULAR SUBTRACTION ALWAYS **ALWAYS** ADD MOD // don't use AVX at AtCoder(=RE) // Trie: MAXDEPTH!=MAXSIZE // suspiciously big numbers? use python, seriosly you don't wanna waste your // time if it overflows mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { doin(); ll n; vs a; cin >> n; getv(a, n); map<string, int> c; ll ans = 0; for (auto &i : a) { sort(all(i)); ans += c[i]++; } cout << ans; }
#pragma GCC optimize("O3") #pragma GCC target("sse4") // #pragma GCC target ("avx2,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define __V vector #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define oit ostream_iterator #define mod 1000000007ll using namespace std; using namespace __gnu_pbds; void doin() { cin.tie(); cout.tie(); ios::sync_with_stdio(0); #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif } template <typename X, typename Y> istream &operator>>(istream &in, pair<X, Y> &a) { in >> a.first >> a.second; return in; } template <typename T> void getv(T &i) { cin >> i; } template <typename T, typename... Ns> void getv(vector<T> &v, int n, Ns... ns) { v.resize(n); for (auto &i : v) getv(i, ns...); } template <typename T> void getv1(T &i) { cin >> i; } template <typename T, typename... Ns> void getv1(vector<T> &v, int n, Ns... ns) { v.resize(n + 1); for (int i = 1; i <= n; i++) getv1(v[i], ns...); } #ifdef _WIN32 #define getchar_unlocked() _getchar_nolock() #define _CRT_DISABLE_PERFCRIT_LOCKS #endif inline void getch(char &x) { while (x = getchar_unlocked(), x < 33) { ; } } inline void getstr(string &str) { str.clear(); char cur; while (cur = getchar_unlocked(), cur < 33) { ; } while (cur > 32) { str += cur; cur = getchar_unlocked(); } } template <typename T> inline bool sc(T &num) { bool neg = 0; int c; num = 0; while (c = getchar_unlocked(), c < 33) { if (c == EOF) return false; } if (c == '-') { neg = 1; c = getchar_unlocked(); } for (; c > 47; c = getchar_unlocked()) num = num * 10 + c - 48; if (neg) num *= -1; return true; } template <typename a, typename b> void minq(a &x, b y) { if (x > y) x = y; } template <typename a, typename b> void maxq(a &x, b y) { if (x < y) x = y; } typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef ll _I; typedef pair<_I, _I> pi; typedef pair<ld, ld> pd; typedef map<_I, _I> mii; typedef __V<_I> vi; typedef __V<char> vc; typedef __V<string> vs; typedef __V<ld> vd; typedef __V<vd> vvd; typedef __V<pi> vpi; typedef __V<__V<_I>> vvi; typedef __V<__V<char>> vvc; typedef __V<__V<pi>> vvpi; using AntonTsypko = void; using arsijo = AntonTsypko; using god = arsijo; // store differences, not the elements for rsq in fenwick or array(for // write-only) Sum Over Subsets + inclusion-exclusion is a thing! (Solved div1E // (383E) using it!) SQRT-heuristic divide items into groups: >sqrt and <=sqrt // and do something according to group(eg. trie for one, z-func for other) - // 1202E uniform_real_distribution<double> double_dist(0, 1); // WHEN DOING MODULAR SUBTRACTION ALWAYS **ALWAYS** ADD MOD // don't use AVX at AtCoder(=RE) // Trie: MAXDEPTH!=MAXSIZE // suspiciously big numbers? use python, seriosly you don't wanna waste your // time if it overflows mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { doin(); ll n; vs a; cin >> n; getv(a, n); map<string, int> c; ll ans = 0; for (auto &i : a) { sort(all(i)); ans += c[i]++; } cout << ans; }
replace
2
3
2
3
0
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<string> s(n); for (int i = 0; i < n; i++) { cin >> s.at(i); sort(s.at(i).begin(), s.at(i).end()); } long long cnt = 0; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (s.at(i) == s.at(j)) cnt++; cout << cnt << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<string> s(n); for (int i = 0; i < n; i++) { cin >> s.at(i); sort(s.at(i).begin(), s.at(i).end()); } sort(s.begin(), s.end()); long long cnt = 0, x; for (long long i = 0; i < n - 1; i++) { x = 1; while (s.at(i) == s.at(i + 1)) { if (i + 1 == n - 1) { x++; break; } i++; x++; } cnt += x * (x - 1) / 2; } cout << cnt << endl; }
replace
11
16
11
25
TLE
p02947
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define endl '\n' #define fcin \ ios::sync_with_stdio(false); \ cin.tie(nullptr); #define inf 0x3f3f3f3f mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); /* * stuff you should look for * int overflow, array bounds * special cases (n=1?), set tle */ map<string, ll> f; ll fat[20]; void init() { fat[0] = fat[1] = 1; for (int i = 2; i <= 20; i++) fat[i] = i * fat[i - 1]; } ll choose(ll a, ll b) { if (a < b) return 0; if (a == b) return 1; if (a - b < b) b = a - b; ll p = fat[a]; ll q = fat[b] * fat[a - b]; return p / q; } int main() { fcin; int n; cin >> n; init(); while (n--) { string a; cin >> a; sort(a.begin(), a.end()); f[a]++; } ll ans = 0; for (auto i : f) { ans += choose(i.second, 2); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define endl '\n' #define fcin \ ios::sync_with_stdio(false); \ cin.tie(nullptr); #define inf 0x3f3f3f3f mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); /* * stuff you should look for * int overflow, array bounds * special cases (n=1?), set tle */ map<string, ll> f; ll fat[20]; void init() { fat[0] = fat[1] = 1; for (int i = 2; i <= 20; i++) fat[i] = i * fat[i - 1]; } ll choose(ll a, ll b) { if (a < b) return 0; if (a == b) return 1; return a * (a - 1) / 2; } int main() { fcin; int n; cin >> n; init(); while (n--) { string a; cin >> a; sort(a.begin(), a.end()); f[a]++; } ll ans = 0; for (auto i : f) { ans += choose(i.second, 2); } cout << ans << endl; return 0; }
replace
32
37
32
33
0
p02947
C++
Runtime Error
#include <bits/stdc++.h> #include <string> using namespace std; #define ll long long ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } // Function to return LCM of two numbers ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } int main() { ll n; cin >> n; map<string, ll> hash; string s; vector<string> vc; for (ll i = 1; i <= n; i++) { cin >> s; sort(s.begin(), s.end()); vc.push_back(s); hash[s]++; } ll count = 0; for (ll i = 1; i <= n; i++) { if (hash[vc[i]]) { count = count + ((hash[vc[i]] * (hash[vc[i]] - 1)) / 2); hash[vc[i]] = hash[vc[i]] - hash[vc[i]]; } } cout << count << endl; }
#include <bits/stdc++.h> #include <string> using namespace std; #define ll long long ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } // Function to return LCM of two numbers ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } int main() { ll n; cin >> n; map<string, ll> hash; string s; vector<string> vc; for (ll i = 1; i <= n; i++) { cin >> s; sort(s.begin(), s.end()); vc.push_back(s); hash[s]++; } ll count = 0; for (ll i = 0; i < vc.size(); i++) { if (hash[vc[i]]) { count = count + ((hash[vc[i]] * (hash[vc[i]] - 1)) / 2); hash[vc[i]] = hash[vc[i]] - hash[vc[i]]; } } cout << count << endl; }
replace
26
27
26
27
0
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, i, j, count = 0; cin >> n; string s[n]; for (i = 0; i < n; i++) { cin >> s[i]; sort(s[i].begin(), s[i].end()); } sort(s, s + n); for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (s[i] == s[j]) count++; else break; } } cout << count; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n, i, j, count = 0; cin >> n; string s[n]; for (i = 0; i < n; i++) { cin >> s[i]; sort(s[i].begin(), s[i].end()); } sort(s, s + n); vector<ll> v; ll temp = 0; for (i = 1; i < n; i++) { if (s[i] == s[i - 1]) temp++; else { v.push_back(temp); temp = 0; } } v.push_back(temp); for (j = 0; j < v.size(); j++) { if (v[j] != 0) { count += ((v[j] * (v[j] + 1)) / 2); } } cout << count; return 0; }
replace
15
21
15
29
TLE
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<string> v; string s; int count = 0; long long result = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> s; sort(s.begin(), s.end()); v.push_back(s); } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { for (int ii = (i + 1); ii < n; ii++) { if (v[i] != v[ii]) { break; } result++; } } cout << result << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<string> v; string s; int count = 0; long long result = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> s; sort(s.begin(), s.end()); v.push_back(s); } sort(v.begin(), v.end()); for (int i = 1; i < n; i++) { if (v[i] == v[i - 1]) { result += ++count; } else { count = 0; } } cout << result << endl; return 0; }
replace
20
27
20
25
TLE
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long LL; int main() { int N; cin >> N; string s[100010]; for (int i = 0; i < N; i++) cin >> s[i]; for (int i = 0; i < N; i++) sort(s[i].begin(), s[i].end()); for (int i = 0; i < N; i++) sort(s, s + N); s[N] = ""; LL ans = 0, same = 0; for (int i = 0; i < N; i++) { same++; if (s[i] != s[i + 1]) { ans += same * (same - 1) / 2; same = 0; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; int main() { int N; cin >> N; string s[100010]; for (int i = 0; i < N; i++) cin >> s[i]; for (int i = 0; i < N; i++) sort(s[i].begin(), s[i].end()); sort(s, s + N); s[N] = ""; LL ans = 0, same = 0; for (int i = 0; i < N; i++) { same++; if (s[i] != s[i + 1]) { ans += same * (same - 1) / 2; same = 0; } } cout << ans << endl; return 0; }
replace
13
15
13
14
TLE
p02947
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<string> s(n); for (size_t i = 0; i < n; i++) { cin >> s.at(i); sort(s.at(i).begin(), s.at(i).end()); } sort(s.begin(), s.end()); int64_t ans = 0; int count = 0; for (size_t i = 0; i < n; i++) { /* code */ if (s.at(i) == s.at(i + 1)) { count++; ans += count; } else { count = 0; } } cout << ans << endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<string> s(n); for (size_t i = 0; i < n; i++) { cin >> s.at(i); sort(s.at(i).begin(), s.at(i).end()); } sort(s.begin(), s.end()); int64_t ans = 0; int count = 0; for (size_t i = 0; i < n - 1; i++) { /* code */ if (s.at(i) == s.at(i + 1)) { count++; ans += count; } else { count = 0; } } cout << ans << endl; }
replace
16
17
16
17
-6
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; typedef unsigned long long ull; using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define repr(i, n) for (int i = n; i >= 0; i--) #define For(i, m, n) for (int i = m; i < n; i++) #define Forr(i, m, n) for (int i = m; i > n; i--) #define Sort(a) sort(a.begin(), a.end()) #define pb push_back #define vll vector<ll> #define vvll vector<vector<ll>> #define INF 999999999999 ll comb(int n, int k) { if (n == k || k == 0) return 1; else return comb(n - 1, k - 1) + comb(n - 1, k); } int main() { int N; ll sum = 0; cin >> N; vector<string> s(N); map<string, int> cnt; rep(i, N) { cin >> s[i]; Sort(s[i]); cnt[s[i]]++; } for (auto itr = cnt.begin(); itr != cnt.end(); itr++) { if (cnt[itr->first] >= 2) sum += comb(cnt[itr->first], 2); } cout << sum << endl; return 0; }
#include <bits/stdc++.h> typedef long long ll; typedef long double ld; typedef unsigned long long ull; using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define repr(i, n) for (int i = n; i >= 0; i--) #define For(i, m, n) for (int i = m; i < n; i++) #define Forr(i, m, n) for (int i = m; i > n; i--) #define Sort(a) sort(a.begin(), a.end()) #define pb push_back #define vll vector<ll> #define vvll vector<vector<ll>> #define INF 999999999999 ll comb(int n, int k) { ll res = 1; if (k > n - k) k = n - k; rep(i, k) { res *= (n - i); res /= (i + 1); } return res; } int main() { int N; ll sum = 0; cin >> N; vector<string> s(N); map<string, int> cnt; rep(i, N) { cin >> s[i]; Sort(s[i]); cnt[s[i]]++; } for (auto itr = cnt.begin(); itr != cnt.end(); itr++) { if (cnt[itr->first] >= 2) sum += comb(cnt[itr->first], 2); } cout << sum << endl; return 0; }
replace
16
20
16
24
TLE
p02947
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #include <string> #include <vector> #define ll long long #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define vi vector<ll> #define el printf("\n") #define N 100001 using namespace std; ll fact(ll n) { ll res = 1; for (int i = 1; i <= n; i++) { res = res * i; } return res; } int main() { IOS; ll a; cin >> a; vector<string> L; for (int i = 0; i < a; i++) { string ch; cin >> ch; sort(ch.begin(), ch.end()); L.push_back(ch); } sort(L.begin(), L.end()); /*for(int i=0;i<a;i++) cout << L[i];*/ ll str = 1; vector<ll> L2; for (int i = 0; i < a - 1; i++) { if (L[i] == L[i + 1]) str++; else { L2.push_back(str); str = 1; } } if (str != 1) L2.push_back(str); ll res = 0; for (int i = 0; i < L2.size(); i++) { if (L2[i] != 1) res += fact(L2[i]) / ((fact(2) * fact(L2[i] - 2))); } cout << res; cout.precision(30); return 0; }
#include <bits/stdc++.h> #include <iostream> #include <string> #include <vector> #define ll long long #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define vi vector<ll> #define el printf("\n") #define N 100001 using namespace std; ll fact(ll n) { ll res = 1; for (int i = 1; i <= n; i++) { res = res * i; } return res; } int main() { IOS; ll a; cin >> a; vector<string> L; for (int i = 0; i < a; i++) { string ch; cin >> ch; sort(ch.begin(), ch.end()); L.push_back(ch); } sort(L.begin(), L.end()); /*for(int i=0;i<a;i++) cout << L[i];*/ ll str = 1; vector<ll> L2; for (int i = 0; i < a - 1; i++) { if (L[i] == L[i + 1]) str++; else { L2.push_back(str); str = 1; } } if (str != 1) L2.push_back(str); ll res = 0; for (int i = 0; i < L2.size(); i++) { if (L2[i] != 1) res += (L2[i] * (L2[i] - 1)) / 2; } cout << res; cout.precision(30); return 0; }
replace
50
51
50
51
0
p02947
C++
Runtime Error
#include <algorithm> #include <cstring> #include <iostream> using namespace std; string a[100001]; int n; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { sort(a[i].begin(), a[i].end()); } sort(a + 1, a + n + 1); long long ans = 0; long long length = 1; /*for(int i=1;i<=n;i++) cout<<a[i]<<endl;*/ for (int i = 1; i <= n; i++) { while (a[i] == a[i + 1]) { length++; i++; } ans += length * (length - 1) / 2; length = 1; } cout << ans; }
#include <algorithm> #include <cstring> #include <iostream> using namespace std; string a[500001]; int n; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { sort(a[i].begin(), a[i].end()); } sort(a + 1, a + n + 1); long long ans = 0; long long length = 1; /*for(int i=1;i<=n;i++) cout<<a[i]<<endl;*/ for (int i = 1; i <= n; i++) { while (a[i] == a[i + 1]) { length++; i++; } ans += length * (length - 1) / 2; length = 1; } cout << ans; }
replace
4
5
4
5
0
p02947
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <stack> #include <vector> #define maxn 200100 using namespace std; int trie1[maxn][26]; int S1[26 * maxn]; int tot1 = 0; int in(char *s) { int root, len; len = strlen(s); root = 0; for (int i = 0; i < len; i++) { int id = s[i] - 'a'; if (!trie1[root][id]) { trie1[root][id] = ++tot1; S1[tot1] = 0; } root = trie1[root][id]; S1[root]++; } return S1[root] - 1; } int main() { int N; scanf("%d", &N); long long ans = 0; while (N--) { char s[15]; cin >> s; sort(s, s + 10); ans += in(s); } printf("%lld\n", ans); }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <stack> #include <vector> #define maxn 2001000 using namespace std; int trie1[maxn][26]; int S1[26 * maxn]; int tot1 = 0; int in(char *s) { int root, len; len = strlen(s); root = 0; for (int i = 0; i < len; i++) { int id = s[i] - 'a'; if (!trie1[root][id]) { trie1[root][id] = ++tot1; S1[tot1] = 0; } root = trie1[root][id]; S1[root]++; } return S1[root] - 1; } int main() { int N; scanf("%d", &N); long long ans = 0; while (N--) { char s[15]; cin >> s; sort(s, s + 10); ans += in(s); } printf("%lld\n", ans); }
replace
8
9
8
9
0
p02947
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG // これつけるとA[N]でもいいらしい // for文のマクロ #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define big 1000000007 int main() { int n; cin >> n; vector<string> s(n); rep(i, n) { cin >> s[i]; sort(s[i].begin(), s[i].end()); } sort(s.begin(), s.end()); /* rep(i,n){ cout<<s[i]<<endl; } */ int64_t sum = 0; int64_t j = 1; int i = 0; while (i < n) { j = 1; if (s[i] == s[i + 1]) { while (s[i] == s[i + j]) { j++; if (i + j == n) { break; } } // 同じのがj個⇒コンビネーション sum += j * (j - 1) / 2; } i = i + j; } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG // これつけるとA[N]でもいいらしい // for文のマクロ #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define big 1000000007 int main() { int n; cin >> n; vector<string> s(n); rep(i, n) { cin >> s[i]; sort(s[i].begin(), s[i].end()); } sort(s.begin(), s.end()); /* rep(i,n){ cout<<s[i]<<endl; } */ int64_t sum = 0; int64_t j = 1; int i = 0; while (i < n - 1) { j = 1; if (s[i] == s[i + 1]) { while (s[i] == s[i + j]) { j++; if (i + j == n) { break; } } // 同じのがj個⇒コンビネーション sum += j * (j - 1) / 2; } i = i + j; } cout << sum << endl; }
replace
23
24
23
24
0
p02947
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long co(int x) { long long y = 0; for (int i = 1; i <= x - 1; i++) { y += i; } return y; } int main() { int n; map<string, int> ma; string s[10000]; scanf("%d", &n); for (int i = 0; i < n; i++) { cin >> s[i]; getchar(); sort(s[i].begin(), s[i].end()); ma[s[i]]++; } map<string, int>::iterator it; long long t = 0; for (it = ma.begin(); it != ma.end(); it++) { if (it->second > 1) t += co(it->second); } printf("%lld", t); return 0; }
#include <bits/stdc++.h> using namespace std; long long co(int x) { long long y = 0; for (int i = 1; i <= x - 1; i++) { y += i; } return y; } int main() { int n; map<string, int> ma; string s[100001]; scanf("%d", &n); for (int i = 0; i < n; i++) { cin >> s[i]; getchar(); sort(s[i].begin(), s[i].end()); ma[s[i]]++; } map<string, int>::iterator it; long long t = 0; for (it = ma.begin(); it != ma.end(); it++) { if (it->second > 1) t += co(it->second); } printf("%lld", t); return 0; }
replace
12
13
12
13
0
p02947
C++
Runtime Error
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cmath> // 変数名にy1が使えなくなるかも…。 #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <stdlib.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> typedef __int128_t int128_t; std::istream & operator>>(std::istream &input, int128_t &value) { // int128_tの入力。入力が64bitに収まる前提。 long long tmp; input >> tmp; value = tmp; return input; } std::ostream & operator<<(std::ostream &output, const int128_t value) { // int128_tの出力。出力が64bitに収まる前提。 output << (long long)value; return output; } // 文字列Sに各文字が何回出現するかカウント。始まりの文字がcで、文字種類数がk // 例えばSに含まれる文字が'a-z'であれば、c='a', k=26とする。 std::vector<int> count_char(const std::string &S, const char c, const int k) { std::vector<int> ret; ret.resize(k, 0); for (int i = 0; i < S.size(); i++) { assert(S[i] - c < k); ret[S[i] - c]++; } return ret; } #define rep(i, begin, end) for (int64_t i = (begin); i <= (end); i++) #define rev(i, begin, end) for (int64_t i = (begin); (end) <= i; i--) void printvec(const std::vector<int> &vec) { for (int i = 0; i < vec.size(); i++) { std::cout << vec[i] << " "; } std::cout << std::endl; } const int MAX_N = 12; int N; std::string S[MAX_N]; std::map<std::string, int128_t> mp; int main(int argc, char **argv) { std::cin >> N; rep(i, 1, N) { std::cin >> S[i]; std::sort(S[i].begin(), S[i].end()); mp[S[i]]++; } int128_t ret = 0; for (auto p : mp) { ret += (p.second * (p.second - 1)) / 2; } std::cout << ret << std::endl; return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <cmath> // 変数名にy1が使えなくなるかも…。 #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <map> #include <queue> #include <set> #include <sstream> #include <stdio.h> #include <stdlib.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> typedef __int128_t int128_t; std::istream & operator>>(std::istream &input, int128_t &value) { // int128_tの入力。入力が64bitに収まる前提。 long long tmp; input >> tmp; value = tmp; return input; } std::ostream & operator<<(std::ostream &output, const int128_t value) { // int128_tの出力。出力が64bitに収まる前提。 output << (long long)value; return output; } // 文字列Sに各文字が何回出現するかカウント。始まりの文字がcで、文字種類数がk // 例えばSに含まれる文字が'a-z'であれば、c='a', k=26とする。 std::vector<int> count_char(const std::string &S, const char c, const int k) { std::vector<int> ret; ret.resize(k, 0); for (int i = 0; i < S.size(); i++) { assert(S[i] - c < k); ret[S[i] - c]++; } return ret; } #define rep(i, begin, end) for (int64_t i = (begin); i <= (end); i++) #define rev(i, begin, end) for (int64_t i = (begin); (end) <= i; i--) void printvec(const std::vector<int> &vec) { for (int i = 0; i < vec.size(); i++) { std::cout << vec[i] << " "; } std::cout << std::endl; } const int MAX_N = 1e5 + 10; int N; std::string S[MAX_N]; std::map<std::string, int128_t> mp; int main(int argc, char **argv) { std::cin >> N; rep(i, 1, N) { std::cin >> S[i]; std::sort(S[i].begin(), S[i].end()); mp[S[i]]++; } int128_t ret = 0; for (auto p : mp) { ret += (p.second * (p.second - 1)) / 2; } std::cout << ret << std::endl; return 0; }
replace
59
60
59
60
0
p02947
C++
Runtime Error
#include <bits/stdc++.h> #define range 524289 #define mod 1000000007 #define eps 1e-9 #define PI 3.14159265358979323846 #define pb push_back #define pf push_front #define mp make_pair #define fi first #define se second #define ALL(V) V.begin(), V.end() #define _ << " " << using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef pair<ll, pair<int, int>> iii; typedef vector<ii> vii; typedef vector<iii> viii; int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; map<string, ll> mp; for (int i = 0; i < n; i++) { string s; cin >> s; sort(s.begin(), s.end()); if (mp.find(s) != mp.end()) { mp[s]++; } else { mp[s] = 1; } } ll sum = 0; for (auto u : mp) { sum += (u.se * (u.se - 1)) / 2; } cout << sum << endl; return 0; }
#include <bits/stdc++.h> #define range 524289 #define mod 1000000007 #define eps 1e-9 #define PI 3.14159265358979323846 #define pb push_back #define pf push_front #define mp make_pair #define fi first #define se second #define ALL(V) V.begin(), V.end() #define _ << " " << using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> ii; typedef pair<ll, pair<int, int>> iii; typedef vector<ii> vii; typedef vector<iii> viii; int main(int argc, char const *argv[]) { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // freopen("error.txt", "w", stderr); // #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; map<string, ll> mp; for (int i = 0; i < n; i++) { string s; cin >> s; sort(s.begin(), s.end()); if (mp.find(s) != mp.end()) { mp[s]++; } else { mp[s] = 1; } } ll sum = 0; for (auto u : mp) { sum += (u.se * (u.se - 1)) / 2; } cout << sum << endl; return 0; }
replace
25
30
25
30
TLE
p02947
C++
Runtime Error
#define _USE_MATH_DEFINES // #include <bits/stdc++.h> // using namespace std; // #define krep(i, k, n) for (int i = k; i < n; ++i) #define rep(i, n) for (int i = 0; i < n; ++i) #define toupp(s) transform(s.begin(), s.end(), s.begin(), ::toupper) // string #define tolow(s) transform(s.begin(), s.end(), s.begin(), ::tolower) // string #define pb(v) push_back(v) #define all(v) v.begin(), v.end() #define mp make_pair #define __sort(v) sort(v.begin(), v.end()) // vector #define __stable_sort(v) stable_sort(v.begin(), v.end()) // vector #define __exists(c, v) (find(all(c), (v)) != (c).end()) // vector #define __unique(v) \ __sort(v); \ v.erase(unique(v.begin(), v.end()), v.end()) // vector #define __swap(a, b, tmp) \ tmp = a; \ a = b; \ b = tmp; #define __memset(a) memset(a, 0, sizeof(a)); // #define dumpVar(v) cerr << setw(30) << #v " = [" << v << "]" << endl #define dumpArr(v, n) \ for (int i = 0; i < n; ++i) { \ cerr << v[i] << ", "; \ } \ cerr << endl #define dumpPair(p) cerr << "<" << p.first << ", " << p.second << ">" << endl // typedef long long ll; typedef vector<string> vs; typedef vector<double> vd; typedef list<int> li_i; typedef list<double> li_d; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<double, double> pdd; // long kaijo(long x); int main() { ios::sync_with_stdio(false); // /* code start */ vector<char> _c_arr; int n; long counter = 0; cin >> n; vi arr; vs s_arr; rep(i, n) { string str = ""; rep(j, 10) { char c; cin >> c; if (str == "") { str = c; } else { int flag = 1; for (int k = 0; k < str.length(); ++k) { if (str[k] > c) { string::iterator it = str.begin() + k; str.insert(it, c); flag = 0; break; } } if (flag) { str = str + c; } } // dumpVar(str); } s_arr.pb(str); } dumpArr(s_arr, n); __sort(s_arr); dumpArr(s_arr, n); for (int i = 0; i < n; ++i) { long same_num = 0; long skip_num = 0; for (int j = i + 1; j < n; ++j) { if (i + 1 == j && s_arr[i] == s_arr[j]) same_num++; if (s_arr[i] == s_arr[j]) { same_num++; skip_num++; } else break; } dumpVar(i); dumpVar(same_num); i += skip_num; counter += kaijo(same_num) / kaijo(same_num - 2) / 2; } cout << counter << endl; /* code end */ // return 0; } long kaijo(long x) { long tmp = 1; krep(i, 1, x + 1) { tmp = tmp * i; } return tmp; } /* 3 3 * 2 * 1 / 2 4 * 3 * 2 * 1 / 2 /2 */
#define _USE_MATH_DEFINES // #include <bits/stdc++.h> // using namespace std; // #define krep(i, k, n) for (int i = k; i < n; ++i) #define rep(i, n) for (int i = 0; i < n; ++i) #define toupp(s) transform(s.begin(), s.end(), s.begin(), ::toupper) // string #define tolow(s) transform(s.begin(), s.end(), s.begin(), ::tolower) // string #define pb(v) push_back(v) #define all(v) v.begin(), v.end() #define mp make_pair #define __sort(v) sort(v.begin(), v.end()) // vector #define __stable_sort(v) stable_sort(v.begin(), v.end()) // vector #define __exists(c, v) (find(all(c), (v)) != (c).end()) // vector #define __unique(v) \ __sort(v); \ v.erase(unique(v.begin(), v.end()), v.end()) // vector #define __swap(a, b, tmp) \ tmp = a; \ a = b; \ b = tmp; #define __memset(a) memset(a, 0, sizeof(a)); // #define dumpVar(v) cerr << setw(30) << #v " = [" << v << "]" << endl #define dumpArr(v, n) \ for (int i = 0; i < n; ++i) { \ cerr << v[i] << ", "; \ } \ cerr << endl #define dumpPair(p) cerr << "<" << p.first << ", " << p.second << ">" << endl // typedef long long ll; typedef vector<string> vs; typedef vector<double> vd; typedef list<int> li_i; typedef list<double> li_d; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<double, double> pdd; // long kaijo(long x); int main() { ios::sync_with_stdio(false); // /* code start */ vector<char> _c_arr; int n; long counter = 0; cin >> n; vi arr; vs s_arr; rep(i, n) { string str = ""; rep(j, 10) { char c; cin >> c; if (str == "") { str = c; } else { int flag = 1; for (int k = 0; k < str.length(); ++k) { if (str[k] > c) { string::iterator it = str.begin() + k; str.insert(it, c); flag = 0; break; } } if (flag) { str = str + c; } } // dumpVar(str); } s_arr.pb(str); } dumpArr(s_arr, n); __sort(s_arr); dumpArr(s_arr, n); for (int i = 0; i < n; ++i) { long same_num = 0; long skip_num = 0; for (int j = i + 1; j < n; ++j) { if (i + 1 == j && s_arr[i] == s_arr[j]) same_num++; if (s_arr[i] == s_arr[j]) { same_num++; skip_num++; } else break; } dumpVar(i); dumpVar(same_num); i += skip_num; counter += same_num * (same_num - 1) / 2; } cout << counter << endl; /* code end */ // return 0; } long kaijo(long x) { long tmp = 1; krep(i, 1, x + 1) { tmp = tmp * i; } return tmp; } /* 3 3 * 2 * 1 / 2 4 * 3 * 2 * 1 / 2 /2 */
replace
105
106
105
106
0
acinnorstt, abbemnoptu, acinnorstt, abbemnoptu, acinnorstt, acinnorstt, i = [0] same_num = [0] i = [1] same_num = [2]
p02947
C++
Runtime Error
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define ll long long using namespace std; const int M = 1000000007; ll n, a, sol; string c, h[10]; set<string> s; map<string, ll> m; int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n; for (int i = 0; i < n; i++) { cin >> c; for (int i = 0; i < 10; i++) h[i] = c.substr(i, 1); sort(h, h + 10); c = ""; for (int i = 0; i < n; i++) c += h[i]; s.insert(c); m[c]++; } for (set<string>::iterator it = s.begin(); it != s.end(); it++) { a = m[*it]; if (a == 2) sol++; if (a > 2) sol += (a * (a - 3)) / 2 + a; } cout << sol; return 0; }
#include <algorithm> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define ll long long using namespace std; const int M = 1000000007; ll n, a, sol; string c, h[10]; set<string> s; map<string, ll> m; int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); cin >> n; for (int i = 0; i < n; i++) { cin >> c; for (int i = 0; i < 10; i++) h[i] = c.substr(i, 1); sort(h, h + 10); c = ""; for (int i = 0; i < 10; i++) c += h[i]; s.insert(c); m[c]++; } for (set<string>::iterator it = s.begin(); it != s.end(); it++) { a = m[*it]; if (a == 2) sol++; if (a > 2) sol += (a * (a - 3)) / 2 + a; } cout << sol; return 0; }
replace
38
39
38
39
0
p02947
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; string s[50000 + 5]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; sort(s[i].begin(), s[i].end()); } sort(s, s + n); long long cnt = 1; long long sum = 0; for (long long i = 0; i < n; i++) { if (s[i] != s[i + 1] && cnt != 1) { sum += (cnt * (cnt - 1)) / 2; cnt = 1; } else if (s[i] != s[i + 1] && cnt == 1) { cnt = 1; } else cnt++; } sum += (cnt * (cnt - 1)) / 2; cout << sum << endl; }
#include <algorithm> #include <iostream> using namespace std; string s[5000000 + 5]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; sort(s[i].begin(), s[i].end()); } sort(s, s + n); long long cnt = 1; long long sum = 0; for (long long i = 0; i < n; i++) { if (s[i] != s[i + 1] && cnt != 1) { sum += (cnt * (cnt - 1)) / 2; cnt = 1; } else if (s[i] != s[i + 1] && cnt == 1) { cnt = 1; } else cnt++; } sum += (cnt * (cnt - 1)) / 2; cout << sum << endl; }
replace
4
5
4
5
0
p02947
C++
Runtime Error
#include <bits/stdc++.h> //Carefully Crafted by hetp111 using namespace std; #define int long long #define double long double #define all(v) (v).begin(), (v).end() #define vi vector<int> #define pii pair<int, int> #define prec(n) fixed << setprecision(n) #define MOD 1000000007 #define eps 1e-8 #define FASTER \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { out << ""; for (auto it = a.begin(); it != a.end(); it++) { if (it != a.begin()) out << " "; out << *it; } out << ""; return out; } template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) { in >> a.first >> a.second; return in; } template <class A> istream &operator>>(istream &in, vector<A> &a) { for (A &i : a) in >> i; return in; } // ifstream cinn("input.txt");ofstream coutt("output.txt"); signed main() { FASTER; int n; cin >> n; vector<string> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; sort(all(v[i])); } sort(all(v)); int ans = 0; for (int i = 0; i < n - 1; i++) { int cnt = 1; while (v[i] == v[i + 1]) { cnt++; i++; } ans += (cnt * (cnt - 1) / 2); } cout << ans; }
#include <bits/stdc++.h> //Carefully Crafted by hetp111 using namespace std; #define int long long #define double long double #define all(v) (v).begin(), (v).end() #define vi vector<int> #define pii pair<int, int> #define prec(n) fixed << setprecision(n) #define MOD 1000000007 #define eps 1e-8 #define FASTER \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { out << ""; for (auto it = a.begin(); it != a.end(); it++) { if (it != a.begin()) out << " "; out << *it; } out << ""; return out; } template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) { in >> a.first >> a.second; return in; } template <class A> istream &operator>>(istream &in, vector<A> &a) { for (A &i : a) in >> i; return in; } // ifstream cinn("input.txt");ofstream coutt("output.txt"); signed main() { FASTER; int n; cin >> n; vector<string> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; sort(all(v[i])); } sort(all(v)); int ans = 0; for (int i = 0; i < n - 1; i++) { int cnt = 1; while (i + 1 < n && v[i] == v[i + 1]) { cnt++; i++; } ans += (cnt * (cnt - 1) / 2); } cout << ans; }
replace
52
53
52
53
0
p02947
Python
Time Limit Exceeded
N = int(input()) L = [list(input()) for _ in range(N)] dic = [] flag = {} res = 0 for i in range(N): L[i].sort() if L[i] in dic: flag[str(L[i])] += 1 res += flag[str(L[i])] else: dic.append(L[i]) flag[str(L[i])] = 0 print(res)
N = int(input()) L = [list(input()) for _ in range(N)] dic = [] flag = {} res = 0 for i in range(N): L[i].sort() if str(L[i]) in flag: flag[str(L[i])] += 1 res += flag[str(L[i])] else: dic.append(L[i]) flag[str(L[i])] = 0 print(res)
replace
10
11
10
11
TLE
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long int #define cases \ int t; \ cin >> t; \ while (t--) #define pb push_back using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; string a[n]; for (ll i = 0; i < n; i++) { cin >> a[i]; sort(a[i].begin(), a[i].end()); } ll cnt = 0; sort(a, a + n); for (ll i = 0; i < n - 1; i++) { for (ll j = i + 1; a[j] == a[i]; j++) cnt++; } cout << cnt << "\n"; return 0; }
#include <bits/stdc++.h> #define ll long long int #define cases \ int t; \ cin >> t; \ while (t--) #define pb push_back using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; string a[n]; for (ll i = 0; i < n; i++) { cin >> a[i]; sort(a[i].begin(), a[i].end()); } ll cnt = 0; sort(a, a + n); for (ll i = 0; i < n - 1; i++) { bool temp = false; ll d; for (ll j = i + 1; a[j] == a[i]; j++) { temp = true; d = j; } if (temp) { for (int j = 0; j <= (d - i); j++) cnt += j; i = d; } } cout << cnt << "\n"; return 0; }
replace
22
24
22
33
TLE
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define rep(i, s, N) for (ll i{s}; i < (N); i++) #define rem(i, N, s) for (ll i{N}; i > (s); i--) using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; const int MOD = (int)1e9 + 7; const string rt = "\n", sp = " "; /* 最大公約数 */ template <typename M, typename N> constexpr common_type_t<M, N> gcd(M a, N b) { return b != 0 ? gcd(b, a % b) : a; } /* 最小公倍数 */ template <typename M, typename N> constexpr common_type_t<M, N> lcm(M a, N b) { return a * b / gcd(a, b); } /* UnionFind */ template <typename T> struct UnionFind { vector<T> par; UnionFind(T n) : par(n, -1) {} void init(T n) { par.assign(n, -1); } T root(T x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(T x, T y) { return root(x) == root(y); } bool merge(T x, T y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; /* コンビネーション */ ll comb(ll n, ll r) { if (n < r) return 0; if (r * 2 > n) r = n - r; ll dividend = 1; ll divisor = 1; for (ll i = 1; i <= r; ++i) { dividend *= (n - i + 1); divisor *= i; } return dividend / divisor; } /* MODコンビネーション */ template <typename T> ll combpm(T N_, T C_) { const int NUM_ = 400001; static ll fact[NUM_ + 1], factr[NUM_ + 1], inv[NUM_ + 1]; if (fact[0] == 0) { inv[1] = fact[0] = factr[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fact[i] = fact[i - 1] * i % MOD, factr[i] = factr[i - 1] * inv[i] % MOD; } if (C_ < 0 || C_ > N_) return 0; return factr[C_] * fact[N_] % MOD * factr[N_ - C_] % MOD; } /* 多次元ベクター */ template <class T> vector<T> mvec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto mvec(size_t a, Ts... ts) { return vector<decltype(mvec<T>(ts...))>(a, mvec<T>(ts...)); } /* 多次元デック */ template <class T> deque<T> mdeq(size_t a) { return deque<T>(a); } template <class T, class... Ts> auto mdeq(size_t a, Ts... ts) { return deque<decltype(mdeq<T>(ts...))>(a, mdeq<T>(ts...)); } /* テスト関数 */ void test(ll n) { cout << "test" << n << endl; } /* 小数点以下 */ void fixsp(ll n) { cout << fixed << setprecision(n); } void defsp(ll n) { cout << defaultfloat << setprecision(n); } /* 重み付きUnionFind */ struct WUnionFind { vector<int> par; vector<int> rank; WUnionFind(int n = 1) { init(n); } void init(int n = 1) { par.resize(n); rank.resize(n); for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); return par[x] = r; } } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; return true; } }; /* 約数列挙 */ deque<ll> divisor(ll n) { deque<ll> ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } /* 最頻値 */ template <typename T> auto mode(T data) { if (data.size() == 1) return *begin(data); sort(begin(data), end(data)); typename decltype(data)::value_type mode{}; size_t n{}, count{1}; for (auto iter = adjacent_find(begin(data), end(data)), last = end(data), next = end(data); iter != last;) { next = upper_bound(iter, last, *iter); count = distance(iter, next); if (n < count) n = count, mode = *iter; iter = adjacent_find(next, last); } return mode; } /* 中央値 */ template <typename T> auto median(T data) { sort(begin(data), end(data)); size_t median_index = data.size() / 2; return ( data.size() % 2 == 0 ? static_cast<double>(data[median_index] + data[median_index - 1]) / 2 : data[median_index]); } /* 整数のpow */ template <typename T> ll multi(T a, T b) { ll ans{1}; rep(i, 0, b) ans *= a; return ans; } /* inf */ template <typename T> T inf() { return numeric_limits<T>::max(); } /* IO高速化 */ void fastio() { ios::sync_with_stdio(false); cin.tie(NULL); } /* ここからコード開始 */ int main() { fastio(); ll N; cin >> N; auto s = mdeq<string>(N); for (auto &&i : s) { cin >> i; sort(all(i)); } ll ans{}; rep(i, 0, N) { rep(j, i + 1, N) { if (s[i] == s[j]) ans++; } } cout << ans; }
#include <bits/stdc++.h> #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define rep(i, s, N) for (ll i{s}; i < (N); i++) #define rem(i, N, s) for (ll i{N}; i > (s); i--) using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; const int MOD = (int)1e9 + 7; const string rt = "\n", sp = " "; /* 最大公約数 */ template <typename M, typename N> constexpr common_type_t<M, N> gcd(M a, N b) { return b != 0 ? gcd(b, a % b) : a; } /* 最小公倍数 */ template <typename M, typename N> constexpr common_type_t<M, N> lcm(M a, N b) { return a * b / gcd(a, b); } /* UnionFind */ template <typename T> struct UnionFind { vector<T> par; UnionFind(T n) : par(n, -1) {} void init(T n) { par.assign(n, -1); } T root(T x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(T x, T y) { return root(x) == root(y); } bool merge(T x, T y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; /* コンビネーション */ ll comb(ll n, ll r) { if (n < r) return 0; if (r * 2 > n) r = n - r; ll dividend = 1; ll divisor = 1; for (ll i = 1; i <= r; ++i) { dividend *= (n - i + 1); divisor *= i; } return dividend / divisor; } /* MODコンビネーション */ template <typename T> ll combpm(T N_, T C_) { const int NUM_ = 400001; static ll fact[NUM_ + 1], factr[NUM_ + 1], inv[NUM_ + 1]; if (fact[0] == 0) { inv[1] = fact[0] = factr[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fact[i] = fact[i - 1] * i % MOD, factr[i] = factr[i - 1] * inv[i] % MOD; } if (C_ < 0 || C_ > N_) return 0; return factr[C_] * fact[N_] % MOD * factr[N_ - C_] % MOD; } /* 多次元ベクター */ template <class T> vector<T> mvec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto mvec(size_t a, Ts... ts) { return vector<decltype(mvec<T>(ts...))>(a, mvec<T>(ts...)); } /* 多次元デック */ template <class T> deque<T> mdeq(size_t a) { return deque<T>(a); } template <class T, class... Ts> auto mdeq(size_t a, Ts... ts) { return deque<decltype(mdeq<T>(ts...))>(a, mdeq<T>(ts...)); } /* テスト関数 */ void test(ll n) { cout << "test" << n << endl; } /* 小数点以下 */ void fixsp(ll n) { cout << fixed << setprecision(n); } void defsp(ll n) { cout << defaultfloat << setprecision(n); } /* 重み付きUnionFind */ struct WUnionFind { vector<int> par; vector<int> rank; WUnionFind(int n = 1) { init(n); } void init(int n = 1) { par.resize(n); rank.resize(n); for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); return par[x] = r; } } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; return true; } }; /* 約数列挙 */ deque<ll> divisor(ll n) { deque<ll> ret; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } /* 最頻値 */ template <typename T> auto mode(T data) { if (data.size() == 1) return *begin(data); sort(begin(data), end(data)); typename decltype(data)::value_type mode{}; size_t n{}, count{1}; for (auto iter = adjacent_find(begin(data), end(data)), last = end(data), next = end(data); iter != last;) { next = upper_bound(iter, last, *iter); count = distance(iter, next); if (n < count) n = count, mode = *iter; iter = adjacent_find(next, last); } return mode; } /* 中央値 */ template <typename T> auto median(T data) { sort(begin(data), end(data)); size_t median_index = data.size() / 2; return ( data.size() % 2 == 0 ? static_cast<double>(data[median_index] + data[median_index - 1]) / 2 : data[median_index]); } /* 整数のpow */ template <typename T> ll multi(T a, T b) { ll ans{1}; rep(i, 0, b) ans *= a; return ans; } /* inf */ template <typename T> T inf() { return numeric_limits<T>::max(); } /* IO高速化 */ void fastio() { ios::sync_with_stdio(false); cin.tie(NULL); } /* ここからコード開始 */ int main() { fastio(); ll N; cin >> N; auto s = mdeq<string>(N); for (auto &&i : s) { cin >> i; sort(all(i)); } sort(all(s)); ll ans{}, nice{1}; rep(i, 0, N - 1) { if (s[i] == s[i + 1]) { ans += nice; nice++; } else { nice = 1; } } cout << ans; }
replace
175
180
175
183
TLE
p02947
C++
Runtime Error
#pragma GCC optimize(3, "Ofast", "inline") #include <algorithm> #include <cstdio> #include <cstring> #include <map> #include <string> #define ll long long using namespace std; int n, tmp[30]; map<string, int> ma; ll ans = 0, sum = 0; char c[15]; inline int read() { int s = 0, t = 1; char c = getchar(); while ((c > '9' || c < '0') && c != '-') c = getchar(); if (c == '-') t = -1, c = getchar(); while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar(); return s * t; } int main(void) { scanf("%d", &n); for (int i = 1; i <= n; i++) { memset(tmp, 0, sizeof(tmp)); scanf("%s", c + 1); for (int j = 1; j <= 10; j++) tmp[c[j] - 'a' + 1]++; string s; for (int j = 1; j <= 30; j++) { for (int k = 1; k <= tmp[j]; k++) s += (j + 'a' - 1); } ma[s]++; ans += ma[s] - 1; } printf("%lld", ans); }
#pragma GCC optimize(3, "Ofast", "inline") #include <algorithm> #include <cstdio> #include <cstring> #include <map> #include <string> #define ll long long using namespace std; int n, tmp[30]; map<string, int> ma; ll ans = 0, sum = 0; char c[15]; inline int read() { int s = 0, t = 1; char c = getchar(); while ((c > '9' || c < '0') && c != '-') c = getchar(); if (c == '-') t = -1, c = getchar(); while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar(); return s * t; } int main(void) { scanf("%d", &n); for (int i = 1; i <= n; i++) { memset(tmp, 0, sizeof(tmp)); scanf("%s", c + 1); for (int j = 1; j <= 10; j++) tmp[c[j] - 'a' + 1]++; string s; for (int j = 1; j <= 26; j++) { for (int k = 1; k <= tmp[j]; k++) s += (j + 'a' - 1); } ma[s]++; ans += ma[s] - 1; } printf("%lld", ans); }
replace
34
35
34
35
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p02947
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) using namespace std; using ll = long long; int main() { ll n; cin >> n; vector<string> s(n); rep(i, n) cin >> s[i]; int i, j; ll cnt = 1, ans = 0; rep(i, n) sort(s[i].begin(), s[i].end()); sort(s.begin(), s.end()); for (i = 0; i < n - 1; i++) { cnt = 1; while (s[i] == s[i + cnt]) { cnt++; } ans += cnt - 1; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; ++i) using namespace std; using ll = long long; int main() { ll n; cin >> n; map<string, int> mp; rep(i, n) { string s; cin >> s; sort(s.begin(), s.end()); mp[s]++; } ll ans = 0; for (auto &p : mp) { int s = p.second; ans += (ll)s * (s - 1) / 2; } cout << ans << endl; return 0; }
replace
8
20
8
19
0
p02947
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair typedef pair<int, int> ii; const int mod = 1e9 + 7; const int inf = 1e9; int add(int x, int y) { return (1ll * x + 1ll * y) % mod; } int del(int x, int y) { return ((1ll * x - 1ll * y) % mod + mod) % mod; } int mul(int x, int y) { return (1ll * x * 1ll * y) % mod; } const int N = 1e5 + 5; int n; string s[N]; long long sum = 0; vector<string> vec; int main() { cin.tie(0), ios::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; sort(s[i].begin(), s[i].end()); vec.pb(s[i]); } sort(vec.begin(), vec.end()); for (int i = 0; i < n; i++) { int j = i; while (vec[i] == vec[j] && j < n) j++; j--; sum += 1ll * (j - i + 1) * (j - i) / (1ll * 2); i = j; } cout << sum; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mp make_pair typedef pair<int, int> ii; const int mod = 1e9 + 7; const int inf = 1e9; int add(int x, int y) { return (1ll * x + 1ll * y) % mod; } int del(int x, int y) { return ((1ll * x - 1ll * y) % mod + mod) % mod; } int mul(int x, int y) { return (1ll * x * 1ll * y) % mod; } const int N = 1e5 + 5; int n; string s[N]; long long sum = 0; vector<string> vec; int main() { cin.tie(0), ios::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; sort(s[i].begin(), s[i].end()); vec.pb(s[i]); } sort(vec.begin(), vec.end()); for (int i = 0; i < n; i++) { int j = i; while (j < n && vec[i] == vec[j] && j < n) j++; j--; sum += 1ll * (j - i + 1) * (j - i) / (1ll * 2); i = j; } cout << sum; }
replace
41
42
41
42
0
p02947
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define leading \ zero str.erase(0, min(str.find_first_not_of('0'), str.size() - 1)); using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; string text = "abcdefghijklmnopqrstuvwxyz"; const int maxn = 1e6 + 7; // .--------------. // | Try First One| // '--------------' // | .--------------. // | | | // V V | // .--------------. | // | AC. |<---. | // '--------------' | | // (True)| |(False) | | // .--------' | | | // | V | | // | .--------------. | | // | | Try Again |----' | // | '--------------' | // | | // | .--------------. | // '->| Try Next One |-------' // '--------------' int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<string> v; for (int i = 0; i < n; i++) { string s; cin >> s; int cnt[26] = {0}; for (auto x : s) cnt[x - 'a']++; string t = ""; for (int j = 0; j < 26; j++) t.push_back((cnt[j] + '0')); v.push_back(t); } int sum = 0; for (int i = 0; i < v.size() - 1; i++) { for (int j = i + 1; j < v.size(); j++) { if (v[i] == v[j]) sum++; } } cout << sum << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define leading \ zero str.erase(0, min(str.find_first_not_of('0'), str.size() - 1)); using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; string text = "abcdefghijklmnopqrstuvwxyz"; const int maxn = 1e6 + 7; // .--------------. // | Try First One| // '--------------' // | .--------------. // | | | // V V | // .--------------. | // | AC. |<---. | // '--------------' | | // (True)| |(False) | | // .--------' | | | // | V | | // | .--------------. | | // | | Try Again |----' | // | '--------------' | // | | // | .--------------. | // '->| Try Next One |-------' // '--------------' int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<string> v; for (int i = 0; i < n; i++) { string s; cin >> s; int cnt[26] = {0}; for (auto x : s) cnt[x - 'a']++; string t = ""; for (int j = 0; j < 26; j++) t.push_back((cnt[j] + '0')); v.push_back(t); } ll sum = 0; map<string, ll> mp; for (auto x : v) mp[x]++; for (map<string, ll>::iterator it = mp.begin(); it != mp.end(); it++) { ll y = it->second; sum += (y * (y - 1)) / 2; } cout << sum << endl; }
replace
50
56
50
57
TLE
p02947
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string str; int n, i, sum = 0; long long int tot = 0; cin >> n; vector<string> vect; for (i = 0; i < n; i++) { cin >> str; sort(str.begin(), str.end()); vect.push_back(str); } sort(vect.begin(), vect.end()); for (i = 0; i < vect.size(); i++) { if (vect[i] == vect[i + 1]) sum++; else sum = 0; tot += sum; } cout << tot << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; int n, i, sum = 0; long long int tot = 0; cin >> n; vector<string> vect; for (i = 0; i < n; i++) { cin >> str; sort(str.begin(), str.end()); vect.push_back(str); } sort(vect.begin(), vect.end()); for (i = 0; i < vect.size() - 1; i++) { if (vect[i] == vect[i + 1]) sum++; else sum = 0; tot += sum; } cout << tot << endl; return 0; }
replace
17
18
17
18
0
p02947
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <algorithm> #include <ccomplex> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <set> #include <sstream> #include <unordered_map> #include <vector> using namespace std; #define MAX 1LL << 60 #define MOD 1000000007 typedef long long ll; #define rep(i, s, e) for (ll i = s; i < e; i++) #define repR(i, s, e) for (ll i = s; i > e; i--) #define reft0(i, j) setfill('0') << setw(i) << j #define fix(i) fixed << setprecision(i) template <class T> inline bool chMax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chMin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline ll gcd(T a, T b) { if (a % b == 0) { return b; } else { return gcd(b, a % b); } } template <class T> inline ll lcm(T a, T b) { return a * b / gcd(a, b); } template <class T> inline ll fact(T a) { if (a <= 1) { return 1; } else { return a * fact(a - 1); } } template <class T> inline ll comb(T a, T b) { return a == 0 ? 0 : fact(a) / fact(b) / fact(a - b); } int main() { ll N; vector<string> s; map<string, ll> cnt; cin >> N; rep(i, 0, N) { string temp; cin >> temp; sort(begin(temp), end(temp)); cnt[temp]++; } ll ans = 0; for (pair<string, ll> ele : cnt) { if (ele.second >= 2) { ans += comb(ele.second, (ll)2); } } cout << ans << endl; return 0; }
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <algorithm> #include <ccomplex> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <set> #include <sstream> #include <unordered_map> #include <vector> using namespace std; #define MAX 1LL << 60 #define MOD 1000000007 typedef long long ll; #define rep(i, s, e) for (ll i = s; i < e; i++) #define repR(i, s, e) for (ll i = s; i > e; i--) #define reft0(i, j) setfill('0') << setw(i) << j #define fix(i) fixed << setprecision(i) template <class T> inline bool chMax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chMin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline ll gcd(T a, T b) { if (a % b == 0) { return b; } else { return gcd(b, a % b); } } template <class T> inline ll lcm(T a, T b) { return a * b / gcd(a, b); } template <class T> inline ll fact(T a) { if (a <= 1) { return 1; } else { return a * fact(a - 1); } } template <class T> inline ll comb(T a, T b) { return a == 0 ? 0 : fact(a) / fact(b) / fact(a - b); } int main() { ll N; vector<string> s; map<string, ll> cnt; cin >> N; rep(i, 0, N) { string temp; cin >> temp; sort(begin(temp), end(temp)); cnt[temp]++; } ll ans = 0; for (pair<string, ll> ele : cnt) { if (ele.second >= 2) { ans += (ele.second * (ele.second - 1) / 2); } } cout << ans << endl; return 0; }
replace
76
77
76
77
0
p02947
Python
Runtime Error
from math import factorial N = int(input()) d = {} for _ in range(N): s = input() s_sorted = "".join(sorted(s)) if not d.get(s_sorted): d[s_sorted] = 1 else: d[s_sorted] += 1 ans = 0 for _, v in d.items(): if v == 1: continue x = factorial(v) / factorial(2) / factorial(v - 2) ans += x print(round(ans))
from math import factorial N = int(input()) d = {} for _ in range(N): s = input() s_sorted = "".join(sorted(s)) if not d.get(s_sorted): d[s_sorted] = 1 else: d[s_sorted] += 1 ans = 0 for _, v in d.items(): if v == 1: continue ans += v * (v - 1) // 2 print(round(ans))
replace
16
18
16
17
0