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
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int dp[3001][3001]; int idx[3001][3001]; bool already[3001][3001]; string a, b; int Solve(int x = 0, int y = 0) { if (already[x][y]) return dp[x][y]; if (x == a.size() || y == b.size()) dp[x][y] = 0; else { dp[x][y] = Solve(x + 1, y); int i = y; for (; i < b.size(); ++i) if (b[i] == a[x]) break; idx[x][y] = i; if (i != b.size() && Solve(x + 1, i + 1) + 1 > dp[x][y]) dp[x][y] = 1 + dp[x + 1][i + 1]; } return dp[x][y]; } void Print(int x = 0, int y = 0) { if (dp[x][y] == 0) return; int i = idx[x][y]; if (i != b.size() && dp[x + 1][i + 1] + 1 > dp[x + 1][y]) { cout << a[x]; Print(x + 1, i + 1); } else Print(x + 1, y); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> a >> b; Solve(); Print(); cout << "\n"; return 0; } // RUL0
#include <bits/stdc++.h> using namespace std; int dp[3001][3001]; int idx[3001][3001]; bool already[3001][3001]; string a, b; int Solve(int x = 0, int y = 0) { if (already[x][y]) return dp[x][y]; already[x][y] = true; if (x == a.size() || y == b.size()) dp[x][y] = 0; else { dp[x][y] = Solve(x + 1, y); int i = y; for (; i < b.size(); ++i) if (b[i] == a[x]) break; idx[x][y] = i; if (i != b.size() && Solve(x + 1, i + 1) + 1 > dp[x][y]) dp[x][y] = 1 + dp[x + 1][i + 1]; } return dp[x][y]; } void Print(int x = 0, int y = 0) { if (dp[x][y] == 0) return; int i = idx[x][y]; if (i != b.size() && dp[x + 1][i + 1] + 1 > dp[x + 1][y]) { cout << a[x]; Print(x + 1, i + 1); } else Print(x + 1, y); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> a >> b; Solve(); Print(); cout << "\n"; return 0; } // RUL0
insert
12
12
12
13
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int dp[1001][1001]; string s, t, ans; int main() { cin >> s >> t; for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = max({dp[i - 1][j - 1] + 1, dp[i - 1][j], dp[i][j - 1]}); else dp[i][j] = max({dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]}); } } int x = s.size(); int y = t.size(); while (x && y) { int Maxx = max({dp[x - 1][y - 1], dp[x][y - 1], dp[x - 1][y]}); if (Maxx == dp[x][y] - 1) { ans += s[x - 1]; x--; y--; } else if (Maxx == dp[x - 1][y]) x--; else y--; } reverse(ans.begin(), ans.end()); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int dp[3001][3001]; string s, t, ans; int main() { cin >> s >> t; for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = max({dp[i - 1][j - 1] + 1, dp[i - 1][j], dp[i][j - 1]}); else dp[i][j] = max({dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]}); } } int x = s.size(); int y = t.size(); while (x && y) { int Maxx = max({dp[x - 1][y - 1], dp[x][y - 1], dp[x - 1][y]}); if (Maxx == dp[x][y] - 1) { ans += s[x - 1]; x--; y--; } else if (Maxx == dp[x - 1][y]) x--; else y--; } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
2
3
2
3
0
p03165
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <bitset> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <tuple> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0; i < n; ++i) #define exout(x) printf("%.10f\n", x) const double pi = acos(-1.0); const ll MOD = 1000000007; const ll INF = 1e10; const ll MAX_N = 1010; // 組み合わせの余りを求める ll fac[MAX_N], finv[MAX_N], inv[MAX_N]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX_N; i++) { fac[i] = fac[i - 1] * i; } } // 二項係数計算 long long COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; if (n > k * 2) { } return fac[n] / (fac[k] * fac[n - k]); } ll dx[4] = {0, 0, -1, 1}; ll dy[4] = {-1, 1, 0, 0}; ll dp[3010][3010]; // long longしか使わない // 素数は1より大きい // lower_boundは指定したkey以上の要素の一番左のイテレータをかえす // upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす int main() { string s, t; cin >> s >> t; ll n = s.length(); ll m = t.length(); rep(i, n) { rep(j, m) { if (s[i] == t[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; } else { dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); } } } string ans; while (n > 0 && m > 0) { if (dp[n][m] == dp[n - 1][m]) { n--; } else if (dp[n][m] == dp[n][m - 1]) { m--; } else { ans = s[n - 1] + ans; } } cout << ans << endl; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <bitset> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <tuple> #include <type_traits> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0; i < n; ++i) #define exout(x) printf("%.10f\n", x) const double pi = acos(-1.0); const ll MOD = 1000000007; const ll INF = 1e10; const ll MAX_N = 1010; // 組み合わせの余りを求める ll fac[MAX_N], finv[MAX_N], inv[MAX_N]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX_N; i++) { fac[i] = fac[i - 1] * i; } } // 二項係数計算 long long COM(ll n, ll k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; if (n > k * 2) { } return fac[n] / (fac[k] * fac[n - k]); } ll dx[4] = {0, 0, -1, 1}; ll dy[4] = {-1, 1, 0, 0}; ll dp[3010][3010]; // long longしか使わない // 素数は1より大きい // lower_boundは指定したkey以上の要素の一番左のイテレータをかえす // upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす int main() { string s, t; cin >> s >> t; ll n = s.length(); ll m = t.length(); rep(i, n) { rep(j, m) { if (s[i] == t[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; } else { dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); } } } string ans; while (n > 0 && m > 0) { if (dp[n][m] == dp[n - 1][m]) { n--; } else if (dp[n][m] == dp[n][m - 1]) { m--; } else { ans = s[n - 1] + ans; n--; m--; } } cout << ans << endl; return 0; }
insert
87
87
87
89
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int m = s.length(); int n = t.length(); int dp[m + 1][n + 1]; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = 0; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int k = dp[m][n]; string lcs('.', k); int i = m; int j = n; int p = k - 1; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { lcs[p] = s[i - 1]; i--; j--; p--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else j--; } cout << lcs; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int m = s.length(); int n = t.length(); int dp[m + 1][n + 1]; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = 0; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int k = dp[m][n]; string lcs(k, '.'); int i = m; int j = n; int p = k - 1; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { lcs[p] = s[i - 1]; i--; j--; p--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else j--; } cout << lcs; }
replace
25
26
25
26
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++) #define FORN(i, b, a) for (int i = (b); _a = (a); i >= _a; i--) #define REP(i, n) for (int i = 0, _n = n; i < n; i++) #define ll long long #define pii pair<int, int> #define re return #define vi vector<int> #define pb push_back #define si set<int> #define in insert #define fl float #define db double #define ld long double #define X first #define Y second #define st string #define ull unsigned long long #define mod 1000000007 #define INF 1000000007 using namespace std; inline void read(int &x) { short negative = 1; x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') negative = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); x *= negative; } int main() { string s, t; string ans = ""; ll dp[330][330]; memset(dp, 0, sizeof(dp)); cin >> s >> t; REP(i, s.size()) REP(j, t.size()) { if (s[i] == t[j]) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]); } int i = s.size(); int j = t.size(); while (i > 0 && j > 0) { if (dp[i][j] == dp[i - 1][j]) i--; else if (dp[i][j] == dp[i][j - 1]) j--; else { ans += s[i - 1]; i--; j--; } } for (int i = ans.size() - 1; i >= 0; i--) cout << ans[i]; cout << endl; return 0; }
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++) #define FORN(i, b, a) for (int i = (b); _a = (a); i >= _a; i--) #define REP(i, n) for (int i = 0, _n = n; i < n; i++) #define ll long long #define pii pair<int, int> #define re return #define vi vector<int> #define pb push_back #define si set<int> #define in insert #define fl float #define db double #define ld long double #define X first #define Y second #define st string #define ull unsigned long long #define mod 1000000007 #define INF 1000000007 using namespace std; inline void read(int &x) { short negative = 1; x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') negative = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); x *= negative; } int main() { string s, t; string ans = ""; ll dp[3030][3030]; memset(dp, 0, sizeof(dp)); cin >> s >> t; REP(i, s.size()) REP(j, t.size()) { if (s[i] == t[j]) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]); } int i = s.size(); int j = t.size(); while (i > 0 && j > 0) { if (dp[i][j] == dp[i - 1][j]) i--; else if (dp[i][j] == dp[i][j - 1]) j--; else { ans += s[i - 1]; i--; j--; } } for (int i = ans.size() - 1; i >= 0; i--) cout << ans[i]; cout << endl; return 0; }
replace
37
38
37
38
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAX = 3005; int dp[MAX][MAX]; int main() { string s, t; cin >> s; cin >> t; for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int x = s.size(), y = t.size(); string ans; while (true) { if (dp[x - 1][y] == dp[x][y]) { x--; } else if (dp[x][y - 1] == dp[x][y]) { y--; } else if (dp[x - 1][y - 1] + 1 == dp[x][y]) { ans += s[x - 1]; x--; y--; } else { break; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int MAX = 3005; int dp[MAX][MAX]; int main() { string s, t; cin >> s; cin >> t; for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int x = s.size(), y = t.size(); string ans; while (x > 0 && y > 0) { if (dp[x - 1][y] == dp[x][y]) { x--; } else if (dp[x][y - 1] == dp[x][y]) { y--; } else if (dp[x - 1][y - 1] + 1 == dp[x][y]) { ans += s[x - 1]; x--; y--; } else { break; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
28
29
28
29
0
p03165
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define mp make_pair #define pb push_back #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long LL; typedef pair<LL, LL> P; const LL mod = 1e9 + 7; const LL LINF = 1LL << 62; const int INF = 1000000000; int main() { string s, t; cin >> s >> t; int n = s.length(), m = t.length(); vector<vector<string>> dp(n + 1, vector<string>(m + 1, "")); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { string p = dp[i][j - 1], q = dp[i - 1][j], r = dp[i - 1][j - 1]; if (p.length() > q.length()) dp[i][j] = p; else dp[i][j] = q; if (s[i - 1] == t[j - 1]) { if (r.length() + 1 > dp[i][j].length()) dp[i][j] = r + s[i - 1]; } } } cout << dp[n][m] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fs first #define sc second #define mp make_pair #define pb push_back #define eb emplace_back #define ALL(A) A.begin(), A.end() #define RALL(A) A.rbegin(), A.rend() typedef long LL; typedef pair<LL, LL> P; const LL mod = 1e9 + 7; const LL LINF = 1LL << 62; const int INF = 1000000000; int main() { string s, t; cin >> s >> t; int n = s.length(), m = t.length(); vector<vector<string>> dp(n + 1, vector<string>(m + 1, "")); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { string p = dp[i][j - 1], q = dp[i - 1][j], r = dp[i - 1][j - 1]; if (p.length() > q.length()) dp[i][j] = p; else dp[i][j] = q; if (s[i - 1] == t[j - 1]) { if (r.length() + 1 > dp[i][j].length()) dp[i][j] = r + s[i - 1]; } dp[i - 1][j - 1] = ""; } } cout << dp[n][m] << endl; return 0; }
insert
31
31
31
32
MLE
p03165
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define X first #define Y second #define pb push_back #define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X)) #define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X)) #define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X)) #define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X)) #define repe(X, Y) for ((X) = 0; (X) < (Y); ++(X)) #define peat(X, Y) for (; (X) < (Y); ++(X)) #define all(X) (X).begin(), (X).end() #define rall(X) (X).rbegin(), (X).rend() #define eb emplace_back #define UNIQUE(X) (X).erase(unique(all(X)), (X).end()) #define Endl endl using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <class T> using vv = vector<vector<T>>; template <class T> ostream &operator<<(ostream &os, const deque<T> &t) { os << "{"; rep(i, t.size()) { os << t[i] << ","; } os << "}" << endl; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "{"; rep(i, t.size()) { os << t[i] << ","; } os << "}" << endl; return os; } template <class T, size_t n> ostream &operator<<(ostream &os, const array<T, n> &t) { os << "{"; rep(i, n) { os << t[i] << ","; } os << "}" << endl; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } template <class S, class T, class U> ostream &operator<<(ostream &os, const tuple<S, T, U> &t) { return os << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")"; } template <class S, class T, class U, class V> ostream &operator<<(ostream &os, const tuple<S, T, U, V> &t) { return os << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << "," << get<3>(t) << ")"; } template <class S, class T, class U, class V, class W> ostream &operator<<(ostream &os, const tuple<S, T, U, V, W> &t) { return os << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << "," << get<3>(t) << "," << get<4>(t) << ")"; } 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; } // #undef NUIP #ifdef NUIP #define out(args...) \ { \ vector<string> a_r_g_s = s_p_l_i_t(#args, ','); \ e_r_r(a_r_g_s.begin(), args); \ } vector<string> s_p_l_i_t(const string &s, char c) { vector<string> v; int d = 0, f = 0; string t; for (char c : s) { if (!d && c == ',') v.pb(t), t = ""; else t += c; if (c == '\"' || c == '\'') f ^= 1; if (!f && c == '(') ++d; if (!f && c == ')') --d; } v.pb(t); return move(v); } void e_r_r(vector<string>::iterator it) {} template <typename T, typename... Args> void e_r_r(vector<string>::iterator it, T a, Args... args) { if (*it == " 1" || *it == "1") cerr << endl; else cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", "; e_r_r(++it, args...); } #else #define out #endif #ifdef __cpp_init_captures template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); } template <class... Args> auto table(int n, Args... args) { auto val = table(args...); return vector<decltype(val)>(n, move(val)); } #endif const ll MOD = 1e9 + 7; int dp[3123][3213]; pii prv[3123][3213]; int main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); string s, t; cin >> s >> t; int n = s.size(), m = t.size(); rep(i, n + 1) rep(j, m + 1) { if (MX(dp[i + 1][j], dp[i][j])) prv[i + 1][j] = pii(1, 0); if (MX(dp[i][j + 1], dp[i][j])) prv[i][j + 1] = pii(0, 1); if (i < n && j < m && s[i] == t[j] && MX(dp[i + 1][j + 1], dp[i][j] + 1)) prv[i + 1][j + 1] = pii(1, 1); } string re; for (int i = n, j = m; i || j;) { pii d = prv[i][j]; if (d.X && d.Y) re += s[i - 1]; i -= d.X; j -= d.Y; } reverse(all(re)); cout << re << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define X first #define Y second #define pb push_back #define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X)) #define reps(X, S, Y) for (int(X) = S; (X) < (Y); ++(X)) #define rrep(X, Y) for (int(X) = (Y)-1; (X) >= 0; --(X)) #define rreps(X, S, Y) for (int(X) = (Y)-1; (X) >= (S); --(X)) #define repe(X, Y) for ((X) = 0; (X) < (Y); ++(X)) #define peat(X, Y) for (; (X) < (Y); ++(X)) #define all(X) (X).begin(), (X).end() #define rall(X) (X).rbegin(), (X).rend() #define eb emplace_back #define UNIQUE(X) (X).erase(unique(all(X)), (X).end()) #define Endl endl using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <class T> using vv = vector<vector<T>>; template <class T> ostream &operator<<(ostream &os, const deque<T> &t) { os << "{"; rep(i, t.size()) { os << t[i] << ","; } os << "}" << endl; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "{"; rep(i, t.size()) { os << t[i] << ","; } os << "}" << endl; return os; } template <class T, size_t n> ostream &operator<<(ostream &os, const array<T, n> &t) { os << "{"; rep(i, n) { os << t[i] << ","; } os << "}" << endl; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } template <class S, class T, class U> ostream &operator<<(ostream &os, const tuple<S, T, U> &t) { return os << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")"; } template <class S, class T, class U, class V> ostream &operator<<(ostream &os, const tuple<S, T, U, V> &t) { return os << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << "," << get<3>(t) << ")"; } template <class S, class T, class U, class V, class W> ostream &operator<<(ostream &os, const tuple<S, T, U, V, W> &t) { return os << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << "," << get<3>(t) << "," << get<4>(t) << ")"; } 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; } // #undef NUIP #ifdef NUIP #define out(args...) \ { \ vector<string> a_r_g_s = s_p_l_i_t(#args, ','); \ e_r_r(a_r_g_s.begin(), args); \ } vector<string> s_p_l_i_t(const string &s, char c) { vector<string> v; int d = 0, f = 0; string t; for (char c : s) { if (!d && c == ',') v.pb(t), t = ""; else t += c; if (c == '\"' || c == '\'') f ^= 1; if (!f && c == '(') ++d; if (!f && c == ')') --d; } v.pb(t); return move(v); } void e_r_r(vector<string>::iterator it) {} template <typename T, typename... Args> void e_r_r(vector<string>::iterator it, T a, Args... args) { if (*it == " 1" || *it == "1") cerr << endl; else cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", "; e_r_r(++it, args...); } #else #define out #endif #ifdef __cpp_init_captures template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); } template <class... Args> auto table(int n, Args... args) { auto val = table(args...); return vector<decltype(val)>(n, move(val)); } #endif const ll MOD = 1e9 + 7; int dp[3123][3213]; pii prv[3123][3213]; int main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); string s, t; cin >> s >> t; int n = s.size(), m = t.size(); fill(dp[0], dp[3123], -1); dp[0][0] = 0; rep(i, n + 1) rep(j, m + 1) { if (MX(dp[i + 1][j], dp[i][j])) prv[i + 1][j] = pii(1, 0); if (MX(dp[i][j + 1], dp[i][j])) prv[i][j + 1] = pii(0, 1); if (i < n && j < m && s[i] == t[j] && MX(dp[i + 1][j + 1], dp[i][j] + 1)) prv[i + 1][j + 1] = pii(1, 1); } string re; for (int i = n, j = m; i || j;) { pii d = prv[i][j]; if (d.X && d.Y) re += s[i - 1]; i -= d.X; j -= d.Y; } reverse(all(re)); cout << re << endl; return 0; }
insert
142
142
142
144
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define pb emplace_back #define fi first #define se second #define int long long using namespace std; mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int l) { uniform_int_distribution<int> uid(0, l - 1); return uid(rang); } int a[3001][3001]; int32_t main() { #ifndef ONLINE_JUDGE freopen("/tmp/input", "r", stdin); freopen("/tmp/output", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(NULL); cout.precision(10); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); string s; cin >> s; string t; cin >> t; int n = s.size(); int m = t.size(); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (s[i] == t[j]) { a[i + 1][j + 1] = a[i][j] + 1; } else { a[i + 1][j + 1] = max(a[i][j + 1], a[i + 1][j]); } } } string s1 = ""; int i = n, j = m; while (i && j) { if (s[i - 1] == t[j - 1]) { s1 += string(1, s[i - 1]); i--; j--; } else { if (a[i - 1][j] > a[i][j - 1]) { i--; } else { j--; } } } reverse(s1.begin(), s1.end()); cout << s1; }
#include <bits/stdc++.h> #define pb emplace_back #define fi first #define se second #define int long long using namespace std; mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int l) { uniform_int_distribution<int> uid(0, l - 1); return uid(rang); } int a[3001][3001]; int32_t main() { // #ifndef ONLINE_JUDGE // freopen("/tmp/input", "r", stdin); // freopen("/tmp/output", "w", stdout); // #endif ios::sync_with_stdio(false); cin.tie(NULL); cout.precision(10); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); string s; cin >> s; string t; cin >> t; int n = s.size(); int m = t.size(); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (s[i] == t[j]) { a[i + 1][j + 1] = a[i][j] + 1; } else { a[i + 1][j + 1] = max(a[i][j + 1], a[i + 1][j]); } } } string s1 = ""; int i = n, j = m; while (i && j) { if (s[i - 1] == t[j - 1]) { s1 += string(1, s[i - 1]); i--; j--; } else { if (a[i - 1][j] > a[i][j - 1]) { i--; } else { j--; } } } reverse(s1.begin(), s1.end()); cout << s1; }
replace
16
20
16
20
0
p03165
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> using std::max; int LCS(char *s1, char *s2, char *res) { int i, j, M, N, k = 0, upbd; M = strlen(s1); N = strlen(s2); int **dp; dp = new int *[M + 2]; for (i = 0; i < M + 2; i++) dp[i] = new int[N + 2]; for (i = 0; i < N + 2; i++) dp[0][i] = 0; for (i = 1; i < M + 1; i++) { dp[i][0] = 0; for (j = 1; j < N + 1; j++) { if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } k = dp[M][N]; i = M; j = N; upbd = k; res[k] = '\0'; while (i > 0 && j > 0) { if (dp[i - 1][j] == dp[i - 1][j - 1] && dp[i - 1][j - 1] == dp[i][j - 1]) { if (dp[i][j] == dp[i - 1][j - 1] + 1) res[--upbd] = s1[i - 1]; i--; j--; } else { if (dp[i - 1][j] == dp[i][j]) i--; else j--; } } for (i = 0; i < M + 2; i++) delete[] dp[i]; delete[] dp; return k; } void solve(char *s) { char *s1, *s2, *res, *target; int N = strlen(s), i, d, maxVal = -1; s1 = new char[N + 1]; s2 = new char[N + 1]; res = new char[N + 1]; target = new char[N + 1]; for (i = 2; i < N - 1; i++) { strcpy(s1, s); s1[i] = 0; strcpy(s2, s + i); d = LCS(s1, s2, res); if (d > maxVal) { maxVal = d; strcpy(target, res); } } printf("%s\n", target); delete[] s1; delete[] s2; delete[] res; delete[] target; } #define MAXLENGTH 303 int main(void) { char s[MAXLENGTH], t[MAXLENGTH], r[MAXLENGTH]; scanf("%s%s", s, t); LCS(s, t, r); printf("%s\n", r); return 0; }
#include <algorithm> #include <cstdio> #include <cstring> using std::max; int LCS(char *s1, char *s2, char *res) { int i, j, M, N, k = 0, upbd; M = strlen(s1); N = strlen(s2); int **dp; dp = new int *[M + 2]; for (i = 0; i < M + 2; i++) dp[i] = new int[N + 2]; for (i = 0; i < N + 2; i++) dp[0][i] = 0; for (i = 1; i < M + 1; i++) { dp[i][0] = 0; for (j = 1; j < N + 1; j++) { if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } k = dp[M][N]; i = M; j = N; upbd = k; res[k] = '\0'; while (i > 0 && j > 0) { if (dp[i - 1][j] == dp[i - 1][j - 1] && dp[i - 1][j - 1] == dp[i][j - 1]) { if (dp[i][j] == dp[i - 1][j - 1] + 1) res[--upbd] = s1[i - 1]; i--; j--; } else { if (dp[i - 1][j] == dp[i][j]) i--; else j--; } } for (i = 0; i < M + 2; i++) delete[] dp[i]; delete[] dp; return k; } #define MAXLENGTH 3003 int main(void) { char s[MAXLENGTH], t[MAXLENGTH], r[MAXLENGTH]; scanf("%s%s", s, t); LCS(s, t, r); printf("%s\n", r); return 0; }
replace
51
77
51
52
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1005; string s, t; int n, m; int dp[MAX_N][MAX_N]; pair<int, int> pre[MAX_N][MAX_N]; int main() { cin >> s >> t; n = s.size(), m = t.size(); s = '0' + s; t = '0' + t; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { // if(s[i]==t[j])dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1); // dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i][j-1])); if (s[i] == t[j] && dp[i][j] < dp[i - 1][j - 1] + 1) { dp[i][j] = dp[i - 1][j - 1] + 1; pre[i][j] = make_pair(i - 1, j - 1); } if (dp[i][j] < dp[i - 1][j]) { dp[i][j] = dp[i - 1][j]; pre[i][j] = make_pair(i - 1, j); } if (dp[i][j] < dp[i][j - 1]) { dp[i][j] = dp[i][j - 1]; pre[i][j] = make_pair(i, j - 1); } } } int nx = n, ny = m; string ans; while (nx && ny) { if (s[nx] == t[ny]) ans = s[nx] + ans; tie(nx, ny) = pre[nx][ny]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 3005; string s, t; int n, m; int dp[MAX_N][MAX_N]; pair<int, int> pre[MAX_N][MAX_N]; int main() { cin >> s >> t; n = s.size(), m = t.size(); s = '0' + s; t = '0' + t; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { // if(s[i]==t[j])dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1); // dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i][j-1])); if (s[i] == t[j] && dp[i][j] < dp[i - 1][j - 1] + 1) { dp[i][j] = dp[i - 1][j - 1] + 1; pre[i][j] = make_pair(i - 1, j - 1); } if (dp[i][j] < dp[i - 1][j]) { dp[i][j] = dp[i - 1][j]; pre[i][j] = make_pair(i - 1, j); } if (dp[i][j] < dp[i][j - 1]) { dp[i][j] = dp[i][j - 1]; pre[i][j] = make_pair(i, j - 1); } } } int nx = n, ny = m; string ans; while (nx && ny) { if (s[nx] == t[ny]) ans = s[nx] + ans; tie(nx, ny) = pre[nx][ny]; } cout << ans << endl; return 0; }
replace
2
3
2
3
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int dp[3000][3000]; void helper(string &s, string &t, int len) { string LCS; int i = 0, j = 0; while (len) { if (s[i] == t[j]) { LCS.push_back(s[i]); i++; j++; len--; } else if (dp[i][j + 1] > dp[i + 1][j]) j++; else i++; } cout << LCS; } int maxlensubseqence(int i, int j, string s, string t) { if (i >= s.length() or j >= t.length()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (s[i] == t[j]) return dp[i][j] = 1 + maxlensubseqence(i + 1, j + 1, s, t); else return dp[i][j] = max(maxlensubseqence(i, j + 1, s, t), maxlensubseqence(i + 1, j, s, t)); } void printmaxlensubsequence(string s, string t) { memset(dp, -1, sizeof dp); int len = maxlensubseqence(0, 0, s, t); helper(s, t, len); // for(int i=0;i<s.length();i++) // { // for(int j=0;j<t.length();j++) // cout<<dp[i][j]<<"\t"; // cout<<endl; // } } int main() { string s, t; cin >> s >> t; printmaxlensubsequence(s, t); return 0; }
#include <bits/stdc++.h> using namespace std; int dp[3000][3000]; void helper(string &s, string &t, int len) { string LCS; int i = 0, j = 0; while (len) { if (s[i] == t[j]) { LCS.push_back(s[i]); i++; j++; len--; } else if (dp[i][j + 1] > dp[i + 1][j]) j++; else i++; } cout << LCS; } int maxlensubseqence(int i, int j, string &s, string &t) { if (i >= s.length() or j >= t.length()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (s[i] == t[j]) return dp[i][j] = 1 + maxlensubseqence(i + 1, j + 1, s, t); else return dp[i][j] = max(maxlensubseqence(i, j + 1, s, t), maxlensubseqence(i + 1, j, s, t)); } void printmaxlensubsequence(string s, string t) { memset(dp, -1, sizeof dp); int len = maxlensubseqence(0, 0, s, t); helper(s, t, len); // for(int i=0;i<s.length();i++) // { // for(int j=0;j<t.length();j++) // cout<<dp[i][j]<<"\t"; // cout<<endl; // } } int main() { string s, t; cin >> s >> t; printmaxlensubsequence(s, t); return 0; }
replace
20
21
20
21
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int dp[1001][1001]; int main() { string s, t; cin >> s >> t; string ans = ""; for (int i = 0; i <= s.length(); i++) { for (int j = 0; j <= t.length(); j++) { if (i == 0 or j == 0) dp[i][j] = 0; else if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int i = s.size(), j = t.size(); while (i > 0 and j > 0) { if (s[i - 1] == t[j - 1]) { ans = s[i - 1] + ans; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else { j--; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[3001][3001]; int main() { string s, t; cin >> s >> t; string ans = ""; for (int i = 0; i <= s.length(); i++) { for (int j = 0; j <= t.length(); j++) { if (i == 0 or j == 0) dp[i][j] = 0; else if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int i = s.size(), j = t.size(); while (i > 0 and j > 0) { if (s[i - 1] == t[j - 1]) { ans = s[i - 1] + ans; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else { j--; } } cout << ans; return 0; }
replace
2
3
2
3
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int n = s.length(), m = t.length(); int dp[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) dp[i][j] = 0; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } string ans = ""; while (n > 0 || m > 0) { if (dp[n - 1][m] == dp[n][m]) { n--; } else if (dp[n][m - 1] == dp[n][m]) { m--; } else { ans.push_back(s[n - 1]); n--; m--; } } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int n = s.length(), m = t.length(); int dp[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) dp[i][j] = 0; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } string ans = ""; while (n > 0 && m > 0) { if (dp[n - 1][m] == dp[n][m]) { n--; } else if (dp[n][m - 1] == dp[n][m]) { m--; } else { ans.push_back(s[n - 1]); n--; m--; } } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
replace
23
24
23
24
0
p03165
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = 100100100; const ll INFLL = 100100100100100100; const int MOD = (int)1e9 + 7; const ll MODLL = (ll)1e9 + 7; const double EPS = 1e-9; int main() { string s, t; cin >> s >> t; vector<vector<int>> dp(3000, vector<int>(3000, 0)); vector<vector<int>> prev_i(3000, vector<int>(3000, 0)); vector<vector<int>> prev_j(3000, vector<int>(3000, 0)); for (int i = 0; i < s.length(); i++) { for (int j = 0; j < t.length(); j++) { if (s[i] == t[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; prev_i[i + 1][j + 1] = i; prev_j[i + 1][j + 1] = j; } else { if (dp[i + 1][j] > dp[i][j + 1]) { dp[i + 1][j + 1] = dp[i + 1][j]; prev_i[i + 1][j + 1] = i + 1; prev_j[i + 1][j + 1] = j; } else { dp[i + 1][j + 1] = dp[i][j + 1]; prev_i[i + 1][j + 1] = i; prev_j[i + 1][j + 1] = j + 1; } } } } int i = s.length(); int j = t.length(); string ans = ""; while (!(i == 0 && j == 0)) { int next_i = prev_i[i][j]; int next_j = prev_j[i][j]; if (next_i < i && next_j < j) { ans = s[next_i] + ans; } i = next_i; j = next_j; } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) using namespace std; typedef long long ll; typedef pair<int, int> P; const int INF = 100100100; const ll INFLL = 100100100100100100; const int MOD = (int)1e9 + 7; const ll MODLL = (ll)1e9 + 7; const double EPS = 1e-9; int main() { string s, t; cin >> s >> t; vector<vector<int>> dp(3010, vector<int>(3010, 0)); vector<vector<int>> prev_i(3010, vector<int>(3010, 0)); vector<vector<int>> prev_j(3010, vector<int>(3010, 0)); for (int i = 0; i < s.length(); i++) { for (int j = 0; j < t.length(); j++) { if (s[i] == t[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; prev_i[i + 1][j + 1] = i; prev_j[i + 1][j + 1] = j; } else { if (dp[i + 1][j] > dp[i][j + 1]) { dp[i + 1][j + 1] = dp[i + 1][j]; prev_i[i + 1][j + 1] = i + 1; prev_j[i + 1][j + 1] = j; } else { dp[i + 1][j + 1] = dp[i][j + 1]; prev_i[i + 1][j + 1] = i; prev_j[i + 1][j + 1] = j + 1; } } } } int i = s.length(); int j = t.length(); string ans = ""; while (!(i == 0 && j == 0)) { int next_i = prev_i[i][j]; int next_j = prev_j[i][j]; if (next_i < i && next_j < j) { ans = s[next_i] + ans; } i = next_i; j = next_j; } cout << ans << endl; return 0; }
replace
29
32
29
32
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03165
C++
Runtime Error
#include <algorithm> #include <iostream> #include <map> #include <string> #include <utility> #include <vector> using namespace std; #undef INT_MIN #undef INT_MAX #define LL long long #define INT_MIN -2147483647 #define INT_MAX 1000000009 #define LL_MIN -9223372036854775807 #define LL_MAX 9223372036854775807 string s, t; int S, T; int dp[3009][3009]; string maxst(string a, string b) { int A = a.size(), B = b.size(); if (A >= B) return a; else return b; } int main() { cin >> s >> t; S = s.size(); T = t.size(); for (int i = 0; i <= S; i++) dp[i][0] = 0; for (int i = 0; i <= T; i++) dp[0][i] = 0; for (int i = 1; i <= S; i++) { for (int j = 1; j <= T; j++) { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); if (s[i - 1] == t[j - 1]) { dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); } // cout << dp[i][j] << " "; } // cout << endl; } string ans; int tn = T, sn = S; while (sn != 0 || tn != 0) { if (dp[sn][tn] != dp[sn - 1][tn] && dp[sn][tn] != dp[sn][tn - 1]) { ans += s[sn - 1]; sn--; tn--; } else if (dp[sn][tn] != dp[sn - 1][tn]) tn--; else sn--; } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; } /* #include<algorithm> #include<iostream> #include<utility> using namespace std; #undef INT_MIN #undef INT_MAX #define LL long long #define INT_MIN -2147483647 #define INT_MAX 2147483647 #define LL_MIN -9223372036854775807 #define LL_MAX 9223372036854775807 int N,W; pair<int,int> b[100]; int w[105],v[105]; LL dp[105][100100]; int main(){ cin >> N >> W; for(int i = 0; i<N; i++){ for(int j=0; j<=W; j++){ dp[i][j]=-1; } } for(int i=0; i<N; i++){ int we,va; cin >> we >> va; b[i]=make_pair(we,va); } for(int i = 0; i <= W; i++) dp[N][i] = 0; for(int i = N-1; i >= 0; i--){ for(int j = W; j >= 0; j--){ dp[i][j] = dp[i+1][j];//not serect i if(j-b[i].first >= 0){ dp[i][j]= max( dp[i][j], dp[i+1][j-b[i].first] + b[i].second ); } } } cout << dp[0][W] << endl; return 0; } */ // union find & prime /* #include<algorithm> #include<iostream> #include<map> #include<string> #include<utility> #include<vector> using namespace std; int n; int num[100100]; vector<pair<int,int>>sortednum; int par[100010]; map<int,int>ppid; int neid(int x){ if(par[x]==x)return x; else{ return par[x]=neid(par[x]); } } void primenum(int inow, int NUM){ for(int i=2; i*i<=NUM; i++){ if(NUM%i==0){ if(ppid.count(i)==0){ ppid[i]=inow; } else{ par[neid(ppid[i])]=inow; } while(NUM%i==0)NUM/=i; } } if(NUM!=1){ if(ppid.count(NUM)==0){ ppid[NUM]=inow; } else{ par[neid(ppid[NUM])]=inow; } } } int main(){ cin >> n; for(int i=0; i<n; i++){ cin >> num[i]; sortednum.push_back(make_pair(num[i],i)); par[i]=i; primenum(i,num[i]); } sort(sortednum.begin(),sortednum.end()); int ans=1; for(int i=0; i<n; i++){ if(neid(i)!=neid(sortednum[i].second)){ ans=0; break; } } cout << ans << endl; return 0; } */ // djikstra /* #include <algorithm> #include <cassert> #include <cstdlib> #include <fstream> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <tuple> #include <unordered_map> #include <vector> using namespace std; int INF = 10000000; priority_queue< pair<int,int> > que; //INF-cost,i int cost[1000]; for(int i=0; i<1000; i++) cost[i] = INF; cost[0] = 0; que.push(make_pair(INF-0, 0)); while(!que.empty){ int now = que.top().second; for(int i=0; i<tonari[now].size(); i++){ if(cost[tonari[now][i]] > cost[now] + distance[now][tonari[now][i]){ cost[tonari[now][i]] = cost[now] + distance[now][tonari[now][i]; que.push(make_pair(INF-cost[tonari[now][i]], tonari[now][i])); } } que.pop(); } */
#include <algorithm> #include <iostream> #include <map> #include <string> #include <utility> #include <vector> using namespace std; #undef INT_MIN #undef INT_MAX #define LL long long #define INT_MIN -2147483647 #define INT_MAX 1000000009 #define LL_MIN -9223372036854775807 #define LL_MAX 9223372036854775807 string s, t; int S, T; int dp[3009][3009]; string maxst(string a, string b) { int A = a.size(), B = b.size(); if (A >= B) return a; else return b; } int main() { cin >> s >> t; S = s.size(); T = t.size(); for (int i = 0; i <= S; i++) dp[i][0] = 0; for (int i = 0; i <= T; i++) dp[0][i] = 0; for (int i = 1; i <= S; i++) { for (int j = 1; j <= T; j++) { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); if (s[i - 1] == t[j - 1]) { dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); } // cout << dp[i][j] << " "; } // cout << endl; } string ans; int tn = T, sn = S; while (sn != 0 && tn != 0) { if (dp[sn][tn] != dp[sn - 1][tn] && dp[sn][tn] != dp[sn][tn - 1]) { ans += s[sn - 1]; sn--; tn--; } else if (dp[sn][tn] != dp[sn - 1][tn]) tn--; else sn--; } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; } /* #include<algorithm> #include<iostream> #include<utility> using namespace std; #undef INT_MIN #undef INT_MAX #define LL long long #define INT_MIN -2147483647 #define INT_MAX 2147483647 #define LL_MIN -9223372036854775807 #define LL_MAX 9223372036854775807 int N,W; pair<int,int> b[100]; int w[105],v[105]; LL dp[105][100100]; int main(){ cin >> N >> W; for(int i = 0; i<N; i++){ for(int j=0; j<=W; j++){ dp[i][j]=-1; } } for(int i=0; i<N; i++){ int we,va; cin >> we >> va; b[i]=make_pair(we,va); } for(int i = 0; i <= W; i++) dp[N][i] = 0; for(int i = N-1; i >= 0; i--){ for(int j = W; j >= 0; j--){ dp[i][j] = dp[i+1][j];//not serect i if(j-b[i].first >= 0){ dp[i][j]= max( dp[i][j], dp[i+1][j-b[i].first] + b[i].second ); } } } cout << dp[0][W] << endl; return 0; } */ // union find & prime /* #include<algorithm> #include<iostream> #include<map> #include<string> #include<utility> #include<vector> using namespace std; int n; int num[100100]; vector<pair<int,int>>sortednum; int par[100010]; map<int,int>ppid; int neid(int x){ if(par[x]==x)return x; else{ return par[x]=neid(par[x]); } } void primenum(int inow, int NUM){ for(int i=2; i*i<=NUM; i++){ if(NUM%i==0){ if(ppid.count(i)==0){ ppid[i]=inow; } else{ par[neid(ppid[i])]=inow; } while(NUM%i==0)NUM/=i; } } if(NUM!=1){ if(ppid.count(NUM)==0){ ppid[NUM]=inow; } else{ par[neid(ppid[NUM])]=inow; } } } int main(){ cin >> n; for(int i=0; i<n; i++){ cin >> num[i]; sortednum.push_back(make_pair(num[i],i)); par[i]=i; primenum(i,num[i]); } sort(sortednum.begin(),sortednum.end()); int ans=1; for(int i=0; i<n; i++){ if(neid(i)!=neid(sortednum[i].second)){ ans=0; break; } } cout << ans << endl; return 0; } */ // djikstra /* #include <algorithm> #include <cassert> #include <cstdlib> #include <fstream> #include <iostream> #include <list> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <tuple> #include <unordered_map> #include <vector> using namespace std; int INF = 10000000; priority_queue< pair<int,int> > que; //INF-cost,i int cost[1000]; for(int i=0; i<1000; i++) cost[i] = INF; cost[0] = 0; que.push(make_pair(INF-0, 0)); while(!que.empty){ int now = que.top().second; for(int i=0; i<tonari[now].size(); i++){ if(cost[tonari[now][i]] > cost[now] + distance[now][tonari[now][i]){ cost[tonari[now][i]] = cost[now] + distance[now][tonari[now][i]; que.push(make_pair(INF-cost[tonari[now][i]], tonari[now][i])); } } que.pop(); } */
replace
51
52
51
52
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define MAX 3000 int dp[MAX + 1][MAX + 1]; string getLCS(string &s, string &t, int len) { string LCS; int i = 0, j = 0; while (len > 0) { if (s[i] == t[j]) { LCS.push_back(s[i]); i++; j++; len--; } else { if (dp[i][j + 1] > dp[i + 1][j]) { j++; } else i++; } } return LCS; } int lenLCS(string s, string t, int i, int j) { if (i >= s.length() || j >= t.length()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (s[i] == t[j]) return dp[i][j] = 1 + lenLCS(s, t, i + 1, j + 1); else { return dp[i][j] = max(lenLCS(s, t, i, j + 1), lenLCS(s, t, i + 1, j)); } } string solve(string s, string t) { memset(dp, -1, sizeof(dp)); int len = lenLCS(s, t, 0, 0); return getLCS(s, t, len); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s, t; cin >> s >> t; cout << solve(s, t); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define MAX 3000 int dp[MAX + 1][MAX + 1]; string getLCS(string &s, string &t, int len) { string LCS; int i = 0, j = 0; while (len > 0) { if (s[i] == t[j]) { LCS.push_back(s[i]); i++; j++; len--; } else { if (dp[i][j + 1] > dp[i + 1][j]) { j++; } else i++; } } return LCS; } int lenLCS(string &s, string &t, int i, int j) { if (i >= s.length() || j >= t.length()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (s[i] == t[j]) return dp[i][j] = 1 + lenLCS(s, t, i + 1, j + 1); else { return dp[i][j] = max(lenLCS(s, t, i, j + 1), lenLCS(s, t, i + 1, j)); } } string solve(string s, string t) { memset(dp, -1, sizeof(dp)); int len = lenLCS(s, t, 0, 0); return getLCS(s, t, len); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s, t; cin >> s >> t; cout << solve(s, t); }
replace
26
27
26
27
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1)); for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = max(dp[i][j], 1 + dp[i - 1][j - 1]); else { dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1])); } } } int i = s.size(), j = t.size(); string ans = ""; while (j > 0) { // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl; if (dp[i][j] == dp[i - 1][j]) { i--; } else if (dp[i][j] == dp[i][j - 1]) { j--; } else { ans = s[i - 1] + ans; i--; j--; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1)); for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = max(dp[i][j], 1 + dp[i - 1][j - 1]); else { dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1])); } } } int i = s.size(), j = t.size(); string ans = ""; while (j > 0 && i > 0) { // cout<<i<<" "<<j<<" "<<dp[i][j]<<endl; if (dp[i][j] == dp[i - 1][j]) { i--; } else if (dp[i][j] == dp[i][j - 1]) { j--; } else { ans = s[i - 1] + ans; i--; j--; } } cout << ans << endl; return 0; }
replace
17
18
17
18
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long int arr[1000000][2], ans[105][100003]; vector<long long int> crr; long long int W, N; int dp[3005][3005]; vector<char> lcs(string s, string t) { int n = s.size(); int m = t.size(); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (s[i] == t[j]) { dp[i + 1][j + 1] = 1 + dp[i][j]; } else { dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } } vector<char> ans; int last = m; int val = dp[n][m]; // for (int i = 0; i <=n; ++i) // { // for(int j=0;j<=m;j++) // { // cout<<dp[i][j]<<" "; // } // cout<<endl; // } // for (int i = n; i >= 0; --i) // { // for (int j = last; j >=0; --j) // { // if(dp[i][j]==val) // { // last=j; // } // else // { // // cout<<i<<" "<<j<<endl; // ans.push_back(t[j]); // last=j; // val--; // break; // } // } // } for (int i = n; i >= 0; --i) { for (int j = last; j >= 0; --j) { if (s[i - 1] == t[j - 1]) { ans.push_back(s[i - 1]); last = j - 1; break; } else if (dp[i - 1][j] > dp[i][j - 1]) { last = j; break; } } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, a, b, c, d, w; string s, t; cin >> s >> t; for (int i = 0; i < 3005; ++i) { for (int j = 0; j < 3005; j++) { if (i != 0 && j != 0) dp[i][j] = -1; } } vector<char> ans; ans = lcs(s, t); reverse(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); ++i) { cout << ans[i]; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long int arr[1000000][2], ans[105][100003]; vector<long long int> crr; long long int W, N; int dp[3005][3005]; vector<char> lcs(string s, string t) { int n = s.size(); int m = t.size(); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (s[i] == t[j]) { dp[i + 1][j + 1] = 1 + dp[i][j]; } else { dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } } vector<char> ans; int last = m; int val = dp[n][m]; // for (int i = 0; i <=n; ++i) // { // for(int j=0;j<=m;j++) // { // cout<<dp[i][j]<<" "; // } // cout<<endl; // } // for (int i = n; i >= 0; --i) // { // for (int j = last; j >=0; --j) // { // if(dp[i][j]==val) // { // last=j; // } // else // { // // cout<<i<<" "<<j<<endl; // ans.push_back(t[j]); // last=j; // val--; // break; // } // } // } for (int i = n; i > 0; --i) { for (int j = last; j > 0; --j) { if (s[i - 1] == t[j - 1]) { ans.push_back(s[i - 1]); last = j - 1; break; } else if (dp[i - 1][j] > dp[i][j - 1]) { last = j; break; } } } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, a, b, c, d, w; string s, t; cin >> s >> t; for (int i = 0; i < 3005; ++i) { for (int j = 0; j < 3005; j++) { if (i != 0 && j != 0) dp[i][j] = -1; } } vector<char> ans; ans = lcs(s, t); reverse(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); ++i) { cout << ans[i]; } cout << endl; return 0; }
replace
57
59
57
59
-11
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int arr[100005]; int dp[3005][3005]; int anss = 0; char rep[100005]; int c = 0; string str1, str2; int lcs(int first, int last) { if (first <= 0 | last <= 0) { return 0; } if (dp[first][last] != -1) return dp[first][last]; if (str1[first] == str2[last] && (first != str1.size() && last != str2.size())) { dp[first][last] = 1 + lcs(first - 1, last - 1); // anss = max(anss,dp[first][last]); } else { dp[first][last] = max(dp[first][last], max(lcs(first - 1, last), lcs(first, last - 1))); } return dp[first][last]; } void printstring() { int i = str1.size(); int j = str2.size(); // int idx = anss; int idx = lcs(str1.size() - 1, str2.size() - 1); char str[anss + 1]; str[idx + 1] = '\0'; while (i > 0 && j > 0) { if (str1[i] == str2[j]) { str[idx] = str1[i]; idx--; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else j--; } cout << str << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 0; i < 3006; i++) for (int j = 0; j < 3006; j++) dp[i][j] = -1; cin >> str1 >> str2; str1 = "X" + str1; str2 = "Y" + str2; string ans = ""; int n = min(str2.size(), str1.size()); int n1 = max(str2.size(), str1.size()); // lcs(str1.size()-1,str2.size()-1); printstring(); }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int arr[100005]; int dp[3005][3005]; int anss = 0; char rep[100005]; int c = 0; string str1, str2; int lcs(int first, int last) { if (first <= 0 | last <= 0) { return 0; } if (dp[first][last] != -1) return dp[first][last]; if (str1[first] == str2[last] && (first != str1.size() && last != str2.size())) { dp[first][last] = 1 + lcs(first - 1, last - 1); anss = max(anss, dp[first][last]); } else { dp[first][last] = max(dp[first][last], max(lcs(first - 1, last), lcs(first, last - 1))); } return dp[first][last]; } void printstring() { int i = str1.size(); int j = str2.size(); // int idx = anss; int idx = lcs(str1.size() - 1, str2.size() - 1); char str[anss + 1]; str[idx + 1] = '\0'; while (i > 0 && j > 0) { if (str1[i] == str2[j]) { str[idx] = str1[i]; idx--; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else j--; } cout << str << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 0; i < 3006; i++) for (int j = 0; j < 3006; j++) dp[i][j] = -1; cin >> str1 >> str2; str1 = "X" + str1; str2 = "Y" + str2; string ans = ""; int n = min(str2.size(), str1.size()); int n1 = max(str2.size(), str1.size()); // lcs(str1.size()-1,str2.size()-1); printstring(); }
replace
18
19
18
19
0
p03165
C++
Runtime Error
/* It's better to sleep happy and tired rather than disappointed and frustrated There's only 1 thing that's impossible and that is defeating a man who doesn't give up */ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mx2 102 #define mx3 1003 #define mx4 10004 #define mx5 100005 #define mx6 1000006 #define mod 1000000007 #define PI 3.14159265 #define deb(x) cout << #x << " " << x << endl; #define fo(i, n) for (int i = 0; i < n; i++) #define Fo(i, k, n) for (int i = k; i < n; i++) typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<pair<int, int>, int> piii; const int inf = 0x3f3f3f3f; int n, m, ans, sum, a, b, dp[3 * mx3][3 * mx3]; string s, t; string solve(int i, int j) { string res; if (i < 0 || j < 0) { return res; } if (s[i - 1] == t[j - 1]) { res = solve(i - 1, j - 1); res += s[i - 1]; return res; } else if (dp[i - 1][j] > dp[i][j - 1]) { res = solve(i - 1, j); return res; } else { res = solve(i, j - 1); return res; } } int main() { ios::sync_with_stdio(false); // int t;cin>>t;while(t--) { cin >> s >> t; n = s.length(); m = t.length(); Fo(i, 1, n + 1) { Fo(j, 1, m + 1) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } cout << solve(n, m); } return 0; }
/* It's better to sleep happy and tired rather than disappointed and frustrated There's only 1 thing that's impossible and that is defeating a man who doesn't give up */ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define pb push_back #define mx2 102 #define mx3 1003 #define mx4 10004 #define mx5 100005 #define mx6 1000006 #define mod 1000000007 #define PI 3.14159265 #define deb(x) cout << #x << " " << x << endl; #define fo(i, n) for (int i = 0; i < n; i++) #define Fo(i, k, n) for (int i = k; i < n; i++) typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<pair<int, int>, int> piii; const int inf = 0x3f3f3f3f; int n, m, ans, sum, a, b, dp[3 * mx3][3 * mx3]; string s, t; string solve(int i, int j) { string res; if (i <= 0 || j <= 0) { return res; } if (s[i - 1] == t[j - 1]) { res = solve(i - 1, j - 1); res += s[i - 1]; return res; } else if (dp[i - 1][j] > dp[i][j - 1]) { res = solve(i - 1, j); return res; } else { res = solve(i, j - 1); return res; } } int main() { ios::sync_with_stdio(false); // int t;cin>>t;while(t--) { cin >> s >> t; n = s.length(); m = t.length(); Fo(i, 1, n + 1) { Fo(j, 1, m + 1) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } cout << solve(n, m); } return 0; }
replace
32
33
32
33
0
p03165
C++
Runtime Error
/** * author: Atreus **/ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e3 + 10; int dp[maxn][maxn], par[maxn][maxn]; int main() { ios_base::sync_with_stdio(false); string s, t; cin >> s >> t; for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (dp[i - 1][j] >= dp[i][j - 1]) par[i][j] = 1; else par[i][j] = 2; if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; par[i][j] = 3; } } } int x = s.size(), y = t.size(); string ans; while (x > 0 and y > 0) { if (par[x][y] == 1) { x--; } else if (par[x][y] == 2) { y--; } else { ans += s[x - 1]; x--, y--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
/** * author: Atreus **/ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 3e3 + 10; int dp[maxn][maxn], par[maxn][maxn]; int main() { ios_base::sync_with_stdio(false); string s, t; cin >> s >> t; for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (dp[i - 1][j] >= dp[i][j - 1]) par[i][j] = 1; else par[i][j] = 2; if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; par[i][j] = 3; } } } int x = s.size(), y = t.size(); string ans; while (x > 0 and y > 0) { if (par[x][y] == 1) { x--; } else if (par[x][y] == 2) { y--; } else { ans += s[x - 1]; x--, y--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
8
9
8
9
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int dp[3001][3001]; int fun(string s1, string s2, int n, int m) { if (n == 0 || m == 0) return dp[n][m] = 0; else if (dp[n][m] != -1) return dp[n][m]; else if (s1[n - 1] == s2[m - 1]) return dp[n][m] = 1 + fun(s1, s2, n - 1, m - 1); else return dp[n][m] = max(fun(s1, s2, n - 1, m), fun(s1, s2, n, m - 1)); } int main() { string s1, s2; cin >> s1 >> s2; int i, j, n = s1.length(), m = s2.length(); for (i = 0; i <= n; i++) { for (j = 0; j <= m; j++) dp[i][j] = -1; } fun(s1, s2, n, m); i = n, j = m; string ans; while (i > 0 && j > 0) { if (s1[i - 1] == s2[j - 1]) { ans.push_back(s1[i - 1]); i--; j--; } else { if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int dp[3001][3001]; int fun(string s1, string s2, int n, int m) { if (n == 0 || m == 0) return dp[n][m] = 0; else if (dp[n][m] != -1) return dp[n][m]; else if (s1[n - 1] == s2[m - 1]) return dp[n][m] = 1 + fun(s1, s2, n - 1, m - 1); else return dp[n][m] = max(fun(s1, s2, n - 1, m), fun(s1, s2, n, m - 1)); } int main() { string s1, s2; cin >> s1 >> s2; int i, j, n = s1.length(), m = s2.length(); for (i = 0; i <= n; i++) { for (j = 0; j <= m; j++) dp[i][j] = -1; } // fun(s1,s2,n,m); for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (s1[i - 1] == s2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } i = n, j = m; string ans; while (i > 0 && j > 0) { if (s1[i - 1] == s2[j - 1]) { ans.push_back(s1[i - 1]); i--; j--; } else { if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
21
22
21
30
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define mk make_pair #define fi first #define se second #define vll vector<ll> #define pii pair<ll, ll> #define vvll vector<vector<ll>> #define pb push_back #define sz(v) (v).size() #define inf 1e18 #define md 1000000007 #define all(v) (v).begin(), (v).end() #define rep(i, a, b) for (ll i = a; i < b; ++i) #define repi(i, a, b) for (ll i = a; i >= b; --i) #define tel(a) \ { cout << a << "\n"; } #define tell(a, b) \ { cout << a << " | " << b << "\n"; } #define telll(a, b, c) \ { cout << a << " | " << b << " | " << c << "\n"; } #define teln(v, n) \ { \ cout << "v- "; \ rep(i, 0, n) cout << v[i] << " "; \ cout << "\n"; \ } #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace std; #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif #define M 110 #define N 1010 string s, t, ans; ll dp[N][N]; ll f(ll i, ll j) { if (!i or !j) return 0; if (dp[i][j] != -1) return dp[i][j]; ll ans = 0; if (s[i - 1] == t[j - 1]) ans = f(i - 1, j - 1) + 1; else ans = max(f(i - 1, j), f(i, j - 1)); return dp[i][j] = ans; } void find(ll i, ll j) { if (!i or !j) return; if (s[i - 1] == t[j - 1]) ans += s[i - 1], find(i - 1, j - 1); else { if (f(i - 1, j) > f(i, j - 1)) find(i - 1, j); else find(i, j - 1); } } int main() { IOS; // freopen("inp.txt", "r", stdin); // freopen("out.txt", "w", stdout); cin >> s >> t; memset(dp, -1, sizeof(dp)); ans = ""; ll n = sz(s), m = sz(t); find(n, m); ll x = sz(ans); if (x) repi(i, x - 1, 0) cout << ans[i]; return 0; }
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double #define mk make_pair #define fi first #define se second #define vll vector<ll> #define pii pair<ll, ll> #define vvll vector<vector<ll>> #define pb push_back #define sz(v) (v).size() #define inf 1e18 #define md 1000000007 #define all(v) (v).begin(), (v).end() #define rep(i, a, b) for (ll i = a; i < b; ++i) #define repi(i, a, b) for (ll i = a; i >= b; --i) #define tel(a) \ { cout << a << "\n"; } #define tell(a, b) \ { cout << a << " | " << b << "\n"; } #define telll(a, b, c) \ { cout << a << " | " << b << " | " << c << "\n"; } #define teln(v, n) \ { \ cout << "v- "; \ rep(i, 0, n) cout << v[i] << " "; \ cout << "\n"; \ } #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace std; #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif #define M 110 #define N 3010 string s, t, ans; ll dp[N][N]; ll f(ll i, ll j) { if (!i or !j) return 0; if (dp[i][j] != -1) return dp[i][j]; ll ans = 0; if (s[i - 1] == t[j - 1]) ans = f(i - 1, j - 1) + 1; else ans = max(f(i - 1, j), f(i, j - 1)); return dp[i][j] = ans; } void find(ll i, ll j) { if (!i or !j) return; if (s[i - 1] == t[j - 1]) ans += s[i - 1], find(i - 1, j - 1); else { if (f(i - 1, j) > f(i, j - 1)) find(i - 1, j); else find(i, j - 1); } } int main() { IOS; // freopen("inp.txt", "r", stdin); // freopen("out.txt", "w", stdout); cin >> s >> t; memset(dp, -1, sizeof(dp)); ans = ""; ll n = sz(s), m = sz(t); find(n, m); ll x = sz(ans); if (x) repi(i, x - 1, 0) cout << ans[i]; return 0; }
replace
53
54
53
54
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #include <unordered_map> #define inputarr(a, n) \ for (ll i = 0; i < n; i++) \ cin >> a[i]; #define prllarr(a, n) \ for (ll i = 0; i < n; i++) \ cout << a[i] << " "; #define pb push_back #define ll long long #define mod 1000000007 #define foi \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(0); #define in(n) scanf("%lld", &n); #define in2(x, y) scanf("%lld %lld", &(x), &(y)); #define in3(x, y, z) scanf("%lld %lld %lld", &(x), &(y), &(z)); #define out(n) printf("%lld\n", n); #define out2(x, y) printf("%lld %lld\n", x, y); #define test(t) \ ll t; \ in(t); \ while (t--) #define set(arr, n, s) \ for (ll i = 0; i < n; i++) { \ arr[i] = s; \ } /*error----- convert every int to long long eg-1LL create array with proper analysis of problem constrain check mod also */ ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } ll modInverse(ll a, ll p) { return power(a, p - 2, p); } // used with feemat little ll gcd(ll x, ll y) { if (x == 0 || y == 0) { return max(y, x); } return gcd(y % x, x); } ll gcdExtended(ll a, ll b, ll &x, ll &y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll gcd = gcdExtended(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } // o(log(b)) // ll ncr(ll n,ll r,ll p){if(r==0){return 1;}return ((((fact[n]* inv[r]) % p) * // inv[n-r]) % p);} ll dp[3005][3005]; int main() { string s, t; cin >> s >> t; for (ll i = 1; i <= s.size(); i++) { for (ll j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } ll i = s.size(); ll j = t.size(); string ans = ""; while (i != 0 || j != 0) { if (s[i - 1] == t[j - 1]) { ans += s[i - 1]; i--; j--; } else { if (dp[i][j] == dp[i - 1][j]) { i--; } else { j--; } } } for (ll i = ans.size() - 1; i >= 0; i--) { cout << ans[i]; } }
#include <bits/stdc++.h> using namespace std; #include <unordered_map> #define inputarr(a, n) \ for (ll i = 0; i < n; i++) \ cin >> a[i]; #define prllarr(a, n) \ for (ll i = 0; i < n; i++) \ cout << a[i] << " "; #define pb push_back #define ll long long #define mod 1000000007 #define foi \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(0); #define in(n) scanf("%lld", &n); #define in2(x, y) scanf("%lld %lld", &(x), &(y)); #define in3(x, y, z) scanf("%lld %lld %lld", &(x), &(y), &(z)); #define out(n) printf("%lld\n", n); #define out2(x, y) printf("%lld %lld\n", x, y); #define test(t) \ ll t; \ in(t); \ while (t--) #define set(arr, n, s) \ for (ll i = 0; i < n; i++) { \ arr[i] = s; \ } /*error----- convert every int to long long eg-1LL create array with proper analysis of problem constrain check mod also */ ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } ll modInverse(ll a, ll p) { return power(a, p - 2, p); } // used with feemat little ll gcd(ll x, ll y) { if (x == 0 || y == 0) { return max(y, x); } return gcd(y % x, x); } ll gcdExtended(ll a, ll b, ll &x, ll &y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll gcd = gcdExtended(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } // o(log(b)) // ll ncr(ll n,ll r,ll p){if(r==0){return 1;}return ((((fact[n]* inv[r]) % p) * // inv[n-r]) % p);} ll dp[3005][3005]; int main() { string s, t; cin >> s >> t; for (ll i = 1; i <= s.size(); i++) { for (ll j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } ll i = s.size(); ll j = t.size(); string ans = ""; while (i != 0 && j != 0) { if (s[i - 1] == t[j - 1]) { ans += s[i - 1]; i--; j--; } else { if (dp[i][j] == dp[i - 1][j]) { i--; } else { j--; } } } for (ll i = ans.size() - 1; i >= 0; i--) { cout << ans[i]; } }
replace
88
89
88
89
0
p03165
C++
Runtime Error
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; template <class T> using V = vector<T>; #define fi first #define se second #define all(v) (v).begin(), (v).end() const ll inf = (1e18); const ll mod = 1000000007; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll c, ll d) { return c / gcd(c, d) * d; } struct __INIT { __INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } __init; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } int dp[1005][1005]; int main() { string s, t; cin >> s >> t; int n = s.size(), m = t.size(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i] == t[j]) chmax(dp[i + 1][j + 1], dp[i][j] + 1); else dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); } } int d = dp[n][m]; string ans = {}; while (1) { if (d == 0) break; if (n > 0 && dp[n - 1][m] == d) n--; else if (m > 0 && dp[n][m - 1] == d) m--; else if (n > 0 && m > 0) { n--; m--; d--; ans.push_back(s[n]); } } reverse(all(ans)); cout << ans << endl; }
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; template <class T> using V = vector<T>; #define fi first #define se second #define all(v) (v).begin(), (v).end() const ll inf = (1e18); const ll mod = 1000000007; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll c, ll d) { return c / gcd(c, d) * d; } struct __INIT { __INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } __init; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } int dp[3005][3005]; int main() { string s, t; cin >> s >> t; int n = s.size(), m = t.size(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i] == t[j]) chmax(dp[i + 1][j + 1], dp[i][j] + 1); else dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); } } int d = dp[n][m]; string ans = {}; while (1) { if (d == 0) break; if (n > 0 && dp[n - 1][m] == d) n--; else if (m > 0 && dp[n][m - 1] == d) m--; else if (n > 0 && m > 0) { n--; m--; d--; ans.push_back(s[n]); } } reverse(all(ans)); cout << ans << endl; }
replace
34
35
34
35
0
p03165
C++
Runtime Error
/*------U Have To DO It------*/ /* BY-> RicoProg */ /* ___ __|__| | |___ */ #include <bits/stdc++.h> using namespace std; //---------------------------------------------------MACROS---------------------------------------------------------- #define ll long long #define beg(i, n) for (int i = 0; i < n; i++) #define beg1(i, n) for (int i = 1; i < n; i++) #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL) #define pb push_back #define mp make_pair #define nl "\n" #define vec(v, n) vector<ll> v(n) #define all(x) x.begin(), x.end() #define pii pair<int, int> #define pll pair<ll, ll> #define mii map<int, int> #define mll map<ll, ll> #define msi map<string, int> #define mci map<char, int> #define f1 first #define f2 second //---------------------------------------------------GLOBAL---------------------------------------------------------- const ll MOD = 998244353; const ll MAX = 1e5 + 7; //---------------------------------------------------FUNCTIONS------------------------------------------------------- struct node { ll n, x, y; bool st; }; node dp[3001][3001]; string s, t; void rec(ll x, ll y) { if (x < 0 || y < 0) { return; } else if (dp[x][y].n != -1) { return; } else if (s[x] == t[y]) { // cout << x << " A " << y << nl ; rec(x - 1, y - 1); if (x - 1 >= 0 && y - 1 >= 0) dp[x][y].n = 1 + dp[x - 1][y - 1].n; else dp[x][y].n = 1 + 0; dp[x][y].st = true; dp[x][y].x = x - 1, dp[x][y].y = y - 1; return; } else { // cout << x << " B " << y << nl ; rec(x - 1, y); rec(x, y - 1); ll a = (x - 1 > -1 && y > -1) * (dp[x - 1][y].n); ll b = (x > -1 && y - 1 > -1) * (dp[x][y - 1].n); dp[x][y].st = false; if (a > b) dp[x][y].x = x - 1, dp[x][y].y = y, dp[x][y].n = a; else dp[x][y].x = x, dp[x][y].y = y - 1, dp[x][y].n = b; return; } return; } void solve() { beg(i, 3001) { beg(j, 3001) dp[i][j].n = -1, dp[i][j].st = false; } cin >> s >> t; rec(s.length() - 1, t.length() - 1); string ans = ""; ll x_n = s.length() - 1, y_n = t.length() - 1, x_t, y_t; while (true) { // cout << x_n << " " << y_n << nl ; if (x_n < 0 || y_n < 0) { break; } x_t = x_n, y_t = y_n; x_n = dp[x_t][y_t].x; y_n = dp[x_t][y_t].y; if (dp[x_t][y_t].st) ans += s[x_t]; } // cout << dp[s.length()-1][t.length()-1].n << nl ; reverse(all(ans)); cout << ans; } //---------------------------------------------------DRIVER---------------------------------------------------------- int main() { fast; int t = 1; // cin >> t ; while (t--) { solve(); } return 0; }
/*------U Have To DO It------*/ /* BY-> RicoProg */ /* ___ __|__| | |___ */ #include <bits/stdc++.h> using namespace std; //---------------------------------------------------MACROS---------------------------------------------------------- #define ll long long #define beg(i, n) for (int i = 0; i < n; i++) #define beg1(i, n) for (int i = 1; i < n; i++) #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL) #define pb push_back #define mp make_pair #define nl "\n" #define vec(v, n) vector<ll> v(n) #define all(x) x.begin(), x.end() #define pii pair<int, int> #define pll pair<ll, ll> #define mii map<int, int> #define mll map<ll, ll> #define msi map<string, int> #define mci map<char, int> #define f1 first #define f2 second //---------------------------------------------------GLOBAL---------------------------------------------------------- const ll MOD = 998244353; const ll MAX = 1e5 + 7; //---------------------------------------------------FUNCTIONS------------------------------------------------------- struct node { ll n, x, y; bool st; }; node dp[3001][3001]; string s, t; void rec(ll x, ll y) { if (x < 0 || y < 0) { return; } else if (dp[x][y].n != -1) { return; } else if (s[x] == t[y]) { // cout << x << " A " << y << nl ; rec(x - 1, y - 1); if (x - 1 >= 0 && y - 1 >= 0) dp[x][y].n = 1 + dp[x - 1][y - 1].n; else dp[x][y].n = 1 + 0; dp[x][y].st = true; dp[x][y].x = x - 1, dp[x][y].y = y - 1; return; } else { // cout << x << " B " << y << nl ; rec(x - 1, y); rec(x, y - 1); ll a, b; a = b = 0; if (x - 1 > -1) a = dp[x - 1][y].n; if (y - 1 > -1) b = dp[x][y - 1].n; dp[x][y].st = false; if (a > b) dp[x][y].x = x - 1, dp[x][y].y = y, dp[x][y].n = a; else dp[x][y].x = x, dp[x][y].y = y - 1, dp[x][y].n = b; return; } return; } void solve() { beg(i, 3001) { beg(j, 3001) dp[i][j].n = -1, dp[i][j].st = false; } cin >> s >> t; rec(s.length() - 1, t.length() - 1); string ans = ""; ll x_n = s.length() - 1, y_n = t.length() - 1, x_t, y_t; while (true) { // cout << x_n << " " << y_n << nl ; if (x_n < 0 || y_n < 0) { break; } x_t = x_n, y_t = y_n; x_n = dp[x_t][y_t].x; y_n = dp[x_t][y_t].y; if (dp[x_t][y_t].st) ans += s[x_t]; } // cout << dp[s.length()-1][t.length()-1].n << nl ; reverse(all(ans)); cout << ans; } //---------------------------------------------------DRIVER---------------------------------------------------------- int main() { fast; int t = 1; // cin >> t ; while (t--) { solve(); } return 0; }
replace
62
64
62
68
-11
p03165
C++
Runtime Error
#include <cstdio> #include <cstring> #include <iostream> using namespace std; string s, t; int f[3001][3001]; int main() { cin >> s >> t; for (int i = s.size(); i >= 0; i--) { for (int j = t.size(); j >= 0; j--) { if (s[i] == t[j]) { f[i][j] = f[i + 1][j + 1] + 1; continue; } f[i][j] = max(f[i][j + 1], f[i + 1][j]); } } int h = 0, k = 0; while (h < s.size() && k < t.size()) { if (s[h] == t[k]) { printf("%c", s[h]); h++; k++; continue; } if (f[h][k] == f[h + 1][k]) { h++; continue; } k++; } }
#include <cstdio> #include <cstring> #include <iostream> using namespace std; string s, t; int f[3100][3100]; int main() { cin >> s >> t; for (int i = s.size(); i >= 0; i--) { for (int j = t.size(); j >= 0; j--) { if (s[i] == t[j]) { f[i][j] = f[i + 1][j + 1] + 1; continue; } f[i][j] = max(f[i][j + 1], f[i + 1][j]); } } int h = 0, k = 0; while (h < s.size() && k < t.size()) { if (s[h] == t[k]) { printf("%c", s[h]); h++; k++; continue; } if (f[h][k] == f[h + 1][k]) { h++; continue; } k++; } }
replace
5
6
5
6
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll unsigned long long int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string A; string B; cin >> A; cin >> B; int n = A.length(); int m = B.length(); int dp[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { dp[i][j] = 0; } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else { if (A[i - 1] == B[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int ans = dp[n][m]; map<int, int> check; vector<char> res; for (int i = n; i >= 1; i--) { for (int j = m; j >= 1; j--) { if (A[i - 1] == B[j - 1] && dp[i][j] == ans && check[i - 1] == 0) { res.push_back(A[i - 1]); check[i - 1]++; ans--; } if (ans == 0) break; } } for (int i = res.size() - 1; i >= 0; i--) { cout << res[i]; } }
#include <bits/stdc++.h> using namespace std; #define ll unsigned long long int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string A; string B; cin >> A; cin >> B; int n = A.length(); int m = B.length(); int dp[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { dp[i][j] = 0; } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else { if (A[i - 1] == B[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int ans = dp[n][m]; map<int, int> check; vector<char> res; for (int i = n; i >= 1; i--) { for (int j = m; j >= 1; j--) { if (A[i - 1] == B[j - 1] && dp[i][j] == ans && check[i - 1] == 0) { res.push_back(A[i - 1]); check[i - 1]++; ans--; } if (ans == 0) break; } } for (int i = res.size() - 1; i >= 0; i--) { cout << res[i]; } }
replace
15
18
15
18
0
p03165
C++
Time Limit Exceeded
#include "bits/stdc++.h" #define fio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define all(V) (V).begin(), (V).end() #define x first #define y second using namespace std; typedef long long ll; template <typename T> using V = vector<T>; int main() { fio; string s, t; cin >> s >> t; V<V<int>> DP(s.size(), V<int>(t.size(), 0)); V<V<pair<int, int>>> prev(s.size(), V<pair<int, int>>(t.size())); for (int si = 0; si < s.size(); si++) { for (int ti = 0; ti < t.size(); ti++) { if (s[si] == t[ti]) { if (si == 0 || ti == 0) { DP[si][ti] = 1; prev[si][ti] = {-1, -1}; } else { DP[si][ti] = 1 + DP[si - 1][ti - 1]; prev[si][ti] = {si - 1, ti - 1}; } } else { if (si) { DP[si][ti] = max(DP[si][ti], DP[si - 1][ti]); if (DP[si][ti] == DP[si - 1][ti]) { prev[si][ti] = {si - 1, ti}; } } if (ti) { DP[si][ti] = max(DP[si][ti], DP[si][ti - 1]); if (DP[si][ti] == DP[si][ti - 1]) { prev[si][ti] = {si, ti - 1}; } } } } } string ans; pair<int, int> cur = {s.size() - 1, t.size() - 1}; while (cur.first != -1) { if (s[cur.x] == t[cur.y]) { ans.push_back(s[cur.x]); } cur = prev[cur.x][cur.y]; } reverse(all(ans)); cout << ans; return 0; }
#include "bits/stdc++.h" #define fio \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define all(V) (V).begin(), (V).end() #define x first #define y second using namespace std; typedef long long ll; template <typename T> using V = vector<T>; int main() { fio; string s, t; cin >> s >> t; V<V<int>> DP(s.size(), V<int>(t.size(), 0)); V<V<pair<int, int>>> prev(s.size(), V<pair<int, int>>(t.size())); for (int si = 0; si < s.size(); si++) { for (int ti = 0; ti < t.size(); ti++) { if (s[si] == t[ti]) { if (si == 0 || ti == 0) { DP[si][ti] = 1; prev[si][ti] = {-1, -1}; } else { DP[si][ti] = 1 + DP[si - 1][ti - 1]; prev[si][ti] = {si - 1, ti - 1}; } } else { prev[si][ti] = {-1, -1}; if (si) { DP[si][ti] = max(DP[si][ti], DP[si - 1][ti]); if (DP[si][ti] == DP[si - 1][ti]) { prev[si][ti] = {si - 1, ti}; } } if (ti) { DP[si][ti] = max(DP[si][ti], DP[si][ti - 1]); if (DP[si][ti] == DP[si][ti - 1]) { prev[si][ti] = {si, ti - 1}; } } } } } string ans; pair<int, int> cur = {s.size() - 1, t.size() - 1}; while (cur.first != -1) { if (s[cur.x] == t[cur.y]) { ans.push_back(s[cur.x]); } cur = prev[cur.x][cur.y]; } reverse(all(ans)); cout << ans; return 0; }
insert
32
32
32
33
TLE
p03165
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> #define ff first #define ss second using namespace std; using pii = pair<int, int>; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); string s, t; cin >> s >> t; vector<vector<pair<int, pii>>> dp( s.length(), vector<pair<int, pii>>(t.length(), {0, {-1, -1}})); if (s[0] == t[0]) dp[0][0] = {1, {0, 0}}; for (int i = 0; i < s.length(); i++) { for (int j = 0; j < t.length(); j++) { if (i + j == 0) continue; if (i > 0 && dp[i][j].ff < dp[i - 1][j].ff) dp[i][j] = dp[i - 1][j]; if (j > 0 && dp[i][j].ff < dp[i][j - 1].ff) dp[i][j] = dp[i][j - 1]; if (s[i] == t[j]) { if (i > 0 && j > 0) dp[i][j].ff = dp[i - 1][j - 1].ff + 1; else dp[i][j].ff = 1; dp[i][j].ss = {i, j}; } } } pii now = {s.length() - 1, t.length() - 1}; string answer; while (now.ff != -1 && now.ss != -1) { now = dp[now.ff][now.ss].ss; answer += s[now.ff]; now.ff -= 1; now.ss -= 1; } reverse(answer.begin(), answer.end()); cout << answer; return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> #define ff first #define ss second using namespace std; using pii = pair<int, int>; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); string s, t; cin >> s >> t; vector<vector<pair<int, pii>>> dp( s.length(), vector<pair<int, pii>>(t.length(), {0, {-1, -1}})); if (s[0] == t[0]) dp[0][0] = {1, {0, 0}}; for (int i = 0; i < s.length(); i++) { for (int j = 0; j < t.length(); j++) { if (i + j == 0) continue; if (i > 0 && dp[i][j].ff < dp[i - 1][j].ff) dp[i][j] = dp[i - 1][j]; if (j > 0 && dp[i][j].ff < dp[i][j - 1].ff) dp[i][j] = dp[i][j - 1]; if (s[i] == t[j]) { if (i > 0 && j > 0) dp[i][j].ff = dp[i - 1][j - 1].ff + 1; else dp[i][j].ff = 1; dp[i][j].ss = {i, j}; } } } pii now = {s.length() - 1, t.length() - 1}; string answer; while (now.ff != -1 && now.ss != -1) { now = dp[now.ff][now.ss].ss; if (now.ff == -1 || now.ss == -1) break; answer += s[now.ff]; now.ff -= 1; now.ss -= 1; } reverse(answer.begin(), answer.end()); cout << answer; return 0; }
insert
55
55
55
57
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; const int N = (1e3) + 3; int memo[N][N]; string s1, s2; int n, m; void lcs(int n, int m) { for (int i = 0; i <= n; ++i) { memo[i][0] = 0; } for (int i = 0; i <= m; ++i) { memo[0][i] = 0; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (s1[i - 1] == s2[j - 1]) { memo[i][j] = 1 + memo[i - 1][j - 1]; } else { memo[i][j] = max(memo[i - 1][j], memo[i][j - 1]); } } } } void printLcs(int n, int m) { if (n == 0 or m == 0) return; if (s1[n - 1] == s2[m - 1]) { printLcs(n - 1, m - 1); printf("%c", s1[n - 1]); } else { if (memo[n][m - 1] > memo[n - 1][m]) printLcs(n, m - 1); else printLcs(n - 1, m); } } int main() { getline(cin, s1); getline(cin, s2); int n = s1.size(); int m = s2.size(); lcs(n, m); printLcs(n, m); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; const int N = (3e3) + 3; int memo[N][N]; string s1, s2; int n, m; void lcs(int n, int m) { for (int i = 0; i <= n; ++i) { memo[i][0] = 0; } for (int i = 0; i <= m; ++i) { memo[0][i] = 0; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (s1[i - 1] == s2[j - 1]) { memo[i][j] = 1 + memo[i - 1][j - 1]; } else { memo[i][j] = max(memo[i - 1][j], memo[i][j - 1]); } } } } void printLcs(int n, int m) { if (n == 0 or m == 0) return; if (s1[n - 1] == s2[m - 1]) { printLcs(n - 1, m - 1); printf("%c", s1[n - 1]); } else { if (memo[n][m - 1] > memo[n - 1][m]) printLcs(n, m - 1); else printLcs(n - 1, m); } } int main() { getline(cin, s1); getline(cin, s2); int n = s1.size(); int m = s2.size(); lcs(n, m); printLcs(n, m); return 0; }
replace
6
7
6
7
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define re reverse using namespace std; signed main() { string b, c, a, d; cin >> b >> c; int lcs[1005][1005] = {}; for (int j = 1; j <= b.size(); j++) { for (int k = 1; k <= c.size(); k++) { if (b[j - 1] == c[k - 1]) lcs[j][k] = lcs[j - 1][k - 1] + 1; else lcs[j][k] = max(lcs[j - 1][k], lcs[j][k - 1]); } } int x = c.size(), y = b.size(); while (true) { if (x == 0 || y == 0) break; if (lcs[y - 1][x] == lcs[y][x - 1] && lcs[y - 1][x - 1] == lcs[y - 1][x] && lcs[y][x] > lcs[y][x - 1]) { a += c[x - 1]; x--, y--; } else { if (lcs[y - 1][x] > lcs[y][x - 1]) y--; else x--; } } reverse(a.begin(), a.end()); cout << a << "\n"; }
#include <bits/stdc++.h> #define re reverse using namespace std; signed main() { string b, c, a, d; cin >> b >> c; int lcs[3005][3005] = {}; for (int j = 1; j <= b.size(); j++) { for (int k = 1; k <= c.size(); k++) { if (b[j - 1] == c[k - 1]) lcs[j][k] = lcs[j - 1][k - 1] + 1; else lcs[j][k] = max(lcs[j - 1][k], lcs[j][k - 1]); } } int x = c.size(), y = b.size(); while (true) { if (x == 0 || y == 0) break; if (lcs[y - 1][x] == lcs[y][x - 1] && lcs[y - 1][x - 1] == lcs[y - 1][x] && lcs[y][x] > lcs[y][x - 1]) { a += c[x - 1]; x--, y--; } else { if (lcs[y - 1][x] > lcs[y][x - 1]) y--; else x--; } } reverse(a.begin(), a.end()); cout << a << "\n"; }
replace
6
7
6
7
0
p03165
C++
Runtime Error
#include <algorithm> #include <bitset> #include <chrono> #include <cstdio> #include <fstream> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, x, y) for (ll i = x; i <= y; i++) #define SIZE(a) ll(a.size()) #define vll vector<ll> #define MEMSET(a, n, m) \ for (ll i = 0; i <= n; i++) \ a[i] = m #define BIT(n) (ll(1) << n) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define UNIQUE_ARRAY(a, x) unique(a + 1, a + x + 1) - a - 1 #define SORT(a, n) sort(a + 1, a + n + 1) #define SORT_O(a, n, order) sort(a + 1, a + n + 1, order) #define PER(i, y, x) for (ll i = y; i >= x; i--) typedef long long ll; using namespace std; struct rect { ll hei; ll pos; }; ll const MAX = 1e3 + 3; ll dp[MAX][MAX] = {}; string s, t; vector<char> ans; int main() { cin >> s >> t; ll n = s.size(); ll m = t.size(); REP(i, 1, n) { REP(j, 1, m) { if (s[i - 1] != t[j - 1]) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } else { dp[i][j] = max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + 1}); } } } ll i = n; ll j = m; while (i != 0 && j != 0) { if (s[i - 1] == t[j - 1] && dp[i][j] - dp[i - 1][j - 1] == 1) { ans.push_back(s[i - 1]); i--; j--; } else { if (dp[i][j] == dp[i - 1][j]) { i--; } else { j--; } } } PER(iii, SIZE(ans) - 1, 0) { cout << ans[iii]; } cout << endl; }
#include <algorithm> #include <bitset> #include <chrono> #include <cstdio> #include <fstream> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define REP(i, x, y) for (ll i = x; i <= y; i++) #define SIZE(a) ll(a.size()) #define vll vector<ll> #define MEMSET(a, n, m) \ for (ll i = 0; i <= n; i++) \ a[i] = m #define BIT(n) (ll(1) << n) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) #define UNIQUE_ARRAY(a, x) unique(a + 1, a + x + 1) - a - 1 #define SORT(a, n) sort(a + 1, a + n + 1) #define SORT_O(a, n, order) sort(a + 1, a + n + 1, order) #define PER(i, y, x) for (ll i = y; i >= x; i--) typedef long long ll; using namespace std; struct rect { ll hei; ll pos; }; ll const MAX = 3e3 + 3; ll dp[MAX][MAX] = {}; string s, t; vector<char> ans; int main() { cin >> s >> t; ll n = s.size(); ll m = t.size(); REP(i, 1, n) { REP(j, 1, m) { if (s[i - 1] != t[j - 1]) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } else { dp[i][j] = max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + 1}); } } } ll i = n; ll j = m; while (i != 0 && j != 0) { if (s[i - 1] == t[j - 1] && dp[i][j] - dp[i - 1][j - 1] == 1) { ans.push_back(s[i - 1]); i--; j--; } else { if (dp[i][j] == dp[i - 1][j]) { i--; } else { j--; } } } PER(iii, SIZE(ans) - 1, 0) { cout << ans[iii]; } cout << endl; }
replace
33
34
33
34
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e3 + 5; const int INF = 1e9 + 7; #define ll long long #define pb push_back #define endl '\n' #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define F first #define S second ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } ll MOD(ll a) { return ((a % INF) + INF) % INF; } ll inv(ll a) { return poww(a, INF - 2, INF); } char a[MAXN][MAXN]; ll dp[MAXN][MAXN]; pair<int, int> par[MAXN][MAXN]; int main() { fast_io; // cout << fixed << setprecision(15); string s, t; cin >> s >> t; s = "." + s; t = "." + t; for (int i = 1; i < s.size(); ++i) { for (int j = 1; j < t.size(); ++j) { int first = dp[i - 1][j], second = dp[i][j - 1], third = 0; if (s[i] == t[j]) third = 1 + dp[i - 1][j - 1]; dp[i][j] = max(first, max(second, third)); if (dp[i][j] == first) par[i][j] = {i - 1, j}; else if (dp[i][j] == second) par[i][j] = {i, j - 1}; else par[i][j] = {i - 1, j - 1}; } } string ans; pair<int, int> now; now.F = s.size() - 1, now.S = t.size() - 1; while (true) { pair<int, int> pars = par[now.F][now.S]; if (pars.F == now.F - 1 && pars.S == now.S - 1) ans += s[now.F]; now.F = pars.F, now.S = pars.S; if (pars.F == 0 || pars.S == 0) break; } reverse(ans.begin(), ans.end()); cout << ans; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e3 + 5; const int INF = 1e9 + 7; #define ll long long #define pb push_back #define endl '\n' #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define F first #define S second ll poww(ll a, ll b, ll md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } ll MOD(ll a) { return ((a % INF) + INF) % INF; } ll inv(ll a) { return poww(a, INF - 2, INF); } char a[MAXN][MAXN]; ll dp[MAXN][MAXN]; pair<int, int> par[MAXN][MAXN]; int main() { fast_io; // cout << fixed << setprecision(15); string s, t; cin >> s >> t; s = "." + s; t = "." + t; for (int i = 1; i < s.size(); ++i) { for (int j = 1; j < t.size(); ++j) { int first = dp[i - 1][j], second = dp[i][j - 1], third = 0; if (s[i] == t[j]) third = 1 + dp[i - 1][j - 1]; dp[i][j] = max(first, max(second, third)); if (dp[i][j] == first) par[i][j] = {i - 1, j}; else if (dp[i][j] == second) par[i][j] = {i, j - 1}; else par[i][j] = {i - 1, j - 1}; } } string ans; pair<int, int> now; now.F = s.size() - 1, now.S = t.size() - 1; while (true) { pair<int, int> pars = par[now.F][now.S]; if (pars.F == now.F - 1 && pars.S == now.S - 1) ans += s[now.F]; now.F = pars.F, now.S = pars.S; if (pars.F == 0 || pars.S == 0) break; } reverse(ans.begin(), ans.end()); cout << ans; }
replace
3
4
3
4
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' #define vi vector<int> #define vb vector<bool> #define pii pair<int, int> #define mod 1000000007 #define ss second #define ff first #define vpii vector<pii> #define vvi vector<vi> #define pb push_back #define vs vector<string> int power(int a, int n) { int res = 1; while (n) { if (n % 2) res *= a; a *= a; n /= 2; } return res; } int func(int i, int j, string &s, string &t, vvi &dp) { if (i == -1 || j == -1) return 0; if (!dp[i][j]) return dp[i][j]; else if (s[i] == t[j]) { dp[i][j] = func(i - 1, j - 1, s, t, dp) + 1; return dp[i][j]; } else { dp[i][j] = max(func(i, j - 1, s, t, dp), func(i - 1, j, s, t, dp)); return dp[i][j]; } } void reverse(string str) { for (int i = str.length() - 1; i >= 0; i--) cout << str[i]; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s, t; cin >> s >> t; vvi dp(s.size(), vi(t.size(), -1)); int n = s.size(), m = t.size(); func(n - 1, m - 1, s, t, dp); int i = n - 1, j = m - 1; string s1 = ""; while (i >= 0 && j >= 0) { if (s[i] == t[j]) { s1 += s[i]; if (i == 0 && j == 0) break; i--; j--; } else { if (i == 0 && j == 0) break; if (i > 0 && dp[i][j] == dp[i - 1][j]) { i--; } else if (j > 0 && dp[i][j] == dp[i][j - 1]) { j--; } } } reverse(s1); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' #define vi vector<int> #define vb vector<bool> #define pii pair<int, int> #define mod 1000000007 #define ss second #define ff first #define vpii vector<pii> #define vvi vector<vi> #define pb push_back #define vs vector<string> int power(int a, int n) { int res = 1; while (n) { if (n % 2) res *= a; a *= a; n /= 2; } return res; } int func(int i, int j, string &s, string &t, vvi &dp) { if (i == -1 || j == -1) return 0; if (dp[i][j] != -1) return dp[i][j]; else if (s[i] == t[j]) { dp[i][j] = func(i - 1, j - 1, s, t, dp) + 1; return dp[i][j]; } else { dp[i][j] = max(func(i, j - 1, s, t, dp), func(i - 1, j, s, t, dp)); return dp[i][j]; } } void reverse(string str) { for (int i = str.length() - 1; i >= 0; i--) cout << str[i]; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s, t; cin >> s >> t; vvi dp(s.size(), vi(t.size(), -1)); int n = s.size(), m = t.size(); func(n - 1, m - 1, s, t, dp); int i = n - 1, j = m - 1; string s1 = ""; while (i >= 0 && j >= 0) { if (s[i] == t[j]) { s1 += s[i]; if (i == 0 && j == 0) break; i--; j--; } else { if (i == 0 && j == 0) break; if (i > 0 && dp[i][j] == dp[i - 1][j]) { i--; } else if (j > 0 && dp[i][j] == dp[i][j - 1]) { j--; } } } reverse(s1); return 0; }
replace
27
28
27
28
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int dp[100][100]; string getLCS(string &s, string &t, int len) { int i = 0, j = 0; string LCS; while (len > 0) { if (s[i] == t[j]) { LCS.push_back(s[i]); i++; j++; len--; } else { if (dp[i][j + 1] > dp[i + 1][j]) j++; else i++; } } return LCS; } int lenLCS(string &s, string &t, int i, int j) // topdown lcs { if (i >= s.length() || j >= t.length()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (s[i] == t[j]) { dp[i][j] = 1 + lenLCS(s, t, i + 1, j + 1); return dp[i][j]; } else { dp[i][j] = max(lenLCS(s, t, i + 1, j), lenLCS(s, t, i, j + 1)); return dp[i][j]; } } string solve(string s, string t) { memset(dp, -1, sizeof dp); int len = lenLCS(s, t, 0, 0); return getLCS(s, t, len); } int main() { string s, t; cin >> s >> t; cout << solve(s, t) << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[3005][3005]; string getLCS(string &s, string &t, int len) { int i = 0, j = 0; string LCS; while (len > 0) { if (s[i] == t[j]) { LCS.push_back(s[i]); i++; j++; len--; } else { if (dp[i][j + 1] > dp[i + 1][j]) j++; else i++; } } return LCS; } int lenLCS(string &s, string &t, int i, int j) // topdown lcs { if (i >= s.length() || j >= t.length()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (s[i] == t[j]) { dp[i][j] = 1 + lenLCS(s, t, i + 1, j + 1); return dp[i][j]; } else { dp[i][j] = max(lenLCS(s, t, i + 1, j), lenLCS(s, t, i, j + 1)); return dp[i][j]; } } string solve(string s, string t) { memset(dp, -1, sizeof dp); int len = lenLCS(s, t, 0, 0); return getLCS(s, t, len); } int main() { string s, t; cin >> s >> t; cout << solve(s, t) << "\n"; return 0; }
replace
3
4
3
4
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define INF (1LL << 60) #define int long long #define magic 100000 using namespace std; int dp[3001][3001]; // i, j // dp[i][j] : lcs of s[1:i] and t[1:j] string ans; int32_t main() { string s, t; cin >> s >> t; for (int i = 0; i < (int)s.size(); i++) { for (int j = 0; j < (int)t.size(); j++) { dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); if (s[i] == t[j]) { dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1); } } } int i = s.size() - 1, j = t.size() - 1; while (i != -1 || j != -1) { if (s[i] == t[j] && dp[i + 1][j + 1] == dp[i][j] + 1) { ans.push_back(s[i]); i--; j--; } else if (dp[i + 1][j + 1] == dp[i + 1][j]) { j--; } else { i--; } } for (int i = (int)ans.size() - 1; i >= 0; i--) { cout << ans[i]; } }
#include <bits/stdc++.h> #define INF (1LL << 60) #define int long long #define magic 100000 using namespace std; int dp[3001][3001]; // i, j // dp[i][j] : lcs of s[1:i] and t[1:j] string ans; int32_t main() { string s, t; cin >> s >> t; for (int i = 0; i < (int)s.size(); i++) { for (int j = 0; j < (int)t.size(); j++) { dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); if (s[i] == t[j]) { dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1); } } } int i = s.size() - 1, j = t.size() - 1; while (i != -1 && j != -1) { if (s[i] == t[j] && dp[i + 1][j + 1] == dp[i][j] + 1) { ans.push_back(s[i]); i--; j--; } else if (dp[i + 1][j + 1] == dp[i + 1][j]) { j--; } else { i--; } } for (int i = (int)ans.size() - 1; i >= 0; i--) { cout << ans[i]; } }
replace
22
23
22
23
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <string> #define ll long long #define ld long double // #define endl '\n' #define With_the_name_of_Allah \ ios::sync_with_stdio(0); \ ios_base::sync_with_stdio(0); \ cin.tie(0), cout.tie(0); #define pi double(acos(-1)) using namespace std; long long mod; long long Fast_pow(long long a, long long b) { if (b == 0) return 1; long long z = Fast_pow(a, b / 2); if (b % 2 == 0) return ((z % mod) * (z % mod)) % mod; else return (((z * z) % mod) * a) % mod; } long long gcd(long long a, long long b) { if (a == 0) return b; else return gcd(b % a, a); } bool prime[1000001]; void sieve() { memset(prime, true, sizeof prime); prime[0] = prime[1] = 0; for (int i = 2; i * i <= 1000000; i++) { if (prime[i] == true) for (int j = i * i; j <= 100000; j += i) prime[j] = false; } } ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); } int res; int ans[1000][1000]; string a, b, c; int fn(int i, int j) { if (i >= a.size() || j >= b.size()) return 0; if (ans[i][j] != -1) return ans[i][j]; int res = 0; if (a[i] == b[j]) res = 1 + fn(i + 1, j + 1), c += a[i]; else { res = max(res, fn(i + 1, j)); res = max(res, fn(i, j + 1)); } ans[i][j] = res; return res; } void pr(int i, int j) { if (i >= a.size() || j >= b.size()) return; else if (a[i] == b[j]) cout << a[i], pr(i + 1, j + 1); else { if (fn(i + 1, j) == ans[i][j]) pr(i + 1, j); else pr(i, j + 1); } } string ss, bb; int f(int i, int j) { if (i == a.size()) return b.size() - j; else if (j == b.size()) return a.size() - i; if (ans[i][j] != -1) return ans[i][j]; res = 1e9; if (a[i] == b[j]) res = f(i + 1, j + 1); else { res = min(f(i + 1, j) + 1, f(i, j + 1) + 1); res = min(res, f(i + 1, j + 1) + 1); } ans[i][j] = res; return res; } int main() { //------lcs------// cin >> a >> b; memset(ans, -1, sizeof ans); /*fn(0,0); pr(0,0); */ fn(0, 0); pr(0, 0); return 0; }
#include <bits/stdc++.h> #include <string> #define ll long long #define ld long double // #define endl '\n' #define With_the_name_of_Allah \ ios::sync_with_stdio(0); \ ios_base::sync_with_stdio(0); \ cin.tie(0), cout.tie(0); #define pi double(acos(-1)) using namespace std; long long mod; long long Fast_pow(long long a, long long b) { if (b == 0) return 1; long long z = Fast_pow(a, b / 2); if (b % 2 == 0) return ((z % mod) * (z % mod)) % mod; else return (((z * z) % mod) * a) % mod; } long long gcd(long long a, long long b) { if (a == 0) return b; else return gcd(b % a, a); } bool prime[1000001]; void sieve() { memset(prime, true, sizeof prime); prime[0] = prime[1] = 0; for (int i = 2; i * i <= 1000000; i++) { if (prime[i] == true) for (int j = i * i; j <= 100000; j += i) prime[j] = false; } } ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); } int res; int ans[3 * 1001][3 * 1001]; string a, b, c; int fn(int i, int j) { if (i >= a.size() || j >= b.size()) return 0; if (ans[i][j] != -1) return ans[i][j]; int res = 0; if (a[i] == b[j]) res = 1 + fn(i + 1, j + 1), c += a[i]; else { res = max(res, fn(i + 1, j)); res = max(res, fn(i, j + 1)); } ans[i][j] = res; return res; } void pr(int i, int j) { if (i >= a.size() || j >= b.size()) return; else if (a[i] == b[j]) cout << a[i], pr(i + 1, j + 1); else { if (fn(i + 1, j) == ans[i][j]) pr(i + 1, j); else pr(i, j + 1); } } string ss, bb; int f(int i, int j) { if (i == a.size()) return b.size() - j; else if (j == b.size()) return a.size() - i; if (ans[i][j] != -1) return ans[i][j]; res = 1e9; if (a[i] == b[j]) res = f(i + 1, j + 1); else { res = min(f(i + 1, j) + 1, f(i, j + 1) + 1); res = min(res, f(i + 1, j + 1) + 1); } ans[i][j] = res; return res; } int main() { //------lcs------// cin >> a >> b; memset(ans, -1, sizeof ans); /*fn(0,0); pr(0,0); */ fn(0, 0); pr(0, 0); return 0; }
replace
49
50
49
50
0
p03165
C++
Runtime Error
/* MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNMMMMMNmmmNNMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNNMMNNNMMMMMMMNNNNNNNNMMMMNmddNNMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmNNNNNNNNNNNNNNNNNNNmmmNNNNNNNNdyhmNMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNmmmNmmmmNmmmNmNNmmNNNmmmmmmmmmmmmmmmmddNNNdhmNMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmmmdddmdhddmmmmmmmmmmmmmmdddddddhhhdhhhhddmNmddNMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmmmmmdhhhyhddddhhhhhhyyhyyhhhddddmddhhyyyyhhddmmmhhmMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmmmddddddyyyyhhyyyoysoooooooooooooosssyhdmmddhhhhdddmmmhhmMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMNNNNNmmddddhdhhhyssssoosoos+/+++++++++++++++ooooossssyyhhhhhhhhddyhmMMMMMMMMM MMMMMMMMMMMMMMMMMMNMMMMNmmmmmddddhhyys+ooo++++oo+///////////////////+++oooooossyysyysyyyddhmNMMMMMMM MMMMMMMMMMMMMMMMMNNNNNNNdmmddddhhhy+o////////////::///:::--::::::://///++ooosssyyyysssssyyddmNMMMMMM MMMMMMMMMMMMMMMMMNNNmmmNddddhhhyss+o/::://:::/::::::-------::::::::::///////+++oossoooossyydmmMMMMMM MMMMMMMMMMMMMMMNNmNmmddmdhhhyssoo++::/://:::::::::-----------------:::::///////++++oooooosyhmmNMMMMM MMMMMMMMMMMMMMNmNdmdmdhmhhyysooo+/::/:::::::--:----------------------:::::::::////++++++oosyhmmMMMMM MMMMMMMMMMMMMNNmNmmdddyhhhsooo++:://--::::::-------------....-----------:::::::://////+++ooshmNNMMMM MMMMMMMMMMMMNNNmNmddhhhhhyo+o++/:/:----::-------:-----.......------------:::::::////////+oosymNNMMMM MMMMMMMMMMMMNNNmNmddhyyyyys/+++:/:---------:/::---...........-.--....--------:::::::///+++osydNNMMMM MMMMMMMMMMMMNNNNNmdddysh+ys//+/:/--------://:--....--...`..--------------------::::::/++++syhhmNMMMM MMMMMMMMMMMNNNNmmmddhhoo++s///:/:-----:::/:--...-::---::://++///:--:::-----------:::/++ooyyhhdmNNMMM MMMMMMMMMMMNNNNmmmmdhho+s:s+/::/------:::---...-::/+++/:////::-::---:::-:--------:::oosooshdmmNNMMMM MMMMMMMMMMMNNNNmmNddhys/+++o/:::-----:::--...-:/+++:-...---:---------------------::oss+oosyhdmMNMMMM MMMMMMMMMMMNNNNmNmddyys//+/+//:-----::------:++/:-....`....---------------------::+yy++++oosshNNMMMM MMMMMMMMMMMNNNNNmdmdsys+//+///-----::------/+/:-...``......---------........---::/sy+///+++ossmNMMMM MMMMMMMMMMMNNNNmmdddyy+++//:::---------:--://:...`....-:///++/:-----........---::soo///++ooosymNMMMM MMMMMMMMMMMNNNNmmdydsy//o:::------------://:-......:/++/+oyhhyo/:---.........--:oos:/ossyyhhhdmNMMMM MMMMMMMMMMMNNNNNmhyyos///+::-----------::-......-:////+oymNNmhoo+-.--...````..-:sy//ohmNNhdNmmmMMMMM MMMMMMMMMMMNMNNNmhs+++//:///:---................:::++:.:NMMMMNdos/.--.``````..-+y+:+-mMMMNhmNmNMMMMM MMMMMMMMMMMMMMNNmy++//+//:/:/---...............-/so:`` .hyNMMMNyoy---``````...-/d/:-`+NMMMNmmNMMMMMM MMMMMMMMMNmmmmNNmy+//:/+/::::--.......```.....-////://:.:shdddhsoo:--.........--ss--::oyhdmmmNMMMMMM MMMMMMMMds+///+osoo+/::/+/::---........```....-.......---::::::/::---........-.::yo---:::/+sdmMMMMMM MMMMMMMh+///:::::--:///:+/:::---....`..`.``.......``````....--:::-----..--....--::ss:--:://oymNMMMMM MMMMMMd::-/o:--::+/-.-/o/+:-:---.........`.....`````````....----------........----:oy/-:://+ohmNMMMM MMMMMMh-:.:y-----:os:-.:o//--:--.........``..````...........--------............---:/yo:::++osdmMMMM MMMMMMN/-:-y:--://-/y+:.:o//-::-............```````........---------.....---......---:oo:::/ooymNMMM MMMMMMMd-:-s--//:-.:+so:-/+//-/:...........``````````.........-----...------..`....--:/s+:::/++hmMMM MMMMMMMMd-/o::/-...-:s+::.///+:+--.........````````````.........---...---..--:-----:/+oso::::/+omNMM MMMMMMMMMm:/s.++:....:s.-+.//+/:/-........````````````````.....-::-...--:/+/:--://++o+oso:::://odNMM MMMMMMMMMMm-/:`:s+:---/s.:o-//+//:-........````````````````...-/+-.....---..-:::://+ossso/:::/+odNMM MMMMMMMMMMMm::.`./+:://+/./+-+/+//-..........`````````````..-:o+.``.......---:::::/+oyyss/:://+ymMMM MMMMMMMMMMMMNo-...::-----.://-o/o/+-...............```````..:o:.````......---:/::://oyysy+://+smNMMM MMMMMMMMMMMMMMd+-..------../:::y++:/.................````../o.````````....--:////++osyhyho//+smNMMMM MMMMMMMMMMMMMMMMmo:-----..-oy/:/so:/-.....................:o..```````..-:/+ossoosyyhhdhyo///omNMMMMM MMMMMMMMMMMMMMMMMMmy/:::+ysoss//+ss:/-...................-o:-----::/+osys/--::-/soosyhs/::/+hNMMMMMM MMMMMMMMMMMMMMMMMMMMNNmNMMdo++o/+oss::-..................-+/++ossssoooossoosyyhyssssyh/::/+hNMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMNmo////sshs::..................-:/-:/ooyyssssossssyssyyhyyhy::/odNMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMmds/::/shds:/-.................::....--/+osyyyyo+oosyyyyyd+:/smNMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMNhhhs/:::ohmh//:--.............-::..`..```..-/++oossooossy+:/hNMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMNdysooso///+shds+/:----------------.......`...-::/+ooossso/+omNMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMNNmdhysyys+/+oo++++sdds+//:----------................--::://///o+mMMMMMMMMMMMMMM MMNNNNNNNNNNNNNmddhhyssos++sssyo/::+oo++oydhsoo//::------.....``........---::::://o/yNMMMMMMMMMMMMMM yyyssyyyyyyyyyyyssoooooooo//sy+oso/:::/+oooyhysso++////:---...............-----:/+++mMMMMMMMMMMMMMMM oooooossssyyyyssoooooooooos/:sh+/+ooo/::-:/+sssssso+++++oo+:--...............--://+hMMMMMMMMMMMMMMMM ++++++ooossssyssoo+oooooooos/:oh+////++++/////+osyyyssoo++shs/:-...........--://+shNMMMMMMMMMMMMMMMM /////+++ooooossso+++++++++++o+:/sy+++//://///+++++osssossysssyys+::-------:+oooydNMMMMMMMMMMMMMMMMMM ///////+++ooooooo+++/+++///////::/+oso+++/+///::::::::////+ooydhyhhyssoosyhdddddmNMMMMMMMMMMMMMMMMMM ////////++++ooo+++++////////:::::::::/++o+++++++/////////++oydysyhhhhdddddddddddddmmNmNNMMMMMMMMMMMM ::://///++++++++++++///////:::::::::::::::////////////++oshhyssyyyhhhdddddddddddhhhhhhdddmmNNMMMMMMM /:::///+//++++++///+///////:::::::::::::::::::::::///++ooooosssssyyhhhhhddhhhhhhhhhhyyyyyyhdddmMMMMM /::://+o////+++/////////:::/:::::::::::::::::::://////++++oooooossyyyhhhhhhhhhhhhyyyssoooossyhdmNMMM */ /// *********** Coded by Olium_MdioxideN *********** #include <bits/stdc++.h> using namespace std; #define ll long long #define llu unsigned long long #define elif else if #define pb push_back #define mp make_pair #define nl '\n' #define forr(i, b, e) for (int i = b; i <= e; i++) #define forrv(i, b, e) for (int i = b; i >= e; i--) #define fors(i, x) for (auto i = x.begin(); i != x.end(); i++) #define F first #define S second #define mset0(x) memset(x, 0, sizeof x); #define mset1(x) memset(x, -1, sizeof x); #define all(x) x.begin(), x.end() #define pqmin(x) priority_queue<x, vector<x>, greater<x>> #define pqmax(x) priority_queue<x> #define sortdsc(x) \ sort(all(x)); \ reverse(all(x)); #define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define vi vector<int> #define pii pair<int, int> #define mpii map<int, int> #define pi 3.14159265358979323846D #define gcd(x, y) __gcd(x, y) #define lcm(x, y) (x * (y / __gcd(x, y))) #define chkbit(x, p) ((x & (1LL << p)) ? 1 : 0) #define setbit(x, p) x |= (1LL << p) #define clrbit(x, p) x = x & (~(1LL << p)) #define revbit(x, p) (chkbit(x, p) ? clrbit(x, p) : setbit(x, p)) #define runtime() \ cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n" ll pow_mod(ll nn, ll pp, ll mm) { ll res = 1; while (pp > 0) { if (pp & 1) res = (res * nn) % mm; pp = pp >> 1; nn = (nn * nn) % mm; } return res; } #ifndef ONLINE_JUDGE vector<string> vec_spltr(string s) { s += ','; vector<string> res; while (!s.empty()) res.push_back(s.substr(0, s.find(','))), s = s.substr(s.find(',') + 1); return res; } void dbg_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); dbg_out(args, idx + 1, LINE_NUM, T...); } #define debug(...) dbg_out(vec_spltr(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) #define in_file freopen("input.c", "r", stdin) #define out_file freopen("output.c", "w", stdout) #else #define debug(...) 0 #define in_file 0 #define out_file 0 #endif /// * * * Main Code Starts Now * * * #define MOD 10000007 #define MAX 100010 void olium(); int main() { // in_file; // out_file; fast; int t = 1; // cin >> t; forr(i, 1, t) { // cout << "Case " << i << ": "; olium(); } return 0; } string s, t; int dp[3005][3005]; char direction[3005][3005]; int lcs(int i, int j) { if (i == s.size() || j == t.size()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (s[i] == t[j]) { direction[i][j] = 'd'; return dp[i][j] = 1 + lcs(i + 1, j + 1); } else { int p1 = lcs(i + 1, j), p2 = lcs(i, j + 1); if (p1 > p2) direction[i][j] = 'i'; else direction[i][j] = 'j'; dp[i][j] = max(p1, p2); } } void olium() { mset1(dp); cin >> s >> t; lcs(0, 0); int si = 0, sj = 0; while (true) { if (si == s.size() && sj == t.size()) break; if (direction[si][sj] == 'd') { cout << s[si]; si++; sj++; } else if (direction[si][sj] == 'i') si++; else sj++; } }
/* MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNMMMMMNmmmNNMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNNMMNNNMMMMMMMNNNNNNNNMMMMNmddNNMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmNNNNNNNNNNNNNNNNNNNmmmNNNNNNNNdyhmNMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNmmmNmmmmNmmmNmNNmmNNNmmmmmmmmmmmmmmmmddNNNdhmNMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmmmdddmdhddmmmmmmmmmmmmmmdddddddhhhdhhhhddmNmddNMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmmmmmdhhhyhddddhhhhhhyyhyyhhhddddmddhhyyyyhhddmmmhhmMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMNNNNNNmmmmddddddyyyyhhyyyoysoooooooooooooosssyhdmmddhhhhdddmmmhhmMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMNNNNNmmddddhdhhhyssssoosoos+/+++++++++++++++ooooossssyyhhhhhhhhddyhmMMMMMMMMM MMMMMMMMMMMMMMMMMMNMMMMNmmmmmddddhhyys+ooo++++oo+///////////////////+++oooooossyysyysyyyddhmNMMMMMMM MMMMMMMMMMMMMMMMMNNNNNNNdmmddddhhhy+o////////////::///:::--::::::://///++ooosssyyyysssssyyddmNMMMMMM MMMMMMMMMMMMMMMMMNNNmmmNddddhhhyss+o/::://:::/::::::-------::::::::::///////+++oossoooossyydmmMMMMMM MMMMMMMMMMMMMMMNNmNmmddmdhhhyssoo++::/://:::::::::-----------------:::::///////++++oooooosyhmmNMMMMM MMMMMMMMMMMMMMNmNdmdmdhmhhyysooo+/::/:::::::--:----------------------:::::::::////++++++oosyhmmMMMMM MMMMMMMMMMMMMNNmNmmdddyhhhsooo++:://--::::::-------------....-----------:::::::://////+++ooshmNNMMMM MMMMMMMMMMMMNNNmNmddhhhhhyo+o++/:/:----::-------:-----.......------------:::::::////////+oosymNNMMMM MMMMMMMMMMMMNNNmNmddhyyyyys/+++:/:---------:/::---...........-.--....--------:::::::///+++osydNNMMMM MMMMMMMMMMMMNNNNNmdddysh+ys//+/:/--------://:--....--...`..--------------------::::::/++++syhhmNMMMM MMMMMMMMMMMNNNNmmmddhhoo++s///:/:-----:::/:--...-::---::://++///:--:::-----------:::/++ooyyhhdmNNMMM MMMMMMMMMMMNNNNmmmmdhho+s:s+/::/------:::---...-::/+++/:////::-::---:::-:--------:::oosooshdmmNNMMMM MMMMMMMMMMMNNNNmmNddhys/+++o/:::-----:::--...-:/+++:-...---:---------------------::oss+oosyhdmMNMMMM MMMMMMMMMMMNNNNmNmddyys//+/+//:-----::------:++/:-....`....---------------------::+yy++++oosshNNMMMM MMMMMMMMMMMNNNNNmdmdsys+//+///-----::------/+/:-...``......---------........---::/sy+///+++ossmNMMMM MMMMMMMMMMMNNNNmmdddyy+++//:::---------:--://:...`....-:///++/:-----........---::soo///++ooosymNMMMM MMMMMMMMMMMNNNNmmdydsy//o:::------------://:-......:/++/+oyhhyo/:---.........--:oos:/ossyyhhhdmNMMMM MMMMMMMMMMMNNNNNmhyyos///+::-----------::-......-:////+oymNNmhoo+-.--...````..-:sy//ohmNNhdNmmmMMMMM MMMMMMMMMMMNMNNNmhs+++//:///:---................:::++:.:NMMMMNdos/.--.``````..-+y+:+-mMMMNhmNmNMMMMM MMMMMMMMMMMMMMNNmy++//+//:/:/---...............-/so:`` .hyNMMMNyoy---``````...-/d/:-`+NMMMNmmNMMMMMM MMMMMMMMMNmmmmNNmy+//:/+/::::--.......```.....-////://:.:shdddhsoo:--.........--ss--::oyhdmmmNMMMMMM MMMMMMMMds+///+osoo+/::/+/::---........```....-.......---::::::/::---........-.::yo---:::/+sdmMMMMMM MMMMMMMh+///:::::--:///:+/:::---....`..`.``.......``````....--:::-----..--....--::ss:--:://oymNMMMMM MMMMMMd::-/o:--::+/-.-/o/+:-:---.........`.....`````````....----------........----:oy/-:://+ohmNMMMM MMMMMMh-:.:y-----:os:-.:o//--:--.........``..````...........--------............---:/yo:::++osdmMMMM MMMMMMN/-:-y:--://-/y+:.:o//-::-............```````........---------.....---......---:oo:::/ooymNMMM MMMMMMMd-:-s--//:-.:+so:-/+//-/:...........``````````.........-----...------..`....--:/s+:::/++hmMMM MMMMMMMMd-/o::/-...-:s+::.///+:+--.........````````````.........---...---..--:-----:/+oso::::/+omNMM MMMMMMMMMm:/s.++:....:s.-+.//+/:/-........````````````````.....-::-...--:/+/:--://++o+oso:::://odNMM MMMMMMMMMMm-/:`:s+:---/s.:o-//+//:-........````````````````...-/+-.....---..-:::://+ossso/:::/+odNMM MMMMMMMMMMMm::.`./+:://+/./+-+/+//-..........`````````````..-:o+.``.......---:::::/+oyyss/:://+ymMMM MMMMMMMMMMMMNo-...::-----.://-o/o/+-...............```````..:o:.````......---:/::://oyysy+://+smNMMM MMMMMMMMMMMMMMd+-..------../:::y++:/.................````../o.````````....--:////++osyhyho//+smNMMMM MMMMMMMMMMMMMMMMmo:-----..-oy/:/so:/-.....................:o..```````..-:/+ossoosyyhhdhyo///omNMMMMM MMMMMMMMMMMMMMMMMMmy/:::+ysoss//+ss:/-...................-o:-----::/+osys/--::-/soosyhs/::/+hNMMMMMM MMMMMMMMMMMMMMMMMMMMNNmNMMdo++o/+oss::-..................-+/++ossssoooossoosyyhyssssyh/::/+hNMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMNmo////sshs::..................-:/-:/ooyyssssossssyssyyhyyhy::/odNMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMmds/::/shds:/-.................::....--/+osyyyyo+oosyyyyyd+:/smNMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMNhhhs/:::ohmh//:--.............-::..`..```..-/++oossooossy+:/hNMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMNdysooso///+shds+/:----------------.......`...-::/+ooossso/+omNMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMNNmdhysyys+/+oo++++sdds+//:----------................--::://///o+mMMMMMMMMMMMMMM MMNNNNNNNNNNNNNmddhhyssos++sssyo/::+oo++oydhsoo//::------.....``........---::::://o/yNMMMMMMMMMMMMMM yyyssyyyyyyyyyyyssoooooooo//sy+oso/:::/+oooyhysso++////:---...............-----:/+++mMMMMMMMMMMMMMMM oooooossssyyyyssoooooooooos/:sh+/+ooo/::-:/+sssssso+++++oo+:--...............--://+hMMMMMMMMMMMMMMMM ++++++ooossssyssoo+oooooooos/:oh+////++++/////+osyyyssoo++shs/:-...........--://+shNMMMMMMMMMMMMMMMM /////+++ooooossso+++++++++++o+:/sy+++//://///+++++osssossysssyys+::-------:+oooydNMMMMMMMMMMMMMMMMMM ///////+++ooooooo+++/+++///////::/+oso+++/+///::::::::////+ooydhyhhyssoosyhdddddmNMMMMMMMMMMMMMMMMMM ////////++++ooo+++++////////:::::::::/++o+++++++/////////++oydysyhhhhdddddddddddddmmNmNNMMMMMMMMMMMM ::://///++++++++++++///////:::::::::::::::////////////++oshhyssyyyhhhdddddddddddhhhhhhdddmmNNMMMMMMM /:::///+//++++++///+///////:::::::::::::::::::::::///++ooooosssssyyhhhhhddhhhhhhhhhhyyyyyyhdddmMMMMM /::://+o////+++/////////:::/:::::::::::::::::::://////++++oooooossyyyhhhhhhhhhhhhyyyssoooossyhdmNMMM */ /// *********** Coded by Olium_MdioxideN *********** #include <bits/stdc++.h> using namespace std; #define ll long long #define llu unsigned long long #define elif else if #define pb push_back #define mp make_pair #define nl '\n' #define forr(i, b, e) for (int i = b; i <= e; i++) #define forrv(i, b, e) for (int i = b; i >= e; i--) #define fors(i, x) for (auto i = x.begin(); i != x.end(); i++) #define F first #define S second #define mset0(x) memset(x, 0, sizeof x); #define mset1(x) memset(x, -1, sizeof x); #define all(x) x.begin(), x.end() #define pqmin(x) priority_queue<x, vector<x>, greater<x>> #define pqmax(x) priority_queue<x> #define sortdsc(x) \ sort(all(x)); \ reverse(all(x)); #define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define vi vector<int> #define pii pair<int, int> #define mpii map<int, int> #define pi 3.14159265358979323846D #define gcd(x, y) __gcd(x, y) #define lcm(x, y) (x * (y / __gcd(x, y))) #define chkbit(x, p) ((x & (1LL << p)) ? 1 : 0) #define setbit(x, p) x |= (1LL << p) #define clrbit(x, p) x = x & (~(1LL << p)) #define revbit(x, p) (chkbit(x, p) ? clrbit(x, p) : setbit(x, p)) #define runtime() \ cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n" ll pow_mod(ll nn, ll pp, ll mm) { ll res = 1; while (pp > 0) { if (pp & 1) res = (res * nn) % mm; pp = pp >> 1; nn = (nn * nn) % mm; } return res; } #ifndef ONLINE_JUDGE vector<string> vec_spltr(string s) { s += ','; vector<string> res; while (!s.empty()) res.push_back(s.substr(0, s.find(','))), s = s.substr(s.find(',') + 1); return res; } void dbg_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); dbg_out(args, idx + 1, LINE_NUM, T...); } #define debug(...) dbg_out(vec_spltr(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) #define in_file freopen("input.c", "r", stdin) #define out_file freopen("output.c", "w", stdout) #else #define debug(...) 0 #define in_file 0 #define out_file 0 #endif /// * * * Main Code Starts Now * * * #define MOD 10000007 #define MAX 100010 void olium(); int main() { // in_file; // out_file; fast; int t = 1; // cin >> t; forr(i, 1, t) { // cout << "Case " << i << ": "; olium(); } return 0; } string s, t; int dp[3005][3005]; char direction[3005][3005]; int lcs(int i, int j) { if (i == s.size() || j == t.size()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (s[i] == t[j]) { direction[i][j] = 'd'; return dp[i][j] = 1 + lcs(i + 1, j + 1); } else { int p1 = lcs(i + 1, j), p2 = lcs(i, j + 1); if (p1 > p2) direction[i][j] = 'i'; else direction[i][j] = 'j'; dp[i][j] = max(p1, p2); } } void olium() { mset1(dp); cin >> s >> t; lcs(0, 0); int si = 0, sj = 0; while (true) { if (si > s.size() || sj > t.size()) break; if (direction[si][sj] == 'd') { cout << s[si]; si++; sj++; } else if (direction[si][sj] == 'i') si++; else sj++; } }
replace
188
189
188
189
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int #define mmax(a, b, c) max(a, max(b, c)) #define mmin(a, b, c) min(a, min(b, c)) #define M 1000000007 #define V vector #define F(a, b, c) for (a = b; a <= c; ++a) #define I cin #define O cout #define pb push_back #define all(x) x.begin(), x.end() #define ff first #define ss second int main() { I.sync_with_stdio(false); I.tie(0); ll ls, lt, i, j; string s, t; I >> s >> t; ls = s.size(); lt = t.size(); vector<vector<ll>> dp(lt + 1, vector<ll>(ls + 1, 0)); string a; F(i, 1, lt) { F(j, 1, ls) { if (s[j - 1] == t[i - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } j = ls; while (1) { while (1) { if (s[j - 1] == t[i - 1]) { a = s[j - 1] + a; --i; --j; } else if (dp[i][j - 1] > dp[i - 1][j]) { --j; } else if (dp[i][j - 1] <= dp[i - 1][j]) { --i; } if (j == 0 || i == 0) break; } if (j == 0 || i == 0) break; } O << a << '\n'; // O<<dp[lt][ls]<<'\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define mmax(a, b, c) max(a, max(b, c)) #define mmin(a, b, c) min(a, min(b, c)) #define M 1000000007 #define V vector #define F(a, b, c) for (a = b; a <= c; ++a) #define I cin #define O cout #define pb push_back #define all(x) x.begin(), x.end() #define ff first #define ss second int main() { I.sync_with_stdio(false); I.tie(0); ll ls, lt, i, j; string s, t; I >> s >> t; ls = s.size(); lt = t.size(); vector<vector<ll>> dp(lt + 1, vector<ll>(ls + 1, 0)); string a; F(i, 1, lt) { F(j, 1, ls) { if (s[j - 1] == t[i - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } i = lt; j = ls; while (1) { while (1) { if (s[j - 1] == t[i - 1]) { a = s[j - 1] + a; --i; --j; } else if (dp[i][j - 1] > dp[i - 1][j]) { --j; } else if (dp[i][j - 1] <= dp[i - 1][j]) { --i; } if (j == 0 || i == 0) break; } if (j == 0 || i == 0) break; } O << a << '\n'; // O<<dp[lt][ls]<<'\n'; return 0; }
insert
33
33
33
34
-11
p03165
C++
Runtime Error
#include <algorithm> #include <functional> #include <iomanip> //! for setprecision(10) #include <iostream> #include <math.h> #include <string> #include <vector> #include <cstring> #include <limits.h> #include <map> #include <queue> #include <set> #include <utility> using namespace std; typedef long long LL; #define rep(i, n) for (int i = 0; i < (n); ++i) #define dump(c) \ { \ for (auto it = c.begin(); it != c.end(); ++it) \ if (it == c.begin()) \ cout << *it; \ else \ cout << ' ' << *it; \ cout << endl; \ } #define dumpMap(m) \ { \ for (auto it : m) \ cout << it.first << "=>" << it.second << ' '; \ } const int MOD = 1000000007; const int MAXLEN = 3000; short dp[MAXLEN][MAXLEN]; int main() { cin.tie(0); ios::sync_with_stdio(0); string s, t; cin >> s >> t; int slen = s.length(); int tlen = t.length(); // is = 0 for (int it = 0; it < tlen; ++it) { if (s[0] == t[it]) { for (; it < tlen; ++it) { dp[0][it] = 1; } } else { dp[0][it] = 0; } } //! it = 0 for (int is = 0; is < slen; ++is) { if (s[is] == t[0]) { for (; is < slen; ++is) { dp[is][0] = 1; } } else { dp[is][0] = 0; } } for (int is = 1; is < slen; ++is) { for (int it = 1; it < tlen; ++it) { if (s[is] == t[it]) { dp[is][it] = dp[is - 1][it - 1] + 1; } else { dp[is][it] = max(dp[is - 1][it], dp[is][it - 1]); } } } string ans; int it = tlen - 1; int is = slen - 1; while (0 < dp[is][it] && 0 <= is && 0 <= it) { while (0 < it && dp[is][it - 1] == dp[is][it]) --it; while (0 < is && dp[is - 1][it] == dp[is][it]) --is; ans = s[is] + ans; --is; --it; } cout << ans << endl; return 0; }
#include <algorithm> #include <functional> #include <iomanip> //! for setprecision(10) #include <iostream> #include <math.h> #include <string> #include <vector> #include <cstring> #include <limits.h> #include <map> #include <queue> #include <set> #include <utility> using namespace std; typedef long long LL; #define rep(i, n) for (int i = 0; i < (n); ++i) #define dump(c) \ { \ for (auto it = c.begin(); it != c.end(); ++it) \ if (it == c.begin()) \ cout << *it; \ else \ cout << ' ' << *it; \ cout << endl; \ } #define dumpMap(m) \ { \ for (auto it : m) \ cout << it.first << "=>" << it.second << ' '; \ } const int MOD = 1000000007; const int MAXLEN = 3000; short dp[MAXLEN][MAXLEN]; int main() { cin.tie(0); ios::sync_with_stdio(0); string s, t; cin >> s >> t; int slen = s.length(); int tlen = t.length(); // is = 0 for (int it = 0; it < tlen; ++it) { if (s[0] == t[it]) { for (; it < tlen; ++it) { dp[0][it] = 1; } } else { dp[0][it] = 0; } } //! it = 0 for (int is = 0; is < slen; ++is) { if (s[is] == t[0]) { for (; is < slen; ++is) { dp[is][0] = 1; } } else { dp[is][0] = 0; } } for (int is = 1; is < slen; ++is) { for (int it = 1; it < tlen; ++it) { if (s[is] == t[it]) { dp[is][it] = dp[is - 1][it - 1] + 1; } else { dp[is][it] = max(dp[is - 1][it], dp[is][it - 1]); } } } string ans; int it = tlen - 1; int is = slen - 1; while (0 <= is && 0 <= it && 0 < dp[is][it]) { while (0 < it && dp[is][it - 1] == dp[is][it]) --it; while (0 < is && dp[is - 1][it] == dp[is][it]) --is; ans = s[is] + ans; --is; --it; } cout << ans << endl; return 0; }
replace
86
87
86
87
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // T+N // #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") #define endl "\n" typedef long long ll; typedef long double ld; typedef unsigned long long ull; template <class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; } template <class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; } const ll mod = 1e9 + 7; template <class T> inline void fix(T &x) { if (x >= mod | x <= -mod) { x %= mod; } if (x < 0) { x += mod; } } #define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl const int MAX_N = 1e3 + 10; ll ans[MAX_N][MAX_N], hist[MAX_N][MAX_N]; string a, b; void output(int i, int j) { if (i == 0 && j == 0) { return; } if (hist[i][j] == 2) { output(i - 1, j - 1); cout << a[i - 1]; } else if (hist[i][j] == 1) { output(i - 1, j); } else if (hist[i][j] == -1) { output(i, j - 1); } } signed main() { // ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> a >> b; ll ret = 0; for (int i = 0; i < MAX_N; i++) { fill_n(ans[i], MAX_N, -mod); } ans[0][0] = 0; for (int i = 0; i <= a.size(); i++) { for (int j = 0; j <= b.size(); j++) { if (a[i] == b[j]) { if (chkmax(ans[i + 1][j + 1], ans[i][j] + 1)) { hist[i + 1][j + 1] = 2; } } if (chkmax(ans[i + 1][j], ans[i][j])) { hist[i + 1][j] = 1; } if (chkmax(ans[i][j + 1], ans[i][j])) { hist[i][j + 1] = -1; } } } output(a.size(), b.size()); return 0; }
#include <bits/stdc++.h> using namespace std; // T+N // #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") #define endl "\n" typedef long long ll; typedef long double ld; typedef unsigned long long ull; template <class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; } template <class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; } const ll mod = 1e9 + 7; template <class T> inline void fix(T &x) { if (x >= mod | x <= -mod) { x %= mod; } if (x < 0) { x += mod; } } #define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl const int MAX_N = 4e3 + 10; ll ans[MAX_N][MAX_N], hist[MAX_N][MAX_N]; string a, b; void output(int i, int j) { if (i == 0 && j == 0) { return; } if (hist[i][j] == 2) { output(i - 1, j - 1); cout << a[i - 1]; } else if (hist[i][j] == 1) { output(i - 1, j); } else if (hist[i][j] == -1) { output(i, j - 1); } } signed main() { // ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> a >> b; ll ret = 0; for (int i = 0; i < MAX_N; i++) { fill_n(ans[i], MAX_N, -mod); } ans[0][0] = 0; for (int i = 0; i <= a.size(); i++) { for (int j = 0; j <= b.size(); j++) { if (a[i] == b[j]) { if (chkmax(ans[i + 1][j + 1], ans[i][j] + 1)) { hist[i + 1][j + 1] = 2; } } if (chkmax(ans[i + 1][j], ans[i][j])) { hist[i + 1][j] = 1; } if (chkmax(ans[i][j + 1], ans[i][j])) { hist[i][j + 1] = -1; } } } output(a.size(), b.size()); return 0; }
replace
26
27
26
27
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define pb push_back typedef long long ll; typedef long double ld; int main() { // ios_base::sync_with_stdio(false); // cin.tie(NULL); ll i, j, N = 3e2 + 10; ll dp[N][N]; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { dp[i][j] = -1; } } for (i = 0; i < N; i++) { dp[i][0] = dp[0][i] = 0; } string s, t, f; cin >> s >> t; for (i = 1; i <= s.size(); i++) { for (j = 1; j <= t.size(); j++) { if (dp[i][j] == -1) { if (s[i - 1] == t[j - 1]) dp[i][j] = max(1 + dp[i - 1][j - 1], max(dp[i - 1][j], dp[i][j - 1])); else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } if (dp[s.size()][t.size()] == 0) { cout << "\n"; return 0; } for (i = s.size(), j = t.size(); i > 0 && j > 0 && dp[i][j] != 0;) { if (dp[i - 1][j] == dp[i][j]) i--; else if (dp[i][j - 1] == dp[i][j]) j--; else if (dp[i - 1][j] != dp[i][j] && dp[i][j - 1] != dp[i][j]) { f.pb(t[j - 1]); i--, j--; } } reverse(f.begin(), f.end()); cout << f << "\n"; /*for (i = 0; i <= s.size(); i++) { for (j = 0; j <= t.size(); j++) cout << dp[i][j] << " "; cout << "\n"; } cout << dp[s.size()][t.size()] << "\n";*/ return 0; }
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define pb push_back typedef long long ll; typedef long double ld; int main() { // ios_base::sync_with_stdio(false); // cin.tie(NULL); ll i, j, N = 3e3 + 10; ll dp[N][N]; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { dp[i][j] = -1; } } for (i = 0; i < N; i++) { dp[i][0] = dp[0][i] = 0; } string s, t, f; cin >> s >> t; for (i = 1; i <= s.size(); i++) { for (j = 1; j <= t.size(); j++) { if (dp[i][j] == -1) { if (s[i - 1] == t[j - 1]) dp[i][j] = max(1 + dp[i - 1][j - 1], max(dp[i - 1][j], dp[i][j - 1])); else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } if (dp[s.size()][t.size()] == 0) { cout << "\n"; return 0; } for (i = s.size(), j = t.size(); i > 0 && j > 0 && dp[i][j] != 0;) { if (dp[i - 1][j] == dp[i][j]) i--; else if (dp[i][j - 1] == dp[i][j]) j--; else if (dp[i - 1][j] != dp[i][j] && dp[i][j - 1] != dp[i][j]) { f.pb(t[j - 1]); i--, j--; } } reverse(f.begin(), f.end()); cout << f << "\n"; /*for (i = 0; i <= s.size(); i++) { for (j = 0; j <= t.size(); j++) cout << dp[i][j] << " "; cout << "\n"; } cout << dp[s.size()][t.size()] << "\n";*/ return 0; }
replace
11
12
11
12
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; int arr[1001][1001]; int main() { string s1, s2; cin >> s1 >> s2; int n1 = s1.length(), n2 = s2.length(); for (int i = 0; i < n1 + 1; i++) { for (int j = 0; j < n2 + 1; j++) { if (i == 0 || j == 0) arr[i][j] = 0; else { if (s1[i - 1] == s2[j - 1]) arr[i][j] = arr[i - 1][j - 1] + 1; else { if (arr[i][j - 1] < arr[i - 1][j]) arr[i][j] = arr[i - 1][j]; else arr[i][j] = arr[i][j - 1]; } } // cout<<arr[i][j]<<" "; } // cout<<endl; } int i = n1, j = n2; int index = arr[n1][n2] - 1; char ans[arr[n1][n2] + 1]; ans[arr[n1][n2]] = '\0'; while (i > 0 && j > 0) { if (arr[i][j] == arr[i][j - 1]) j--; else if (arr[i][j] == arr[i - 1][j]) i--; else { // cout<<s1[i-1]<<endl; ans[index--] = s1[i - 1]; i--; j--; } } cout << ans << endl; }
#include <bits/stdc++.h> #include <iostream> using namespace std; int arr[3001][3001]; int main() { string s1, s2; cin >> s1 >> s2; int n1 = s1.length(), n2 = s2.length(); for (int i = 0; i < n1 + 1; i++) { for (int j = 0; j < n2 + 1; j++) { if (i == 0 || j == 0) arr[i][j] = 0; else { if (s1[i - 1] == s2[j - 1]) arr[i][j] = arr[i - 1][j - 1] + 1; else { if (arr[i][j - 1] < arr[i - 1][j]) arr[i][j] = arr[i - 1][j]; else arr[i][j] = arr[i][j - 1]; } } // cout<<arr[i][j]<<" "; } // cout<<endl; } int i = n1, j = n2; int index = arr[n1][n2] - 1; char ans[arr[n1][n2] + 1]; ans[arr[n1][n2]] = '\0'; while (i > 0 && j > 0) { if (arr[i][j] == arr[i][j - 1]) j--; else if (arr[i][j] == arr[i - 1][j]) i--; else { // cout<<s1[i-1]<<endl; ans[index--] = s1[i - 1]; i--; j--; } } cout << ans << endl; }
replace
4
5
4
5
0
p03165
C++
Runtime Error
// Abhigyan's Code: // YOU DON'T DARE TO COPY :) #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int ull; #define checker cout << "CHECKED" #define modulo 1000000007 ll dp[1000][1000]; void lcs(string x, string y) { ll s1 = x.size(); ll s2 = y.size(); for (ll i = 0; i <= s1; i++) { for (ll j = 0; j <= s2; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else if (x[i - 1] == y[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string x, y; cin >> x >> y; ll i = x.size(); ll j = y.size(); lcs(x, y); vector<char> st(dp[x.size()][y.size()]); ll curr; curr = dp[x.size()][y.size()] - 1; // cout<<curr<<endl; while (i > 0 && j > 0 && curr >= 0) { if (x[i - 1] == y[j - 1]) { st[curr] = x[i - 1]; i--; j--; curr--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } for (auto z : st) cout << z; return 0; }
// Abhigyan's Code: // YOU DON'T DARE TO COPY :) #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int ull; #define checker cout << "CHECKED" #define modulo 1000000007 ll dp[10000][10000]; void lcs(string x, string y) { ll s1 = x.size(); ll s2 = y.size(); for (ll i = 0; i <= s1; i++) { for (ll j = 0; j <= s2; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else if (x[i - 1] == y[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string x, y; cin >> x >> y; ll i = x.size(); ll j = y.size(); lcs(x, y); vector<char> st(dp[x.size()][y.size()]); ll curr; curr = dp[x.size()][y.size()] - 1; // cout<<curr<<endl; while (i > 0 && j > 0 && curr >= 0) { if (x[i - 1] == y[j - 1]) { st[curr] = x[i - 1]; i--; j--; curr--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } for (auto z : st) cout << z; return 0; }
replace
10
11
10
11
0
p03165
C++
Runtime Error
/*BINH :))*/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long llu; #define IO \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define f first #define s second #define mp make_pair #define pb push_back #define ep emplace_back #define sz(a) (int)a.size() #define FOR(x, a, b) for (int x = a; x <= b; x++) #define FOD(x, a, b) for (int x = a; x >= b; x--) #define REP(x, a, b) for (int x = a; x < b; x++) #define RED(x, a, b) for (int x = a; x > b; x--) #define EL cout << endl #define maxn 1005 void init() {} string a, b; int f[1000][1000]; int main() { IO init(); cin >> a >> b; int m = a.size(), n = b.size(); a = '0' + a; b = '0' + b; FOR(i, 1, m) { FOR(j, 1, n) { if (a[i] == b[j]) f[i][j] = f[i - 1][j - 1] + 1; else { f[i][j] = max(f[i - 1][j], f[i][j - 1]); } } } int i = m, j = n; string res; while (i > 0 && j > 0) { if (a[i] == b[j]) { res = a[i] + res; i--; j--; } else { if (f[i - 1][j] > f[i][j - 1]) { i--; } else j--; } } cout << res; }
/*BINH :))*/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long llu; #define IO \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define f first #define s second #define mp make_pair #define pb push_back #define ep emplace_back #define sz(a) (int)a.size() #define FOR(x, a, b) for (int x = a; x <= b; x++) #define FOD(x, a, b) for (int x = a; x >= b; x--) #define REP(x, a, b) for (int x = a; x < b; x++) #define RED(x, a, b) for (int x = a; x > b; x--) #define EL cout << endl #define maxn 1005 void init() {} string a, b; int f[3001][3001]; int main() { IO init(); cin >> a >> b; int m = a.size(), n = b.size(); a = '0' + a; b = '0' + b; FOR(i, 1, m) { FOR(j, 1, n) { if (a[i] == b[j]) f[i][j] = f[i - 1][j - 1] + 1; else { f[i][j] = max(f[i - 1][j], f[i][j - 1]); } } } int i = m, j = n; string res; while (i > 0 && j > 0) { if (a[i] == b[j]) { res = a[i] + res; i--; j--; } else { if (f[i - 1][j] > f[i][j - 1]) { i--; } else j--; } } cout << res; }
replace
25
26
25
26
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define ff first #define ss second #define pb push_back #define pf push_front #define mp make_pair #define pu push #define pp pop_back #define in insert #define ld long double #define endl '\n' #define debug cout << "Hold right there sparky.....\n"; #define forn(low, high, i) for (i = low; i < high; i++) #define forrev(high, low, i) for (i = high; i >= low; i--) #define trace(x) cerr << #x << ": " << x << " " << endl; #define zeroes(x) memset(x, 0, sizeof(x)) typedef long long int ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; const ll mod = 1e9 + 7; const ll maxn = 100000; int main() { IOS string s, t; cin >> s >> t; s = "$" + s; t = "$" + t; ll dp[300][300]; for (ll i = 0; i < s.size(); i++) { for (ll j = 0; j < t.size(); j++) { if (i == 0 || j == 0) { dp[i][j] = 0; continue; } if (s[i] == t[j]) { dp[i][j] = (dp[i - 1][j - 1]) + 1; // trace(dp[i][j]); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } ll i = s.size() - 1; ll j = t.size() - 1; string ans = ""; while (i > 0 && j > 0) { if (s[i] == t[j]) { ans += (s[i]); i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else j--; } reverse(ans.begin(), ans.end()); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define ff first #define ss second #define pb push_back #define pf push_front #define mp make_pair #define pu push #define pp pop_back #define in insert #define ld long double #define endl '\n' #define debug cout << "Hold right there sparky.....\n"; #define forn(low, high, i) for (i = low; i < high; i++) #define forrev(high, low, i) for (i = high; i >= low; i--) #define trace(x) cerr << #x << ": " << x << " " << endl; #define zeroes(x) memset(x, 0, sizeof(x)) typedef long long int ll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; const ll mod = 1e9 + 7; const ll maxn = 100000; int main() { IOS string s, t; cin >> s >> t; s = "$" + s; t = "$" + t; ll dp[3005][3005]; for (ll i = 0; i < s.size(); i++) { for (ll j = 0; j < t.size(); j++) { if (i == 0 || j == 0) { dp[i][j] = 0; continue; } if (s[i] == t[j]) { dp[i][j] = (dp[i - 1][j - 1]) + 1; // trace(dp[i][j]); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } ll i = s.size() - 1; ll j = t.size() - 1; string ans = ""; while (i > 0 && j > 0) { if (s[i] == t[j]) { ans += (s[i]); i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else j--; } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
41
42
41
42
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define pii pair<int, int> #define f first #define s second #define p 1000000007 using namespace std; string s, t; int a[3000][3000]; int b[3000][3000]; int lcs(int i, int j) { if (i == -1 || j == -1) return 0; if (s[i] == t[j]) { b[i][j] = 1; return a[i][j] = lcs(i - 1, j - 1) + 1; } if (lcs(i - 1, j) > lcs(i, j - 1)) { b[i][j] = 2; return a[i][j] = lcs(i - 1, j); } b[i][j] = 3; return a[i][j] = lcs(i, j - 1); } int main() { cin >> s >> t; memset(a, -1, sizeof(a)); lcs(s.size() - 1, t.size() - 1); string s1; int i = s.size() - 1, j = t.size() - 1; while (i >= 0 && j >= 0) { if (b[i][j] == 1) { s1.push_back(s[i]); i--; j--; } else if (b[i][j] == 2) { i--; } else { j--; } } reverse(s1.begin(), s1.end()); cout << s1; return 0; }
#include <bits/stdc++.h> #define pii pair<int, int> #define f first #define s second #define p 1000000007 using namespace std; string s, t; int a[3000][3000]; int b[3000][3000]; int lcs(int i, int j) { if (i == -1 || j == -1) return 0; if (a[i][j] != -1) return a[i][j]; if (s[i] == t[j]) { b[i][j] = 1; return a[i][j] = lcs(i - 1, j - 1) + 1; } if (lcs(i - 1, j) > lcs(i, j - 1)) { b[i][j] = 2; return a[i][j] = lcs(i - 1, j); } b[i][j] = 3; return a[i][j] = lcs(i, j - 1); } int main() { cin >> s >> t; memset(a, -1, sizeof(a)); lcs(s.size() - 1, t.size() - 1); string s1; int i = s.size() - 1, j = t.size() - 1; while (i >= 0 && j >= 0) { if (b[i][j] == 1) { s1.push_back(s[i]); i--; j--; } else if (b[i][j] == 2) { i--; } else { j--; } } reverse(s1.begin(), s1.end()); cout << s1; return 0; }
insert
12
12
12
14
TLE
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define ll long long using namespace std; string a, b; const int MAXN = 3005; ll dp[MAXN][MAXN]; int n, m; string ans; ll solve(int i, int j) { if (i == n) return 0; if (j == m) return 0; ll r = dp[i][j]; if (r != -1) return r; if (a[i] == b[j]) r = solve(i + 1, j + 1) + 1; else r = max(solve(i + 1, j), solve(i, j + 1)); return r; } void build(int i, int j) { if (i == n) return; if (j == m) return; ll answer = solve(i, j); if (a[i] == b[j] && answer == solve(i + 1, j + 1) + 1) { ans.push_back(a[i]); build(i + 1, j + 1); } else if (answer == solve(i + 1, j)) build(i + 1, j); else if (answer == solve(i, j + 1)) build(i, j + 1); return; } int main() { fast; cin >> a >> b; n = a.size(); m = b.size(); memset(dp, -1, sizeof dp); solve(0, 0); build(0, 0); cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define ll long long using namespace std; string a, b; const int MAXN = 3005; ll dp[MAXN][MAXN]; int n, m; string ans; ll solve(int i, int j) { if (i == n) return 0; if (j == m) return 0; ll r = dp[i][j]; if (r != -1) return r; if (a[i] == b[j]) r = solve(i + 1, j + 1) + 1; else r = max(solve(i + 1, j), solve(i, j + 1)); return dp[i][j] = r; } void build(int i, int j) { if (i == n) return; if (j == m) return; ll answer = solve(i, j); if (a[i] == b[j] && answer == solve(i + 1, j + 1) + 1) { ans.push_back(a[i]); build(i + 1, j + 1); } else if (answer == solve(i + 1, j)) build(i + 1, j); else if (answer == solve(i, j + 1)) build(i, j + 1); return; } int main() { fast; cin >> a >> b; n = a.size(); m = b.size(); memset(dp, -1, sizeof dp); solve(0, 0); build(0, 0); cout << ans << "\n"; return 0; }
replace
24
25
24
25
TLE
p03165
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> using namespace std; #define all(v) ((v).begin()), ((v).end()) #define sz(v) ((int)((v).size())) #define clr(v, d) memset(v, d, sizeof(v)) #define rep(i, v) for (int i = 0; i < sz(v); ++i) #define lp(i, n) for (int i = 0; i < (int)(n); ++i) typedef long long ll; const double EPS = (1e-7); int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } #define pb push_back #define P(x) cout << #x << " = { " << x << " }\n" clock_t T; #define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << endl void GOAT() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("output.txt", "w ", stdout); #else #endif } string a, b, ans = ""; int mem[3001][3001]; int lcs(int i, int j) { if (i >= sz(a) || j >= sz(b)) return 0; int &ret = mem[i][j]; if (~ret) return ret; ret = -1; if (a[i] == b[j]) { // += a[i]; return ret = lcs(i + 1, j + 1) + 1; } int ch1 = lcs(i, j + 1); int ch2 = lcs(i + 1, j); return ret = max(ch1, ch2); } void solve(int i, int j) { if (i >= sz(a) || j >= sz(b)) return; if (a[i] == b[j]) { ans += a[i]; solve(i + 1, j + 1); } else { if (mem[i + 1][j] >= mem[i][j + 1]) solve(i + 1, j); else solve(i, j + 1); } } int main() { GOAT(); clr(mem, -1); cin >> a >> b; lcs(0, 0); solve(0, 0); cout << ans << endl; }
#define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> using namespace std; #define all(v) ((v).begin()), ((v).end()) #define sz(v) ((int)((v).size())) #define clr(v, d) memset(v, d, sizeof(v)) #define rep(i, v) for (int i = 0; i < sz(v); ++i) #define lp(i, n) for (int i = 0; i < (int)(n); ++i) typedef long long ll; const double EPS = (1e-7); int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } #define pb push_back #define P(x) cout << #x << " = { " << x << " }\n" clock_t T; #define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << endl void GOAT() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); // freopen("output.txt", "w ", stdout); #else #endif } string a, b, ans = ""; int mem[3001][3001]; int lcs(int i, int j) { if (i >= sz(a) || j >= sz(b)) return 0; int &ret = mem[i][j]; if (~ret) return ret; ret = -1; if (a[i] == b[j]) { // += a[i]; return ret = lcs(i + 1, j + 1) + 1; } int ch1 = lcs(i, j + 1); int ch2 = lcs(i + 1, j); return ret = max(ch1, ch2); } void solve(int i, int j) { if (i >= sz(a) || j >= sz(b)) return; if (a[i] == b[j]) { ans += a[i]; solve(i + 1, j + 1); } else { if (mem[i + 1][j] >= mem[i][j + 1]) solve(i + 1, j); else solve(i, j + 1); } } int main() { // GOAT(); clr(mem, -1); cin >> a >> b; lcs(0, 0); solve(0, 0); cout << ans << endl; }
replace
58
59
58
59
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<ll> vll; const ll inf = 1e18 + 7; const int maxn = 1e3 + 10; const int maxq = 5e2 + 10; const int alf = 26; const ll dlm = 1e9 + 7; const int del = 998244353; const int eps = 1e-7; int dp[maxn][maxn]; pair<int, int> path[maxn][maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); string z1, z2; cin >> z1 >> z2; string s = "", t = ""; s += " "; s += z1; t += " "; t += z2; for (int i = 1; i < s.size(); i++) { for (int j = 1; j < t.size(); j++) { if (s[i] == t[j]) { dp[i][j] = dp[i - 1][j - 1] + 1; path[i][j] = {i - 1, j - 1}; } else { if (dp[i - 1][j] > dp[i][j - 1]) { dp[i][j] = dp[i - 1][j]; path[i][j] = {i - 1, j}; } else { dp[i][j] = dp[i][j - 1]; path[i][j] = {i, j - 1}; } } } } string rre = ""; string re = ""; int i = s.size() - 1, j = t.size() - 1; while (i > 0 && j > 0) { int x = path[i][j].first; int y = path[i][j].second; if (x == (i - 1) && y == (j - 1)) { rre += s[i]; } i = x; j = y; } for (int z = rre.size() - 1; z >= 0; z--) { re += (rre[z]); } cout << re << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<ll> vll; const ll inf = 1e18 + 7; const int maxn = 3e3 + 10; const int maxq = 5e2 + 10; const int alf = 26; const ll dlm = 1e9 + 7; const int del = 998244353; const int eps = 1e-7; int dp[maxn][maxn]; pair<int, int> path[maxn][maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); string z1, z2; cin >> z1 >> z2; string s = "", t = ""; s += " "; s += z1; t += " "; t += z2; for (int i = 1; i < s.size(); i++) { for (int j = 1; j < t.size(); j++) { if (s[i] == t[j]) { dp[i][j] = dp[i - 1][j - 1] + 1; path[i][j] = {i - 1, j - 1}; } else { if (dp[i - 1][j] > dp[i][j - 1]) { dp[i][j] = dp[i - 1][j]; path[i][j] = {i - 1, j}; } else { dp[i][j] = dp[i][j - 1]; path[i][j] = {i, j - 1}; } } } } string rre = ""; string re = ""; int i = s.size() - 1, j = t.size() - 1; while (i > 0 && j > 0) { int x = path[i][j].first; int y = path[i][j].second; if (x == (i - 1) && y == (j - 1)) { rre += s[i]; } i = x; j = y; } for (int z = rre.size() - 1; z >= 0; z--) { re += (rre[z]); } cout << re << endl; return 0; }
replace
8
9
8
9
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define ld long double #define all(x) (x.begin()), (x.end()) #define sz(x) ((ll)x.size()) #define FAST_IO \ ios_base ::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); using namespace std; #ifdef LOCAL #include "uj.h" #endif const ll oo = 1e16; const int maxn = 1e3 + 10; string s, t; int N, M; ll dp[maxn][maxn]; ll Brute(int i, int j) { if (i == N || j == M) return 0; ll &ans = dp[i][j]; if (ans != -1) return ans; if (s[i] == t[j]) return ans = Brute(i + 1, j + 1) + 1; ll a = Brute(i + 1, j); ll b = Brute(i, j + 1); return ans = max(a, b); } string res; void build(int i, int j) { if (i == N || j == M) return; if (s[i] == t[j]) { res += s[i]; build(i + 1, j + 1); return; } ll optimal = Brute(i, j); ll a = Brute(i + 1, j); if (optimal == a) build(i + 1, j); else build(i, j + 1); } void test_case() { cin >> s >> t; N = sz(s), M = sz(t); memset(dp, -1, sizeof(dp)); ll check = Brute(0, 0); // debug() << owo(check); build(0, 0); cout << res << endl; } int main() { FAST_IO; int tt; // cin>>tt; tt = 1; while (tt--) { test_case(); } #ifndef ONLINE_JUDGE cerr << "Running Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double #define all(x) (x.begin()), (x.end()) #define sz(x) ((ll)x.size()) #define FAST_IO \ ios_base ::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); using namespace std; #ifdef LOCAL #include "uj.h" #endif const ll oo = 1e16; const int maxn = 1e4; string s, t; int N, M; ll dp[maxn][maxn]; ll Brute(int i, int j) { if (i == N || j == M) return 0; ll &ans = dp[i][j]; if (ans != -1) return ans; if (s[i] == t[j]) return ans = Brute(i + 1, j + 1) + 1; ll a = Brute(i + 1, j); ll b = Brute(i, j + 1); return ans = max(a, b); } string res; void build(int i, int j) { if (i == N || j == M) return; if (s[i] == t[j]) { res += s[i]; build(i + 1, j + 1); return; } ll optimal = Brute(i, j); ll a = Brute(i + 1, j); if (optimal == a) build(i + 1, j); else build(i, j + 1); } void test_case() { cin >> s >> t; N = sz(s), M = sz(t); memset(dp, -1, sizeof(dp)); ll check = Brute(0, 0); // debug() << owo(check); build(0, 0); cout << res << endl; } int main() { FAST_IO; int tt; // cin>>tt; tt = 1; while (tt--) { test_case(); } #ifndef ONLINE_JUDGE cerr << "Running Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
replace
13
14
13
14
0
Running Time: 0.042397 s.
p03165
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 int long long #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 fone for (i = 0; i < n; i++) #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 = 5000 * 100 + 1; const ll N2 = 1000 * 1000 + 5; const ld PI = 3.14159265; const ll R = 250; ll gcd(ll a, ll b) { if (!b) return a; return gcd(b, a % b); } 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; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } signed main() { fastio(); string s, t; cin >> s >> t; vector<vector<ll>> dp(3000, vector<ll>(3000, 0LL)); for (int i = 1; i <= s.length(); i++) { for (int j = 1; j <= t.length(); j++) { // cout<<dp[i][j]; if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; // cout<<s[i-1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } // cout<<dp[i][j]<<endl; } } int index = dp[s.length()][t.length()]; int i = s.length(); int j = t.length(); char lcs[index + 1]; lcs[index] = '\0'; while (i > 0 && j > 0) { // If current character in X[] and Y are same, then // current character is part of LCS if (s[i - 1] == t[j - 1]) { lcs[index - 1] = s[i - 1]; // Put current character in result i--; j--; index--; // reduce values of i, j and index } // If not same, then find the larger of two and // go in the direction of larger value else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } cout << lcs; }
#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 int long long #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 fone for (i = 0; i < n; i++) #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 = 5000 * 100 + 1; const ll N2 = 1000 * 1000 + 5; const ld PI = 3.14159265; const ll R = 250; ll gcd(ll a, ll b) { if (!b) return a; return gcd(b, a % b); } 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; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } signed main() { fastio(); string s, t; cin >> s >> t; vector<vector<ll>> dp(4000, vector<ll>(4000, 0LL)); for (int i = 1; i <= s.length(); i++) { for (int j = 1; j <= t.length(); j++) { // cout<<dp[i][j]; if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; // cout<<s[i-1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } // cout<<dp[i][j]<<endl; } } int index = dp[s.length()][t.length()]; int i = s.length(); int j = t.length(); char lcs[index + 1]; lcs[index] = '\0'; while (i > 0 && j > 0) { // If current character in X[] and Y are same, then // current character is part of LCS if (s[i - 1] == t[j - 1]) { lcs[index - 1] = s[i - 1]; // Put current character in result i--; j--; index--; // reduce values of i, j and index } // If not same, then find the larger of two and // go in the direction of larger value else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } cout << lcs; }
replace
51
52
51
52
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define int long long int #define endl "\n" #define pb push_back #define mp make_pair #define ff first #define ss second #define all(c) c.begin(), c.end() struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; void solve() { string s, t; cin >> s >> t; int n = s.length(), m = t.length(); int dp[n + 1][m + 1]; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } string subseq = ""; int i = n, j = m; while (i != 0 || j != 0) { if (s[i - 1] == t[j - 1]) { subseq += s[i - 1]; i -= 1; j -= 1; } else if (dp[i][j] == dp[i - 1][j]) { i -= 1; } else { j -= 1; } } reverse(all(subseq)); cout << subseq; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define int long long int #define endl "\n" #define pb push_back #define mp make_pair #define ff first #define ss second #define all(c) c.begin(), c.end() struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; void solve() { string s, t; cin >> s >> t; int n = s.length(), m = t.length(); int dp[n + 1][m + 1]; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } string subseq = ""; int i = n, j = m; while (i != 0 && j != 0) { if (s[i - 1] == t[j - 1]) { subseq += s[i - 1]; i -= 1; j -= 1; } else if (dp[i][j] == dp[i - 1][j]) { i -= 1; } else { j -= 1; } } reverse(all(subseq)); cout << subseq; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
replace
53
54
53
54
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define nl '\n' #define vec vector<ll> #define pr pair<ll, ll> #define vecp vector<pr> #define pq priority_queue<ll> #define pqp priority_queue<pr> #define ff first #define ss second #define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i++) #define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--) #define fa(x, v) for (auto x : v) #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define max4(a, b, c, d) max(a, max3(b, c, d)) #define min4(a, b, c, d) min(a, min3(b, c, d)) #define PI 3.14159265 const ll MOD = 1000000007; ll powerm(ll a, ll b, ll MOD) { a %= MOD; ll res = 1; while (b > 0) { if (b & 1) res = res * a % MOD; a = a * a % MOD; b >>= 1; } return res; } ll power(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } ll binarySearch(ll a[], ll low, ll high, ll key) { while (low <= high) { ll mid = low + ((high - low) / 2); if (a[mid] < key) { low = mid + 1; } else if (a[mid] > key) { high = mid - 1; } else { return mid; } } return -1; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } vec sieve; void Sieve(int N) { const ll maxn = N + 1; sieve.resize(maxn); f(i, 0, maxn) sieve[i] = i; sieve[1] = -1; sieve[0] = -1; f(i, 2, maxn) if (i == sieve[i]) for (ll j = 2 * i; j < maxn; j += i) if (sieve[j] == j) sieve[j] = i; } ll dp[3001][3001]; ll recur(ll n, ll m, string s1, string s2) { if (dp[n][m] != -1) return dp[n][m]; if (n == 0 || m == 0) { dp[n][m] = 0; return 0; } if (s1[n - 1] == s2[m - 1]) dp[n][m] = 1 + recur(n - 1, m - 1, s1, s2); else dp[n][m] = max(recur(n - 1, m, s1, s2), recur(n, m - 1, s1, s2)); return dp[n][m]; } int main() { fast string s1, s2; cin >> s1 >> s2; ll n = s1.length(); ll m = s2.length(); f(i, 0, n + 1) f(j, 0, m + 1) dp[i][j] = -1; ll lcs = recur(n, m, s1, s2); string final = ""; if (lcs >= 1) { while (n > 0 && m > 0) { if (s1[n - 1] == s2[m - 1]) { final += s1[n - 1]; m--; n--; } else { if (dp[n - 1][m] > dp[n][m - 1]) { n--; } else { m--; } } } for (ll i = final.length() - 1; i >= 0; i--) { cout << final[i]; } } else cout << "" << nl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define nl '\n' #define vec vector<ll> #define pr pair<ll, ll> #define vecp vector<pr> #define pq priority_queue<ll> #define pqp priority_queue<pr> #define ff first #define ss second #define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i++) #define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i--) #define fa(x, v) for (auto x : v) #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define max4(a, b, c, d) max(a, max3(b, c, d)) #define min4(a, b, c, d) min(a, min3(b, c, d)) #define PI 3.14159265 const ll MOD = 1000000007; ll powerm(ll a, ll b, ll MOD) { a %= MOD; ll res = 1; while (b > 0) { if (b & 1) res = res * a % MOD; a = a * a % MOD; b >>= 1; } return res; } ll power(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } ll binarySearch(ll a[], ll low, ll high, ll key) { while (low <= high) { ll mid = low + ((high - low) / 2); if (a[mid] < key) { low = mid + 1; } else if (a[mid] > key) { high = mid - 1; } else { return mid; } } return -1; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } vec sieve; void Sieve(int N) { const ll maxn = N + 1; sieve.resize(maxn); f(i, 0, maxn) sieve[i] = i; sieve[1] = -1; sieve[0] = -1; f(i, 2, maxn) if (i == sieve[i]) for (ll j = 2 * i; j < maxn; j += i) if (sieve[j] == j) sieve[j] = i; } ll dp[3001][3001]; ll recur(ll n, ll m, string &s1, string &s2) { if (dp[n][m] != -1) return dp[n][m]; if (n == 0 || m == 0) { dp[n][m] = 0; return 0; } if (s1[n - 1] == s2[m - 1]) dp[n][m] = 1 + recur(n - 1, m - 1, s1, s2); else dp[n][m] = max(recur(n - 1, m, s1, s2), recur(n, m - 1, s1, s2)); return dp[n][m]; } int main() { fast string s1, s2; cin >> s1 >> s2; ll n = s1.length(); ll m = s2.length(); f(i, 0, n + 1) f(j, 0, m + 1) dp[i][j] = -1; ll lcs = recur(n, m, s1, s2); string final = ""; if (lcs >= 1) { while (n > 0 && m > 0) { if (s1[n - 1] == s2[m - 1]) { final += s1[n - 1]; m--; n--; } else { if (dp[n - 1][m] > dp[n][m - 1]) { n--; } else { m--; } } } for (ll i = final.length() - 1; i >= 0; i--) { cout << final[i]; } } else cout << "" << nl; return 0; }
replace
76
77
76
77
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define ld long double #define endl "\n" #define ull unsigned long long #define ALL(v) ((v).begin()), ((v).end()) #define INCREASING(v) sort(ALL(v)) #define DECREASING(v) INCREASING(v), reverse(ALL(v)) #define SZ(v) ((int)((v).size())) #define LP(c, x, n) for (int c = x; c < n; c++) #define LO(c, n, x) for (int c = n; c >= x; c--) #define watch(x) cout << (#x) << " is " << (x) << endl #define PB push_back #define F first #define S second #define MP make_pair #define PI (2 * acos(0.0)) #define EPS 1e-8 #define INF 1.0 / 0.0 #define Yala_Besor3a \ ios_base::sync_with_stdio(0), ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define ReadFile freopen("delivery.in", "r", stdin) #define WriteFile freopen("output.txt", "w", stdout) using namespace std; const int OO = 0x3f3f3f3f, NegOO = -1 * OO, N = 1e3 + 5, mod = 998244353; int dp[N][N]; string x, z; int szx, szz; int solve(int i, int j) { if (i == szx || j == szz) return 0; int &rf = dp[i][j]; if (~rf) return rf; if (x[i] == z[j]) rf = max(1 + solve(i + 1, j + 1), rf); else rf = max(rf, max(solve(i + 1, j), solve(i, j + 1))); return rf; } void Build(int i, int j) { if (i == szx || j == szz) return; if (x[i] == z[j]) { cout << x[i]; Build(i + 1, j + 1); } else { int ch1 = solve(i + 1, j); int ch2 = solve(i, j + 1); if (ch1 >= ch2) Build(i + 1, j); else Build(i, j + 1); } return; } int main() { cin >> x >> z; szx = SZ(x), szz = SZ(z); memset(dp, -1, sizeof(dp)); solve(0, 0); Build(0, 0); return 0; }
#include <bits/stdc++.h> #define ll long long #define ld long double #define endl "\n" #define ull unsigned long long #define ALL(v) ((v).begin()), ((v).end()) #define INCREASING(v) sort(ALL(v)) #define DECREASING(v) INCREASING(v), reverse(ALL(v)) #define SZ(v) ((int)((v).size())) #define LP(c, x, n) for (int c = x; c < n; c++) #define LO(c, n, x) for (int c = n; c >= x; c--) #define watch(x) cout << (#x) << " is " << (x) << endl #define PB push_back #define F first #define S second #define MP make_pair #define PI (2 * acos(0.0)) #define EPS 1e-8 #define INF 1.0 / 0.0 #define Yala_Besor3a \ ios_base::sync_with_stdio(0), ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define ReadFile freopen("delivery.in", "r", stdin) #define WriteFile freopen("output.txt", "w", stdout) using namespace std; const int OO = 0x3f3f3f3f, NegOO = -1 * OO, N = 3e3 + 5, mod = 998244353; int dp[N][N]; string x, z; int szx, szz; int solve(int i, int j) { if (i == szx || j == szz) return 0; int &rf = dp[i][j]; if (~rf) return rf; if (x[i] == z[j]) rf = max(1 + solve(i + 1, j + 1), rf); else rf = max(rf, max(solve(i + 1, j), solve(i, j + 1))); return rf; } void Build(int i, int j) { if (i == szx || j == szz) return; if (x[i] == z[j]) { cout << x[i]; Build(i + 1, j + 1); } else { int ch1 = solve(i + 1, j); int ch2 = solve(i, j + 1); if (ch1 >= ch2) Build(i + 1, j); else Build(i, j + 1); } return; } int main() { cin >> x >> z; szx = SZ(x), szz = SZ(z); memset(dp, -1, sizeof(dp)); solve(0, 0); Build(0, 0); return 0; }
replace
24
25
24
25
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #define M 100 int m, n; int MAT[M][M]; char str1[M], str2[M]; int LCS() { for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) { MAT[i][j] = 0; } else if (str1[i - 1] == str2[j - 1]) { MAT[i][j] = 1 + MAT[i - 1][j - 1]; } else { MAT[i][j] = max(MAT[i - 1][j], MAT[i][j - 1]); } } } return MAT[m][n]; } int main() { scanf("%s %s", &str1, &str2); m = strlen(str1); n = strlen(str2); // memset(dp,-1,sizeof(dp)); // int indx=lcs(m,n); int indx = LCS(); if (indx == 0) { printf(" \n"); return 0; } int i = m, j = n; char ans[indx + 1]; ans[indx] = '\0'; while (i > 0 && j > 0) { if (str1[i - 1] == str2[j - 1]) { ans[indx - 1] = str1[i - 1]; i--, j--, indx--; } else if (MAT[i - 1][j] > MAT[i][j - 1]) { i--; } else { j--; } } printf("%s\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #define M 3001 int m, n; int MAT[M][M]; char str1[M], str2[M]; int LCS() { for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) { MAT[i][j] = 0; } else if (str1[i - 1] == str2[j - 1]) { MAT[i][j] = 1 + MAT[i - 1][j - 1]; } else { MAT[i][j] = max(MAT[i - 1][j], MAT[i][j - 1]); } } } return MAT[m][n]; } int main() { scanf("%s %s", &str1, &str2); m = strlen(str1); n = strlen(str2); // memset(dp,-1,sizeof(dp)); // int indx=lcs(m,n); int indx = LCS(); if (indx == 0) { printf(" \n"); return 0; } int i = m, j = n; char ans[indx + 1]; ans[indx] = '\0'; while (i > 0 && j > 0) { if (str1[i - 1] == str2[j - 1]) { ans[indx - 1] = str1[i - 1]; i--, j--, indx--; } else if (MAT[i - 1][j] > MAT[i][j - 1]) { i--; } else { j--; } } printf("%s\n", ans); return 0; }
replace
8
9
8
9
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define M 1000000007 #define deb(x) cout << #x << " " << x << endl; #define foi(a, b) for (int i = a; i < b; i++) #define foj(a, b) for (int j = a; j < b; j++) #define fod(a, b) for (int i = a; i > b; i--) #define MAX 1e9 #define MIN -1e9 #define nl cout << "\n"; #define show(arr, a, b) \ for (int i = a; i < b; i++) \ cout << arr[i] << " "; \ cout << endl; #define mp(a, b) pair<ll, ll> p(a, b) #define pb push_back typedef pair<ll, ll> p1; typedef vector<ll> vecd; typedef vector<string> vecsd; typedef map<ll, ll> mpd; typedef set<ll> setd; typedef multiset<ll> msetd; ll dp[3002][3002]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s1, s2; cin >> s1 >> s2; foi(0, s1.length() + 1) { foj(0, s2.length() + 1) dp[i][j] = 0; } foi(0, s1.length() + 1) { foj(0, s2.length() + 1) { if (i == 0 || j == 0) dp[i][j] = 0; else { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } } string ans = ""; ll i = s1.length(), j = s2.length(); while (i > 0 || j > 0) { if (dp[i][j] == (dp[i - 1][j - 1] + 1)) { ans = s1[i - 1] + ans; i--; j--; } else { if (dp[i][j] == dp[i - 1][j]) i--; if (dp[i][j] == dp[i][j - 1]) j--; } } cout << ans << "\n"; // cout<<dp[s1.length()][s2.length()]<<"\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define M 1000000007 #define deb(x) cout << #x << " " << x << endl; #define foi(a, b) for (int i = a; i < b; i++) #define foj(a, b) for (int j = a; j < b; j++) #define fod(a, b) for (int i = a; i > b; i--) #define MAX 1e9 #define MIN -1e9 #define nl cout << "\n"; #define show(arr, a, b) \ for (int i = a; i < b; i++) \ cout << arr[i] << " "; \ cout << endl; #define mp(a, b) pair<ll, ll> p(a, b) #define pb push_back typedef pair<ll, ll> p1; typedef vector<ll> vecd; typedef vector<string> vecsd; typedef map<ll, ll> mpd; typedef set<ll> setd; typedef multiset<ll> msetd; ll dp[3002][3002]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s1, s2; cin >> s1 >> s2; foi(0, s1.length() + 1) { foj(0, s2.length() + 1) dp[i][j] = 0; } foi(0, s1.length() + 1) { foj(0, s2.length() + 1) { if (i == 0 || j == 0) dp[i][j] = 0; else { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } } string ans = ""; ll i = s1.length(), j = s2.length(); while (i > 0 && j > 0) { if (dp[i][j] == (dp[i - 1][j - 1] + 1) && s1[i - 1] == s2[j - 1]) { ans = s1[i - 1] + ans; i--; j--; } else { if (dp[i][j] == dp[i - 1][j]) i--; if (dp[i][j] == dp[i][j - 1]) j--; } } cout << ans << "\n"; // cout<<dp[s1.length()][s2.length()]<<"\n"; return 0; }
replace
46
48
46
48
0
p03165
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> #define ll long long #define pll pair<ll, ll> #define pii pair<int, int> #define pli pair<ll, int> #define pil pair<int, ll> #define mod 1000000007 #define mod1 1000000009 #define mod2 1000000021 #define INF 500001 #define MAX 1000000001 using namespace std; /* 🐣🐥 */ int dp[3002][3002], cp; char ans[3002]; int main() { char a[3002], b[3002]; int al, bl, ap, bp; scanf("%s %s", a + 1, b + 1); al = strlen(a + 1), bl = strlen(b + 1); for (int i = 1; i <= al; i++) { for (int j = 1; j <= bl; j++) { dp[i][j] = max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (a[i] == b[j])}); } } ap = al, bp = bl; while (ap >= 0 && bp >= 0) { if (dp[ap - 1][bp] == dp[ap][bp]) ap--; else if (dp[ap][bp - 1] == dp[ap][bp]) bp--; else { ans[cp++] = a[ap]; ap--, bp--; } } for (int i = cp - 1; i >= 0; i--) printf("%c", ans[i]); return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <unordered_set> #include <vector> #define ll long long #define pll pair<ll, ll> #define pii pair<int, int> #define pli pair<ll, int> #define pil pair<int, ll> #define mod 1000000007 #define mod1 1000000009 #define mod2 1000000021 #define INF 500001 #define MAX 1000000001 using namespace std; /* 🐣🐥 */ int dp[3002][3002], cp; char ans[3002]; int main() { char a[3002], b[3002]; int al, bl, ap, bp; scanf("%s %s", a + 1, b + 1); al = strlen(a + 1), bl = strlen(b + 1); for (int i = 1; i <= al; i++) { for (int j = 1; j <= bl; j++) { dp[i][j] = max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (a[i] == b[j])}); } } ap = al, bp = bl; while (ap >= 1 && bp >= 1) { if (dp[ap - 1][bp] == dp[ap][bp]) ap--; else if (dp[ap][bp - 1] == dp[ap][bp]) bp--; else { ans[cp++] = a[ap]; ap--, bp--; } } for (int i = cp - 1; i >= 0; i--) printf("%c", ans[i]); return 0; }
replace
40
41
40
41
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define MAX 3000 #define ll long long #define mod (1000000000 + 7) #define Fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); using namespace std; int T[MAX][MAX]; string LCS(string X, string Y, int m, int n) { if (m == 0 || n == 0) return string(""); if (X[m - 1] == Y[n - 1]) { return LCS(X, Y, m - 1, n - 1) + X[m - 1]; } if (T[m - 1][n] > T[m][n - 1]) return LCS(X, Y, m - 1, n); else return LCS(X, Y, m, n - 1); } int main() { string s, t; cin >> s >> t; int l = s.length(); int m = t.length(); for (int j = 0; j <= m; j++) T[0][j] = 0; for (int i = 0; i <= l; i++) T[i][0] = 0; for (int i = 1; i <= l; i++) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) T[i][j] = T[i - 1][j - 1] + 1; else T[i][j] = max(T[i - 1][j], T[i][j - 1]); } } cout << LCS(s, t, l, m); }
#include <bits/stdc++.h> #define MAX 3010 #define ll long long #define mod (1000000000 + 7) #define Fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); using namespace std; int T[MAX][MAX]; string LCS(string X, string Y, int m, int n) { if (m == 0 || n == 0) return string(""); if (X[m - 1] == Y[n - 1]) { return LCS(X, Y, m - 1, n - 1) + X[m - 1]; } if (T[m - 1][n] > T[m][n - 1]) return LCS(X, Y, m - 1, n); else return LCS(X, Y, m, n - 1); } int main() { string s, t; cin >> s >> t; int l = s.length(); int m = t.length(); for (int j = 0; j <= m; j++) T[0][j] = 0; for (int i = 0; i <= l; i++) T[i][0] = 0; for (int i = 1; i <= l; i++) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) T[i][j] = T[i - 1][j - 1] + 1; else T[i][j] = max(T[i - 1][j], T[i][j - 1]); } } cout << LCS(s, t, l, m); }
replace
1
2
1
2
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // #define int long long #define pb push_back #define F first #define S second #define endl "\n" #define rep(i, a, b) for (int i = a; i < (int)b; i++) #define req(i, a, b) for (int i = (int)b - 1; i >= a; i--) #define all(a) (a).begin(), (a).end() #define ret(x) \ { \ cout << x << endl; \ return; \ } #define sz(x) (int)x.size() #define type(x) typeid(x).name() typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<vector<int>> vii; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } #ifdef LOCAL #define wa(x) cerr << (#x) << " --- " << (x) << endl #define pvi(v) \ { \ for (auto it : v) \ cerr << it << " "; \ cerr << endl; \ } #define line1 cerr << "---------------------------" << endl; #else #define wa // #define line1 // #define pvi // #define printf // #endif void solve(); signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(10); int t = 1; // cin>>t; while (t--) { solve(); line1; } printf("Time : %fms\n", (1000 * (double)clock()) / CLOCKS_PER_SEC); return 0; } //___________________________________________________________________________________________________ void solve() { string a, b; cin >> a >> b; int la = a.size(); int lb = b.size(); // dp[i][j] : // Length of LCS when we consider the first i characters // of string_a and first j characters of string_b. vector<vi> dp(la + 1, vi(lb + 1, 0)); rep(i, 1, la + 1) { rep(j, 1, lb + 1) { if (a[i - 1] == b[j - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); else dp[i][j] = max({dp[i][j], dp[i - 1][j], dp[i][j - 1]}); } } int i = la, j = lb; string ans = {}; while (i != 0 || j != 0) { if (i > 0 && j > 0 && dp[i][j] == dp[i - 1][j - 1] + 1) { if (a[i - 1] == b[j - 1]) { ans += a[i - 1]; i--, j--; } } else if (j > 0 && dp[i][j] == dp[i][j - 1]) j--; else if (i > 0 && dp[i][j] == dp[i - 1][j]) i--; } reverse(all(ans)); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; // #define int long long #define pb push_back #define F first #define S second #define endl "\n" #define rep(i, a, b) for (int i = a; i < (int)b; i++) #define req(i, a, b) for (int i = (int)b - 1; i >= a; i--) #define all(a) (a).begin(), (a).end() #define ret(x) \ { \ cout << x << endl; \ return; \ } #define sz(x) (int)x.size() #define type(x) typeid(x).name() typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<vector<int>> vii; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } #ifdef LOCAL #define wa(x) cerr << (#x) << " --- " << (x) << endl #define pvi(v) \ { \ for (auto it : v) \ cerr << it << " "; \ cerr << endl; \ } #define line1 cerr << "---------------------------" << endl; #else #define wa // #define line1 // #define pvi // #define printf // #endif void solve(); signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(10); int t = 1; // cin>>t; while (t--) { solve(); line1; } printf("Time : %fms\n", (1000 * (double)clock()) / CLOCKS_PER_SEC); return 0; } //___________________________________________________________________________________________________ void solve() { string a, b; cin >> a >> b; int la = a.size(); int lb = b.size(); // dp[i][j] : // Length of LCS when we consider the first i characters // of string_a and first j characters of string_b. vector<vi> dp(la + 1, vi(lb + 1, 0)); rep(i, 1, la + 1) { rep(j, 1, lb + 1) { if (a[i - 1] == b[j - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); else dp[i][j] = max({dp[i][j], dp[i - 1][j], dp[i][j - 1]}); } } int i = la, j = lb; string ans = {}; while (i != 0 || j != 0) { if (i > 0 && j > 0 && a[i - 1] == b[j - 1]) { ans += a[i - 1]; i--, j--; } else if (j > 0 && dp[i][j] == dp[i][j - 1]) j--; else if (i > 0 && dp[i][j] == dp[i - 1][j]) i--; } reverse(all(ans)); cout << ans << endl; }
replace
84
89
84
87
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const ll MOD = 1000000007; // const ll MOD = 998244353; const ll INF = MOD * MOD; struct mint { ll x; mint(ll 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 { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } using edge = struct { ll to; ll cost; }; struct point { ll x; ll y; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } point &operator+=(const ll a) { x += a, y += a; return *this; } point &operator-=(const ll a) { x -= a, y -= a; return *this; } point &operator*=(const ll a) { x *= a, y *= a; return *this; } point &operator+=(const point p) { x += p.x, y += p.y; return *this; } point &operator-=(const point p) { x -= p.x, y -= p.y; return *this; } point &operator*=(const point p) { x *= p.x, y *= p.y; return *this; } void operator++(int) { x++, y++; } void operator++() { x++, y++; } void operator--(int) { x--, y--; } void operator--() { x--, y--; } point operator+(const point p) const { return point(*this) += p; } point operator-(const point p) const { return point(*this) -= p; } point operator*(const point p) const { return point(*this) *= p; } }; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::tuple<ll, ll, ll> tp3; typedef std::tuple<ll, ll, ll, ll> tp4; typedef std::map<string, ll> msl; typedef std::map<char, ll> mcl; typedef std::map<ll, ll> mll; typedef std::vector<ll> vl; typedef std::vector<vl> vl2; typedef std::vector<vl2> vl3; typedef std::vector<vl3> vl4; typedef std::vector<mint> vmi; typedef std::vector<vmi> vmi2; typedef std::vector<vmi2> vmi3; typedef std::vector<vmi3> vmi4; typedef std::vector<bool> vb; typedef std::vector<vb> vb2; typedef std::vector<vb2> vb3; typedef std::vector<vb3> vb4; typedef std::vector<pl> vpl; typedef std::vector<tp3> vtp3; typedef std::vector<tp4> vtp4; typedef std::vector<point> points; // priority queue. Taking from the higher value. Don't forget calling !q.empty() typedef std::priority_queue<ll> pq; // priority queue. Taking from the lower value typedef std::priority_queue<ll, vl, greater<ll>> pql; typedef std::vector<vector<edge>> Graph; const ll N_DIGITS = 60; const long double EPS = 1e-9; const long double PI = 3.14159265358979323846; points dirs = { {-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0} // self }; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(mint a) { return to_string(a.x); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define mt make_tuple #define pr(a) cout << (a) #define prl(a) cout << (a) << endl #define prl2(a, b) cout << (a) << " " << (b) << endl #define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) cout << (a) << " " #define prs2(a, b) cout << (a) << " " << (b) << " " #define prs3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e) #define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) #define in7(a, b, c, d, e, f, g) \ cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g) #define e1 first #define e2 second #define ltos(n) to_string((n)) #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ cout << fixed << setprecision(digits) << (num) << endl; #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) 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; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { // complexity O(EV^2) ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; struct UnionFind { vl parents, set_size; set<ll> root_idx; ll n_groups; UnionFind(ll n) { parents = set_size = vl(n); n_groups = n; rep(i, n) { parents[i] = i; set_size[i] = 1LL; root_idx.insert(i); } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; root_idx.erase(y); n_groups--; } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } ll num_union() const { return n_groups; } }; struct Doubling { // ABC167D ll n; ll sz; vl2 next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly input a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; class LowestCommonAncestor { public: ll N, logN; vl depth, len; Graph tree; vl2 parents; LowestCommonAncestor(ll n, ll offset = -1, bool is_weighted = true) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vl2(logN, vl(N)); tree = Graph(N); input(N, offset, is_weighted); init(0, -1, 0, 0); build(); } void add_edge(ll from, ll to, ll dist) { tree[from].pb({to, dist}); tree[to].pb({from, dist}); } void input(ll n, ll offset = -1, bool is_weighted = true) { rep(i, n - 1) { ll a, b, d = 1; in2(a, b); a += offset, b += offset; if (is_weighted) in1(d); add_edge(a, b, d); } } void init(ll source, ll parent, ll d, ll l) { depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()) { ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build() { rep(k, logN - 1) rep(n, N) { // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v) { if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN) { if (parents[k][u] != parents[k][v]) { u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v) { ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; struct BinaryIndexedTree { ll n, ini; vl dat; BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, d); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ if (i < 0) return ini; ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { // a[l] + a[l + 1] + ... + a[r - 1] + a[r] return query(r) - query(l - 1); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vb visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; struct StronglyConnectedComponents { ll n, n_cmp; vl2 graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return n_cmp = num; } }; struct CombinationMemo { ll sz, mod; vl facts, facts_inv, minv; CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) { facts.resize(sz + 5); facts_inv.resize(sz + 5); minv.resize(sz + 5); init(); } void init() { facts[0] = facts[1] = 1; minv[1] = 1; facts_inv[0] = facts_inv[1] = 1; For(i, 2, sz + 3) { facts[i] = (i * facts[i - 1]) % mod; minv[i] = mod - minv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * minv[i] % mod; } } ll nCk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; val *= facts_inv[r]; return val % mod; } }; struct PowerMemo { ll sz, mod, base; vl powB; PowerMemo(ll sz, ll base, ll _mod) : sz(sz), base(base), mod(_mod) { powB.resize(sz + 5); init(); } void init() { powB[0] = 1; rep(i, sz + 3) powB[i + 1] = (powB[i] * base) % mod; } ll operator[](ll k) { return powB[k]; } }; struct Grid2D { Graph graph; ll Width, Height; Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); } ll pos_to_idx(point p) { return p.y * Width + p.x; } point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; } bool undefined_region(point p, vb2 &block) { if (p.x < 0 || p.x > Width - 1) return true; if (p.y < 0 || p.y > Height - 1) return true; if (block[p.x][p.y]) return true; return false; } void build(vb2 &block, ll val = 1) { rep(x, Width) rep(y, Height) { point p = {x, y}; ll idx1 = pos_to_idx(p); ll idx2; if (block[x][y]) continue; rep(i, 4) { point nxt = p + dirs[i]; idx2 = pos_to_idx(nxt); if (!undefined_region(nxt, block)) graph[idx1].pb( {idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd) } } } }; struct Cumulative1D { vl cum; ll n; Cumulative1D(ll n) : n(n) { cum = vl(n + 1, 0); } void build(vl &vec) { rep(i, n) cum[i + 1] = vec[i] + cum[i]; } ll sum(ll l, ll r) { // vec[l] + vec[l + 1] + ... + vec[r] return cum[r + 1] - cum[l]; } }; struct Cumulative2D { vl2 cum; ll w, h; Cumulative2D(ll w, ll h) : w(w), h(h) { cum = vl2(w + 1, vl(h + 1, 0)); } template <typename T> void build(vector<T> &vec) { // never forget building rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vec[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; } ll func(ll x, ll y, ll dx, ll dy) { // 1-indexed // the rectangle of (x, y), (x + dx, y), (x, y + dy) and (x + dx, y + dy) // think about the case of (1, 1, 1, 1). if (x + dx > w || y + dy > h) return -INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } vl2 pascal_triangle(ll n) { /* Complexity: O(n^2) The upper bound of n is nearly 50. Parameters ---------- n; the size of returned vector Returns ------- comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i */ vl2 comb(n + 1, vl(n + 1)); comb[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb[i][j] += comb[i - 1][j]; if (j > 0) comb[i][j] += comb[i - 1][j - 1]; } return comb; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } ll bin_search(ll key, ll A[], ll left, ll right) { // return the index idx where A[idx] = key. // A[left] is start and A[right] is end.. // In other words, A[right], not A[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (A[mid] == key) return mid; else if (A[mid] > key) right = mid - 1; else if (A[mid] < key) left = mid + 1; } return -1; } /* // vs[lb - 1] < key <= vs[lb] lb = lower_bound(all(vs), key); // vs[ub - 1] <= key < vs[lb] ub = upper_bound(all(vs), key); ll bin_search_temp(ll left, ll right, callable judge){ while(right > left){ // when seeking lower bound ll mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; // when seeking upper bound ll mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } return right; } trinary_search // Care the value EPS!!! Compare to the condition ld left = 0; ld right = p; while(abs(right - left) > EPS){ ld left2 = (2 * left + right) / 3.0; ld right2 = (left + 2 * right) / 3.0; ld f1 = tak_func(left2); ld f2 = tak_func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } */ str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } mll prime_factorization(ll n) { ll i = 2; mll table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } ll next_combination(ll sub) { /* nCk ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; if (x != 0) return (((sub & ~y) / x) >> 1) | y; else return INF; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F vl z_algorithm(str s) { /* Paramters --------- str: the string of interest Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ ll n = s.length(); vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll big_number_mod(str s, ll mod) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth) % mod; val %= mod; tenth *= 10; tenth %= mod; idx++; } return val; } ll big_number_compare(str s1, str s2) { if (s1.length() > s2.length()) return 1; else if (s1.length() < s2.length()) return -1; else if (s1 == s2) return 0; return 2 * (s1 > s2) - 1; } ll string_to_ll(str s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth); tenth *= 10; idx++; } return val; } str reflected_string(str s) { str t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(point l_begin, point l_end, point p) { ll xl1 = l_begin.x, yl1 = l_begin.y; ll xl2 = l_end.x, yl2 = l_end.y; ll xp = p.x, yp = p.y; ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) { ll x1 = l1_begin.x, y1 = l1_begin.y; ll x2 = l1_end.x, y2 = l1_end.y; ll x3 = l2_begin.x, y3 = l2_begin.y; ll x4 = l2_end.x, y4 = l2_end.y; ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } ld space_of_triangle(point p1, point p2, point p3) { ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } pair<point, ll> center_2det_of_3points(point p1, point p2, point p3) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; // center of 2 points ll c2x_2 = x2 + x1, c2y_2 = y2 + y1; ll c3x_2 = x3 + x1, c3y_2 = y3 + y1; // vertical vector of 2 lines ll b2y = x2 - x1, b2x = -y2 + y1; ll b3y = x3 - x1, b3x = -y3 + y1; ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x; ll det = -b3y * b2x + b3x * b2y; if (det == 0) return {{INF, INF}, det}; ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2; return {{cx_2det, cy_2det}, det}; } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BinaryIndexedTree bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = rank + 1; j < n; j++) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { // O(ElogE) sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v) { ll n = v.size(); vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return lower_bound(all(dp), INF) - dp.begin(); } void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre, bool trace = false) { priority_queue<pl, vpl, greater<pl>> edge_costs; ll n = graph.size(); dist = vl(n, INF); if (trace) vertex_pre = vl(n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { ll idx, cost; tie(cost, idx) = edge_costs.top(); edge_costs.pop(); if (dist[idx] < cost) continue; for (auto e : graph[idx]) { if (dist[e.to] > dist[idx] + e.cost) { dist[e.to] = dist[idx] + e.cost; if (trace) vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g, vl &vertex_pre) { vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } void warshall_floyd(vl2 &dist) { ll n = dist.size(); // Dont forget the initialization // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // ABC061D bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) { rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) { dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) { // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != // -1) ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(n, goal, graph, dist)) return false; return true; } void read_graph(ll n_vertex, ll n_edges, Graph &graph, ll cost = -1, ll directed = false, ll offset = -1) { graph.resize(n_vertex); rep(i, n_edges) { ll from, to, c = cost; if (cost == -1) in3(from, to, c); else in2(from, to); from += offset, to += offset; graph[from].pb({to, cost}); if (!directed) graph[to].pb({from, cost}); } } void read_vector(ll n, vl &v, ll offset = 0) { v.resize(n); rep(i, n) { in1(v[i]); v[i] += offset; } } void read_pair_vector(ll n, vpl &v, ll offset1 = 0, ll offset2 = 0, bool rev = false) { v.resize(n); rep(i, n) { ll a, b; in2(a, b); a += offset1, b += offset2; if (!rev) v[i] = {a, b}; else v[i] = {b, a}; } } void read_2DMap(ll w, ll h, vb2 &block, char b) { block = vb2(w, vb(h, false)); str s; rep(y, h) { in1(s); rep(x, w) block[x][y] = (s[x] == b); } } /* diameter of tree Graph tree; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree */ /* # 2. The usage of next_permutation and combination (factorial search) ll a[8]; rep(i, 8) a[i] = i; sort(a, a + 8); do{ }while(next_permutation(a, a+n)); # 4. imos method // used when we would like to count the number which // shows how many times the numbers between l and r belongs to smt. // This method is composed of three process. ll n, m, s[MAX_M], l, r; in2(n, m); rep(i, m) s[i] = 0; // 1st step rep(i, n){ in3(l, r, c); l--; r--; // if l starts from 1. s[l] += c; s[r + 1] -= c; } // 2nd step rep(i, m - 1) s[i + 1] += s[i]; // 3rd step: judgement... #5. shakutori method (syakutori, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ if (l == r) r++; while(r < n && cond) r++; l++; } prl(answer); #11. bfs ABC146D, ABC007C 1. first create a tree. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = graph[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = graph[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } */ /* x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & ~ x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 ####### Attention ####### S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & ~(1 << i) -> S - {i} __builtin_popcountl(i) -> the number of 1 in binary #Conditional Operator condition ? true : false; # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; bool okay = false; ll answer = 0; str s, t, u; ll n, m, h, w, k, Q; ll a, b, c, d, idx, cnt; vl as, bs, cs, ds; vb2 block; vb visited; Graph graph; void solve() { in2(s, t); n = s.length(), m = t.length(); vl2 dp(n + 1, vl(m + 1, 0)); vector<vpl> pre(n + 1, vpl(m + 1, {0, 0})); For(i, 1, n + 1) For(j, 1, m + 1) { if (dp[i - 1][j] < dp[i][j - 1]) { dp[i][j] = dp[i][j - 1]; pre[i][j] = {i, j - 1}; } else { dp[i][j] = dp[i - 1][j]; pre[i][j] = {i - 1, j}; } if (s[i - 1] == t[j - 1]) { if (chmax(dp[i][j], dp[i - 1][j - 1] + 1)) pre[i][j] = {i - 1, j - 1}; } } str ans = ""; ll i = n, j = m; while (i != 0 && j != 0) { if (s[i - 1] == t[j - 1]) ans += s[i - 1]; tie(i, j) = pre[i][j]; } k = ans.length(); ll hoge = dp[n][m]; assert(k <= hoge); assert(k >= hoge); reverse(all(ans)); prl(ans); } void test(ll num = 0, bool verbose = false) { rep(i, max(1LL, num)) { ll t = clock(); if (verbose) prl4("\n#####", i + 1, "#####", "\n## Answer ##"); solve(); if (verbose) { prl(""); prl_time(t); } } } int main(void) { test(0, false); // test(4, true); // assert(n <= 30); // assert(k <= 400); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const ll MOD = 1000000007; // const ll MOD = 998244353; const ll INF = MOD * MOD; struct mint { ll x; mint(ll 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 { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } using edge = struct { ll to; ll cost; }; struct point { ll x; ll y; bool operator<(const point &p) const { if (x == p.x) return y < p.y; return x < p.x; } point &operator+=(const ll a) { x += a, y += a; return *this; } point &operator-=(const ll a) { x -= a, y -= a; return *this; } point &operator*=(const ll a) { x *= a, y *= a; return *this; } point &operator+=(const point p) { x += p.x, y += p.y; return *this; } point &operator-=(const point p) { x -= p.x, y -= p.y; return *this; } point &operator*=(const point p) { x *= p.x, y *= p.y; return *this; } void operator++(int) { x++, y++; } void operator++() { x++, y++; } void operator--(int) { x--, y--; } void operator--() { x--, y--; } point operator+(const point p) const { return point(*this) += p; } point operator-(const point p) const { return point(*this) -= p; } point operator*(const point p) const { return point(*this) *= p; } }; struct undirected_edge { ll from; ll to; ll cost; bool operator<(const undirected_edge &ue) const { return cost < ue.cost; } }; typedef string str; typedef std::pair<ll, ll> pl; typedef std::tuple<ll, ll, ll> tp3; typedef std::tuple<ll, ll, ll, ll> tp4; typedef std::map<string, ll> msl; typedef std::map<char, ll> mcl; typedef std::map<ll, ll> mll; typedef std::vector<ll> vl; typedef std::vector<vl> vl2; typedef std::vector<vl2> vl3; typedef std::vector<vl3> vl4; typedef std::vector<mint> vmi; typedef std::vector<vmi> vmi2; typedef std::vector<vmi2> vmi3; typedef std::vector<vmi3> vmi4; typedef std::vector<bool> vb; typedef std::vector<vb> vb2; typedef std::vector<vb2> vb3; typedef std::vector<vb3> vb4; typedef std::vector<pl> vpl; typedef std::vector<tp3> vtp3; typedef std::vector<tp4> vtp4; typedef std::vector<point> points; // priority queue. Taking from the higher value. Don't forget calling !q.empty() typedef std::priority_queue<ll> pq; // priority queue. Taking from the lower value typedef std::priority_queue<ll, vl, greater<ll>> pql; typedef std::vector<vector<edge>> Graph; const ll N_DIGITS = 60; const long double EPS = 1e-9; const long double PI = 3.14159265358979323846; points dirs = { {-1, 0}, {1, 0}, {0, 1}, {0, -1}, // four directions {1, 1}, {-1, 1}, {-1, -1}, {1, -1}, // diagonal {0, 0} // self }; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(mint a) { return to_string(a.x); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++) #define revrep(i, n) for (ll(i) = n - 1; (i) >= 0; (i)--) #define For(i, a, b) for (ll(i) = (a); (i) < (b); (i)++) #define revFor(i, b, a) for (ll(i) = (b)-1; (i) >= (a); (i)--) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define isUpper(c) ('a' - c > 0) #define isNum(c) (0 <= (c) - '0' && (c) - '0' <= 9) #define toLower(c) char((c) + 0x20) #define toUpper(c) char((c)-0x20) #define pb push_back #define mp make_pair #define mt make_tuple #define pr(a) cout << (a) #define prl(a) cout << (a) << endl #define prl2(a, b) cout << (a) << " " << (b) << endl #define prl3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl #define prl4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl #define prs(a) cout << (a) << " " #define prs2(a, b) cout << (a) << " " << (b) << " " #define prs3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " " #define prs4(a, b, c, d) \ cout << (a) << " " << (b) << " " << (c) << " " << (d) << " " #define yn(condition) \ if ((condition)) \ prl("Yes"); \ else \ prl("No"); #define YN(condition) \ if ((condition)) \ prl("YES"); \ else \ prl("NO"); #define in1(a) cin >> (a) #define in2(a, b) cin >> (a) >> (b) #define in3(a, b, c) cin >> (a) >> (b) >> (c) #define in4(a, b, c, d) cin >> (a) >> (b) >> (c) >> (d) #define in5(a, b, c, d, e) cin >> (a) >> (b) >> (c) >> (d) >> (e) #define in6(a, b, c, d, e, f) cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) #define in7(a, b, c, d, e, f, g) \ cin >> (a) >> (b) >> (c) >> (d) >> (e) >> (f) >> (g) #define e1 first #define e2 second #define ltos(n) to_string((n)) #define Forchar(c, a, z) for (char(c) = (a); (c) <= (z); (c)++) #define cntchar(s, c) count(all((s)), c) #define substring(s, start, end) s.substr((start), (end) - (start) + 1) #define prl_nd(num, digits) \ cout << fixed << setprecision(digits) << (num) << endl; #define prl_time(s) \ prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]"); #define char_to_str(c) string(1, (c)) #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) 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; } struct MaxFlow { struct F_edge { ll to, rev, capacity; F_edge(ll to, ll rev, ll capacity) : to(to), rev(rev), capacity(capacity) {} }; typedef vector<F_edge> F_edges; vector<F_edges> graph; ll n_vertex; // level is the shortest path to get a given node from the source node. vl level, iter; MaxFlow(ll n_vertex) : n_vertex(n_vertex) { graph.resize(n_vertex); } void add_edge(ll from, ll to, ll capacity) { graph[from].pb({to, ll(graph[to].size()), capacity}); graph[to].pb({from, ll(graph[from].size()) - 1, 0}); } void bfs(ll source) { level = vl(n_vertex, -1); level[source] = 0; queue<ll> q; q.push(source); while (!q.empty()) { ll vertex = q.front(); q.pop(); rep(i, graph[vertex].size()) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; // if the flow can be into the target node, implement below. if (cap_target > 0 && level[target] < 0) { level[target] = level[vertex] + 1; q.push(target); } } } } ll dfs(ll vertex, ll sink, ll flow) { if (vertex == sink) return flow; for (ll &i = iter[vertex]; i < graph[vertex].size(); i++) { ll target = graph[vertex][i].to; ll cap_target = graph[vertex][i].capacity; ll rev_target = graph[vertex][i].rev; // if capasitiy is not full yet and target is farther, // then assign current flow if (cap_target > 0 && level[vertex] < level[target]) { ll d = dfs(target, sink, min(cap_target, flow)); if (d > 0) { // if the flow successfully reaches the sink, reduce the // flow from the capacity graph[vertex][i].capacity -= d; graph[target][rev_target].capacity += d; return d; } } } return 0; } ll dinic(ll source, ll sink) { // complexity O(EV^2) ll flow = 0; while (true) { bfs(source); // if there is no path leading to the sink, the maximum flow is 0. if (level[sink] < 0) return flow; iter = vl(n_vertex, 0); ll f; while ((f = dfs(source, sink, INF)) > 0) flow += f; } } }; struct UnionFind { vl parents, set_size; set<ll> root_idx; ll n_groups; UnionFind(ll n) { parents = set_size = vl(n); n_groups = n; rep(i, n) { parents[i] = i; set_size[i] = 1LL; root_idx.insert(i); } } ll root_find(ll x) { if (parents[x] == x) return x; return parents[x] = root_find(parents[x]); } void unite(ll x, ll y) { // priority for x is larger than that of y x = root_find(x); y = root_find(y); if (x == y) return; parents[y] = x, set_size[x] += set_size[y]; root_idx.erase(y); n_groups--; } bool is_same(ll x, ll y) { // connected or not return root_find(x) == root_find(y); } ll size(ll x) { return set_size[root_find(x)]; } ll num_union() const { return n_groups; } }; struct Doubling { // ABC167D ll n; ll sz; vl2 next; /* next[k + 1][i] := next[k][next[k][i]] next[0][i] := edge[i] e.g. a0, a1, ..., an-1 / 0 <= ai <= n - 1 a0 -> a[a0] -> a[a[a0]] -> ... -> a[a[...[a[0]]]] (m times) Let the function repeatedly input a[i] m times be f[m](a[i]) - get(i, x) returns f[x](a[i]) - lower_bound(i, j) returns minimum x which satisfies f[x](a[i]) >= j. if not possible returns n. */ // edge[i]: the step size for one iteration Doubling(vl &edge) : n(edge.size()), sz(62) { next.resize(sz, vl(n, -1)); rep(i, n) next[0][i] = edge[i]; rep(k, sz - 1) rep(i, n) next[k + 1][i] = next[k][next[k][i]]; } ll get(ll i, ll x) { ll ret = i; rep(bit, sz) { if (!(x >> bit & 1)) continue; ret = next[bit][ret]; } return ret; } ll lower_bound(ll i, ll j) { ll cur = i, acc = 0; revrep(wid, sz) { if (next[wid][cur] < j) { acc += 1LL << wid; cur = next[wid][cur]; } } return min(n, acc + 1); } }; class LowestCommonAncestor { public: ll N, logN; vl depth, len; Graph tree; vl2 parents; LowestCommonAncestor(ll n, ll offset = -1, bool is_weighted = true) { N = n; logN = 0; while (N > (1LL << logN)) logN++; depth = vl(N); len = vl(N); parents = vl2(logN, vl(N)); tree = Graph(N); input(N, offset, is_weighted); init(0, -1, 0, 0); build(); } void add_edge(ll from, ll to, ll dist) { tree[from].pb({to, dist}); tree[to].pb({from, dist}); } void input(ll n, ll offset = -1, bool is_weighted = true) { rep(i, n - 1) { ll a, b, d = 1; in2(a, b); a += offset, b += offset; if (is_weighted) in1(d); add_edge(a, b, d); } } void init(ll source, ll parent, ll d, ll l) { depth[source] = d; parents[0][source] = parent; len[source] = l; rep(i, tree[source].size()) { ll target = tree[source][i].to; ll cost = tree[source][i].cost; if (target == parent) continue; init(target, source, d + 1, cost + l); } } void build() { rep(k, logN - 1) rep(n, N) { // if there is no parent, -1. // otherwise, the parent of the parent is the parent. if (parents[k][n] < 0) parents[k + 1][n] = -1; else parents[k + 1][n] = parents[k][parents[k][n]]; } } ll query(ll u, ll v) { if (depth[u] > depth[v]) swap(u, v); rep(k, logN) if ((depth[v] - depth[u]) >> k & 1) v = parents[k][v]; if (u == v) return u; revrep(k, logN) { if (parents[k][u] != parents[k][v]) { u = parents[k][u]; v = parents[k][v]; } } return parents[0][u]; } ll distance(ll u, ll v) { ll w = query(u, v); return len[u] + len[v] - 2 * len[w]; } }; struct BinaryIndexedTree { ll n, ini; vl dat; BinaryIndexedTree(ll n, ll ini = 0) : dat(n + 1, ini), n(n), ini(ini){}; // x: 1001 1010 1100 1011 1101 1111 // x & - x: 0001 0010 0100 0001 0001 0001 // ->: 1010 1100 10000 1100 1100 10000 ll update_func(ll val, ll d) { // if maximum -> max(val, dat) // return max(val, d); // if cumulative sum return val + d; } ll query(ll i) { /* v[0] + v[1] + ... + v[i] e.g.) i = 10101 itr1. 10101 -> 10100 itr2. 10100 -> 10000 itr3. 10000 -> 00000 (break) */ if (i < 0) return ini; ll ret = 0; for (ll j = i; j >= 0; j = (j & (j + 1)) - 1) { ret = update_func(ret, dat[j]); } return ret; } ll query(ll l, ll r) { // a[l] + a[l + 1] + ... + a[r - 1] + a[r] return query(r) - query(l - 1); } ll lower_bound(ll key) { // v[0] + v[1] + ... + v[left - 1] < key <= v[0] + v[1] + ... + v[left] if (key <= 0) return 0; ll left = 0, right = 1; while (right <= n) right *= 2; for (ll i = right; i > 0; i /= 2) { if (left + i <= n && dat[left + i - 1] < key) { key -= dat[left + i - 1]; left += i; } } return left; } void update(ll i, ll val) { /* e.g.) i = 10101, n = 11111 itr1. i: 10101, i+1: 10110 -> 10111 itr2. i: 10111, i+1: 11000 -> 11111 (break) */ if (i < 0) return; for (ll j = i; j < n; j |= j + 1) { dat[j] = update_func(val, dat[j]); } } }; struct SegmentTree { ll n, ini, minimize; vl dat; // when seeking minimum // ini = INF // when seeking maximum // ini = -INF SegmentTree(ll n_, bool minimize_ = true) { n = 1; minimize = minimize_; if (minimize) ini = INF; else ini = -INF; while (n < n_) n *= 2; dat.resize(2 * n - 1); rep(i, 2 * n - 1) dat[i] = ini; }; void update(ll idx, ll val) { idx += n - 1; if (minimize && dat[idx] <= val) return; if (!minimize && dat[idx] >= val) return; dat[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; // when seeking minimum if (minimize) dat[idx] = min(dat[idx * 2 + 1], dat[idx * 2 + 2]); // when seeking maximum else dat[idx] = max(dat[idx * 2 + 1], dat[idx * 2 + 2]); } } ll query(ll l, ll r) { // ### NOTE ### // the range is [l, r] // l, l + 1, ..., r r++; // to adjust to this method return query_segment(l, r, 0, 0, n); } ll query_segment(ll a, ll b, ll idx, ll l, ll r) { assert(a < b); if (r <= a || b <= l) return ini; if (a <= l && r <= b) return dat[idx]; else { ll seg1 = query_segment(a, b, idx * 2 + 1, l, (l + r) / 2); ll seg2 = query_segment(a, b, idx * 2 + 2, (l + r) / 2, r); // when seeking minimum if (minimize) return min(seg1, seg2); // when seeking maximum else return max(seg1, seg2); } } }; template <class Target> class RerootingTreeDP { public: using T = typename Target::type; struct DP_edge { ll to, rev; // rev is the index to trace the source node. T value; // objective value }; private: ll n; void dfs_fwd(ll source, ll parent) { ll par_idx = -1; vector<T> values; rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) { par_idx = i; continue; } dfs_fwd(e.to, source); values.pb(e.value); } // If the parent != -1, update the value on edge from parent to source if (par_idx != -1) { ll src_idx = tree[source][par_idx].rev; // update values on the edge from parent to source tree[parent][src_idx].value = Target::merge(values); } } void dfs_bwd(ll source, ll parent) { vector<T> values; for (auto &&e : tree[source]) values.pb(e.value); values = Target::evaluate(values); rep(i, tree[source].size()) { const DP_edge &e = tree[source][i]; if (e.to == parent) continue; // tree[e.to][e.rev]: e.to -> source tree[e.to][e.rev].value = values[i]; dfs_bwd(e.to, source); } } public: UnionFind uf; vector<vector<DP_edge>> tree; RerootingTreeDP(ll n) : n(n), uf(n), tree(n) {} void add_edge(ll u, ll v, T val) { assert(!uf.is_same(u, v)); tree[u].pb({v, ll(tree[v].size()), val}); tree[v].pb({u, ll(tree[u].size()) - 1, val}); uf.unite(u, v); } void dp() { vb visited(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_fwd(i, -1); visited[uf.root_find(i)] = true; } visited.assign(n, false); rep(i, n) { if (visited[uf.root_find(i)]) continue; dfs_bwd(i, -1); visited[uf.root_find(i)] = true; } } ll size() const { return tree.size(); } }; // ABC160F is one example // Modify the functions evaluate and merge based on given problems struct Merger { using type = ll; static type merge(const vector<type> &value) { // merge the result below the source node // each value is from each child node of the source node // value[i] := f(child i) // Here, we would like to obtain f(source) using f(child i) (i = 0, 1, ..., // n_children) ll ret = 1; for (auto &&v : value) ret += v; return ret; } static vector<type> evaluate(const vector<type> &value) { // value[i] := f(source -> child i) // we would like to obtain f(child i -> source) // child j (j != i) is the grandchildren of child i // represent f(child i -> source) using f(source -> j) (j != i) // L[i + 1] := the result using f(source -> k) (k = 0, 1, ..., i) // R[i] := the result using f(source -> k) (k = i, i + 1, ..., n_children) const ll n_children = value.size(); vl L(n_children + 1, 0), R(n_children + 1, 0); rep(i, n_children) L[i + 1] = L[i] + value[i]; revrep(i, n_children) R[i] = R[i + 1] + value[i]; vl ret(n_children); rep(i, n_children) ret[i] = L[i] + R[i + 1] + 1; return ret; } }; struct StronglyConnectedComponents { ll n, n_cmp; vl2 graph, graph_rev, dag, cmp; vl order, visited, cmp_idx; StronglyConnectedComponents() {} StronglyConnectedComponents(ll sz) : n(sz), graph(sz), graph_rev(sz), visited(sz), cmp_idx(sz) {} void add_edge(ll from, ll to) { graph[from].pb(to); graph_rev[to].pb(from); } void input(ll m, ll offset = -1) { ll a, b; rep(i, m) { in2(a, b); add_edge(a + offset, b + offset); } } ll operator[](ll k) { return cmp_idx[k]; } void dfs_fwd(ll source) { visited[source] = 1; rep(i, graph[source].size()) { ll target = graph[source][i]; if (!visited[target]) dfs_fwd(target); } order.pb(source); } void dfs_bwd(ll source, ll num) { visited[source] = 1, cmp_idx[source] = num; cmp[num].pb(source); rep(i, graph_rev[source].size()) { ll target = graph_rev[source][i]; if (!visited[target]) dfs_bwd(target, num); } } ll build() { fill(all(visited), 0); order.clear(); rep(i, n) if (!visited[i]) dfs_fwd(i); fill(all(visited), 0); ll num = 0; revrep(i, order.size()) { if (!visited[order[i]]) { dag.pb(vl()); cmp.pb(vl()); dfs_bwd(order[i], num++); } } rep(i, n) for (ll to : graph[i]) if (cmp_idx[i] != cmp_idx[to]) dag[cmp_idx[i]] .pb(cmp_idx[to]); rep(i, num) { sort(all(dag[i])); dag[i].erase(unique(all(dag[i])), dag[i].end()); } return n_cmp = num; } }; struct CombinationMemo { ll sz, mod; vl facts, facts_inv, minv; CombinationMemo(ll sz, ll _mod) : sz(sz), mod(_mod) { facts.resize(sz + 5); facts_inv.resize(sz + 5); minv.resize(sz + 5); init(); } void init() { facts[0] = facts[1] = 1; minv[1] = 1; facts_inv[0] = facts_inv[1] = 1; For(i, 2, sz + 3) { facts[i] = (i * facts[i - 1]) % mod; minv[i] = mod - minv[mod % i] * (mod / i) % mod; facts_inv[i] = facts_inv[i - 1] * minv[i] % mod; } } ll nCk(ll n, ll r) { if (n == r && n == 0) return 1; else if (n <= 0 || r < 0 || r > n) return 0; ll val = (facts[n] * facts_inv[n - r]) % mod; val *= facts_inv[r]; return val % mod; } }; struct PowerMemo { ll sz, mod, base; vl powB; PowerMemo(ll sz, ll base, ll _mod) : sz(sz), base(base), mod(_mod) { powB.resize(sz + 5); init(); } void init() { powB[0] = 1; rep(i, sz + 3) powB[i + 1] = (powB[i] * base) % mod; } ll operator[](ll k) { return powB[k]; } }; struct Grid2D { Graph graph; ll Width, Height; Grid2D(ll w, ll h) : Width(w), Height(h) { graph.resize(w * h); } ll pos_to_idx(point p) { return p.y * Width + p.x; } point idx_to_pos(ll idx) { return {idx % Width, idx / Width}; } bool undefined_region(point p, vb2 &block) { if (p.x < 0 || p.x > Width - 1) return true; if (p.y < 0 || p.y > Height - 1) return true; if (block[p.x][p.y]) return true; return false; } void build(vb2 &block, ll val = 1) { rep(x, Width) rep(y, Height) { point p = {x, y}; ll idx1 = pos_to_idx(p); ll idx2; if (block[x][y]) continue; rep(i, 4) { point nxt = p + dirs[i]; idx2 = pos_to_idx(nxt); if (!undefined_region(nxt, block)) graph[idx1].pb( {idx2, val}); // dist[idx1][idx2] = val; (warshall-floyd) } } } }; struct Cumulative1D { vl cum; ll n; Cumulative1D(ll n) : n(n) { cum = vl(n + 1, 0); } void build(vl &vec) { rep(i, n) cum[i + 1] = vec[i] + cum[i]; } ll sum(ll l, ll r) { // vec[l] + vec[l + 1] + ... + vec[r] return cum[r + 1] - cum[l]; } }; struct Cumulative2D { vl2 cum; ll w, h; Cumulative2D(ll w, ll h) : w(w), h(h) { cum = vl2(w + 1, vl(h + 1, 0)); } template <typename T> void build(vector<T> &vec) { // never forget building rep(x, w + 1) cum[x][0] = 0; rep(y, h + 1) cum[0][y] = 0; rep(y, h) rep(x, w) cum[x + 1][y + 1] = cum[x][y + 1] + vec[x][y]; rep(x, w + 1) rep(y, h) cum[x][y + 1] += cum[x][y]; } ll func(ll x, ll y, ll dx, ll dy) { // 1-indexed // the rectangle of (x, y), (x + dx, y), (x, y + dy) and (x + dx, y + dy) // think about the case of (1, 1, 1, 1). if (x + dx > w || y + dy > h) return -INF; ll val = cum[x + dx][y + dy]; val += cum[x][y]; val -= cum[x][y + dy]; val -= cum[x + dx][y]; return val; } }; ll gcd(ll m, ll n) { ll a = max(m, n); ll b = min(m, n); while (b != 1 && b != 0) { a %= b; swap(a, b); } return b == 1 ? 1 : a; } ll lcm(ll m, ll n) { return m / gcd(m, n) * n; } ll power_normal(ll a, ll power) { ll value = 1; while (power != 0) { if (power & 1) value = value * a; a = a * a; power = power >> 1; } return value; } vl2 pascal_triangle(ll n) { /* Complexity: O(n^2) The upper bound of n is nearly 50. Parameters ---------- n; the size of returned vector Returns ------- comb[i][j]: combination(i, j). 0 <= i <= n, 0 <= j <= i */ vl2 comb(n + 1, vl(n + 1)); comb[0][0] = 1; For(i, 1, n + 1) rep(j, i + 1) { comb[i][j] += comb[i - 1][j]; if (j > 0) comb[i][j] += comb[i - 1][j - 1]; } return comb; } ld log_combination(ll n, ll r) { if (n == r && n == 0) return 0; else if (n <= 0 || r < 0 || r > n) return -INF; ld val = 0; for (ll i = 0; i < r; i++) { val += log(n - i); val -= log(i + 1); } return val; } ll bin_search(ll key, ll A[], ll left, ll right) { // return the index idx where A[idx] = key. // A[left] is start and A[right] is end.. // In other words, A[right], not A[right - 1], must be defined. while (right >= left) { ll mid = left + (right - left) / 2; if (A[mid] == key) return mid; else if (A[mid] > key) right = mid - 1; else if (A[mid] < key) left = mid + 1; } return -1; } /* // vs[lb - 1] < key <= vs[lb] lb = lower_bound(all(vs), key); // vs[ub - 1] <= key < vs[lb] ub = upper_bound(all(vs), key); ll bin_search_temp(ll left, ll right, callable judge){ while(right > left){ // when seeking lower bound ll mid = (right + left) / 2; if (judge(mid)) right = mid; else left = mid + 1; // when seeking upper bound ll mid = (right + left + 1) / 2; if (judge(mid)) left = mid; else right = mid - 1; } return right; } trinary_search // Care the value EPS!!! Compare to the condition ld left = 0; ld right = p; while(abs(right - left) > EPS){ ld left2 = (2 * left + right) / 3.0; ld right2 = (left + 2 * right) / 3.0; ld f1 = tak_func(left2); ld f2 = tak_func(right2); if (f1 <= f2) right = right2; else if (f2 <= f1) left = left2; } */ str bin_expression(ll n, ll dig) { str s = ""; rep(i, dig) { s += ltos(n % 2); n /= 2; } reverse(all(s)); return s; } bool is_prime(ll n) { if (n <= 1) return false; for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } mll prime_factorization(ll n) { ll i = 2; mll table; while (i * i <= n) { while (n % i == 0) { table[i]++; n /= i; } i++; } if (n > 1) table[n] = 1; return table; } vl divisor_table(ll n) { vl table; ll i = 1; while (i * i <= n) { if (n % i == 0) { table.pb(i); if (i * i != n) table.pb(n / i); } i++; } sort(all(table)); return table; } ll next_combination(ll sub) { /* nCk ll bit = (1 << k) - 1; for (; bit < (1 << n); bit = next_combination(bit)){ bool ith = bit & (1 << i); procedures... } sub & -sub: the binary which shares the last digit whose value is 1 in sub sub + x : carry up the last digit ~y : the binary whose digits are 1 if y's digit is 0. (sub & ~y) / x: reduce the same number of 0s after first 1 in x from (sub & ~y). */ ll x = sub & -sub, y = sub + x; if (x != 0) return (((sub & ~y) / x) >> 1) | y; else return INF; } // just change the input if you want to change the target. // If you want to check the common sequences in two strings, // combine them. e.g. ABC150F vl z_algorithm(str s) { /* Paramters --------- str: the string of interest Returns ------- res[i] is the maximum number of K which satisfies s[:K] == s[i:i + K] for each i = 0, 1, 2, ..., n - 1. */ ll n = s.length(); vl res(n); res[0] = n; ll i1 = 1, i2 = 0; while (i1 < n) { /* i1: the starting point i2: the length of substring */ while (i1 + i2 < n && s[i2] == s[i1 + i2]) ++i2; res[i1] = i2; if (i2 == 0) { ++i1; continue; } ll i3 = 1; // update the already seen points while (i1 + i3 < n && i3 + res[i3] < i2) { res[i1 + i3] = res[i3]; ++i3; } // update up to i1 + i3 and the next possible minimum length is i2 - i3 (= // res[i3]) i1 += i3, i2 -= i3; } return res; } ll big_number_mod(str s, ll mod) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth) % mod; val %= mod; tenth *= 10; tenth %= mod; idx++; } return val; } ll big_number_compare(str s1, str s2) { if (s1.length() > s2.length()) return 1; else if (s1.length() < s2.length()) return -1; else if (s1 == s2) return 0; return 2 * (s1 > s2) - 1; } ll string_to_ll(str s) { ll l = s.length(); ll idx = 0; ll val = 0; ll tenth = 1; while (idx < l) { ll m = s[l - 1 - idx] - '0'; val += (m * tenth); tenth *= 10; idx++; } return val; } str reflected_string(str s) { str t, u; ll n = s.length(); t = s; reverse(all(t)); u = substring(t, 0, n - 2) + s + substring(t, 1, n - 1); return u; } ld distance_between_point_line(point l_begin, point l_end, point p) { ll xl1 = l_begin.x, yl1 = l_begin.y; ll xl2 = l_end.x, yl2 = l_end.y; ll xp = p.x, yp = p.y; ll a = yl2 - yl1; ll b = -xl2 + xl1; ll c = -a * xl2 - b * yl2; return abs(ld(a * xp + b * yp + c)) / ld(sqrt(a * a + b * b)); } bool is_cross(point l1_begin, point l1_end, point l2_begin, point l2_end) { ll x1 = l1_begin.x, y1 = l1_begin.y; ll x2 = l1_end.x, y2 = l1_end.y; ll x3 = l2_begin.x, y3 = l2_begin.y; ll x4 = l2_end.x, y4 = l2_end.y; ll val1 = (x1 - x2) * (y3 - y1) + (y1 - y2) * (x1 - x3); ll val2 = (x1 - x2) * (y4 - y1) + (y1 - y2) * (x1 - x4); ll val3 = (x3 - x4) * (y1 - y3) + (y3 - y4) * (x3 - x1); ll val4 = (x3 - x4) * (y2 - y3) + (y3 - y4) * (x3 - x2); return val1 * val2 < 0 && val3 * val4 < 0; } ld space_of_triangle(point p1, point p2, point p3) { ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; ll v1 = x2 - x1; ll u1 = y2 - y1; ll v2 = x3 - x1; ll u2 = y3 - y1; ld s = ld(v1 * u2 - u1 * v2) / ld(2); return abs(s); } pair<point, ll> center_2det_of_3points(point p1, point p2, point p3) { // the center of circle on the given three points // return the determinant value and the product of center points and 2 * // determinant value ll x1 = p1.x, y1 = p1.y; ll x2 = p2.x, y2 = p2.y; ll x3 = p3.x, y3 = p3.y; // center of 2 points ll c2x_2 = x2 + x1, c2y_2 = y2 + y1; ll c3x_2 = x3 + x1, c3y_2 = y3 + y1; // vertical vector of 2 lines ll b2y = x2 - x1, b2x = -y2 + y1; ll b3y = x3 - x1, b3x = -y3 + y1; ll x1_2 = c3x_2 * b3y - c3y_2 * b3x, y1_2 = c2x_2 * b2y - c2y_2 * b2x; ll det = -b3y * b2x + b3x * b2y; if (det == 0) return {{INF, INF}, det}; ll cx_2det = -b2x * x1_2 + b3x * y1_2, cy_2det = -b2y * x1_2 + b3y * y1_2; return {{cx_2det, cy_2det}, det}; } ll inversion_number(vl a, ll a_max) { /* Paramters --------- a: vector<ll> All the elements must be non-negative. Prefably the elements are compressed to reduce the computational cost. a_max: ll The maximum value of the vector a or the value bigger than the value stated previously. */ BinaryIndexedTree bit(a_max + 1); ll val = 0; rep(i, a.size()) { // i is the number of elements that have lower index than a[i]. // call the number of elements that have lower value than a[i] // by subtracting these two, the residual number is the number of elements // that have larger value val += i - bit.query(a[i] - 1); // cumulative sum from 0 to a[i] - 1 bit.update(a[i], 1); } return val; } template <typename T> vector<T> compress(vector<T> v) { // sort and remove all the duplicated values sort(all(v)); v.erase(unique(all(v)), v.end()); return v; } template <typename T> map<T, ll> dict(const vector<T> &v) { map<T, ll> d; rep(i, v.size()) d[v[i]] = i; return d; } points compress2D(vl xs, vl ys) { /* NOTE ---- Add the corner points if required */ ll n = xs.size(); vl xcs = compress(xs), ycs = compress(ys); map<ll, ll> xd = dict(xcs), yd = dict(ycs); points ps(n); rep(i, n) xs[i] = xd[xs[i]], ys[i] = yd[ys[i]]; rep(i, n) ps[i] = {xs[i], ys[i]}; sort(all(ps)); return ps; } void GaussJordanBitVector(vl &bs) { ll n = bs.size(); ll rank = 0; ll j = 0; revrep(i, N_DIGITS) { for (j = rank; j < n; j++) if (bs[j] & (1LL << i)) break; if (j == n) continue; if (j > rank) bs[rank] ^= bs[j]; for (j = rank + 1; j < n; j++) bs[j] = min(bs[j], bs[j] ^ bs[rank]); rank++; } } ll kruskal(vector<undirected_edge> &es, ll n_vertex) { // O(ElogE) sort(all(es)); UnionFind uf(n_vertex); ll min_cost = 0; rep(i, es.size()) { undirected_edge &e = es[i]; if (!uf.is_same(e.from, e.to)) { min_cost += e.cost; uf.unite(e.from, e.to); } } return min_cost; } ll LongestIncreasedSequence(vl &v) { ll n = v.size(); vl dp(n, INF); rep(i, n) * lower_bound(all(dp), v[i]) = v[i]; return lower_bound(all(dp), INF) - dp.begin(); } void dijkstra(ll start, Graph &graph, vl &dist, vl &vertex_pre, bool trace = false) { priority_queue<pl, vpl, greater<pl>> edge_costs; ll n = graph.size(); dist = vl(n, INF); if (trace) vertex_pre = vl(n, -1); dist[start] = 0; edge_costs.push(pl(0, start)); while (!edge_costs.empty()) { ll idx, cost; tie(cost, idx) = edge_costs.top(); edge_costs.pop(); if (dist[idx] < cost) continue; for (auto e : graph[idx]) { if (dist[e.to] > dist[idx] + e.cost) { dist[e.to] = dist[idx] + e.cost; if (trace) vertex_pre[e.to] = idx; edge_costs.push(pl(dist[e.to], e.to)); } } } } vl get_predecessor(ll g, vl &vertex_pre) { vl path; for (; g != -1; g = vertex_pre[g]) path.pb(g); reverse(all(path)); return path; } void warshall_floyd(vl2 &dist) { ll n = dist.size(); // Dont forget the initialization // rep(i, n) rep(j, n) dist[i][j] = INF * (i != j); rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // ABC061D bool find_negative_cycle(ll n, ll goal, Graph &graph, vl &dist) { rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[e.to] != INF && dist[e.to] > dist[v] + e.cost) { dist[e.to] = -INF; if (goal == -1) return true; else if (goal == e.to) return true; } } return false; } bool bellman_ford(ll start, ll goal, Graph &graph, vl &dist) { // if there is a closed circuit, it returns false. (when goal == -1) // if the distance to goal cannot be obtained, it returns false (when goal != // -1) ll n = graph.size(); dist = vl(n, INF); dist[start] = 0; rep(i, n) rep(v, n) rep(k, graph[v].size()) { edge e = graph[v][k]; if (dist[v] != INF && dist[e.to] > dist[v] + e.cost) dist[e.to] = dist[v] + e.cost; } if (find_negative_cycle(n, goal, graph, dist)) return false; return true; } void read_graph(ll n_vertex, ll n_edges, Graph &graph, ll cost = -1, ll directed = false, ll offset = -1) { graph.resize(n_vertex); rep(i, n_edges) { ll from, to, c = cost; if (cost == -1) in3(from, to, c); else in2(from, to); from += offset, to += offset; graph[from].pb({to, cost}); if (!directed) graph[to].pb({from, cost}); } } void read_vector(ll n, vl &v, ll offset = 0) { v.resize(n); rep(i, n) { in1(v[i]); v[i] += offset; } } void read_pair_vector(ll n, vpl &v, ll offset1 = 0, ll offset2 = 0, bool rev = false) { v.resize(n); rep(i, n) { ll a, b; in2(a, b); a += offset1, b += offset2; if (!rev) v[i] = {a, b}; else v[i] = {b, a}; } } void read_2DMap(ll w, ll h, vb2 &block, char b) { block = vb2(w, vb(h, false)); str s; rep(y, h) { in1(s); rep(x, w) block[x][y] = (s[x] == b); } } /* diameter of tree Graph tree; ll dM = 0, vM = 0, v2 = 0; void dfs1(ll source, ll parent, ll d){ if (d > dM) dM = d, vM = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs1(target, source, d + 1); } } void dfs2(ll source, ll parent, ll d){ if (dM <= d) dM = d, v2 = source; rep(i, tree[source].size()){ ll target = tree[source][i].to; if (target == parent) continue; dfs2(target, source, d + 1); } } dfs(0, -1, 0); dfs2(vM, -1, 0); prl2(vM + 1, v2 + 1); // the two edges of tree */ /* # 2. The usage of next_permutation and combination (factorial search) ll a[8]; rep(i, 8) a[i] = i; sort(a, a + 8); do{ }while(next_permutation(a, a+n)); # 4. imos method // used when we would like to count the number which // shows how many times the numbers between l and r belongs to smt. // This method is composed of three process. ll n, m, s[MAX_M], l, r; in2(n, m); rep(i, m) s[i] = 0; // 1st step rep(i, n){ in3(l, r, c); l--; r--; // if l starts from 1. s[l] += c; s[r + 1] -= c; } // 2nd step rep(i, m - 1) s[i + 1] += s[i]; // 3rd step: judgement... #5. shakutori method (syakutori, two pointers technique) // 1. strech right side while the condition is met. // 2. renew the answer // 3. increments left side // 4. Back to 1. (l <= r must be satisfied all the time.) ll l = 0; ll r = 0; while (l < n){ if (l == r) r++; while(r < n && cond) r++; l++; } prl(answer); #11. bfs ABC146D, ABC007C 1. first create a tree. 2. start searching from a node. 3. do some processes and push nodes connected with a given target node in BFS. 4. repeat a series of procedure until queue is empty. queue<pl> q; void bfs(ll source, ll parents){ ll n_edge = graph[source].size(); if (parents != -1) dist[source] = min(dist[source], dist[parents] + 1); if (visited[source]) return; visited[source] = true; rep(idx, n_edge){ ll target = graph[source][idx].to; if (target == parents) continue; q.push(mp(target, source)); } } q.push(mp(sg.e1, -1)); while(!q.empty()){ pl source = q.front(); q.pop(); bfs(source.e1, source.e2); } */ /* x1: 0000 0001 0010 0101 0110 0111 0111 x2: xxxx 0001 0011 0100 0101 1000 0110 x1 & x2: 0000 0001 0010 0100 0100 0000 0110 x: 1001 1010 1100 1011 1101 1111 x & ~ x: 0001 0010 0100 0001 0001 0001 sum: 1010 1100 10000 1100 1100 10000 ####### Attention ####### S & (1 << i) -> if true, i in S S | (1 << i) -> S union {i} S & ~(1 << i) -> S - {i} __builtin_popcountl(i) -> the number of 1 in binary #Conditional Operator condition ? true : false; # inclusion-exclusion principle |U[i = 1 to n] Ai| = sum[i = 1 to n] |Ai| - sum[i < j]|Ai ^ Aj| + ... + (-1)^(n - 1) |^[i = 1 to n]Ai| */ const ll MAX_N = 200005; bool okay = false; ll answer = 0; str s, t, u; ll n, m, h, w, k, Q; ll a, b, c, d, idx, cnt; vl as, bs, cs, ds; vb2 block; vb visited; Graph graph; void solve() { in2(s, t); n = s.length(), m = t.length(); vpl ps; rep(i, n) rep(j, m) if (s[i] == t[j]) ps.pb({i, -j}); sort(all(ps)); ll sz = ps.size(); vl ids(sz), dp(sz, INF); rep(i, sz) ids[i] = -ps[i].e2; ll leng = LongestIncreasedSequence(ids); vl num(sz, 0); rep(i, sz) { ll lb = lower_bound(all(dp), ids[i]) - dp.begin(); dp[lb] = ids[i]; num[i] = lb + 1; } ll cur = leng; string ans = ""; revrep(i, sz) { if (num[i] == cur) { ans += t[ids[i]]; cur--; } if (cur == 0) break; } reverse(all(ans)); prl(ans); } void test(ll num = 0, bool verbose = false) { rep(i, max(1LL, num)) { ll t = clock(); if (verbose) prl4("\n#####", i + 1, "#####", "\n## Answer ##"); solve(); if (verbose) { prl(""); prl_time(t); } } } int main(void) { test(0, false); // test(4, true); // assert(n <= 30); // assert(k <= 400); return 0; }
replace
1,698
1,728
1,698
1,726
0
p03165
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define mkp make_pair #define pr(num) cout << num << "\n" #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) #define INF 1000000000000000000 #define MOD 1000000007LL #define MAX 100100 using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef map<ll, ll> Map; string s, t; ll dp[3003][3003]; void printLcs(ll i, ll j) { if (i == 0 && j == 0) return; if (s[i - 1] == t[j - 1]) { printLcs(i - 1, j - 1); cout << s[i - 1]; } else if (dp[i - 1][j] >= dp[i][j - 1]) { printLcs(i - 1, j); } else { printLcs(i, j - 1); } } int main(void) { cin >> s >> t; ll i, j; ll slen = s.size(); ll tlen = t.size(); for (i = 1; i <= slen; i++) { for (j = 1; j <= tlen; j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } printLcs(slen, tlen); cout << "\n"; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define mkp make_pair #define pr(num) cout << num << "\n" #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) #define INF 1000000000000000000 #define MOD 1000000007LL #define MAX 100100 using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef map<ll, ll> Map; string s, t; ll dp[3003][3003]; void printLcs(ll i, ll j) { if (i == 0 || j == 0) return; if (s[i - 1] == t[j - 1]) { printLcs(i - 1, j - 1); cout << s[i - 1]; } else if (dp[i - 1][j] >= dp[i][j - 1]) { printLcs(i - 1, j); } else { printLcs(i, j - 1); } } int main(void) { cin >> s >> t; ll i, j; ll slen = s.size(); ll tlen = t.size(); for (i = 1; i <= slen; i++) { for (j = 1; j <= tlen; j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } printLcs(slen, tlen); cout << "\n"; }
replace
29
30
29
30
0
p03165
C++
Runtime Error
/*COMPETITIVE PROGRAMMING C++ TEMPLATE */ #include <algorithm> #include <climits> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define all(x) x.begin(), x.end() #define debug(x) cout << #x << " = " << x << "\n" #define MOD 1000000007 const long double PI = 3.141592653589793236L; typedef long long int ll; typedef long double ld; using namespace std; void solve() { string s; string t; cin >> s >> t; int n = s.size(); int m = t.size(); vector<vector<int>> dp(n + 1, vector<int>(m)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } string ans = ""; int i = n, j = m; while (i && j) { if (s[i - 1] == t[j - 1]) { ans += s[i - 1]; i--; j--; } else { if (dp[i][j - 1] > dp[i - 1][j]) { j--; } else { i--; } } } reverse(all(ans)); cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; // cin >> T; while (T--) { solve(); } return 0; }
/*COMPETITIVE PROGRAMMING C++ TEMPLATE */ #include <algorithm> #include <climits> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define all(x) x.begin(), x.end() #define debug(x) cout << #x << " = " << x << "\n" #define MOD 1000000007 const long double PI = 3.141592653589793236L; typedef long long int ll; typedef long double ld; using namespace std; void solve() { string s; string t; cin >> s >> t; int n = s.size(); int m = t.size(); vector<vector<int>> dp(n + 1, vector<int>(m + 1)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } string ans = ""; int i = n, j = m; while (i && j) { if (s[i - 1] == t[j - 1]) { ans += s[i - 1]; i--; j--; } else { if (dp[i][j - 1] > dp[i - 1][j]) { j--; } else { i--; } } } reverse(all(ans)); cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T = 1; // cin >> T; while (T--) { solve(); } return 0; }
replace
35
36
35
36
0
p03165
C++
Runtime Error
/* Simplicity and Goodness */ #include <bits/stdc++.h> using namespace std; #define scn(n) scanf("%d", &n) #define lscn(n) scanf("%lld", &n) #define pri(n) printf("%d ", (int)(n)) #define prin(n) printf("%d\n", (int)(n)) #define lpri(n) printf("%lld ", n) #define lprin(n) printf("%lld\n", n) #define deb(x) cout << #x << " = " << x << ' '; #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define tc \ int tt; \ scn(tt); \ while (tt--) #define pb push_back #define mp make_pair #define F first #define S second using ll = long long; const int inf = INT_MAX; const int ninf = INT_MIN; const int mod = 998244353; const int N = 2e5 + 2; int dp[1002][1002]; int main() { string s, ts; cin >> s >> ts; int n = (int)s.size(), m = (int)ts.size(); dp[0][0] = 0; rep(i, 1, n + 1) { rep(j, 1, m + 1) { if (s[i - 1] == ts[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } string ans = ""; int i = n, j = m; while (i > 0 and j > 0) { if (dp[i][j] > dp[i - 1][j] and dp[i][j] > dp[i][j - 1]) { ans += s[i - 1]; j--; i--; } else if (dp[i][j] == dp[i - 1][j]) { i--; } else { j--; } } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
/* Simplicity and Goodness */ #include <bits/stdc++.h> using namespace std; #define scn(n) scanf("%d", &n) #define lscn(n) scanf("%lld", &n) #define pri(n) printf("%d ", (int)(n)) #define prin(n) printf("%d\n", (int)(n)) #define lpri(n) printf("%lld ", n) #define lprin(n) printf("%lld\n", n) #define deb(x) cout << #x << " = " << x << ' '; #define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define tc \ int tt; \ scn(tt); \ while (tt--) #define pb push_back #define mp make_pair #define F first #define S second using ll = long long; const int inf = INT_MAX; const int ninf = INT_MIN; const int mod = 998244353; const int N = 2e5 + 2; int dp[3002][3002]; int main() { string s, ts; cin >> s >> ts; int n = (int)s.size(), m = (int)ts.size(); dp[0][0] = 0; rep(i, 1, n + 1) { rep(j, 1, m + 1) { if (s[i - 1] == ts[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } string ans = ""; int i = n, j = m; while (i > 0 and j > 0) { if (dp[i][j] > dp[i - 1][j] and dp[i][j] > dp[i][j - 1]) { ans += s[i - 1]; j--; i--; } else if (dp[i][j] == dp[i - 1][j]) { i--; } else { j--; } } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
replace
26
27
26
27
0
p03165
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; string lcs(string s1, string s2) { string ret; int n = s1.length(), m = s2.length(); vector<vector<int>> dp(n + 1, vector<int>(m + 1)); for (int i = 0; i < n; i++) { dp[i][0] = 0; } for (int i = 0; i < m; i++) { dp[0][i] = 0; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int i = n, j = m; while (i > 0 && j > 0) { while (dp[i][j] == dp[i][j - 1]) j--; while (dp[i][j] == dp[i - 1][j]) i--; ret.push_back(s2[j - 1]); i--; j--; } reverse(ret.begin(), ret.end()); return ret; } int main() { string s1, s2; cin >> s1 >> s2; cout << lcs(s1, s2); return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; string lcs(string s1, string s2) { string ret; int n = s1.length(), m = s2.length(); vector<vector<int>> dp(n + 1, vector<int>(m + 1)); for (int i = 0; i < n; i++) { dp[i][0] = 0; } for (int i = 0; i < m; i++) { dp[0][i] = 0; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int i = n, j = m; while (ret.length() < dp[n][m]) { while (dp[i][j] == dp[i][j - 1]) j--; while (dp[i][j] == dp[i - 1][j]) i--; ret.push_back(s2[j - 1]); i--; j--; } reverse(ret.begin(), ret.end()); return ret; } int main() { string s1, s2; cin >> s1 >> s2; cout << lcs(s1, s2); return 0; }
replace
26
27
26
27
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; ll dp[t.size() + 1][s.size() + 1]; for (ll i = 0; i <= s.size(); i++) dp[0][i] = 0; for (ll i = 0; i <= t.size(); i++) dp[i][0] = 0; for (ll i = 1; i <= t.size(); i++) { for (ll j = 1; j <= s.size(); j++) { if (t[i - 1] == s[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } vector<char> ans; if (dp[t.size()][s.size()] == 0) { cout << ""; return 0; } ll h = s.size(), v = t.size(); while (h != 0 || v != 0) { if (s[h - 1] == t[v - 1]) { ans.push_back(s[h - 1]); h--; v--; } else if (dp[v][h - 1] > dp[v - 1][h]) { h--; } else { v--; } } for (ll i = ans.size() - 1; i >= 0; i--) cout << ans[i]; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; ll dp[t.size() + 1][s.size() + 1]; for (ll i = 0; i <= s.size(); i++) dp[0][i] = 0; for (ll i = 0; i <= t.size(); i++) dp[i][0] = 0; for (ll i = 1; i <= t.size(); i++) { for (ll j = 1; j <= s.size(); j++) { if (t[i - 1] == s[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } vector<char> ans; if (dp[t.size()][s.size()] == 0) { cout << ""; return 0; } ll h = s.size(), v = t.size(); while (h != 0 && v != 0) { if (s[h - 1] == t[v - 1]) { ans.push_back(s[h - 1]); h--; v--; } else if (dp[v][h - 1] > dp[v - 1][h]) { h--; } else { v--; } } for (ll i = ans.size() - 1; i >= 0; i--) cout << ans[i]; }
replace
28
29
28
29
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fore(i, s, e) for (int i = s; i < e; i++) typedef long long ll; const int N = 3005; ll dp[N][N]; string s, t, r; ll calc(int i, int j) { if (i == s.size() || j == t.size()) return 0; ll &r = dp[i][j]; if (r != -1) return r; if (s[i] == t[j]) return r = 1 + calc(i + 1, j + 1); else return r = max(calc(i + 1, j), calc(i, j + 1)); } int main() { cin >> s >> t; memset(dp, -1, sizeof dp); calc(0, 0); int i = 0, j = 0; if (dp[0][0] != 0) while (i < s.size() || j < t.size()) { if (s[i] == t[j]) r += s[i], i++, j++; else if (dp[i + 1][j] > dp[i][j + 1]) i++; else j++; } cout << r << endl; return 0; } // https://atcoder.jp/contests/dp/tasks/dp_f
#include <bits/stdc++.h> using namespace std; #define fore(i, s, e) for (int i = s; i < e; i++) typedef long long ll; const int N = 3005; ll dp[N][N]; string s, t, r; ll calc(int i, int j) { if (i == s.size() || j == t.size()) return 0; ll &r = dp[i][j]; if (r != -1) return r; if (s[i] == t[j]) return r = 1 + calc(i + 1, j + 1); else return r = max(calc(i + 1, j), calc(i, j + 1)); } int main() { cin >> s >> t; memset(dp, -1, sizeof dp); calc(0, 0); int i = 0, j = 0; while (i < s.size() && j < t.size()) { if (s[i] == t[j]) r += s[i], i++, j++; else if (dp[i + 1][j] > dp[i][j + 1]) i++; else j++; } cout << r << endl; return 0; } // https://atcoder.jp/contests/dp/tasks/dp_f
replace
33
42
33
41
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define sz(x) (int)x.size() #define yes cout << "Yes\n" #define no cout << "No\n" #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define ft first #define sd second typedef pair<int, int> pii; typedef long double ld; typedef vector<int> vi; typedef vector<bool> vb; mt19937_64 rnd(time(0)); void start() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // cout << fixed << setprecision(10); // cerr << fixed << setprecision(10); #ifdef MY freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif } signed main() { start(); string s, t; cin >> s >> t; vector<vi> d(sz(s) + 1, vi(sz(t) + 1)); s = ' ' + s; t = ' ' + t; // d[i][j] - длина наибольшей общей подпоследовательности где взяли // <= i элементов из s и <= j элементов из t // i for (int i = 1; i < sz(s); ++i) { for (int j = 1; j < sz(t); ++j) { d[i][j] = max(d[i - 1][j], d[i][j - 1]); if (s[i] == t[j]) d[i][j] = max(d[i][j], d[i - 1][j - 1] + 1); } } int x = sz(s) - 1, y = sz(t) - 1; if (d[x][y]) { string ans; while (x && y) { if (s[x] == t[y]) { ans.push_back(s[x]); x--; y--; } else { if (d[x][y] == d[x - 1][y]) { x--; } else { y--; } } } reverse(all(ans)); cout << ans; } for (int i = 0; i < sz(s); ++i) { for (int j = 0; j < sz(t); ++j) cerr << d[i][j] << ' '; cerr << endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long #define sz(x) (int)x.size() #define yes cout << "Yes\n" #define no cout << "No\n" #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define ft first #define sd second typedef pair<int, int> pii; typedef long double ld; typedef vector<int> vi; typedef vector<bool> vb; mt19937_64 rnd(time(0)); void start() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // cout << fixed << setprecision(10); // cerr << fixed << setprecision(10); #ifdef MY freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif } signed main() { start(); string s, t; cin >> s >> t; vector<vi> d(sz(s) + 1, vi(sz(t) + 1)); s = ' ' + s; t = ' ' + t; // d[i][j] - длина наибольшей общей подпоследовательности где взяли // <= i элементов из s и <= j элементов из t // i for (int i = 1; i < sz(s); ++i) { for (int j = 1; j < sz(t); ++j) { d[i][j] = max(d[i - 1][j], d[i][j - 1]); if (s[i] == t[j]) d[i][j] = max(d[i][j], d[i - 1][j - 1] + 1); } } int x = sz(s) - 1, y = sz(t) - 1; if (d[x][y]) { string ans; while (x && y) { if (s[x] == t[y]) { ans.push_back(s[x]); x--; y--; } else { if (d[x][y] == d[x - 1][y]) { x--; } else { y--; } } } reverse(all(ans)); cout << ans; } }
delete
68
73
68
68
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define maxn 1005 using namespace std; char s[maxn], t[maxn], ans[maxn]; int n, m, cc, dp[maxn][maxn], px[maxn][maxn], py[maxn][maxn]; void dfs(int x, int y) { if (x == 0 && y == 0) return; int xx = px[x][y], yy = py[x][y]; if (xx < x && yy < y) ans[++cc] = s[xx]; dfs(xx, yy); } int main() { cin >> s >> t; n = strlen(s); m = strlen(t); for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) { if (!i && !j) continue; dp[i][j] = -1; if (i > 0 && dp[i][j] < dp[i - 1][j]) { px[i][j] = i - 1; py[i][j] = j; dp[i][j] = dp[i - 1][j]; } if (j > 0 && dp[i][j] < dp[i][j - 1]) { px[i][j] = i; py[i][j] = j - 1; dp[i][j] = dp[i][j - 1]; } if (i > 0 && j > 0 && dp[i][j] < dp[i - 1][j - 1] + (s[i - 1] == t[j - 1])) { px[i][j] = i - 1; py[i][j] = j - 1; dp[i][j] = dp[i - 1][j - 1] + (s[i - 1] == t[j - 1]); } } dfs(n, m); for (int i = cc; i; i--) printf("%c", ans[i]); puts(""); return 0; }
#include <bits/stdc++.h> #define maxn 3005 using namespace std; char s[maxn], t[maxn], ans[maxn]; int n, m, cc, dp[maxn][maxn], px[maxn][maxn], py[maxn][maxn]; void dfs(int x, int y) { if (x == 0 && y == 0) return; int xx = px[x][y], yy = py[x][y]; if (xx < x && yy < y) ans[++cc] = s[xx]; dfs(xx, yy); } int main() { cin >> s >> t; n = strlen(s); m = strlen(t); for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) { if (!i && !j) continue; dp[i][j] = -1; if (i > 0 && dp[i][j] < dp[i - 1][j]) { px[i][j] = i - 1; py[i][j] = j; dp[i][j] = dp[i - 1][j]; } if (j > 0 && dp[i][j] < dp[i][j - 1]) { px[i][j] = i; py[i][j] = j - 1; dp[i][j] = dp[i][j - 1]; } if (i > 0 && j > 0 && dp[i][j] < dp[i - 1][j - 1] + (s[i - 1] == t[j - 1])) { px[i][j] = i - 1; py[i][j] = j - 1; dp[i][j] = dp[i - 1][j - 1] + (s[i - 1] == t[j - 1]); } } dfs(n, m); for (int i = cc; i; i--) printf("%c", ans[i]); puts(""); return 0; }
replace
1
2
1
2
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef pair<ll, ll> pll; string s1, s2; int dp[3000][3000]; pii prv[3000][3000]; string ans; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s1 >> s2; for (int i = 1; i <= s1.size(); i++) for (int j = 1; j <= s2.size(); j++) { if (s1[i - 1] == s2[j - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); if (dp[i][j] == dp[i - 1][j - 1] + 1) prv[i][j] = {i - 1, j - 1}; if (dp[i][j] == dp[i][j - 1]) prv[i][j] = {i, j - 1}; if (dp[i][j] == dp[i - 1][j]) prv[i][j] = {i - 1, j}; } int pos1 = s1.size(), pos2 = s2.size(); while (pos1 != 0 && pos2 != 0) { if (prv[pos1][pos2] == pii(pos1 - 1, pos2 - 1)) { ans += s1[pos1 - 1]; pos1--, pos2--; } else if (prv[pos1][pos2] == pii(pos1 - 1, pos2)) pos1--; else if (prv[pos1][pos2] == pii(pos1, pos2 - 1)) pos2--; } reverse(ans.begin(), ans.end()); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef pair<ll, ll> pll; string s1, s2; int dp[3001][3001]; pii prv[3001][3001]; string ans; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s1 >> s2; for (int i = 1; i <= s1.size(); i++) for (int j = 1; j <= s2.size(); j++) { if (s1[i - 1] == s2[j - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); if (dp[i][j] == dp[i - 1][j - 1] + 1) prv[i][j] = {i - 1, j - 1}; if (dp[i][j] == dp[i][j - 1]) prv[i][j] = {i, j - 1}; if (dp[i][j] == dp[i - 1][j]) prv[i][j] = {i - 1, j}; } int pos1 = s1.size(), pos2 = s2.size(); while (pos1 != 0 && pos2 != 0) { if (prv[pos1][pos2] == pii(pos1 - 1, pos2 - 1)) { ans += s1[pos1 - 1]; pos1--, pos2--; } else if (prv[pos1][pos2] == pii(pos1 - 1, pos2)) pos1--; else if (prv[pos1][pos2] == pii(pos1, pos2 - 1)) pos2--; } reverse(ans.begin(), ans.end()); cout << ans << '\n'; return 0; }
replace
7
9
7
9
-11
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> //PBDS #include <ext/pb_ds/tree_policy.hpp> //PBDS #define int long long int #define vi vector<int> #define vii vector<pair<int, int>> #define pi pair<int, int> #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define pb push_back #define pf push_front #define fi first #define se second #define mi map<int, int> #define PI 3.141592653589 #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define f(i, x, n) for (int i = x; i < n; i++) #define rf(i, x, n) for (int i = n - 1; i >= x; i--) #define check(x) cerr << #x << " : " << x << endl; #define ordered_set \ tree<int, null_type, less_equal<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define fbo find_by_order // return iterator PBDS #define ook order_of_key // return number :)) PBDS #define baar exit(0) #define INF 1000000000000000000 using namespace __gnu_pbds; using namespace std; int powmod(int base, int exponent, int mod) { int ans = 1; while (exponent) { if (exponent & 1) ans = (ans * base) % mod; base = (base * base) % mod; exponent /= 2; } return ans; } string ans = ""; int LCS[3000][3000]; void findLCS(string text1, string text2, int n, int m) { if (n == 0 or m == 0) return; if (text1[n - 1] == text2[m - 1]) { ans += (text1[n - 1]); findLCS(text1, text2, n - 1, m - 1); } else { if (LCS[n - 1][m] >= LCS[n][m - 1]) { findLCS(text1, text2, n - 1, m); } else { findLCS(text1, text2, n, m - 1); } } } signed main() { fast string text1, text2; cin >> text1 >> text2; int n = text1.length(); int m = text2.length(); memset(LCS, 0, sizeof(LCS)); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 or j == 0) LCS[i][j] = 0; else if (text1[i - 1] == text2[j - 1]) LCS[i][j] = LCS[i - 1][j - 1] + 1; else LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]); } } findLCS(text1, text2, n, m); reverse(all(ans)); cout << ans; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> //PBDS #include <ext/pb_ds/tree_policy.hpp> //PBDS #define int long long int #define vi vector<int> #define vii vector<pair<int, int>> #define pi pair<int, int> #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define pb push_back #define pf push_front #define fi first #define se second #define mi map<int, int> #define PI 3.141592653589 #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define f(i, x, n) for (int i = x; i < n; i++) #define rf(i, x, n) for (int i = n - 1; i >= x; i--) #define check(x) cerr << #x << " : " << x << endl; #define ordered_set \ tree<int, null_type, less_equal<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define fbo find_by_order // return iterator PBDS #define ook order_of_key // return number :)) PBDS #define baar exit(0) #define INF 1000000000000000000 using namespace __gnu_pbds; using namespace std; int powmod(int base, int exponent, int mod) { int ans = 1; while (exponent) { if (exponent & 1) ans = (ans * base) % mod; base = (base * base) % mod; exponent /= 2; } return ans; } string ans = ""; int LCS[3005][3005]; void findLCS(string text1, string text2, int n, int m) { if (n == 0 or m == 0) return; if (text1[n - 1] == text2[m - 1]) { ans += (text1[n - 1]); findLCS(text1, text2, n - 1, m - 1); } else { if (LCS[n - 1][m] >= LCS[n][m - 1]) { findLCS(text1, text2, n - 1, m); } else { findLCS(text1, text2, n, m - 1); } } } signed main() { fast string text1, text2; cin >> text1 >> text2; int n = text1.length(); int m = text2.length(); memset(LCS, 0, sizeof(LCS)); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 or j == 0) LCS[i][j] = 0; else if (text1[i - 1] == text2[j - 1]) LCS[i][j] = LCS[i - 1][j - 1] + 1; else LCS[i][j] = max(LCS[i - 1][j], LCS[i][j - 1]); } } findLCS(text1, text2, n, m); reverse(all(ans)); cout << ans; return 0; }
replace
42
43
42
43
0
p03165
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int main(int argc, char *argv[]) { string s, t; cin >> s; cin >> t; // int dp[3003][3003]; int dp[3003][300]; std::fill(dp[0], dp[0] + 3003, 0); for (int i = 1; i <= s.size(); i++) { std::fill(dp[i], dp[i] + 300, 0); for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int i = s.size(), j = t.size(); string ret = ""; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ret += s[i - 1]; i--, j--; } else if (dp[i][j - 1] > dp[i - 1][j]) { j--; } else { i--; } } reverse(ret.begin(), ret.end()); puts(ret.c_str()); return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int main(int argc, char *argv[]) { string s, t; cin >> s; cin >> t; int dp[3003][3003]; std::fill(dp[0], dp[0] + 3003, 0); for (int i = 1; i <= s.size(); i++) { std::fill(dp[i], dp[i] + 300, 0); for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int i = s.size(), j = t.size(); string ret = ""; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ret += s[i - 1]; i--, j--; } else if (dp[i][j - 1] > dp[i - 1][j]) { j--; } else { i--; } } reverse(ret.begin(), ret.end()); puts(ret.c_str()); return 0; }
replace
18
20
18
19
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define umap unordered_map #define flush fflush(stdout) #define all(v) v.begin(), v.end() #define st first #define nd second #define ln '\n' #define MAX 1000000009 #define MOD 1000000007 #define N 3005 using namespace std; typedef long long ll; typedef pair<int, int> pii; string s1, s2; int LCS[N][N]; int main() { #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> s1 >> s2; for (int i = 0; i < s1.size(); i++) { for (int j = 0; j < s2.size(); j++) { if (s1[i] == s2[j]) { LCS[i][j] = 1 + (j == 0 || i == 0 ? 0 : LCS[i - 1][j - 1]); } else { LCS[i][j] = max((i == 0 ? 0 : LCS[i - 1][j]), (j == 0 ? 0 : LCS[i][j - 1])); } } } int i = s1.size() - 1, j = s2.size() - 1; string ans = ""; while (i != -1 && j != -1) { if (s1[i] == s2[j]) { ans += s1[i]; i--; j--; } else if (LCS[i - 1][j] > LCS[i][j - 1]) i--; else j--; } reverse(all(ans)); cout << ans; }
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define umap unordered_map #define flush fflush(stdout) #define all(v) v.begin(), v.end() #define st first #define nd second #define ln '\n' #define MAX 1000000009 #define MOD 1000000007 #define N 3005 using namespace std; typedef long long ll; typedef pair<int, int> pii; string s1, s2; int LCS[N][N]; int main() { #ifndef ONLINE_JUDGE // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> s1 >> s2; for (int i = 0; i < s1.size(); i++) { for (int j = 0; j < s2.size(); j++) { if (s1[i] == s2[j]) { LCS[i][j] = 1 + (j == 0 || i == 0 ? 0 : LCS[i - 1][j - 1]); } else { LCS[i][j] = max((i == 0 ? 0 : LCS[i - 1][j]), (j == 0 ? 0 : LCS[i][j - 1])); } } } int i = s1.size() - 1, j = s2.size() - 1; string ans = ""; while (i != -1 && j != -1) { if (s1[i] == s2[j]) { ans += s1[i]; i--; j--; } else if ((i == 0 ? 0 : LCS[i - 1][j]) > (j == 0 ? 0 : LCS[i][j - 1])) i--; else j--; } reverse(all(ans)); cout << ans; }
replace
43
44
43
44
0
p03165
C++
Runtime Error
// ¯\_(ツ)_/¯ #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define pb push_back #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define ll long long #define pi pair<int, int> const int N = 1e5, MOD = 998244353; using namespace std; string a, b; int dp[3005][3005]; int main() { ios::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE freopen("tst.txt", "r", stdin); #endif cin >> a >> b; for (int i = 0; i < sz(a); i++) { for (int j = 0; j < sz(b); j++) { if (a[i] == b[j]) dp[i + 1][j + 1] = 1 + dp[i][j]; else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } string ans; pair<int, int> curr = {sz(a), sz(b)}; while (curr.first != 0 && curr.second != 0) { int i = curr.first, j = curr.second; if (a[i - 1] == b[j - 1]) ans.pb(a[i - 1]), curr = {i - 1, j - 1}; else { if (dp[i - 1][j] > dp[i][j - 1]) curr = {i - 1, j}; else curr = {i, j - 1}; } } reverse(all(ans)); cout << ans << '\n'; }
// ¯\_(ツ)_/¯ #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define pb push_back #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define ll long long #define pi pair<int, int> const int N = 1e5, MOD = 998244353; using namespace std; string a, b; int dp[3005][3005]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> a >> b; for (int i = 0; i < sz(a); i++) { for (int j = 0; j < sz(b); j++) { if (a[i] == b[j]) dp[i + 1][j + 1] = 1 + dp[i][j]; else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } string ans; pair<int, int> curr = {sz(a), sz(b)}; while (curr.first != 0 && curr.second != 0) { int i = curr.first, j = curr.second; if (a[i - 1] == b[j - 1]) ans.pb(a[i - 1]), curr = {i - 1, j - 1}; else { if (dp[i - 1][j] > dp[i][j - 1]) curr = {i - 1, j}; else curr = {i, j - 1}; } } reverse(all(ans)); cout << ans << '\n'; }
delete
20
23
20
20
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ln '\n'; int lim = 1e6; int inf = 1e9 + 7; ll dp[3001][3001]; pair<int, int> nextdp[3001][3001]; ll solve(int i, int j, int n1, int n2, string s1, string s2) { if (i == n1 or j == n2) { return 0; } if (dp[i][j] != -1) { return dp[i][j]; } else { ll a1 = -1; if (s1[i] == s2[j]) { a1 = 1 + solve(i + 1, j + 1, n1, n2, s1, s2); nextdp[i][j] = make_pair(i + 1, j + 1); // final.push_back(s1[i]); dp[i][j] = a1; return a1; } ll a2 = solve(i + 1, j, n1, n2, s1, s2); ll a3 = solve(i, j + 1, n1, n2, s1, s2); if (a2 > a3) { dp[i][j] = a2; nextdp[i][j] = make_pair(i + 1, j); } else { dp[i][j] = a3; nextdp[i][j] = make_pair(i, j + 1); } return dp[i][j]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s, t; cin >> s >> t; int n1 = s.size(), n2 = t.size(); for (int i = 0; i < 3001; i++) { for (int j = 0; j < 3001; j++) { dp[i][j] = -1; nextdp[i][j] = make_pair(-1, -1); } } ll ans = solve(0, 0, n1, n2, s, t); int i = 0, j = 0; string final = ""; while (true) { if (i == n1 or j == n2) { break; } if (s[i] == t[j]) { final += string(1, s[i]); } pair<int, int> p = nextdp[i][j]; i = p.first; j = p.second; } cout << final; cout << ln; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ln '\n'; int lim = 1e6; int inf = 1e9 + 7; ll dp[3001][3001]; pair<int, int> nextdp[3001][3001]; ll solve(int i, int j, int n1, int n2, string &s1, string &s2) { if (i == n1 or j == n2) { return 0; } if (dp[i][j] != -1) { return dp[i][j]; } else { ll a1 = -1; if (s1[i] == s2[j]) { a1 = 1 + solve(i + 1, j + 1, n1, n2, s1, s2); nextdp[i][j] = make_pair(i + 1, j + 1); // final.push_back(s1[i]); dp[i][j] = a1; return a1; } ll a2 = solve(i + 1, j, n1, n2, s1, s2); ll a3 = solve(i, j + 1, n1, n2, s1, s2); if (a2 > a3) { dp[i][j] = a2; nextdp[i][j] = make_pair(i + 1, j); } else { dp[i][j] = a3; nextdp[i][j] = make_pair(i, j + 1); } return dp[i][j]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s, t; cin >> s >> t; int n1 = s.size(), n2 = t.size(); for (int i = 0; i < 3001; i++) { for (int j = 0; j < 3001; j++) { dp[i][j] = -1; nextdp[i][j] = make_pair(-1, -1); } } ll ans = solve(0, 0, n1, n2, s, t); int i = 0, j = 0; string final = ""; while (true) { if (i == n1 or j == n2) { break; } if (s[i] == t[j]) { final += string(1, s[i]); } pair<int, int> p = nextdp[i][j]; i = p.first; j = p.second; } cout << final; cout << ln; }
replace
8
9
8
9
TLE
p03165
C++
Runtime Error
#include "bits/stdc++.h" #include <string> using namespace std; int n1, n2; short dp[3000][3000]; char bt[3000][3000]; int main() { char a[3000], b[3000]; cin >> a; cin >> b; n1 = strlen(a); n2 = strlen(b); for (int i = 0; i <= n1; i++) { for (int j = 0; j <= n2; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else { if (dp[i - 1][j] > dp[i][j - 1]) { bt[i][j] = 0; dp[i][j] = dp[i - 1][j]; } else { bt[i][j] = 1; dp[i][j] = dp[i][j - 1]; } if (a[i - 1] == b[j - 1]) { if (dp[i - 1][j - 1] + 1 > dp[i][j]) { dp[i][j] = dp[i - 1][j - 1] + 1; bt[i][j] = 2; } } } } } int answer = dp[n1][n2]; if (answer < 1) { cout << ""; return 0; } vector<char> res; int i = n1; int j = n2; while (i > 0 && j > 0) { if (bt[i][j] == 0) { i--; } else if (bt[i][j] == 1) { j--; } else if (bt[i][j] == 2) { res.push_back(a[i - 1]); i--; j--; } } reverse(res.begin(), res.end()); for (auto c : res) { cout << c; } return 0; }
#include "bits/stdc++.h" #include <string> using namespace std; int n1, n2; short dp[3001][3001]; char bt[3001][3001]; int main() { char a[3000], b[3000]; cin >> a; cin >> b; n1 = strlen(a); n2 = strlen(b); for (int i = 0; i <= n1; i++) { for (int j = 0; j <= n2; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else { if (dp[i - 1][j] > dp[i][j - 1]) { bt[i][j] = 0; dp[i][j] = dp[i - 1][j]; } else { bt[i][j] = 1; dp[i][j] = dp[i][j - 1]; } if (a[i - 1] == b[j - 1]) { if (dp[i - 1][j - 1] + 1 > dp[i][j]) { dp[i][j] = dp[i - 1][j - 1] + 1; bt[i][j] = 2; } } } } } int answer = dp[n1][n2]; if (answer < 1) { cout << ""; return 0; } vector<char> res; int i = n1; int j = n2; while (i > 0 && j > 0) { if (bt[i][j] == 0) { i--; } else if (bt[i][j] == 1) { j--; } else if (bt[i][j] == 2) { res.push_back(a[i - 1]); i--; j--; } } reverse(res.begin(), res.end()); for (auto c : res) { cout << c; } return 0; }
replace
6
8
6
8
0
p03165
C++
Runtime Error
#include <algorithm> #include <array> #include <assert.h> #include <bits/stdc++.h> #include <bitset> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <math.h> #include <queue> #include <sstream> // istringstream buffer(myString); #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <tuple> #include <unordered_map> #include <unordered_set> // #include <boost/functional/hash.hpp> using namespace std; #define endl "\n"; #define bit(x, i) (x & (1 << i)) // select the bit of position i of x #define lowbit(x) \ ((x) & \ ((x) ^ \ ((x)-1))) // get the lowest bit of x; example: x=12; 1100 => 100 => 4 #define LL long long #define ULL unsigned long long #define inf 1000000007 #define mod 1000000007 #define PII pair<int, int> #define V vector #define VI vector<int> #define VS vector<string> #define ALL(x) x.begin(), x.end() // handy for function like "sort()" #define pi 3.14159265358979323846 // set a to the maximum of a and b #define REMAX(a, b) (a) = max((a), (b)); #define REMIN(a, b) (a) = min((a), (b)); // conversion string removeZeros(string str) { return str.erase(0, min(str.find_first_not_of('0'), str.size() - 1)); } // remove leading zeros #define char2int(c) (c - '0') #define int2char(i) (static_cast<char>(i)) #define bin2int(bin) (bitset<1001>(bin).to_ulong()) #define int2bin(i) removeZeros(bitset<1001>(i).to_string()) // str.erase(0, min(str.find_first_not_of('0'), str.size()-1)); // remove // leading zeros void reverseStr(string &str) { int n = str.length(); for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]); } // works for unordered_set, unordered_map.. #define HMAP unordered_map #define SET unordered_set #define HAS(umap, key) (umap.find(key) != umap.end()) #define COUNTER(m, j) \ if (HAS(m, j)) { \ m[j]++; \ } else { \ m[j] = 1; \ } // loop #define FOREACH(vec, i) for (auto i = vec.begin(); i != vec.end(); i++) // read line #define READ_INTS(vec, n) \ vector<LL> vec(n); \ for (LL i = 0; i < n; i++) { \ cin >> vec[i]; \ }; \ cin.ignore(); #define READ_DOUBLES(vec, n) \ vector<double> vec(n); \ FOREACH(vec, i) { cin >> *i; }; \ cin.ignore(); #define READ_STRING(vec) \ string vec; \ getline(cin, vec); #define READ_INT(i) \ LL i; \ cin >> i; \ cin.ignore(); // #define SPLIT_STRINGS(vec, str) istringstream iss(str); vector<string> // vec((istream_iterator<string>(iss)), istream_iterator<string>()); debug #define dd(var) cerr << #var << "=" << (var) << " | " #define ddv(vec) \ FOREACH(vec, i) { cout << *i << " "; } \ cout << endl; #define ddvv(vec) \ for (auto &i : vec) { \ for (auto &j : i) { \ cout << j << " "; \ }; \ cout << endl \ }; void solve() { READ_STRING(A); READ_STRING(B); const int len_A = A.size(); const int len_B = B.size(); vector<vector<int>> dp(len_A + 1, vector<int>(len_B + 1)); for (LL i = 1; i <= len_A; i++) { for (LL j = 1; j <= len_B; j++) { if (A[i - 1] == B[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int i = len_A; int j = len_B; string res = ""; while (i > 0 || j > 0) { if (A[i - 1] == B[j - 1]) { res += A[i - 1]; i--; j--; } else if (dp[i][j - 1] > dp[i - 1][j]) { j--; } else { i--; } } reverseStr(res); cout << res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // READ_INT(t); // for (LL i = 0; i < t; i++) { solve(); // } return 0; }
#include <algorithm> #include <array> #include <assert.h> #include <bits/stdc++.h> #include <bitset> #include <deque> #include <fstream> #include <iomanip> #include <iostream> #include <math.h> #include <queue> #include <sstream> // istringstream buffer(myString); #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <tuple> #include <unordered_map> #include <unordered_set> // #include <boost/functional/hash.hpp> using namespace std; #define endl "\n"; #define bit(x, i) (x & (1 << i)) // select the bit of position i of x #define lowbit(x) \ ((x) & \ ((x) ^ \ ((x)-1))) // get the lowest bit of x; example: x=12; 1100 => 100 => 4 #define LL long long #define ULL unsigned long long #define inf 1000000007 #define mod 1000000007 #define PII pair<int, int> #define V vector #define VI vector<int> #define VS vector<string> #define ALL(x) x.begin(), x.end() // handy for function like "sort()" #define pi 3.14159265358979323846 // set a to the maximum of a and b #define REMAX(a, b) (a) = max((a), (b)); #define REMIN(a, b) (a) = min((a), (b)); // conversion string removeZeros(string str) { return str.erase(0, min(str.find_first_not_of('0'), str.size() - 1)); } // remove leading zeros #define char2int(c) (c - '0') #define int2char(i) (static_cast<char>(i)) #define bin2int(bin) (bitset<1001>(bin).to_ulong()) #define int2bin(i) removeZeros(bitset<1001>(i).to_string()) // str.erase(0, min(str.find_first_not_of('0'), str.size()-1)); // remove // leading zeros void reverseStr(string &str) { int n = str.length(); for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]); } // works for unordered_set, unordered_map.. #define HMAP unordered_map #define SET unordered_set #define HAS(umap, key) (umap.find(key) != umap.end()) #define COUNTER(m, j) \ if (HAS(m, j)) { \ m[j]++; \ } else { \ m[j] = 1; \ } // loop #define FOREACH(vec, i) for (auto i = vec.begin(); i != vec.end(); i++) // read line #define READ_INTS(vec, n) \ vector<LL> vec(n); \ for (LL i = 0; i < n; i++) { \ cin >> vec[i]; \ }; \ cin.ignore(); #define READ_DOUBLES(vec, n) \ vector<double> vec(n); \ FOREACH(vec, i) { cin >> *i; }; \ cin.ignore(); #define READ_STRING(vec) \ string vec; \ getline(cin, vec); #define READ_INT(i) \ LL i; \ cin >> i; \ cin.ignore(); // #define SPLIT_STRINGS(vec, str) istringstream iss(str); vector<string> // vec((istream_iterator<string>(iss)), istream_iterator<string>()); debug #define dd(var) cerr << #var << "=" << (var) << " | " #define ddv(vec) \ FOREACH(vec, i) { cout << *i << " "; } \ cout << endl; #define ddvv(vec) \ for (auto &i : vec) { \ for (auto &j : i) { \ cout << j << " "; \ }; \ cout << endl \ }; void solve() { READ_STRING(A); READ_STRING(B); const int len_A = A.size(); const int len_B = B.size(); vector<vector<int>> dp(len_A + 1, vector<int>(len_B + 1)); for (LL i = 1; i <= len_A; i++) { for (LL j = 1; j <= len_B; j++) { if (A[i - 1] == B[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int i = len_A; int j = len_B; string res = ""; while (i > 0 && j > 0) { if (A[i - 1] == B[j - 1]) { res += A[i - 1]; i--; j--; } else if (dp[i][j - 1] > dp[i - 1][j]) { j--; } else { i--; } } reverseStr(res); cout << res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); // READ_INT(t); // for (LL i = 0; i < t; i++) { solve(); // } return 0; }
replace
122
123
122
123
0
p03165
C++
Time Limit Exceeded
/* """Bismillahir Rahmanur Rahim""" */ #include <bits/stdc++.h> using namespace std; #define pi 2 * acos(0.0) #define ll long long int #define pb push_back #define pf push_front const ll sz = 1000001; #define ek(x) __builtin_popcount(x) #define ses '\n' #define stm istringstream #define sob(x) x.begin(), x.end() #define ghora \ ios_base::sync_with_stdio(0); \ cin.tie(0); #define gcd __gcd ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); } #define tin \ ll T; \ cin >> T; \ for (ll o = 1; o <= T; o++) #define tout cout << "Case " << o << ": "; string s, t; ll l1, l2; ll dp[3010][3010]; ll lcs(ll i, ll j) { if (i == l1 || j == l2) return 0; if (dp[i][j] > 0) return dp[i][j]; ll ans; if (s[i] == t[j]) ans = 1 + lcs(i + 1, j + 1); else { ll v1 = lcs(i + 1, j); ll v2 = lcs(i, j + 1); ans = max(v1, v2); } return dp[i][j] = ans; } string ans; void slcs(ll i, ll j) { if (i == l1 || j == l2) { cout << ans << ses; return; } if (s[i] == t[j]) { ans += s[i]; slcs(i + 1, j + 1); } else { if (dp[i + 1][j] > dp[i][j + 1]) slcs(i + 1, j); else slcs(i, j + 1); } } int main() { cin >> s >> t; memset(dp, -1, sizeof(dp)); l1 = s.size(); l2 = t.size(); ll x = lcs(0, 0); // cout<<x<<ses; slcs(0, 0); // cout<<"o"<<ses; return 0; } /* -------------------- | ~SOHAN~ | | ~Chandler68~ | -------------------- || VALAR MORGULIS||==|| ALL MEN MUST DIE || \\ Power Is Power// || I Can Do This All day || // We are on a Break \\ // How you doin'? \\ || Say My Name || ~~ || I Am The Who Knocks || // I Am Ted Mosby Architect \\ || It Is Legen --wait for it -- dary ,Legendary || \\ Penny - Penny - Penny // -- Bazinga */
/* """Bismillahir Rahmanur Rahim""" */ #include <bits/stdc++.h> using namespace std; #define pi 2 * acos(0.0) #define ll long long int #define pb push_back #define pf push_front const ll sz = 1000001; #define ek(x) __builtin_popcount(x) #define ses '\n' #define stm istringstream #define sob(x) x.begin(), x.end() #define ghora \ ios_base::sync_with_stdio(0); \ cin.tie(0); #define gcd __gcd ll lcm(ll x, ll y) { return (x * y) / gcd(x, y); } #define tin \ ll T; \ cin >> T; \ for (ll o = 1; o <= T; o++) #define tout cout << "Case " << o << ": "; string s, t; ll l1, l2; ll dp[3010][3010]; ll lcs(ll i, ll j) { if (i == l1 || j == l2) return 0; if (dp[i][j] != -1) return dp[i][j]; ll ans; if (s[i] == t[j]) ans = 1 + lcs(i + 1, j + 1); else { ll v1 = lcs(i + 1, j); ll v2 = lcs(i, j + 1); ans = max(v1, v2); } return dp[i][j] = ans; } string ans; void slcs(ll i, ll j) { if (i == l1 || j == l2) { cout << ans << ses; return; } if (s[i] == t[j]) { ans += s[i]; slcs(i + 1, j + 1); } else { if (dp[i + 1][j] > dp[i][j + 1]) slcs(i + 1, j); else slcs(i, j + 1); } } int main() { cin >> s >> t; memset(dp, -1, sizeof(dp)); l1 = s.size(); l2 = t.size(); ll x = lcs(0, 0); // cout<<x<<ses; slcs(0, 0); // cout<<"o"<<ses; return 0; } /* -------------------- | ~SOHAN~ | | ~Chandler68~ | -------------------- || VALAR MORGULIS||==|| ALL MEN MUST DIE || \\ Power Is Power// || I Can Do This All day || // We are on a Break \\ // How you doin'? \\ || Say My Name || ~~ || I Am The Who Knocks || // I Am Ted Mosby Architect \\ || It Is Legen --wait for it -- dary ,Legendary || \\ Penny - Penny - Penny // -- Bazinga */
replace
33
34
33
34
TLE
p03166
C++
Time Limit Exceeded
#include "bits/stdc++.h" #pragma GCC optimize "03" using namespace std; #define int long long int #define pb push_back #define pii pair<int, int> #define fi first #define se second #define rep(i, a, b) for (int i = a; i < b; ++i) #define dbg(x) \ { cerr << "> " << #x << ": " << x << endl; } #define dbg2(x, y) \ { cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << endl; } #define dbg3(x, y, z) \ { \ cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << " , " << #z \ << ": " << z << endl; \ } #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #ifndef LOCAL #define endl '\n' #endif const int inf = INT_MAX; const double eps = 0.0000001; const double PI = acos(-1.0); const int MOD = 1e9 + 7; const int N = 1e5 + 5; vector<int> g[N]; int dp[N]; int solve(int n) { if (dp[n]) return dp[n]; for (auto i : g[n]) { dp[n] = max(dp[n], 1 + solve(i)); } return dp[n]; } signed main() { #ifdef LOCAL freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif IOS; int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int a, b; cin >> a >> b; g[a].pb(b); } } int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, solve(i)); cout << ans; return 0; }
#include "bits/stdc++.h" #pragma GCC optimize "03" using namespace std; #define int long long int #define pb push_back #define pii pair<int, int> #define fi first #define se second #define rep(i, a, b) for (int i = a; i < b; ++i) #define dbg(x) \ { cerr << "> " << #x << ": " << x << endl; } #define dbg2(x, y) \ { cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << endl; } #define dbg3(x, y, z) \ { \ cerr << "> " << #x << ": " << x << " , " << #y << ": " << y << " , " << #z \ << ": " << z << endl; \ } #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #ifndef LOCAL #define endl '\n' #endif const int inf = INT_MAX; const double eps = 0.0000001; const double PI = acos(-1.0); const int MOD = 1e9 + 7; const int N = 1e5 + 5; vector<int> g[N]; int dp[N]; int solve(int n) { if (dp[n]) return dp[n]; for (auto i : g[n]) { dp[n] = max(dp[n], 1 + solve(i)); } return dp[n]; } signed main() { #ifdef LOCAL freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif IOS; int n, m; cin >> n >> m; for (int j = 0; j < m; j++) { int a, b; cin >> a >> b; g[a].pb(b); } int ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, solve(i)); cout << ans; return 0; }
replace
53
59
53
57
TLE
p03166
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) #define REP(i, k, n) for (long long i = k; i < (long long)(n); i++) #define all(a) a.begin(), a.end() #define pb push_back #define eb emplace_back #define lb(v, k) (lower_bound(all(v), k) - v.begin()) #define ub(v, k) (upper_bound(all(v), k) - v.begin()) #define fi first #define se second #define pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T, vector<T>, greater<T>> #define dame(a) \ { \ out(a); \ return 0; \ } #define decimal cout << fixed << setprecision(15); typedef long long ll; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> PP; typedef tuple<ll, ll, ll, ll> PPP; typedef multiset<ll> S; using vi = vector<ll>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vp = vector<P>; using vvp = vector<vp>; using vb = vector<bool>; using vvb = vector<vb>; const ll inf = 1001001001001001001; const int INF = 1001001001; const int mod = 1000000007; const double eps = 1e-10; template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> void out(T a) { cout << a << '\n'; } template <class T> void outp(T a) { cout << '(' << a.fi << ',' << a.se << ')' << '\n'; } template <class T> void outvp(T v) { rep(i, v.size()) cout << '(' << v[i].fi << ',' << v[i].se << ')'; cout << '\n'; } template <class T> void outvvp(T v) { rep(i, v.size()) outvp(v[i]); } template <class T> void outv(T v) { rep(i, v.size()) { if (i) cout << ' '; cout << v[i]; } cout << '\n'; } template <class T> void outvv(T v) { rep(i, v.size()) outv(v[i]); } template <class T> bool isin(T x, T l, T r) { return (l) <= (x) && (x) <= (r); } template <class T> void yesno(T b) { if (b) out("yes"); else out("no"); } template <class T> void YesNo(T b) { if (b) out("Yes"); else out("No"); } template <class T> void YESNO(T b) { if (b) out("YES"); else out("NO"); } template <class T> void noyes(T b) { if (b) out("no"); else out("yes"); } template <class T> void NoYes(T b) { if (b) out("No"); else out("Yes"); } template <class T> void NOYES(T b) { if (b) out("NO"); else out("YES"); } void outs(ll a, ll b) { if (a >= inf - 100) out(b); else out(a); } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll modpow(ll a, ll b) { a %= mod; if (b == 0) return 1; if (b & 1) return a * modpow(a, b - 1) % mod; ll k = modpow(a, b / 2); return k * k % mod; } vvi g; vi dp; ll dfs(int i) { if (dp[i] != -1) return dp[i]; ll res = 0; for (ll x : g[i]) chmax(res, dfs(x) + 1); return res; } int main() { ll n, m; cin >> n >> m; g = vvi(n); rep(i, m) { ll a, b; cin >> a >> b; a--; b--; g[a].pb(b); } dp = vi(n, -1); ll ans = 0; rep(i, n) chmax(ans, dfs(i)); out(ans); }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (long long i = 0; i < (long long)(n); i++) #define REP(i, k, n) for (long long i = k; i < (long long)(n); i++) #define all(a) a.begin(), a.end() #define pb push_back #define eb emplace_back #define lb(v, k) (lower_bound(all(v), k) - v.begin()) #define ub(v, k) (upper_bound(all(v), k) - v.begin()) #define fi first #define se second #define pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T, vector<T>, greater<T>> #define dame(a) \ { \ out(a); \ return 0; \ } #define decimal cout << fixed << setprecision(15); typedef long long ll; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> PP; typedef tuple<ll, ll, ll, ll> PPP; typedef multiset<ll> S; using vi = vector<ll>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vp = vector<P>; using vvp = vector<vp>; using vb = vector<bool>; using vvb = vector<vb>; const ll inf = 1001001001001001001; const int INF = 1001001001; const int mod = 1000000007; const double eps = 1e-10; template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> void out(T a) { cout << a << '\n'; } template <class T> void outp(T a) { cout << '(' << a.fi << ',' << a.se << ')' << '\n'; } template <class T> void outvp(T v) { rep(i, v.size()) cout << '(' << v[i].fi << ',' << v[i].se << ')'; cout << '\n'; } template <class T> void outvvp(T v) { rep(i, v.size()) outvp(v[i]); } template <class T> void outv(T v) { rep(i, v.size()) { if (i) cout << ' '; cout << v[i]; } cout << '\n'; } template <class T> void outvv(T v) { rep(i, v.size()) outv(v[i]); } template <class T> bool isin(T x, T l, T r) { return (l) <= (x) && (x) <= (r); } template <class T> void yesno(T b) { if (b) out("yes"); else out("no"); } template <class T> void YesNo(T b) { if (b) out("Yes"); else out("No"); } template <class T> void YESNO(T b) { if (b) out("YES"); else out("NO"); } template <class T> void noyes(T b) { if (b) out("no"); else out("yes"); } template <class T> void NoYes(T b) { if (b) out("No"); else out("Yes"); } template <class T> void NOYES(T b) { if (b) out("NO"); else out("YES"); } void outs(ll a, ll b) { if (a >= inf - 100) out(b); else out(a); } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll modpow(ll a, ll b) { a %= mod; if (b == 0) return 1; if (b & 1) return a * modpow(a, b - 1) % mod; ll k = modpow(a, b / 2); return k * k % mod; } vvi g; vi dp; ll dfs(int i) { if (dp[i] != -1) return dp[i]; ll res = 0; for (ll x : g[i]) chmax(res, dfs(x) + 1); dp[i] = res; return res; } int main() { ll n, m; cin >> n >> m; g = vvi(n); rep(i, m) { ll a, b; cin >> a >> b; a--; b--; g[a].pb(b); } dp = vi(n, -1); ll ans = 0; rep(i, n) chmax(ans, dfs(i)); out(ans); }
insert
133
133
133
134
TLE
p03166
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define SORT(c) sort((c).begin(), (c).end()) typedef long long ll; typedef pair<int, int> P; typedef vector<int> V; typedef map<int, int> M; constexpr ll INF = 1e18; constexpr ll MOD = 1e9 + 7; constexpr double PI = 3.14159265358979323846; constexpr int dx[] = {0, 0, 1, -1}; constexpr int dy[] = {1, -1, 0, 0}; int n, m, x[112345], y[112345]; V g[112345]; int dist[112345]; int dfs(int i) { if (dist[i] != -1) return dist[i]; if (!g[i].size()) return 0; int res = 0; REP(j, g[i].size()) res = max(res, dfs(g[i][j])); return res + 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; REP(i, m) { cin >> x[i] >> y[i]; g[x[i]].push_back(y[i]); } memset(dist, -1, sizeof(dist)); int res = 0; for (int i = 1; i <= n; i++) res = max(res, dfs(i)); cout << res << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define SORT(c) sort((c).begin(), (c).end()) typedef long long ll; typedef pair<int, int> P; typedef vector<int> V; typedef map<int, int> M; constexpr ll INF = 1e18; constexpr ll MOD = 1e9 + 7; constexpr double PI = 3.14159265358979323846; constexpr int dx[] = {0, 0, 1, -1}; constexpr int dy[] = {1, -1, 0, 0}; int n, m, x[112345], y[112345]; V g[112345]; int dist[112345]; int dfs(int i) { if (dist[i] != -1) return dist[i]; if (!g[i].size()) return 0; int res = 0; REP(j, g[i].size()) res = max(res, dfs(g[i][j])); dist[i] = res + 1; return res + 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; REP(i, m) { cin >> x[i] >> y[i]; g[x[i]].push_back(y[i]); } memset(dist, -1, sizeof(dist)); int res = 0; for (int i = 1; i <= n; i++) res = max(res, dfs(i)); cout << res << endl; return 0; }
insert
49
49
49
51
TLE
p03166
C++
Time Limit Exceeded
// includes #include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdint> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> // macros #define ll long long int #define pb push_back #define mk make_pair #define pq priority_queue #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) using namespace std; // types typedef pair<int, int> P; typedef pair<ll, int> Pl; typedef pair<ll, ll> Pll; typedef pair<double, double> Pd; // constants const int inf = 1e9; const ll linf = 1LL << 50; const double EPS = 1e-10; // solve vector<int> edge[100001]; ll dp[100001]; ll dfs(int x) { if (edge[x].size() == 0) return 0; if (dp[x] >= 0) return dp[x]; ll res = 0; rep(i, edge[x].size()) { res = max(res, 1 + dfs(edge[x][i])); } return res; } int main(int argc, char const *argv[]) { int n, m; cin >> n >> m; rep(i, m) { int x, y; cin >> x >> y; x--; y--; edge[x].pb(y); } fill(dp, dp + n + 1, -1); ll res = 0; rep(i, n) { res = max(res, dfs(i)); } cout << res << endl; return 0; }
// includes #include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdint> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <utility> #include <vector> // macros #define ll long long int #define pb push_back #define mk make_pair #define pq priority_queue #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) using namespace std; // types typedef pair<int, int> P; typedef pair<ll, int> Pl; typedef pair<ll, ll> Pll; typedef pair<double, double> Pd; // constants const int inf = 1e9; const ll linf = 1LL << 50; const double EPS = 1e-10; // solve vector<int> edge[100001]; ll dp[100001]; ll dfs(int x) { if (edge[x].size() == 0) return 0; if (dp[x] >= 0) return dp[x]; ll res = 0; rep(i, edge[x].size()) { res = max(res, 1 + dfs(edge[x][i])); } return dp[x] = res; } int main(int argc, char const *argv[]) { int n, m; cin >> n >> m; rep(i, m) { int x, y; cin >> x >> y; x--; y--; edge[x].pb(y); } fill(dp, dp + n + 1, -1); ll res = 0; rep(i, n) { res = max(res, dfs(i)); } cout << res << endl; return 0; }
replace
51
52
51
52
TLE
p03166
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int f(vector<vector<int>> &E, vector<int> &dp, int s) { if (dp[s] != -1) { return dp[s]; } else { int ans = 0; for (int i = 0; i < E[s].size(); i++) { ans = max(ans, f(E, dp, E[s][i]) + 1); } return ans; } } int main() { int N, M; cin >> N >> M; vector<vector<int>> E(N); for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; x--; y--; E[x].push_back(y); } vector<int> dp(N, -1); int ans = 0; for (int i = 0; i < N; i++) { ans = max(ans, f(E, dp, i)); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int f(vector<vector<int>> &E, vector<int> &dp, int s) { if (dp[s] != -1) { return dp[s]; } else { int ans = 0; for (int i = 0; i < E[s].size(); i++) { ans = max(ans, f(E, dp, E[s][i]) + 1); } dp[s] = ans; return ans; } } int main() { int N, M; cin >> N >> M; vector<vector<int>> E(N); for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; x--; y--; E[x].push_back(y); } vector<int> dp(N, -1); int ans = 0; for (int i = 0; i < N; i++) { ans = max(ans, f(E, dp, i)); } cout << ans << endl; }
insert
10
10
10
11
TLE
p03166
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; int N, M; vector<int> next_verts[100000]; vector<int> longest_path_length_from_vert(100000, -1); int calc_longest_path_from(int now) { if (longest_path_length_from_vert[now] >= 0) { return longest_path_length_from_vert[now]; } if (next_verts[now].size() == 0) { longest_path_length_from_vert[now] = 0; return 0; } int ret = 0; for (auto itr = next_verts[now].begin(); itr != next_verts[now].end(); itr++) { ret = max(ret, calc_longest_path_from(*itr)); } return ret + 1; } int main() { cin >> N >> M; for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; x--; y--; next_verts[x].push_back(y); } int ans = 0; for (int i = 0; i < N; i++) { longest_path_length_from_vert[i] = calc_longest_path_from(i); ans = max(ans, longest_path_length_from_vert[i]); } cout << ans << endl; }
#include <iostream> #include <vector> using namespace std; int N, M; vector<int> next_verts[100000]; vector<int> longest_path_length_from_vert(100000, -1); int calc_longest_path_from(int now) { if (longest_path_length_from_vert[now] >= 0) { return longest_path_length_from_vert[now]; } if (next_verts[now].size() == 0) { longest_path_length_from_vert[now] = 0; return 0; } int ret = 0; for (auto itr = next_verts[now].begin(); itr != next_verts[now].end(); itr++) { ret = max(ret, calc_longest_path_from(*itr)); } longest_path_length_from_vert[now] = ret + 1; return ret + 1; } int main() { cin >> N >> M; for (int i = 0; i < M; i++) { int x, y; cin >> x >> y; x--; y--; next_verts[x].push_back(y); } int ans = 0; for (int i = 0; i < N; i++) { longest_path_length_from_vert[i] = calc_longest_path_from(i); ans = max(ans, longest_path_length_from_vert[i]); } cout << ans << endl; }
insert
22
22
22
23
TLE
p03166
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define FORN(i, j, k) for (int i = j; i < k; i++) #define FORR(i, j, k) for (int i = j; i >= k; i--) #define REP(i, n) FORN(i, 0, n) #define int long long #define pii pair<int, int> #define vi vector<int> #define pb push_back #define mp make_pair #define f first #define s second #define endl '\n' const int MODO = 1e+9 + 7; const int MAX = 1e+5; int n, m; int dp[MAX]; vector<int> adj[MAX]; vector<bool> visit(MAX, false); void dfs(int node) { visit[node] = true; dp[node] = 0; for (int child : adj[node]) { if (!visit[child]) { dfs(child); } dp[node] = max(dp[node], dp[child] + 1); } } void solve() { int x, y; REP(i, m) { cin >> x >> y; adj[x].pb(y); } int res = 0; FORN(i, 1, n + 1) { if (!visit[i]) { dfs(i); } } FORN(i, 1, n + 1) { res = max(res, dp[i]); } cout << res << endl; return; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; solve(); return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define FORN(i, j, k) for (int i = j; i < k; i++) #define FORR(i, j, k) for (int i = j; i >= k; i--) #define REP(i, n) FORN(i, 0, n) #define int long long #define pii pair<int, int> #define vi vector<int> #define pb push_back #define mp make_pair #define f first #define s second #define endl '\n' const int MODO = 1e+9 + 7; const int MAX = 1e+5 + 5; int n, m; int dp[MAX]; vector<int> adj[MAX]; vector<bool> visit(MAX, false); void dfs(int node) { visit[node] = true; dp[node] = 0; for (int child : adj[node]) { if (!visit[child]) { dfs(child); } dp[node] = max(dp[node], dp[child] + 1); } } void solve() { int x, y; REP(i, m) { cin >> x >> y; adj[x].pb(y); } int res = 0; FORN(i, 1, n + 1) { if (!visit[i]) { dfs(i); } } FORN(i, 1, n + 1) { res = max(res, dp[i]); } cout << res << endl; return; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; solve(); return 0; }
replace
26
27
26
27
0
p03166
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAX 10010 vector<int> g[MAX]; int main() { int n, m; cin >> n >> m; int in[MAX] = {0}; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; in[v]++; g[u].push_back(v); } queue<int> q; int d[MAX] = {0}; for (int i = 0; i < n; i++) if (in[i] == 0) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : g[u]) { in[v]--; if (!in[v]) { d[v] = d[u] + 1; q.push(v); } } } int r = -1; for (int i = 0; i < n; i++) r = max(r, d[i]); cout << r << '\n'; }
#include <bits/stdc++.h> using namespace std; #define MAX 100010 vector<int> g[MAX]; int main() { int n, m; cin >> n >> m; int in[MAX] = {0}; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; in[v]++; g[u].push_back(v); } queue<int> q; int d[MAX] = {0}; for (int i = 0; i < n; i++) if (in[i] == 0) q.push(i); while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : g[u]) { in[v]--; if (!in[v]) { d[v] = d[u] + 1; q.push(v); } } } int r = -1; for (int i = 0; i < n; i++) r = max(r, d[i]); cout << r << '\n'; }
replace
2
3
2
3
0
p03166
C++
Time Limit Exceeded
#include <bits/stdc++.h> // #define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define forn(i, o, n) for (int i = o; i < n; i++) #define int long long // #define endl "\n" #define mod 1000000007 #define double long double #define pb push_back #define swap(x, y) (x ^= y ^= x ^= y) using namespace std; void dfs(vector<vector<int>> g, vector<int> &visit, vector<int> &dist, int i) { visit[i] = 1; for (auto j : g[i]) { if (!visit[j]) dfs(g, visit, dist, j); dist[i] = max(dist[i], dist[j] + 1); } } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n, m; cin >> n >> m; vector<vector<int>> g(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); } std::vector<int> visit(n, 0); vector<int> dist(n, 0); for (int i = 0; i < n; i++) { if (!visit[i]) dfs(g, visit, dist, i); } int ans = 0; for (auto j : dist) ans = max(ans, j); cout << ans << endl; }
#include <bits/stdc++.h> // #define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) #define forn(i, o, n) for (int i = o; i < n; i++) #define int long long // #define endl "\n" #define mod 1000000007 #define double long double #define pb push_back #define swap(x, y) (x ^= y ^= x ^= y) using namespace std; void dfs(vector<vector<int>> &g, vector<int> &visit, vector<int> &dist, int i) { visit[i] = 1; for (auto j : g[i]) { if (!visit[j]) dfs(g, visit, dist, j); dist[i] = max(dist[i], dist[j] + 1); } } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n, m; cin >> n >> m; vector<vector<int>> g(n); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; g[a].push_back(b); } std::vector<int> visit(n, 0); vector<int> dist(n, 0); for (int i = 0; i < n; i++) { if (!visit[i]) dfs(g, visit, dist, i); } int ans = 0; for (auto j : dist) ans = max(ans, j); cout << ans << endl; }
replace
12
13
12
13
TLE
p03166
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using VI = vector<ll>; using VV = vector<VI>; using VS = vector<string>; // tourist set template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // tourist set end template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define FOR(i, a, b) for (ll i = (a); i < (b); ++i) #define rep(i, b) FOR(i, 0, b) #define ALL(v) (v).begin(), (v).end() #define p(s) cout << (s) << endl #define p2(s, t) cout << (s) << " " << (t) << endl #define br() p("") #define pn(s) cout << (#s) << " " << (s) << endl #define p_yes() p("YES") #define p_no() p("NO") #define pb(s) push_back(s) #define SZ(x) ((int)(x).size()) void no() { p_no(); exit(0); } void yes() { p_yes(); exit(0); } const ll mod = 1e9 + 7; const ll inf = 1e18; void vprint(vector<ll> A) { ll L = A.size(); FOR(i, 0, L) { if (i) cout << ' '; cout << A[i]; } cout << endl; } VI starts; // 始点が複数ありうる // 戻り値:indexのvector VI topological_sort(VV &G) { ll N = G.size(); // 入次数 VI IN(N, 0); rep(i, N) { for (ll to : G[i]) { IN[to]++; } } stack<ll> st; // 入次数0のものを入れる rep(i, N) { if (IN[i] == 0) { st.push(i); starts.push_back(i); } } VI ret; while (!st.empty()) { ll i = st.top(); st.pop(); ret.push_back(i); for (ll to : G[i]) { IN[to]--; if (IN[to] == 0) { st.push(to); } } } // 成功したか確認 for (ll in : IN) { if (in != 0) { // 失敗(DAGじゃなかった) VI empty_vector; return empty_vector; } } // 正常の戻り値 return ret; } VV G; ll dp[100010]; // iに行くまでの最長コスト int main() { cin.tie(0); ios::sync_with_stdio(false); // input ll N, M; cin >> N >> M; G.resize(N); rep(i, M) { ll x, y; cin >> x >> y; x--; y--; G[x].push_back(y); } auto sorted = topological_sort(G); debug(sorted); debug(starts); queue<ll> que; map<ll, ll> mp; for (ll a : starts) { que.push(a); mp[a]++; } debug(mp); while (!que.empty()) { ll cur = que.front(); que.pop(); if (mp[cur] > 1) { mp[cur]--; debug("suteru", cur); continue; // 捨てる } else { mp[cur]--; } for (ll to : G[cur]) { // 更新するときのみpush if (dp[to] < dp[cur] + 1) { dp[to] = dp[cur] + 1; debug(to, dp[to]); que.push(to); mp[to]++; } } } ll ans = 0; rep(i, 100010) { chmax(ans, dp[i]); } p(ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using VI = vector<ll>; using VV = vector<VI>; using VS = vector<string>; // tourist set template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif // tourist set end template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define FOR(i, a, b) for (ll i = (a); i < (b); ++i) #define rep(i, b) FOR(i, 0, b) #define ALL(v) (v).begin(), (v).end() #define p(s) cout << (s) << endl #define p2(s, t) cout << (s) << " " << (t) << endl #define br() p("") #define pn(s) cout << (#s) << " " << (s) << endl #define p_yes() p("YES") #define p_no() p("NO") #define pb(s) push_back(s) #define SZ(x) ((int)(x).size()) void no() { p_no(); exit(0); } void yes() { p_yes(); exit(0); } const ll mod = 1e9 + 7; const ll inf = 1e18; void vprint(vector<ll> A) { ll L = A.size(); FOR(i, 0, L) { if (i) cout << ' '; cout << A[i]; } cout << endl; } VI starts; // 始点が複数ありうる // 戻り値:indexのvector VI topological_sort(VV &G) { ll N = G.size(); // 入次数 VI IN(N, 0); rep(i, N) { for (ll to : G[i]) { IN[to]++; } } stack<ll> st; // 入次数0のものを入れる rep(i, N) { if (IN[i] == 0) { st.push(i); starts.push_back(i); } } VI ret; while (!st.empty()) { ll i = st.top(); st.pop(); ret.push_back(i); for (ll to : G[i]) { IN[to]--; if (IN[to] == 0) { st.push(to); } } } // 成功したか確認 for (ll in : IN) { if (in != 0) { // 失敗(DAGじゃなかった) VI empty_vector; return empty_vector; } } // 正常の戻り値 return ret; } VV G; ll dp[100010]; // iに行くまでの最長コスト int main() { cin.tie(0); ios::sync_with_stdio(false); // input ll N, M; cin >> N >> M; G.resize(N); rep(i, M) { ll x, y; cin >> x >> y; x--; y--; G[x].push_back(y); } auto sorted = topological_sort(G); debug(sorted); debug(starts); queue<ll> que; map<ll, ll> mp; for (ll a : starts) { que.push(a); mp[a]++; } debug(mp); for (ll cu : sorted) { for (ll to : G[cu]) { chmax(dp[to], dp[cu] + 1); } } // while(!que.empty()){ // ll cur = que.front(); que.pop(); // if(mp[cur]>1){ // mp[cur]--; // debug("suteru", cur); // continue; // 捨てる // }else{ // mp[cur]--; // } // for(ll to : G[cur]){ // // 更新するときのみpush // if(dp[to] < dp[cur]+1){ // dp[to] = dp[cur]+1; // debug(to, dp[to]); // que.push(to); // mp[to]++; // } // } // } ll ans = 0; rep(i, 100010) { chmax(ans, dp[i]); } p(ans); return 0; }
replace
220
242
220
247
TLE
p03166
C++
Runtime Error
#include <algorithm> #include <iostream> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; unordered_map<int, vector<int>> edges; vector<ll> dp(10002, 0); bool visited[10002] = {false}; ll helper(int i) { if (visited[i]) return dp[i]; // 访问过的节点,直接返回i节点的Longest Path else { dp[i] = 0; visited[i] = true; // 访问过当前这个节点 auto next = edges[i]; for (auto j : next) { dp[i] = max(dp[i], helper(j) + 1); } return dp[i]; } } int main(int argc, char *argv[]) { int N, M; cin >> N >> M; int a, b; for (int i = 0; i < M; i++) { cin >> a >> b; edges[a].push_back(b); } ll res = 0; for (int i = 1; i <= N; i++) { res = max(res, helper(i)); } cout << res; return 0; }
#include <algorithm> #include <iostream> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; unordered_map<int, vector<int>> edges; vector<ll> dp(100002, 0); bool visited[100002] = {false}; ll helper(int i) { if (visited[i]) return dp[i]; // 访问过的节点,直接返回i节点的Longest Path else { dp[i] = 0; visited[i] = true; // 访问过当前这个节点 auto next = edges[i]; for (auto j : next) { dp[i] = max(dp[i], helper(j) + 1); } return dp[i]; } } int main(int argc, char *argv[]) { int N, M; cin >> N >> M; int a, b; for (int i = 0; i < M; i++) { cin >> a >> b; edges[a].push_back(b); } ll res = 0; for (int i = 1; i <= N; i++) { res = max(res, helper(i)); } cout << res; return 0; }
replace
9
11
9
11
0
p03166
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll int using namespace __gnu_pbds; using namespace std; #define mod 1000000007 #define endl "\n" #define pb push_back #define ff first #define ss second #define mp make_pair #define lb lower_bound #define f(i, p, n) for (ll i = p; i < n; i++) #define debug(x) cout << "[" << #x << ": " << x << "]" << endl typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; ll exp(ll a, ll b, ll m); ll dp[100005] = {0}; vector<ll> tr[100005]; ll dfs(ll a) { if (dp[a] != 0) return dp[a]; ll ma = 0; f(i, 0, tr[a].size()) { ma = max(ma, dfs(tr[a][i])); } return ma + 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); /* #ifndef ONLINE_JUDGE freopen("/home/savish/Desktop/2019/io/input.txt", "r", stdin); freopen("/home/savish/Desktop/2019/io/output.txt", "w", stdout); #endif */ ll n, m; cin >> n >> m; f(i, 1, n + 1) dp[i] = 0; ll x, y; f(i, 0, m) { cin >> x >> y; tr[x].pb(y); } ll ans = 0; f(i, 1, n + 1) if (dp[i] == 0) ans = max(ans, dfs(i)); cout << ans - 1 << endl; return 0; } ll exp(ll a, ll b, ll m) { if (b == 0) { return 1; } ll temp = exp(a, b / 2, m); temp = (temp * temp) % m; if (b & 1) { return (temp * (a % m)) % m; } return temp; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll int using namespace __gnu_pbds; using namespace std; #define mod 1000000007 #define endl "\n" #define pb push_back #define ff first #define ss second #define mp make_pair #define lb lower_bound #define f(i, p, n) for (ll i = p; i < n; i++) #define debug(x) cout << "[" << #x << ": " << x << "]" << endl typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; ll exp(ll a, ll b, ll m); ll dp[100005] = {0}; vector<ll> tr[100005]; ll dfs(ll a) { if (dp[a] != 0) return dp[a]; ll ma = 0; f(i, 0, tr[a].size()) { ma = max(ma, dfs(tr[a][i])); } dp[a] = ma + 1; return ma + 1; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); /* #ifndef ONLINE_JUDGE freopen("/home/savish/Desktop/2019/io/input.txt", "r", stdin); freopen("/home/savish/Desktop/2019/io/output.txt", "w", stdout); #endif */ ll n, m; cin >> n >> m; f(i, 1, n + 1) dp[i] = 0; ll x, y; f(i, 0, m) { cin >> x >> y; tr[x].pb(y); } ll ans = 0; f(i, 1, n + 1) if (dp[i] == 0) ans = max(ans, dfs(i)); cout << ans - 1 << endl; return 0; } ll exp(ll a, ll b, ll m) { if (b == 0) { return 1; } ll temp = exp(a, b / 2, m); temp = (temp * temp) % m; if (b & 1) { return (temp * (a % m)) % m; } return temp; }
insert
29
29
29
30
TLE
p03166
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define pb push_back #define mp make_pair #define ff first #define ss second using namespace std; vector<vector<int>> g; vector<int> dp; int dfs(int root) { for (int i = 0; i < g[root].size(); i++) { int u = g[root][i]; int x = dfs(u); if (dp[u] == -1) dp[root] = max(dp[root], 1 + x); else dp[root] = max(dp[root], 1 + dp[u]); } if (dp[root] == -1) dp[root] = 0; return dp[root]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; g.resize(n + 1); dp.resize(n + 1, -1); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; g[x].pb(y); } for (int i = 1; i <= n; i++) g[0].pb(i); int ans = dfs(0); cout << ans - 1; return 0; }
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define pb push_back #define mp make_pair #define ff first #define ss second using namespace std; vector<vector<int>> g; vector<int> dp; int dfs(int root) { if (dp[root] != -1) return dp[root]; for (int i = 0; i < g[root].size(); i++) { int u = g[root][i]; int x = dfs(u); if (dp[u] == -1) dp[root] = max(dp[root], 1 + x); else dp[root] = max(dp[root], 1 + dp[u]); } if (dp[root] == -1) dp[root] = 0; return dp[root]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; g.resize(n + 1); dp.resize(n + 1, -1); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; g[x].pb(y); } for (int i = 1; i <= n; i++) g[0].pb(i); int ans = dfs(0); cout << ans - 1; return 0; }
insert
11
11
11
13
TLE
p03166
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define ll long long int #define ld long double #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define mp make_pair #define pb push_back #define vll vector<ll> #define mod(n) n % 1000000007 #define sp << " " #define precision(n) cout << fixed << setprecision(n); #define startTime \ time_t start, end; \ time(&start); #define endTime \ time(&end); \ double tt = double(end - start); \ cout << "Time taken : " << fixed << tt << setprecision(5); \ cout << " sec" << endl; using namespace std; ll dp[100001]; ll color[100001]; void dfsVisit(vll graph[], ll u) { color[u] = 1; if (graph[u].empty()) { dp[u] = 0; return; } for (auto x : graph[u]) { dfsVisit(graph, x); dp[u] = max(dp[u], 1 + dp[x]); } return; } void dfs(vll graph[], ll n) { for (int i = 0; i < n; i++) { if (color[i] != 1) dfsVisit(graph, i); } } int main() { IOS; ll n, m, u, v; cin >> n >> m; vll graph[n]; for (int i = 0; i < m; i++) { cin >> u >> v; graph[--u].pb(--v); } dfs(graph, n); ll ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, dp[i]); } cout << ans << endl; }
#include <bits/stdc++.h> #define ll long long int #define ld long double #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define mp make_pair #define pb push_back #define vll vector<ll> #define mod(n) n % 1000000007 #define sp << " " #define precision(n) cout << fixed << setprecision(n); #define startTime \ time_t start, end; \ time(&start); #define endTime \ time(&end); \ double tt = double(end - start); \ cout << "Time taken : " << fixed << tt << setprecision(5); \ cout << " sec" << endl; using namespace std; ll dp[100001]; ll color[100001]; void dfsVisit(vll graph[], ll u) { color[u] = 1; if (graph[u].empty()) { dp[u] = 0; return; } for (auto x : graph[u]) { if (color[x] == 0) dfsVisit(graph, x); dp[u] = max(dp[u], 1 + dp[x]); } return; } void dfs(vll graph[], ll n) { for (int i = 0; i < n; i++) { if (color[i] != 1) dfsVisit(graph, i); } } int main() { IOS; ll n, m, u, v; cin >> n >> m; vll graph[n]; for (int i = 0; i < m; i++) { cin >> u >> v; graph[--u].pb(--v); } dfs(graph, n); ll ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, dp[i]); } cout << ans << endl; }
replace
34
35
34
36
TLE
p03166
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long const int nax = 10010; vector<int> adj[nax]; int in_degree[nax], dist[nax]; bool vis[nax]; void dfs(int a) { vis[a] = true; for (int x : adj[a]) { dist[x] = max(dist[x], dist[a] + 1); --in_degree[x]; if (in_degree[x] == 0) dfs(x); } } int main() { int n, m, x, y; cin >> n >> m; while (m--) { cin >> x >> y; adj[x].push_back(y); ++in_degree[y]; } for (int i = 1; i <= n; i++) { if (!vis[i] && in_degree[i] == 0) dfs(i); } int answer = 0; for (int i = 1; i <= n; i++) { answer = max(answer, dist[i]); } cout << answer; }
#include <bits/stdc++.h> using namespace std; #define ll long long const int nax = 1e5 + 5; vector<int> adj[nax]; int in_degree[nax], dist[nax]; bool vis[nax]; void dfs(int a) { vis[a] = true; for (int x : adj[a]) { dist[x] = max(dist[x], dist[a] + 1); --in_degree[x]; if (in_degree[x] == 0) dfs(x); } } int main() { int n, m, x, y; cin >> n >> m; while (m--) { cin >> x >> y; adj[x].push_back(y); ++in_degree[y]; } for (int i = 1; i <= n; i++) { if (!vis[i] && in_degree[i] == 0) dfs(i); } int answer = 0; for (int i = 1; i <= n; i++) { answer = max(answer, dist[i]); } cout << answer; }
replace
4
5
4
5
0
p03166
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define endl '\n' #define N 1000001 #define mod 1e9 + 7 using namespace std; typedef long long ll; vector<vector<ll>> v(100001); ll dp[100001]; ll sol(ll a) { if (dp[a] != -1) return dp[a]; ll res = 0; for (ll i = 0; i < v[a].size(); i++) { res = max(res, sol(v[a][i]) + 1); } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n, m; cin >> n >> m; ll x, y; for (ll i = 0; i < m; i++) { cin >> x >> y; v[x].push_back(y); } ll ans = 0; for (ll i = 1; i <= n; i++) dp[i] = -1; for (ll i = 1; i <= n; i++) { ans = max(ans, sol(i)); } cout << ans; return 0; }
#include <bits/stdc++.h> #define endl '\n' #define N 1000001 #define mod 1e9 + 7 using namespace std; typedef long long ll; vector<vector<ll>> v(100001); ll dp[100001]; ll sol(ll a) { if (dp[a] != -1) return dp[a]; ll res = 0; for (ll i = 0; i < v[a].size(); i++) { res = max(res, sol(v[a][i]) + 1); } return dp[a] = res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n, m; cin >> n >> m; ll x, y; for (ll i = 0; i < m; i++) { cin >> x >> y; v[x].push_back(y); } ll ans = 0; for (ll i = 1; i <= n; i++) dp[i] = -1; for (ll i = 1; i <= n; i++) { ans = max(ans, sol(i)); } cout << ans; return 0; }
replace
15
16
15
16
TLE
p03166
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; vector<vector<int>> adj; vector<int> ans, visited; void dfs(int cur) { int amax = -1; visited[cur] = 1; for (int i : adj[cur]) { dfs(i); if (amax < ans[i]) amax = ans[i]; } ans[cur] = amax + 1; } int main() { int n, m, x, y; cin >> n >> m; adj.resize(n + 1); ans.resize(n + 1); visited.resize(n + 1, 0); for (int i = 1; i <= m; i++) { cin >> x >> y; adj[x].push_back(y); } int amax = 0; for (int i = 1; i <= n; i++) { if (!visited[i]) dfs(i); if (amax < ans[i]) amax = ans[i]; } cout << amax; }
#include <iostream> #include <vector> using namespace std; vector<vector<int>> adj; vector<int> ans, visited; void dfs(int cur) { int amax = -1; visited[cur] = 1; for (int i : adj[cur]) { if (!visited[i]) dfs(i); if (amax < ans[i]) amax = ans[i]; } ans[cur] = amax + 1; } int main() { int n, m, x, y; cin >> n >> m; adj.resize(n + 1); ans.resize(n + 1); visited.resize(n + 1, 0); for (int i = 1; i <= m; i++) { cin >> x >> y; adj[x].push_back(y); } int amax = 0; for (int i = 1; i <= n; i++) { if (!visited[i]) dfs(i); if (amax < ans[i]) amax = ans[i]; } cout << amax; }
replace
10
11
10
12
TLE
p03166
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef std::vector<ll> lls; constexpr ll INF = 1LL << 60; constexpr ll MOD = 998244353L; template <class T> inline void chmin(T &a, T b) { if (a > b) a = b; } template <class T> inline void chmax(T &a, T b) { if (a < b) a = b; } template <class T> inline T MaxE(vector<T> &v, ll S, ll E) { T m = v[S]; for (ll i = S; i <= E; i++) chmax(m, v[i]); return m; } // v[S]~v[E]の最大値 template <class T> inline T MinE(vector<T> &v, ll S, ll E) { T m = v[S]; for (ll i = S; i <= E; i++) chmin(m, v[i]); return m; } template <class T> inline T MaxE(vector<T> &v, ll N) { return MaxE(v, 0, N - 1); } // 先頭N個中の最大値 template <class T> inline T MinE(vector<T> &v, ll N) { return MinE(v, 0, N - 1); } // 先頭N個中の最小値 template <class T> inline T MaxE(vector<T> &v) { T m = v[0]; for (T e : v) chmax(m, e); return m; } template <class T> inline T MinE(vector<T> &v) { T m = v[0]; for (T e : v) chmin(m, e); return m; } void cinv(lls &llVec, ll n, bool to_0_origin = false) { ll val; llVec.reserve((size_t)(n)); for (ll i = 0; i < n; i++) { cin >> val; if (to_0_origin) val--; llVec.push_back(val); } } ll rec(ll v, std::vector<lls> &G, lls &tbl) { if (tbl[v] != -INF) return tbl[v]; ll maxval = 0; for (auto vv : G[v]) { chmax(maxval, rec(vv, G, tbl) + 1); } return maxval; } int main() { ll N, M; cin >> N >> M; std::vector<lls> G(N); for (ll i = 0; i < M; i++) { ll xx, yy; cin >> xx >> yy; xx--; yy--; G[xx].push_back(yy); } // テーブル初期化 lls tbl(N, -INF); // メモ化再帰 ll maxval = -INF; for (ll v = 0; v < N; v++) { chmax(maxval, rec(v, G, tbl)); } cout << maxval << '\n'; return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef std::vector<ll> lls; constexpr ll INF = 1LL << 60; constexpr ll MOD = 998244353L; template <class T> inline void chmin(T &a, T b) { if (a > b) a = b; } template <class T> inline void chmax(T &a, T b) { if (a < b) a = b; } template <class T> inline T MaxE(vector<T> &v, ll S, ll E) { T m = v[S]; for (ll i = S; i <= E; i++) chmax(m, v[i]); return m; } // v[S]~v[E]の最大値 template <class T> inline T MinE(vector<T> &v, ll S, ll E) { T m = v[S]; for (ll i = S; i <= E; i++) chmin(m, v[i]); return m; } template <class T> inline T MaxE(vector<T> &v, ll N) { return MaxE(v, 0, N - 1); } // 先頭N個中の最大値 template <class T> inline T MinE(vector<T> &v, ll N) { return MinE(v, 0, N - 1); } // 先頭N個中の最小値 template <class T> inline T MaxE(vector<T> &v) { T m = v[0]; for (T e : v) chmax(m, e); return m; } template <class T> inline T MinE(vector<T> &v) { T m = v[0]; for (T e : v) chmin(m, e); return m; } void cinv(lls &llVec, ll n, bool to_0_origin = false) { ll val; llVec.reserve((size_t)(n)); for (ll i = 0; i < n; i++) { cin >> val; if (to_0_origin) val--; llVec.push_back(val); } } ll rec(ll v, std::vector<lls> &G, lls &tbl) { if (tbl[v] != -INF) return tbl[v]; ll maxval = 0; for (auto vv : G[v]) { chmax(maxval, rec(vv, G, tbl) + 1); } tbl[v] = maxval; return maxval; } int main() { ll N, M; cin >> N >> M; std::vector<lls> G(N); for (ll i = 0; i < M; i++) { ll xx, yy; cin >> xx >> yy; xx--; yy--; G[xx].push_back(yy); } // テーブル初期化 lls tbl(N, -INF); // メモ化再帰 ll maxval = -INF; for (ll v = 0; v < N; v++) { chmax(maxval, rec(v, G, tbl)); } cout << maxval << '\n'; return 0; }
insert
64
64
64
65
TLE
p03166
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int M = 1e3 + 3; int n, k, vis[M], ans, dp[M]; vector<int> adj[M], g[M], lf; int dfs(int u) { int &ret = dp[u]; if (ret != -1) return ret; ret = 1; for (int i : adj[u]) { ret = max(ret, dfs(i) + 1); } return ret; } int main() { scanf("%d %d", &n, &k); while (k--) { int x, y; cin >> x >> y; adj[x].push_back(y); } memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) { ans = max(ans, dfs(i)); } cout << ans - 1; }
#include <bits/stdc++.h> using namespace std; const int M = 1e5 + 5; int n, k, vis[M], ans, dp[M]; vector<int> adj[M], g[M], lf; int dfs(int u) { int &ret = dp[u]; if (ret != -1) return ret; ret = 1; for (int i : adj[u]) { ret = max(ret, dfs(i) + 1); } return ret; } int main() { scanf("%d %d", &n, &k); while (k--) { int x, y; cin >> x >> y; adj[x].push_back(y); } memset(dp, -1, sizeof dp); for (int i = 1; i <= n; i++) { ans = max(ans, dfs(i)); } cout << ans - 1; }
replace
4
5
4
5
0
p03166
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <iostream> using namespace std; int dfs(int currentnode, vector<vector<int>> &g, unordered_set<int> visited, unordered_set<int> &currentvisit, int dp[], int &ans) { if (visited.find(currentnode) == visited.end()) // already visited this,dp array has the answer { return dp[currentnode]; } visited.erase(currentnode); // visited currentvisit.erase(currentnode); // visited in local visited int currentlongest = 0; for (int i = 0; i < g[currentnode].size(); i++) { int neighbour = g[currentnode][i]; if (currentvisit.find(neighbour) != currentvisit.end()) // neighbour is not visited { currentlongest = max(currentlongest, 1 + dfs(neighbour, g, visited, currentvisit, dp, ans)); } } currentvisit.insert(currentnode); dp[currentnode] = currentlongest; ans = max(ans, currentlongest); return currentlongest; } void findit(vector<vector<int>> &g, int n, int e) { unordered_set<int> visited; unordered_set<int> currentvisit; int dp[n + 1]; for (int i = 1; i <= n; i++) { visited.insert(i); currentvisit.insert(i); dp[i] = 0; // not solved yet } int ans = 0; for (int i = 1; i <= n; i++) { if (visited.find(i) != visited.end()) // this node is never visited { dfs(i, g, visited, currentvisit, dp, ans); } } cout << ans; } int main() { int n; cin >> n; int e; cin >> e; vector<vector<int>> g(n + 1); for (int i = 0; i < e; i++) { int e1; int e2; cin >> e1; cin >> e2; g[e1].push_back(e2); } findit(g, n, e); return 0; }
#include <bits/stdc++.h> #include <iostream> using namespace std; int dfs(int currentnode, vector<vector<int>> &g, unordered_set<int> &visited, unordered_set<int> &currentvisit, int dp[], int &ans) { if (visited.find(currentnode) == visited.end()) // already visited this,dp array has the answer { return dp[currentnode]; } visited.erase(currentnode); // visited currentvisit.erase(currentnode); // visited in local visited int currentlongest = 0; for (int i = 0; i < g[currentnode].size(); i++) { int neighbour = g[currentnode][i]; if (currentvisit.find(neighbour) != currentvisit.end()) // neighbour is not visited { currentlongest = max(currentlongest, 1 + dfs(neighbour, g, visited, currentvisit, dp, ans)); } } currentvisit.insert(currentnode); dp[currentnode] = currentlongest; ans = max(ans, currentlongest); return currentlongest; } void findit(vector<vector<int>> &g, int n, int e) { unordered_set<int> visited; unordered_set<int> currentvisit; int dp[n + 1]; for (int i = 1; i <= n; i++) { visited.insert(i); currentvisit.insert(i); dp[i] = 0; // not solved yet } int ans = 0; for (int i = 1; i <= n; i++) { if (visited.find(i) != visited.end()) // this node is never visited { dfs(i, g, visited, currentvisit, dp, ans); } } cout << ans; } int main() { int n; cin >> n; int e; cin >> e; vector<vector<int>> g(n + 1); for (int i = 0; i < e; i++) { int e1; int e2; cin >> e1; cin >> e2; g[e1].push_back(e2); } findit(g, n, e); return 0; }
replace
4
5
4
5
TLE