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
p03164
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; signed main() { ll n, m; cin >> n >> m; vector<ll> a(n); vector<ll> b(n); for (ll i = 0; i < n; ++i) cin >> a[i] >> b[i]; vector<vector<ll>> dp(n + 1, vector<ll>(1000 * n + 1, 1e12)); dp[0][0] = 0; for (ll i = 0; i < n; ++i) { for (ll j = 0; j <= 1000 * n; ++j) { if (dp[i][j] == 1e9) continue; dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); dp[i + 1][j + b[i]] = min(dp[i + 1][j + b[i]], dp[i][j] + a[i]); } } for (ll i = 1000 * n; i >= 0; --i) { if (dp[n][i] <= m) { cout << i << endl; return 0; } } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; signed main() { ll n, m; cin >> n >> m; vector<ll> a(n); vector<ll> b(n); for (ll i = 0; i < n; ++i) cin >> a[i] >> b[i]; vector<vector<ll>> dp(n + 1, vector<ll>(1000 * n + 1, 1e12)); dp[0][0] = 0; for (ll i = 0; i < n; ++i) { for (ll j = 0; j <= 1000 * n; ++j) { if (dp[i][j] == 1e12) continue; dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); dp[i + 1][j + b[i]] = min(dp[i + 1][j + b[i]], dp[i][j] + a[i]); } } for (ll i = 1000 * n; i >= 0; --i) { if (dp[n][i] <= m) { cout << i << endl; return 0; } } }
replace
17
18
17
18
0
p03164
C++
Runtime Error
#include <bits/stdc++.h> #define DUMP(x) cout << #x << " = " << (x) << endl; #define FOR(i, m, n) for (int i = m; i < n; i++) #define IFOR(i, m, n) for (int i = n - 1; i >= m; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) #define FOREACH(x, a) for (auto &(x) : (a)) #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; const ll INF = 1LL << 60; const int MOD = 1000000007; /* テンプレートここまで */ template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int N, W; cin >> N >> W; vector<int> v(N + 1, 0), w(N + 1, 0); FOR(i, 1, N + 1) { cin >> w[i] >> v[i]; } int V_MAX = 1000; vector<vector<ll>> dp(N + 1, vector<ll>(N * V_MAX + 1, W + 1)); FOR(i, 0, N + 1) { dp[i][0] = 0; } FOR(i, 0, N) { REP(j, N * V_MAX) { chmin(dp[i + 1][j + v[i + 1]], dp[i][j] + w[i + 1]); chmin(dp[i + 1][j], dp[i][j]); } } int ans = 0; REP(i, N * V_MAX + 1) { if (dp[N][i] <= W) chmax(ans, i); } cout << ans << endl; }
#include <bits/stdc++.h> #define DUMP(x) cout << #x << " = " << (x) << endl; #define FOR(i, m, n) for (int i = m; i < n; i++) #define IFOR(i, m, n) for (int i = n - 1; i >= m; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) #define FOREACH(x, a) for (auto &(x) : (a)) #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; const ll INF = 1LL << 60; const int MOD = 1000000007; /* テンプレートここまで */ template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int N, W; cin >> N >> W; vector<int> v(N + 1, 0), w(N + 1, 0); FOR(i, 1, N + 1) { cin >> w[i] >> v[i]; } int V_MAX = 1000; vector<vector<ll>> dp(N + 1, vector<ll>(N * V_MAX + 1, W + 1)); FOR(i, 0, N + 1) { dp[i][0] = 0; } FOR(i, 0, N) { REP(j, N * V_MAX) { if (dp[i][j] <= W) chmin(dp[i + 1][j + v[i + 1]], dp[i][j] + w[i + 1]); chmin(dp[i + 1][j], dp[i][j]); } } int ans = 0; REP(i, N * V_MAX + 1) { if (dp[N][i] <= W) chmax(ans, i); } cout << ans << endl; }
replace
44
45
44
46
-6
corrupted size vs. prev_size
p03164
C++
Runtime Error
// reference ----> errichto stream #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll inf = 1e18; int main(int argc, char **argv) { #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int n, w; cin >> n >> w; vector<int> val(n), wei(n); int sum = 0; for (int i = 0; i < n; i++) { cin >> wei[i] >> val[i]; sum += (val[i]); } vector<ll> dp(sum + 1, inf); // min weight with value sum + 1 dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = sum - val[i]; j >= 0; j--) { dp[j + val[i]] = min(dp[j + val[i]], dp[j] + wei[i]); } } ll ans = 0; for (ll i = 0; i <= sum; i++) { if (dp[i] <= w) { ans = i; } } cout << ans << endl; return 0; }
// reference ----> errichto stream #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll inf = 1e18; int main(int argc, char **argv) { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, w; cin >> n >> w; vector<int> val(n), wei(n); int sum = 0; for (int i = 0; i < n; i++) { cin >> wei[i] >> val[i]; sum += (val[i]); } vector<ll> dp(sum + 1, inf); // min weight with value sum + 1 dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = sum - val[i]; j >= 0; j--) { dp[j + val[i]] = min(dp[j + val[i]], dp[j] + wei[i]); } } ll ans = 0; for (ll i = 0; i <= sum; i++) { if (dp[i] <= w) { ans = i; } } cout << ans << endl; return 0; }
delete
6
10
6
6
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03164
C++
Runtime Error
// dp[i][v] = tuye i taye aval ye tedadi ro var darim ke majume arzeshashun // beshe v, hade aghal vazneshun che ghadre ! dp[0][v[0]] = w[0] dp[0][0] = 0 // else dp[0][i] = oo #include <bits/stdc++.h> using namespace std; const int N = 110; const int V = 1e3 + 10; long long n, W, v[N], w[N], dp[N][N * V]; int main() { ios::sync_with_stdio(0); cin >> n >> W; for (int i = 0; i < n; i++) cin >> v[i] >> w[i]; memset(dp, 63, sizeof(dp)); dp[0][0] = 0; dp[0][v[0]] = w[0]; for (int i = 1; i < n; i++) { for (int j = 0; j < N * V; j++) { dp[i][j] = dp[i - 1][j]; if (j - v[i] >= 0) dp[i][j] = min(dp[i - 1][j - v[i]] + w[i], dp[i - 1][j]); } } for (int i = N * V - 1; ~i; i--) if (dp[n - 1][i] <= W) { cout << i << endl; return 0; } return 0; }
// dp[i][v] = tuye i taye aval ye tedadi ro var darim ke majume arzeshashun // beshe v, hade aghal vazneshun che ghadre ! dp[0][v[0]] = w[0] dp[0][0] = 0 // else dp[0][i] = oo #include <bits/stdc++.h> using namespace std; const int N = 110; const int V = 1e3 + 10; long long n, W, v[N], w[N], dp[N][N * V]; int main() { ios::sync_with_stdio(0); cin >> n >> W; for (int i = 0; i < n; i++) cin >> w[i] >> v[i]; memset(dp, 63, sizeof(dp)); dp[0][0] = 0; dp[0][v[0]] = w[0]; for (int i = 1; i < n; i++) { for (int j = 0; j < N * V; j++) { dp[i][j] = dp[i - 1][j]; if (j - v[i] >= 0) dp[i][j] = min(dp[i - 1][j - v[i]] + w[i], dp[i - 1][j]); } } for (int i = N * V - 1; ~i; i--) if (dp[n - 1][i] <= W) { cout << i << endl; return 0; } return 0; }
replace
18
19
18
19
0
p03164
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define VRSORT(v) sort(v.begin(), v.end(), greater<int>()); #define INF 999999999 #define MOD 1000000007 #define M_PI 3.14159265358979323846 #define ALL(X) (X).begin(), (X).end() #ifdef __LOCAL #define DBG(X) cout << #X << " = " << (X) << endl; #define SAY(X) cout << (X) << endl; #else #define DBG(X) #define SAY(X) #endif #ifdef __LOCAL #include <filesystem> namespace fs = std::filesystem; #endif using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; int dx[] = {1, 0, -1, 0, 0}; int dy[] = {0, 1, 0, -1, 0}; const int INT_INF = (int)(2e9); const ll LL_INF = (ll)(2e18); const int NIL = -1; static mt19937 _g(time(nullptr)); std::string pad(int num) { char buffer[4]; std::snprintf(buffer, sizeof(buffer), "%03d", num); return buffer; } inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); } inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }; template <typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); } template <typename T, typename S> inline ostream &operator<<(ostream &os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline T fetch() { T ret; cin >> ret; return ret; } template <typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto &elem : ret) cin >> elem; return ret; } int N, Wmax; vector<ll> V, W; void input() { #ifdef __LOCAL fs::path p = __FILE__; fs::path input, output; input = output = p.parent_path(); input += string("/input/") + string(p.stem()) + string(".txt"); output += string("/output/") + string(p.stem()) + string(".txt"); freopen(input.c_str(), "r", stdin); freopen(output.c_str(), "w", stdout); #endif cin >> N >> Wmax; for (int i = 0; i < N; i++) { ll v, w; cin >> w >> v; W.push_back(w); V.push_back(v); } } // dp[i][v]:= // i個目までの品物からいくつかを選び価値の合計がvであるときの重さの合計の最小値 int solve() { vector<map<int, ll>> dp(N + 1); int Vmax = accumulate(ALL(V), 0); dp[0].emplace(0, 0); for (int i = 1; i <= N; i++) { for (auto const &vw : dp[i - 1]) { if (vw.second > Wmax) continue; dp[i][vw.first] = (dp[i][vw.first] == 0) ? vw.second : min(dp[i][vw.first], vw.second); if (vw.second + W[i - 1] > Wmax) continue; dp[i][vw.first + V[i - 1]] = (dp[i][vw.first + V[i - 1]] == 0) ? vw.second + W[i - 1] : min(dp[i][vw.first + V[i - 1]], vw.second + W[i - 1]); } // TLE // for (int j = 0; j <= Vmax; j++) // { // // if(dp[i-1][j]==0 && dp[i-1][j-V[i-1]]==0) continue; // // if(dp[i-1][j-V[i-1]]==0) {dp[i][j] = dp[i-1][j]; continue;} // // if(dp[i-1][j]==0) {dp[i][j] = dp[i-1][j-V[i-1]]+W[i-1]; continue;} // // else dp[i][j] = min(dp[i-1][j],dp[i-1][j-V[i-1]]+W[i-1]); // } } // int ans=0; // for (int j=Vmax;j>0;j--) // { // if(dp[N][j]==0) continue; // // if(v.second <= Wmax) ans = max(ans,v.first); // cout << j << endl; // return 0; // } auto pr = std::max_element( std::begin(dp[N]), std::end(dp[N]), [](const auto &p1, const auto &p2) { return p1.first < p2.first; }); cout << pr->first << endl; // for (auto const &v:dp[N]) // { // // if(dp[N][j]==0) continue; // // if(v.second <= Wmax) ans = max(ans,v.first); // ans = max(ans,v.first); // } // cout << ans << endl; return 0; } int main() { input(); solve(); return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define VRSORT(v) sort(v.begin(), v.end(), greater<int>()); #define INF 999999999 #define MOD 1000000007 #define M_PI 3.14159265358979323846 #define ALL(X) (X).begin(), (X).end() #ifdef __LOCAL #define DBG(X) cout << #X << " = " << (X) << endl; #define SAY(X) cout << (X) << endl; #else #define DBG(X) #define SAY(X) #endif #ifdef __LOCAL #include <filesystem> namespace fs = std::filesystem; #endif using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; int dx[] = {1, 0, -1, 0, 0}; int dy[] = {0, 1, 0, -1, 0}; const int INT_INF = (int)(2e9); const ll LL_INF = (ll)(2e18); const int NIL = -1; static mt19937 _g(time(nullptr)); std::string pad(int num) { char buffer[4]; std::snprintf(buffer, sizeof(buffer), "%03d", num); return buffer; } inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); } inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }; template <typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); } template <typename T, typename S> inline ostream &operator<<(ostream &os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline T fetch() { T ret; cin >> ret; return ret; } template <typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto &elem : ret) cin >> elem; return ret; } int N, Wmax; vector<ll> V, W; void input() { #ifdef __LOCAL fs::path p = __FILE__; fs::path input, output; input = output = p.parent_path(); input += string("/input/") + string(p.stem()) + string(".txt"); output += string("/output/") + string(p.stem()) + string(".txt"); freopen(input.c_str(), "r", stdin); freopen(output.c_str(), "w", stdout); #endif cin >> N >> Wmax; for (int i = 0; i < N; i++) { ll v, w; cin >> w >> v; W.push_back(w); V.push_back(v); } } // dp[i][v]:= // i個目までの品物からいくつかを選び価値の合計がvであるときの重さの合計の最小値 int solve() { vector<map<int, ll>> dp(N + 1); int Vmax = accumulate(ALL(V), 0); dp[0].emplace(0, 0); for (int i = 1; i <= N; i++) { for (auto const &vw : dp[i - 1]) { if (vw.second > Wmax) continue; dp[i][vw.first] = (dp[i][vw.first] == 0) ? vw.second : min(dp[i][vw.first], vw.second); if (vw.second + W[i - 1] > Wmax) continue; dp[i][vw.first + V[i - 1]] = (dp[i][vw.first + V[i - 1]] == 0) ? vw.second + W[i - 1] : min(dp[i][vw.first + V[i - 1]], vw.second + W[i - 1]); } dp[i - 1].clear(); // TLE // for (int j = 0; j <= Vmax; j++) // { // // if(dp[i-1][j]==0 && dp[i-1][j-V[i-1]]==0) continue; // // if(dp[i-1][j-V[i-1]]==0) {dp[i][j] = dp[i-1][j]; continue;} // // if(dp[i-1][j]==0) {dp[i][j] = dp[i-1][j-V[i-1]]+W[i-1]; continue;} // // else dp[i][j] = min(dp[i-1][j],dp[i-1][j-V[i-1]]+W[i-1]); // } } // int ans=0; // for (int j=Vmax;j>0;j--) // { // if(dp[N][j]==0) continue; // // if(v.second <= Wmax) ans = max(ans,v.first); // cout << j << endl; // return 0; // } auto pr = std::max_element( std::begin(dp[N]), std::end(dp[N]), [](const auto &p1, const auto &p2) { return p1.first < p2.first; }); cout << pr->first << endl; // for (auto const &v:dp[N]) // { // // if(dp[N][j]==0) continue; // // if(v.second <= Wmax) ans = max(ans,v.first); // ans = max(ans,v.first); // } // cout << ans << endl; return 0; } int main() { input(); solve(); return 0; }
insert
120
120
120
121
TLE
p03164
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define ll long long #define li long #define pb push_back #define mem(arr, x) memset(arr, x, sizeof(arr)) ll n, W; ll w[101], v[101]; ll dp[101][100010]; ll totalV = 0; ll minWeight(ll end, ll Val) { if (Val <= 0) return 0; if (end == 0) return 1000000010; if (dp[end][Val] != 1000000010) return dp[end][Val]; // if(v[end-1]>Val) // return dp[end][Val]=minWeight(end-1,Val); // else return dp[end][Val] = min(w[end - 1] + minWeight(end - 1, Val - v[end - 1]), minWeight(end - 1, Val)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll i, j; cin >> n >> W; for (i = 0; i < n; i++) { cin >> w[i] >> v[i]; totalV += v[i]; } for (i = 0; i <= n; i++) for (j = 0; j <= totalV; j++) dp[i][j] = 1000000010; for (i = totalV; i >= 1; i--) { if (minWeight(n, i) <= W) break; } cout << i; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define ll long long #define li long #define pb push_back #define mem(arr, x) memset(arr, x, sizeof(arr)) ll n, W; ll w[101], v[101]; ll dp[101][100010]; ll totalV = 0; ll minWeight(ll end, ll Val) { if (Val <= 0) return 0; if (end == 0) return INT_MAX; if (dp[end][Val] != 1000000010) return dp[end][Val]; // if(v[end-1]>Val) // return dp[end][Val]=minWeight(end-1,Val); // else return dp[end][Val] = min(w[end - 1] + minWeight(end - 1, Val - v[end - 1]), minWeight(end - 1, Val)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll i, j; cin >> n >> W; for (i = 0; i < n; i++) { cin >> w[i] >> v[i]; totalV += v[i]; } for (i = 0; i <= n; i++) for (j = 0; j <= totalV; j++) dp[i][j] = 1000000010; for (i = totalV; i >= 1; i--) { if (minWeight(n, i) <= W) break; } cout << i; return 0; }
replace
17
18
17
18
TLE
p03164
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll INF = 1e18 + 5; int main(int argc, char **argv) { ll N = 0, W = 0; cin >> N >> W; vector<ll> weights(N); vector<ll> values(N); for (int i = 0; i < N; ++i) { cin >> weights[i] >> values[i]; } vector<ll> dp(1000000, INF); dp[0] = 0; for (int item = 0; item < N; ++item) { for (ll value = dp.size() - 1; value >= 0; --value) { dp[value + values[item]] = min(dp[value] + weights[item], dp[value + values[item]]); } } ll m = 0; for (ll i = 0; i < dp.size(); ++i) { if (dp[i] <= W && i > m) { m = i; } } cout << m << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll INF = 1e18 + 5; int main(int argc, char **argv) { ll N = 0, W = 0; cin >> N >> W; vector<ll> weights(N); vector<ll> values(N); for (int i = 0; i < N; ++i) { cin >> weights[i] >> values[i]; } vector<ll> dp(1000000, INF); dp[0] = 0; for (int item = 0; item < N; ++item) { for (ll value = dp.size() - 1; value >= 0; --value) { if (value + values[item] < dp.size()) { dp[value + values[item]] = min(dp[value] + weights[item], dp[value + values[item]]); } } } ll m = 0; for (ll i = 0; i < dp.size(); ++i) { if (dp[i] <= W && i > m) { m = i; } } cout << m << "\n"; return 0; }
replace
22
24
22
26
0
p03164
C++
Runtime Error
#include <cmath> #include <iostream> using namespace std; #define MAXN 120 #define MAXV 100020 #define INF 110000000001 long long N, W; long long w[MAXN], v[MAXN]; long long dp[MAXN][MAXV]; int main() { cin >> N >> W; for (long long i = 0; i < N; i++) { cin >> w[i] >> v[i]; } for (long long i = 0; i < N; i++) { for (long long j = 0; j < MAXV; j++) { dp[i][j] = INF; } } dp[0][0] = 0; dp[0][v[0]] = w[0]; for (long long i = 0; i < N; i++) { for (long long j = 0; j < MAXV; j++) { if (v[i] > j) { if (dp[i - 1][j] == INF) { } else { dp[i][j] = dp[i - 1][j]; } } else { if (dp[i - 1][j] != INF && dp[i - 1][j - v[i]] != INF) { dp[i][j] = min(w[i] + dp[i - 1][j - v[i]], dp[i - 1][j]); } else if (dp[i - 1][j] != INF) { dp[i][j] = dp[i - 1][j]; } else if (dp[i - 1][j - v[i]] != INF) { dp[i][j] = dp[i - 1][j - v[i]] + w[i]; } } } } long long ans = 0; for (long long i = 1; i < MAXV; i++) { if (dp[N - 1][i] <= W) ans = i; } cout << ans << endl; }
#include <cmath> #include <iostream> using namespace std; #define MAXN 120 #define MAXV 100020 #define INF 110000000001 long long N, W; long long w[MAXN], v[MAXN]; long long dp[MAXN][MAXV]; int main() { cin >> N >> W; for (long long i = 0; i < N; i++) { cin >> w[i] >> v[i]; } for (long long i = 0; i < N; i++) { for (long long j = 0; j < MAXV; j++) { dp[i][j] = INF; } } dp[0][0] = 0; dp[0][v[0]] = w[0]; for (long long i = 1; i < N; i++) { for (long long j = 0; j < MAXV; j++) { if (v[i] > j) { if (dp[i - 1][j] == INF) { } else { dp[i][j] = dp[i - 1][j]; } } else { if (dp[i - 1][j] != INF && dp[i - 1][j - v[i]] != INF) { dp[i][j] = min(w[i] + dp[i - 1][j - v[i]], dp[i - 1][j]); } else if (dp[i - 1][j] != INF) { dp[i][j] = dp[i - 1][j]; } else if (dp[i - 1][j - v[i]] != INF) { dp[i][j] = dp[i - 1][j - v[i]] + w[i]; } } } } long long ans = 0; for (long long i = 1; i < MAXV; i++) { if (dp[N - 1][i] <= W) ans = i; } cout << ans << endl; }
replace
25
26
25
26
-11
p03164
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define ll long long #define li long #define pb push_back #define mem(arr, x) memset(arr, x, sizeof(arr)) ll n, W; ll w[101], v[101]; ll dp[101][100010]; ll totalV = 0; ll minWeight(ll end, ll Val) { if (Val <= 0) return 0; if (end == 0) return 1000000010; // return INT_MAX; // 2147483647 if (dp[end][Val] != 1000000010) return dp[end][Val]; if (v[end - 1] > Val) return dp[end][Val] = minWeight(end - 1, Val); else return dp[end][Val] = min(w[end - 1] + minWeight(end - 1, Val - v[end - 1]), minWeight(end - 1, Val)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll i, j; cin >> n >> W; for (i = 0; i < n; i++) { cin >> w[i] >> v[i]; totalV += v[i]; } for (i = 0; i <= n; i++) for (j = 0; j <= totalV; j++) dp[i][j] = 1000000010; for (i = totalV; i >= 1; i--) { if (minWeight(n, i) <= W) break; } cout << i; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define ll long long #define li long #define pb push_back #define mem(arr, x) memset(arr, x, sizeof(arr)) ll n, W; ll w[101], v[101]; ll dp[101][100010]; ll totalV = 0; ll minWeight(ll end, ll Val) { if (Val <= 0) return 0; if (end == 0) // return 1000000010; return INT_MAX; // 2147483647 if (dp[end][Val] != 1000000010) return dp[end][Val]; if (v[end - 1] > Val) return dp[end][Val] = minWeight(end - 1, Val); else return dp[end][Val] = min(w[end - 1] + minWeight(end - 1, Val - v[end - 1]), minWeight(end - 1, Val)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll i, j; cin >> n >> W; for (i = 0; i < n; i++) { cin >> w[i] >> v[i]; totalV += v[i]; } for (i = 0; i <= n; i++) for (j = 0; j <= totalV; j++) dp[i][j] = 1000000010; for (i = totalV; i >= 1; i--) { if (minWeight(n, i) <= W) break; } cout << i; return 0; }
replace
17
19
17
19
TLE
p03164
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <limits.h> #include <vector> using namespace std; const long long inf = 1e12; int main() { int n, t; cin >> n >> t; long long pricesum = 0; vector<int> w(n), p(n); for (int i = 0; i < n; i++) { cin >> w[i] >> p[i]; pricesum += p[i]; } vector<long long> dp(pricesum + 1, inf); dp[0] = 0; for (int i = 1; i <= n; i++) { for (int j = pricesum; j >= 0; j--) { dp[j] = min(w[i - 1] + dp[j - p[i - 1]], dp[j]); } } long long ans = LLONG_MIN; long long ansprice = 0; for (int j = 1; j <= pricesum; j++) { if ((dp[j] <= t) && (ans + dp[j] > ans)) { ans = ans + dp[j]; ansprice = j; } } cout << ansprice << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <limits.h> #include <vector> using namespace std; const long long inf = 1e12; int main() { int n, t; cin >> n >> t; long long pricesum = 0; vector<int> w(n), p(n); for (int i = 0; i < n; i++) { cin >> w[i] >> p[i]; pricesum += p[i]; } vector<long long> dp(pricesum + 1, inf); dp[0] = 0; for (int i = 1; i <= n; i++) { for (int j = pricesum; j >= p[i - 1]; j--) { dp[j] = min(w[i - 1] + dp[j - p[i - 1]], dp[j]); } } long long ans = LLONG_MIN; long long ansprice = 0; for (int j = 1; j <= pricesum; j++) { if ((dp[j] <= t) && (ans + dp[j] > ans)) { ans = ans + dp[j]; ansprice = j; } } cout << ansprice << endl; return 0; }
replace
24
25
24
25
0
p03164
C++
Runtime Error
// #pragma GCC optimize "trapv" #include <bits/stdc++.h> #define ll long long int #define fab(a, b, i) for (int i = a; i < b; i++) #define pb push_back #define db double #define mp make_pair #define endl "\n" #define f first #define se second #define all(x) x.begin(), x.end() #define MOD 1000000007 #define BIG 1e10 #define quick \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) using namespace std; ll dp[100005][105]; ll a[105], b[105], n, w; ll recurse(ll val, int i) { if (val <= 0) return 0; if (i == n) return BIG; if (dp[val][i] != -1) return dp[val][i]; dp[val][i] = min(recurse(val, i + 1), a[i] + recurse(val - b[i], i + 1)); return dp[val][i]; } int main() { quick; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ll n,w; cin >> n >> w; fab(0, n, i) cin >> a[i] >> b[i]; memset(dp, -1, sizeof(dp)); for (int i = 100002; i >= 0; i--) { // cout<<recurse(i,0)<<endl; if (recurse(i, 0) <= w) { cout << i; return 0; } } return 0; }
// #pragma GCC optimize "trapv" #include <bits/stdc++.h> #define ll long long int #define fab(a, b, i) for (int i = a; i < b; i++) #define pb push_back #define db double #define mp make_pair #define endl "\n" #define f first #define se second #define all(x) x.begin(), x.end() #define MOD 1000000007 #define BIG 1e10 #define quick \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) using namespace std; ll dp[100005][105]; ll a[105], b[105], n, w; ll recurse(ll val, int i) { if (val <= 0) return 0; if (i == n) return BIG; if (dp[val][i] != -1) return dp[val][i]; dp[val][i] = min(recurse(val, i + 1), a[i] + recurse(val - b[i], i + 1)); return dp[val][i]; } int main() { quick; // ll n,w; cin >> n >> w; fab(0, n, i) cin >> a[i] >> b[i]; memset(dp, -1, sizeof(dp)); for (int i = 100002; i >= 0; i--) { // cout<<recurse(i,0)<<endl; if (recurse(i, 0) <= w) { cout << i; return 0; } } return 0; }
replace
33
37
33
34
0
p03164
C++
Runtime Error
#include <algorithm> #include <climits> #include <iostream> #include <string.h> #include <vector> #define int int64_t #define endl "\n" #define INF INT_MAX - 2 using namespace std; void io() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int n, capacity; int knapsack(vector<int> &weight, vector<int> &price, int maxProfit) { vector<vector<int>> dp(102, vector<int>(1000, 0)); // base case initiliazation for (int N = 0; N <= n; N++) { for (int profit = 0; profit <= maxProfit; profit++) { if (N == 0 && profit == 0) { dp[N][profit] = 0; } else if (N == 0) { dp[N][profit] = INF; } else if (price[N - 1] <= profit) { dp[N][profit] = min(weight[N - 1] + dp[N - 1][profit - price[N - 1]], dp[N - 1][profit]); } else { dp[N][profit] = dp[N - 1][profit]; } } } int ans = 0; for (int profit = 0; profit <= maxProfit; profit++) { if (dp[n][profit] <= capacity && dp[n][profit] != INF) { ans = profit; } } return ans; } void Solution() { cin >> n >> capacity; vector<int> weight(n); vector<int> price(n); int maxProfit = 0; for (int i = 0; i < n; i++) { cin >> weight[i] >> price[i]; maxProfit += price[i]; } cout << knapsack(weight, price, maxProfit); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // io(); Solution(); }
#include <algorithm> #include <climits> #include <iostream> #include <string.h> #include <vector> #define int int64_t #define endl "\n" #define INF INT_MAX - 2 using namespace std; void io() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int n, capacity; int knapsack(vector<int> &weight, vector<int> &price, int maxProfit) { vector<vector<int>> dp(102, vector<int>(100000, 0)); // base case initiliazation for (int N = 0; N <= n; N++) { for (int profit = 0; profit <= maxProfit; profit++) { if (N == 0 && profit == 0) { dp[N][profit] = 0; } else if (N == 0) { dp[N][profit] = INF; } else if (price[N - 1] <= profit) { dp[N][profit] = min(weight[N - 1] + dp[N - 1][profit - price[N - 1]], dp[N - 1][profit]); } else { dp[N][profit] = dp[N - 1][profit]; } } } int ans = 0; for (int profit = 0; profit <= maxProfit; profit++) { if (dp[n][profit] <= capacity && dp[n][profit] != INF) { ans = profit; } } return ans; } void Solution() { cin >> n >> capacity; vector<int> weight(n); vector<int> price(n); int maxProfit = 0; for (int i = 0; i < n; i++) { cin >> weight[i] >> price[i]; maxProfit += price[i]; } cout << knapsack(weight, price, maxProfit); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // io(); Solution(); }
replace
17
18
17
18
0
p03164
C++
Runtime Error
// bismillahir rahmanir rahim //Author:Fayed Anik #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type,less<int>, // rb_tree_tag,tree_order_statistics_node_update> #define ll long long #define lf double #define pb(x) push_back(x) #define ull unsigned long long #define sfl(a) scanf("%lld", &a) #define sf(a) scanf("%d", &a) #define pf(a) printf("%d\n", a) #define pfl(a) printf("%lld\n", a) #define FOR(x, n) for (ll x = 1; x <= n; ++x) #define pii pair<ll, ll> #define mp(a, b) make_pair(a, b) #define UNIQUE(v) v.resize(distance(v.begin(), unique(v.begin(), v.end()))) #define mod 1000000007 #define INF 2e18 #define EPS 1e-15 #define f1 first #define f2 second #define all(v) v.begin(), v.end() #define PI acos(-1) #define printminusone printf("-1\n") #define bug printf("bug") #define FILEIN freopen("in.txt", "r", stdin) #define FILEOUT freopen("out.txt", "w", stdout) // ll SET(ll mask,ll pos){ return mask = (mask | (1ll<<pos)); } // ll RESET(ll mask,ll pos){ return mask = mask & ~(1ll<<pos); } // bool CHECK(ll mask,ll pos) { return (bool) (mask & (1ll<<pos)); } // priority_queue <ll, vector<ll>, greater<ll> > pq; // int dx[]={0,0,1,-1,1,1,-1,-1}; // int dy[]={1,-1,0,0,1,-1,1,-1}; ll dp[100][100005], a[1005], b[1005], n, target; ll F(ll pos, ll now) { if (pos > n) { if (now >= target) return 0; else return INF; } ll &ret = dp[pos][now]; if (~ret) return ret; ret = a[pos] + F(pos + 1, now + b[pos]); ret = min(ret, F(pos + 1, now)); return dp[pos][now] = ret; } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ll w; sfl(n), sfl(w); for (ll i = 1; i <= n; i++) { sfl(a[i]), sfl(b[i]); } ll lw = 0, hi = 1e5, mid, ans = 0, val; while (lw <= hi) { mid = (lw + hi) / 2; target = mid; memset(dp, -1, sizeof dp); val = F(1, 0); if (val <= w) { ans = mid; lw = mid + 1; } else { hi = mid - 1; } } pfl(ans); return 0; }
// bismillahir rahmanir rahim //Author:Fayed Anik #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; // using namespace __gnu_pbds; // #define ordered_set tree<int, null_type,less<int>, // rb_tree_tag,tree_order_statistics_node_update> #define ll long long #define lf double #define pb(x) push_back(x) #define ull unsigned long long #define sfl(a) scanf("%lld", &a) #define sf(a) scanf("%d", &a) #define pf(a) printf("%d\n", a) #define pfl(a) printf("%lld\n", a) #define FOR(x, n) for (ll x = 1; x <= n; ++x) #define pii pair<ll, ll> #define mp(a, b) make_pair(a, b) #define UNIQUE(v) v.resize(distance(v.begin(), unique(v.begin(), v.end()))) #define mod 1000000007 #define INF 2e18 #define EPS 1e-15 #define f1 first #define f2 second #define all(v) v.begin(), v.end() #define PI acos(-1) #define printminusone printf("-1\n") #define bug printf("bug") #define FILEIN freopen("in.txt", "r", stdin) #define FILEOUT freopen("out.txt", "w", stdout) // ll SET(ll mask,ll pos){ return mask = (mask | (1ll<<pos)); } // ll RESET(ll mask,ll pos){ return mask = mask & ~(1ll<<pos); } // bool CHECK(ll mask,ll pos) { return (bool) (mask & (1ll<<pos)); } // priority_queue <ll, vector<ll>, greater<ll> > pq; // int dx[]={0,0,1,-1,1,1,-1,-1}; // int dy[]={1,-1,0,0,1,-1,1,-1}; ll dp[101][100001], a[101], b[101], n, target; ll F(ll pos, ll now) { if (pos > n) { if (now >= target) return 0; else return INF; } ll &ret = dp[pos][now]; if (~ret) return ret; ret = a[pos] + F(pos + 1, now + b[pos]); ret = min(ret, F(pos + 1, now)); return dp[pos][now] = ret; } int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ll w; sfl(n), sfl(w); for (ll i = 1; i <= n; i++) { sfl(a[i]), sfl(b[i]); } ll lw = 0, hi = 1e5, mid, ans = 0, val; while (lw <= hi) { mid = (lw + hi) / 2; target = mid; memset(dp, -1, sizeof dp); val = F(1, 0); if (val <= w) { ans = mid; lw = mid + 1; } else { hi = mid - 1; } } pfl(ans); return 0; }
replace
42
43
42
43
0
p03164
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define fo(i, n) for (int i = 0; i < n; i++) #define Fo(i, k, n) for (int i = k; i < n; i++) #define endl "\n" #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define vec(x) vector<x> #define matrix(x) vector<vector<x>> #define mem(a, b) memset(a, b, sizeof a) #define vii vector<pair<int, int>> #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define MOD 998244353 #define deb(x) cerr << #x << " = " << x << "\n" #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define all(v) v.begin(), v.end() #define rep(i, a, b) for (int i = a; i < b; ++i) #define len(s) s.length() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; int power(int a, int b, int m = mod) { int ans = 1; while (b > 0) { if (b & 1) ans = (ans * a) % m; a = (a * a) % m; b >>= 1; } return ans; } void angshuGod() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } // const int N = 1e5+1; const double pi = acos(-1); int N, W; int dp[110][1100]; int wt[110], val[110]; int sum = 0; int find(int v, int idx) { if (idx == N) { return v == 0 ? 0 : INT_MAX; } if (dp[idx][v] != -1) return dp[idx][v]; int ans = find(v, idx + 1); if (val[idx] <= v) ans = min(ans, wt[idx] + find(v - val[idx], idx + 1)); return dp[idx][v] = ans; } void solve() { cin >> N >> W; mem(dp, -1); for (int i = 0; i < N; i++) { cin >> wt[i] >> val[i]; sum += val[i]; } for (int i = sum; i >= 0; i--) { if (find(i, 0) <= W) { cout << i << "\n"; return; } } cout << "0"; } int32_t main() { angshuGod(); int t; t = 1; // cin>>t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define fo(i, n) for (int i = 0; i < n; i++) #define Fo(i, k, n) for (int i = k; i < n; i++) #define endl "\n" #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define vec(x) vector<x> #define matrix(x) vector<vector<x>> #define mem(a, b) memset(a, b, sizeof a) #define vii vector<pair<int, int>> #define pii pair<int, int> #define vi vector<int> #define mii map<int, int> #define pqb priority_queue<int> #define pqs priority_queue<int, vi, greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define MOD 998244353 #define deb(x) cerr << #x << " = " << x << "\n" #define inf 1e18 #define ps(x, y) fixed << setprecision(y) << x #define mk(arr, n, type) type *arr = new type[n]; #define w(x) \ int x; \ cin >> x; \ while (x--) #define all(v) v.begin(), v.end() #define rep(i, a, b) for (int i = a; i < b; ++i) #define len(s) s.length() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; int power(int a, int b, int m = mod) { int ans = 1; while (b > 0) { if (b & 1) ans = (ans * a) % m; a = (a * a) % m; b >>= 1; } return ans; } void angshuGod() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } // const int N = 1e5+1; const double pi = acos(-1); int N, W; int dp[110][100006]; int wt[110], val[110]; int sum = 0; int find(int v, int idx) { if (idx == N) { return v == 0 ? 0 : INT_MAX; } if (dp[idx][v] != -1) return dp[idx][v]; int ans = find(v, idx + 1); if (val[idx] <= v) ans = min(ans, wt[idx] + find(v - val[idx], idx + 1)); return dp[idx][v] = ans; } void solve() { cin >> N >> W; mem(dp, -1); for (int i = 0; i < N; i++) { cin >> wt[i] >> val[i]; sum += val[i]; } for (int i = sum; i >= 0; i--) { if (find(i, 0) <= W) { cout << i << "\n"; return; } } cout << "0"; } int32_t main() { angshuGod(); int t; t = 1; // cin>>t; while (t--) { solve(); } return 0; }
replace
69
70
69
70
0
p03164
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int using namespace std; const int mx = 1e3 + 10; ll w[200], v[200], dp[mx][200], n, W; ll find_max(ll val, ll index) { if (index == n) return val == 0 ? 0 : INT_MAX; if (~dp[val][index]) return dp[val][index]; ll ans = find_max(val, index + 1); if (v[index] <= val) { ans = min(ans, find_max(val - v[index], index + 1) + w[index]); } return dp[val][index] = ans; } ll calc(ll mxx) { for (ll i = mxx; i >= 1; i--) { if (find_max(i, 0) <= W) { return i; } } return 0; } void solve() { ll mxx = 0; cin >> n >> W; memset(dp, -1, sizeof dp); for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; mxx += v[i]; } cout << calc(mxx) << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin>>t; while (t--) { solve(); } }
#include <bits/stdc++.h> #define ll long long int using namespace std; const int mx = 100009; ll w[mx], v[mx], dp[mx][200], n, W; ll find_max(ll val, ll index) { if (index == n) return val == 0 ? 0 : INT_MAX; if (~dp[val][index]) return dp[val][index]; ll ans = find_max(val, index + 1); if (v[index] <= val) { ans = min(ans, find_max(val - v[index], index + 1) + w[index]); } return dp[val][index] = ans; } ll calc(ll mxx) { for (ll i = mxx; i >= 1; i--) { if (find_max(i, 0) <= W) { return i; } } return 0; } void solve() { ll mxx = 0; cin >> n >> W; memset(dp, -1, sizeof dp); for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; mxx += v[i]; } cout << calc(mxx) << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin>>t; while (t--) { solve(); } }
replace
4
6
4
6
0
p03164
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int N, W, w[101], v[102]; long long dp[102][10003]; long long rec(int pos, int val) { // printf("%d %d\n", pos, val); if (dp[pos][val] != -1) { return dp[pos][val]; } long long ans = 2000000000000; if (pos != 0) { ans = rec(pos - 1, val); } if (v[pos] <= val && pos != 0) { ans = min(ans, w[pos] + rec(pos - 1, val - v[pos])); } else if (pos == 0 && v[pos] == val) { ans = w[pos]; } else if (pos == 0 && val == 0) { ans = 0; } dp[pos][val] = ans; // printf("%lld\n", ans); return ans; } int main() { scanf("%d %d", &N, &W); for (int i = 0; i < N; ++i) { scanf("%d %d", &w[i], &v[i]); } memset(dp, -1, sizeof(dp)); for (int i = N * 1000 + 1; i >= 0; --i) { for (int j = N - 1; j >= 0; --j) { if (rec(j, i) <= W) { printf("%d", i); return 0; } } } }
#include <bits/stdc++.h> using namespace std; int N, W, w[101], v[102]; long long dp[102][100003]; long long rec(int pos, int val) { // printf("%d %d\n", pos, val); if (dp[pos][val] != -1) { return dp[pos][val]; } long long ans = 2000000000000; if (pos != 0) { ans = rec(pos - 1, val); } if (v[pos] <= val && pos != 0) { ans = min(ans, w[pos] + rec(pos - 1, val - v[pos])); } else if (pos == 0 && v[pos] == val) { ans = w[pos]; } else if (pos == 0 && val == 0) { ans = 0; } dp[pos][val] = ans; // printf("%lld\n", ans); return ans; } int main() { scanf("%d %d", &N, &W); for (int i = 0; i < N; ++i) { scanf("%d %d", &w[i], &v[i]); } memset(dp, -1, sizeof(dp)); for (int i = N * 1000 + 1; i >= 0; --i) { for (int j = N - 1; j >= 0; --j) { if (rec(j, i) <= W) { printf("%d", i); return 0; } } } }
replace
3
4
3
4
0
p03164
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fore(i, s, e) for (int i = s; i < e; i++) const int N = 100; const int V = 10005; typedef long long ll; int n, w; ll dp[N][V]; ll ww[N], v[V]; int main() { cin >> n >> w; int s = 0; fore(i, 1, n + 1) { cin >> ww[i] >> v[i]; s += v[i]; } fore(i, 0, s + 1) dp[0][i] = 1e17; fore(i, 0, n + 1) dp[i][0] = 0; fore(i, 1, n + 1) { fore(j, 1, s + 1) { dp[i][j] = dp[i - 1][j]; if (v[i] <= j) dp[i][j] = min(dp[i][j], dp[i - 1][j - v[i]] + ww[i]); } } int res = 0; fore(j, 0, s + 1) if (dp[n][j] <= w) res = max(res, j); cout << res << endl; }
#include <bits/stdc++.h> using namespace std; #define fore(i, s, e) for (int i = s; i < e; i++) const int N = 200; const int V = 2e5; typedef long long ll; int n, w; ll dp[N][V]; ll ww[N], v[V]; int main() { cin >> n >> w; int s = 0; fore(i, 1, n + 1) { cin >> ww[i] >> v[i]; s += v[i]; } fore(i, 0, s + 1) dp[0][i] = 1e17; fore(i, 0, n + 1) dp[i][0] = 0; fore(i, 1, n + 1) { fore(j, 1, s + 1) { dp[i][j] = dp[i - 1][j]; if (v[i] <= j) dp[i][j] = min(dp[i][j], dp[i - 1][j - v[i]] + ww[i]); } } int res = 0; fore(j, 0, s + 1) if (dp[n][j] <= w) res = max(res, j); cout << res << endl; }
replace
6
8
6
8
0
p03164
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; using vl = vector<ll>; /* short */ #define pb push_back #define mp make_pair #define Fi first #define Se second #define ALL(v) begin(v), end(v) #define RALL(v) rbegin(v), rend(v) /* REPmacro */ #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FORR(i, a, b) for (int i = (a); i >= (b); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define FOREACH(x, a) for (auto x : a) /* exchange */ #define CHMIN(a, b) (a) = min((ll)(a), (ll)(b)) #define CHMAX(a, b) (a) = max((ll)(a), (ll)(b)) /* function */ #define IN(x) cin >> x #define DEBUG(x) cerr << (x) << " " #define LN() cerr << "\n" #define PRINT(x) cout << (x) << endl #define BR cout << endl /* const */ const int ARRAY = 100005; const int INF = 1001001001; // 10^9 const ll LINF = 1001001001001001001; // 10^18 const int MOD = 1e9 + 7; ll ret = 0; string s; ll w[101]; ll v[101]; ll dp[101][ARRAY]; ll knapsack(ll i, ll j) { if (i < 0 && j <= 0) return 0; if (i < 0 && j > 0) return INF; if (dp[i][j] != -1) return dp[i][j]; ll ans = min(knapsack(i - 1, j), w[i] + knapsack(i - 1, j - v[i])); dp[i][j] = ans; return ans; } int main(void) { ll N, W; IN(N); IN(W); REP(i, N) { IN(w[i]); IN(v[i]); } REP(i, 101) { REP(j, ARRAY) { dp[i][j] = -1; } } REP(i, ARRAY) { ll weight = knapsack(N - 1, i); // DEBUG(N-1); // DEBUG(i); // DEBUG("=>"); // DEBUG(weight); // LN(); if (weight > W) { ret = i - 1; break; } } PRINT(ret); }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; using vl = vector<ll>; /* short */ #define pb push_back #define mp make_pair #define Fi first #define Se second #define ALL(v) begin(v), end(v) #define RALL(v) rbegin(v), rend(v) /* REPmacro */ #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FORR(i, a, b) for (int i = (a); i >= (b); i--) #define REP(i, n) for (int i = 0; i < (n); i++) #define FOREACH(x, a) for (auto x : a) /* exchange */ #define CHMIN(a, b) (a) = min((ll)(a), (ll)(b)) #define CHMAX(a, b) (a) = max((ll)(a), (ll)(b)) /* function */ #define IN(x) cin >> x #define DEBUG(x) cerr << (x) << " " #define LN() cerr << "\n" #define PRINT(x) cout << (x) << endl #define BR cout << endl /* const */ const int ARRAY = 100005; const int INF = 1001001001; // 10^9 const ll LINF = 1001001001001001001; // 10^18 const int MOD = 1e9 + 7; ll ret = 0; string s; ll w[101]; ll v[101]; ll dp[101][ARRAY]; ll knapsack(ll i, ll j) { if (j <= 0) return 0; if (i < 0 && j > 0) return INF; if (dp[i][j] != -1) return dp[i][j]; ll ans = min(knapsack(i - 1, j), w[i] + knapsack(i - 1, j - v[i])); dp[i][j] = ans; return ans; } int main(void) { ll N, W; IN(N); IN(W); REP(i, N) { IN(w[i]); IN(v[i]); } REP(i, 101) { REP(j, ARRAY) { dp[i][j] = -1; } } REP(i, ARRAY) { ll weight = knapsack(N - 1, i); // DEBUG(N-1); // DEBUG(i); // DEBUG("=>"); // DEBUG(weight); // LN(); if (weight > W) { ret = i - 1; break; } } PRINT(ret); }
replace
44
45
44
45
0
p03164
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long n, k; long long w[109], v[109]; long long dp[109][100009]; int main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> w[i] >> v[i]; for (int i = 0; i < n; i++) { for (int j = 1; j < 100009; j++) { dp[i][j] = 1000000000000009; } } dp[0][v[0]] = w[0]; for (int i = 1; i < n; i++) { for (int j = 0; j < 100009; j++) { if (j - v[0] >= 0) dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i]); else dp[i][j] = dp[i - 1][j]; } } for (int i = 100001; i >= 0; i--) { if (dp[n - 1][i] <= k) { cout << i << endl; exit(0); } } }
#include <bits/stdc++.h> using namespace std; long long n, k; long long w[109], v[109]; long long dp[109][100009]; int main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> w[i] >> v[i]; for (int i = 0; i < n; i++) { for (int j = 1; j < 100009; j++) { dp[i][j] = 1000000000000009; } } dp[0][v[0]] = w[0]; for (int i = 1; i < n; i++) { for (int j = 0; j < 100009; j++) { if (j - v[i] >= 0) dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - v[i]] + w[i]); else dp[i][j] = dp[i - 1][j]; } } for (int i = 100001; i >= 0; i--) { if (dp[n - 1][i] <= k) { cout << i << endl; exit(0); } } }
replace
17
18
17
18
0
p03164
C++
Runtime Error
/** * Coded by : lucky_21 * --------Lokesh Singh **/ #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define F first #define S second #define pb push_back #define lb lower_bound #define ub upper_bound #define pii pair<int, int> #define all(x) x.begin(), x.end() #define fix fixed << setprecision(10) #define rep(i, a, b) for (int i = int(a); i <= int(b); i++) #define repb(i, b, a) for (int i = int(b); i >= int(a); i--) #define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) typedef double db; typedef long long ll; const int N = 1e5 + 5; const int mod = 1e9 + 7; ll n, W, V, w[N], v[N], dp[N][105]; ll rec(int val, int i) { if (i > n) return (val <= 0 ? 0 : 1e9); ll &cache = dp[val][i]; if (cache != -1) return cache; rec(val - 1, i); cache = rec(val, i + 1); cache = min(cache, w[i] + rec(val - v[i], i + 1)); return cache; } signed main() { FastIO; cin >> n >> W; rep(i, 1, n) cin >> w[i] >> v[i], V += v[i]; memset(dp, -1, sizeof dp); rec(V, 1); int ans = 0; rep(i, 1, V) { rep(j, 1, n) if (dp[i][j] != -1 and dp[i][j] <= W) ans = i; } cout << ans; return 0; }
/** * Coded by : lucky_21 * --------Lokesh Singh **/ #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define F first #define S second #define pb push_back #define lb lower_bound #define ub upper_bound #define pii pair<int, int> #define all(x) x.begin(), x.end() #define fix fixed << setprecision(10) #define rep(i, a, b) for (int i = int(a); i <= int(b); i++) #define repb(i, b, a) for (int i = int(b); i >= int(a); i--) #define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) typedef double db; typedef long long ll; const int N = 1e5 + 5; const int mod = 1e9 + 7; ll n, W, V, w[N], v[N], dp[N][105]; ll rec(int val, int i) { if (val == 0) return 0; if (i > n or val < 0) return 1e9; ll &cache = dp[val][i]; if (cache != -1) return cache; rec(val - 1, i); cache = rec(val, i + 1); cache = min(cache, w[i] + rec(val - v[i], i + 1)); return cache; } signed main() { FastIO; cin >> n >> W; rep(i, 1, n) cin >> w[i] >> v[i], V += v[i]; memset(dp, -1, sizeof dp); rec(V, 1); int ans = 0; rep(i, 1, V) { rep(j, 1, n) if (dp[i][j] != -1 and dp[i][j] <= W) ans = i; } cout << ans; return 0; }
replace
36
38
36
40
0
p03164
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; long long N, maxW; long long dp[102][100002]; struct asdfklj { long long val, w; } arr[102]; long long knapsack(long long i, long long current) { if (current < 0) return INT_MAX; if (i == N) { if (current == 0) return 0; return INT_MAX; } long long &ret = dp[i][current]; return ret = min(arr[i].w + knapsack(i + 1, current - arr[i].val), knapsack(i + 1, current)); } int main() { cin >> N >> maxW; memset(dp, -1, sizeof(dp)); for (long long i = 0; i < N; i++) cin >> arr[i].w >> arr[i].val; for (long long i = 100000;; i--) if (knapsack(0, i) <= maxW) printf("%d", i), exit(0); }
#include <bits/stdc++.h> using namespace std; long long N, maxW; long long dp[102][100002]; struct asdfklj { long long val, w; } arr[102]; long long knapsack(long long i, long long current) { if (current < 0) return INT_MAX; if (i == N) { if (current == 0) return 0; return INT_MAX; } long long &ret = dp[i][current]; if (ret != -1) return ret; return ret = min(arr[i].w + knapsack(i + 1, current - arr[i].val), knapsack(i + 1, current)); } int main() { cin >> N >> maxW; memset(dp, -1, sizeof(dp)); for (long long i = 0; i < N; i++) cin >> arr[i].w >> arr[i].val; for (long long i = 100000;; i--) if (knapsack(0, i) <= maxW) printf("%d", i), exit(0); }
insert
18
18
18
20
TLE
p03164
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <map> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #define loopi(x, y) for (int i = x; i < (y); i++) #define rloopi(x, y) for (int i = x; i >= (y); i--) #define rloopj(x, y) for (int j = x; j >= (y); j--) #define loopj(x, y) for (int j = x; j < (y); j++) #define nl cout << "\n"; #define ll long long int using namespace std; #define mod 1000000007 ll power(ll a, ll p) { if (p == 0) return 1; ll t = power(a, p / 2) % mod; if (p % 2 == 0) return t * t % mod; return (a * t % mod) * t % mod; } int main() { ios ::sync_with_stdio(false); cin.tie(nullptr); // ios_base :: sync_with_stdio(false); // cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n, W; cin >> n >> W; vector<int> w(n); vector<int> v(n); ll max_val = 0; loopi(0, n) { cin >> w[i] >> v[i]; max_val += v[i]; } vector<int> dp(max_val + 1, 1000000001); dp[0] = 0; loopj(0, n) { rloopi(max_val, v[j]) { dp[i] = min(dp[i - v[j]] + w[j], dp[i]); } } int ans; rloopi(max_val, 0) { if (dp[i] <= W) { ans = i; break; } } cout << ans; return 0; }
#include <algorithm> #include <climits> #include <cmath> #include <iostream> #include <map> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #define loopi(x, y) for (int i = x; i < (y); i++) #define rloopi(x, y) for (int i = x; i >= (y); i--) #define rloopj(x, y) for (int j = x; j >= (y); j--) #define loopj(x, y) for (int j = x; j < (y); j++) #define nl cout << "\n"; #define ll long long int using namespace std; #define mod 1000000007 ll power(ll a, ll p) { if (p == 0) return 1; ll t = power(a, p / 2) % mod; if (p % 2 == 0) return t * t % mod; return (a * t % mod) * t % mod; } int main() { ios ::sync_with_stdio(false); cin.tie(nullptr); // ios_base :: sync_with_stdio(false); // cin.tie(NULL); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int n, W; cin >> n >> W; vector<int> w(n); vector<int> v(n); ll max_val = 0; loopi(0, n) { cin >> w[i] >> v[i]; max_val += v[i]; } vector<int> dp(max_val + 1, 1000000001); dp[0] = 0; loopj(0, n) { rloopi(max_val, v[j]) { dp[i] = min(dp[i - v[j]] + w[j], dp[i]); } } int ans; rloopi(max_val, 0) { if (dp[i] <= W) { ans = i; break; } } cout << ans; return 0; }
replace
38
42
38
42
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03164
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll inf = 1LL << 60; int w[110], v[110]; long long dp[10010]; int main() { ios_base::sync_with_stdio(false); int n, W; cin >> n >> W; int V = 0; for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; V += v[i]; } /// dp[v] = minimum weight that i can achieve with value = v for (int i = 0; i <= V; i++) dp[i] = inf; dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = V - v[i]; j >= 0; j--) { dp[j + v[i]] = min(dp[j + v[i]], dp[j] + w[i] * 1LL); } } int ans = 0; for (int i = 0; i <= V; i++) { if (dp[i] <= W) { ans = i; } } cout << ans << endl; } /* * E **/
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll inf = 1LL << 60; int w[110], v[110]; ll dp[100010]; int main() { ios_base::sync_with_stdio(false); int n, W; cin >> n >> W; int V = 0; for (int i = 0; i < n; i++) { cin >> w[i] >> v[i]; V += v[i]; } /// dp[v] = minimum weight that i can achieve with value = v for (int i = 0; i <= V; i++) dp[i] = inf; dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = V - v[i]; j >= 0; j--) { dp[j + v[i]] = min(dp[j + v[i]], dp[j] + w[i] * 1LL); } } int ans = 0; for (int i = 0; i <= V; i++) { if (dp[i] <= W) { ans = i; } } cout << ans << endl; } /* * E **/
replace
9
10
9
10
0
p03164
C++
Runtime Error
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #define int long long #define pb push_back #define pi pair<int, int> #define vi vector<int> #define all(v) v.begin(), v.end() #define MOD 1000000007 #define F first #define S second #define FOR(i, r) for (int i = 0; i < r; i++) #define REP(i, l, r) for (int i = l; i < r; i++) #define RER(i, l, r) for (int i = l; i >= r; i--) #define print(v) \ for (auto i : v) \ cout << i << ' '; #define FASTIO \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define mem(arr, k) memset(arr, k, sizeof(arr)); #define pii acos(-1.0) #define PI 3.1415926535897932385 #define Sin(a) sin((pi * a) / 180) #define test() \ int x; \ cin >> x; \ while (x--) using namespace std; const int inf = (int)1e17 + 9; int dp[102][10002]; int32_t main() { FASTIO; int n, w, s = 0; cin >> n >> w; vector<int> wt(n), val(n); REP(i, 0, n) { cin >> wt[i] >> val[i]; s += val[i]; } for (int i = 0; i <= n; i++) { for (int j = 0; j <= s; j++) dp[i][j] = inf; } for (int i = 0; i <= n; i++) dp[i][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= s; j++) { dp[i][j] = min(dp[i][j], dp[i - 1][j]); if (j >= val[i - 1]) { dp[i][j] = min(dp[i][j], dp[i - 1][j - val[i - 1]] + wt[i - 1]); } } } int ans = -1; for (int j = 1; j <= s; j++) { if (dp[n][j] <= w) { ans = max(ans, j); } } cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <iterator> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #define int long long #define pb push_back #define pi pair<int, int> #define vi vector<int> #define all(v) v.begin(), v.end() #define MOD 1000000007 #define F first #define S second #define FOR(i, r) for (int i = 0; i < r; i++) #define REP(i, l, r) for (int i = l; i < r; i++) #define RER(i, l, r) for (int i = l; i >= r; i--) #define print(v) \ for (auto i : v) \ cout << i << ' '; #define FASTIO \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define mem(arr, k) memset(arr, k, sizeof(arr)); #define pii acos(-1.0) #define PI 3.1415926535897932385 #define Sin(a) sin((pi * a) / 180) #define test() \ int x; \ cin >> x; \ while (x--) using namespace std; const int inf = (int)1e17 + 9; int dp[102][100002]; int32_t main() { FASTIO; int n, w, s = 0; cin >> n >> w; vector<int> wt(n), val(n); REP(i, 0, n) { cin >> wt[i] >> val[i]; s += val[i]; } for (int i = 0; i <= n; i++) { for (int j = 0; j <= s; j++) dp[i][j] = inf; } for (int i = 0; i <= n; i++) dp[i][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= s; j++) { dp[i][j] = min(dp[i][j], dp[i - 1][j]); if (j >= val[i - 1]) { dp[i][j] = min(dp[i][j], dp[i - 1][j - val[i - 1]] + wt[i - 1]); } } } int ans = -1; for (int j = 1; j <= s; j++) { if (dp[n][j] <= w) { ans = max(ans, j); } } cout << ans << endl; return 0; }
replace
46
47
46
47
0
p03164
C++
Runtime Error
// https://atcoder.jp/contests/dp/tasks/dp_e #include <bits/stdc++.h> #define loop(i, a, b) for (i = (a); i < (b); i++) #define pb push_back #define eb emplace_back #define all(v) (v).begin(), (v).end() #define fr first #define sc second #define mk make_pair #define endl '\n' #define MOD 1000000007 using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<ll, ll> pl; typedef pair<int, int> pi; const int mx = 1e5 + 10; ll n, w, ans = 0; ll arr[101][2]; ll dp[100][mx]; void go(ll idx) { if (idx == n + 1) return; for (ll i = 0; i < mx; i++) { dp[idx][i] = dp[idx - 1][i]; if (i >= arr[idx][1]) { dp[idx][i] = min(dp[idx][i], dp[idx - 1][i - arr[idx][1]] + arr[idx][0]); } if (dp[idx][i] <= w) { // cout<<dp[idx][i]<<' '; // cout<<i<<' '<<idx<<endl; ans = max(ans, i); } } go(idx + 1); } void solve() { cin >> n >> w; for (ll i = 0; i < 100; i++) for (ll j = 0; j < mx; j++) dp[i][j] = 1e18; dp[0][0] = 0; for (ll i = 1; i <= n; i++) { cin >> arr[i][0] >> arr[i][1]; // w and val } go(1); cout << ans << endl; } int main() { int t; t = 1; while (t--) solve(); }
// https://atcoder.jp/contests/dp/tasks/dp_e #include <bits/stdc++.h> #define loop(i, a, b) for (i = (a); i < (b); i++) #define pb push_back #define eb emplace_back #define all(v) (v).begin(), (v).end() #define fr first #define sc second #define mk make_pair #define endl '\n' #define MOD 1000000007 using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<ll, ll> pl; typedef pair<int, int> pi; const int mx = 1e5 + 10; ll n, w, ans = 0; ll arr[102][2]; ll dp[102][mx]; void go(ll idx) { if (idx == n + 1) return; for (ll i = 0; i < mx; i++) { dp[idx][i] = dp[idx - 1][i]; if (i >= arr[idx][1]) { dp[idx][i] = min(dp[idx][i], dp[idx - 1][i - arr[idx][1]] + arr[idx][0]); } if (dp[idx][i] <= w) { // cout<<dp[idx][i]<<' '; // cout<<i<<' '<<idx<<endl; ans = max(ans, i); } } go(idx + 1); } void solve() { cin >> n >> w; for (ll i = 0; i < 100; i++) for (ll j = 0; j < mx; j++) dp[i][j] = 1e18; dp[0][0] = 0; for (ll i = 1; i <= n; i++) { cin >> arr[i][0] >> arr[i][1]; // w and val } go(1); cout << ans << endl; } int main() { int t; t = 1; while (t--) solve(); }
replace
19
21
19
21
0
p03164
C++
Runtime Error
#include <bits/stdc++.h> /* #pragma GCC optimize("-Ofast") //#pragma GCC optimize("trapv") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native") #pragma GCC optimize("-ffast-math") #pragma GCC optimize("-funroll-loops")*/ #define I inline void #define S struct #define vi vector<int> #define vii vector<pair<int, int>> #define pii pair<int, int> #define pll pair<ll, ll> using namespace std; using ll = long long; using ld = long double; const int N = 1e5 + 7, mod = 1e9 + 7; const ll inf = 1e11; // How interesting! int n, w; ll dp[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("in.in" , "r" , stdin) ; cin >> n >> w; for (int j = 1; j < N; j++) { dp[j] = inf; } for (int i = 1; i <= n; i++) { ll x, y; cin >> x >> y; for (int j = N - 1; ~j; j--) { dp[j + y] = min(dp[j + y], dp[j] + x); } } int ans = 0; for (int i = 0; i < N; i++) { if (dp[i] <= w) ans = i; } cout << ans; return 0; }
#include <bits/stdc++.h> /* #pragma GCC optimize("-Ofast") //#pragma GCC optimize("trapv") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native") #pragma GCC optimize("-ffast-math") #pragma GCC optimize("-funroll-loops")*/ #define I inline void #define S struct #define vi vector<int> #define vii vector<pair<int, int>> #define pii pair<int, int> #define pll pair<ll, ll> using namespace std; using ll = long long; using ld = long double; const int N = 1e5 + 7, mod = 1e9 + 7; const ll inf = 1e11; // How interesting! int n, w; ll dp[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen("in.in" , "r" , stdin) ; cin >> n >> w; for (int j = 1; j < N; j++) { dp[j] = inf; } for (int i = 1; i <= n; i++) { ll x, y; cin >> x >> y; for (int j = N - y - 1; ~j; j--) { dp[j + y] = min(dp[j + y], dp[j] + x); } } int ans = 0; for (int i = 0; i < N; i++) { if (dp[i] <= w) ans = i; } cout << ans; return 0; }
replace
41
42
41
42
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define all(cont) cont.begin(), cont.end() #define pb push_back #define fi first #define se second #define DEBUG(x) cerr << (#x) << ": " << (x) << '\n' using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef long long ll; typedef unsigned long long ull; template <class T> bool uin(T &a, T b) { return (a <= b ? false : (a = b, true)); } template <class T> bool uax(T &a, T b) { return (a >= b ? false : (a = b, true)); } // ifstream f(".in"); // ofstream g(".out"); int main() { ios::sync_with_stdio(0); cin.tie(0); #ifdef LOCAL_DEFINE freopen(".in", "r", stdin); #endif string s, t; cin >> s >> t; int n = s.length(); int m = t.length(); s = " " + s; t = " " + t; vector<vector<int>> dp(n + 2, vector<int>(m + 2)); vector<vector<pair<int, int>>> from(n + 2, vector<pair<int, int>>(m + 2, {-1, -1})); auto setFrom = [&](int val, pair<int, int> curr, pair<int, int> old) { if (uax(dp[curr.first][curr.second], val)) { from[curr.first][curr.second] = old; } }; int ans = 0; pair<int, int> p = {0, 0}; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (s[i] == t[j]) { setFrom(1 + dp[i - 1][j - 1], {i, j}, {i - 1, j - 1}); } setFrom(dp[i - 1][j], {i, j}, {i - 1, j}); setFrom(dp[i][j - 1], {i, j}, {i, j - 1}); if (uax(ans, dp[i][j])) { p = {i, j}; } } } if (ans == 0) { cout << ""; return 0; } string lcs; while (p.first != 0 && p.second != 0) { if (s[p.first] == t[p.second]) { lcs += s[p.first]; } p = from[p.first][p.second]; } reverse(lcs.begin(), lcs.end()); cout << lcs << '\n'; #ifdef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
#include <bits/stdc++.h> #define all(cont) cont.begin(), cont.end() #define pb push_back #define fi first #define se second #define DEBUG(x) cerr << (#x) << ": " << (x) << '\n' using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef long long ll; typedef unsigned long long ull; template <class T> bool uin(T &a, T b) { return (a <= b ? false : (a = b, true)); } template <class T> bool uax(T &a, T b) { return (a >= b ? false : (a = b, true)); } // ifstream f(".in"); // ofstream g(".out"); int main() { ios::sync_with_stdio(0); cin.tie(0); #ifdef LOCAL_DEFINE freopen(".in", "r", stdin); #endif string s, t; cin >> s >> t; int n = s.length(); int m = t.length(); s = " " + s; t = " " + t; vector<vector<int>> dp(n + 2, vector<int>(m + 2)); vector<vector<pair<int, int>>> from(n + 2, vector<pair<int, int>>(m + 2, {0, 0})); auto setFrom = [&](int val, pair<int, int> curr, pair<int, int> old) { if (uax(dp[curr.first][curr.second], val)) { from[curr.first][curr.second] = old; } }; int ans = 0; pair<int, int> p = {0, 0}; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (s[i] == t[j]) { setFrom(1 + dp[i - 1][j - 1], {i, j}, {i - 1, j - 1}); } setFrom(dp[i - 1][j], {i, j}, {i - 1, j}); setFrom(dp[i][j - 1], {i, j}, {i, j - 1}); if (uax(ans, dp[i][j])) { p = {i, j}; } } } if (ans == 0) { cout << ""; return 0; } string lcs; while (p.first != 0 && p.second != 0) { if (s[p.first] == t[p.second]) { lcs += s[p.first]; } p = from[p.first][p.second]; } reverse(lcs.begin(), lcs.end()); cout << lcs << '\n'; #ifdef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif return 0; }
replace
39
40
39
40
0
p03165
C++
Time Limit Exceeded
/* Author: Alam Khan AUST CSE 40th Batch */ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<ll> vl; typedef deque<ll> dl; typedef stack<ll> stl; typedef set<ll> sl; typedef map<string, ll> msl; typedef map<ll, ll> mll; #define sf(n) scanf("%lld", &n) #define sff(n, m) scanf("%lld %lld", &n, &m) #define sfff(n, m, r) scanf("%lld %lld %lld", &n, &m, &r) #define sfs(n) scanf("%s", n) #define pf(n) printf("%lld\n", n) #define pff(n, m) printf("%lld %lld\n", n, m) #define pfff(n, m, r) printf("%lld %lld %lld\n", n, m, r) #define pfs(n) printf("%s\n", n) #define pfcs(i, n) printf("Case %lld: %lld\n", i, n) #define pb push_back #define prf printf #define inf 2e18 #define low -1000000000000 #define PI acos(-1.0) #define rep1(i, n) for (i = 1; i < n; i++) #define rep0(i, n) for (i = 0; i < n; i++) #define rep(i, a, n) for (i = a; i < n; i++) #define repe1(i, n) for (i = 1; i <= n; i++) #define repe0(i, n) for (i = 0; i <= n; i++) #define repe(i, a, n) for (i = a; i <= n; i++) #define endl "\n" #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define fr freopen("input.txt", "r", stdin) #define fw freopen("output.txt", "w", stdout) ll dp[3009][3009]; string a, b; ll LCS(ll i, ll j) { if (i == 0 || j == 0) { return dp[i][j] = 0; } if (a[i - 1] == b[j - 1]) { return dp[i][j] = 1 + LCS(i - 1, j - 1); } else { return dp[i][j] = max(LCS(i - 1, j), LCS(i, j - 1)); } } void path(ll i, ll j) { if (i == 0 || j == 0) return; if (a[i - 1] == b[j - 1]) { path(i - 1, j - 1); cout << a[i - 1]; } else { if (dp[i][j] == dp[i - 1][j]) { path(i - 1, j); } else path(i, j - 1); } } int main() { ll i, n, t, k, j, x = 0, y = 0, m; cin >> a >> b; memset(dp, -1, sizeof dp); n = a.size(); m = b.size(); x = LCS(n, m); path(n, m); return 0; }
/* Author: Alam Khan AUST CSE 40th Batch */ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef vector<ll> vl; typedef deque<ll> dl; typedef stack<ll> stl; typedef set<ll> sl; typedef map<string, ll> msl; typedef map<ll, ll> mll; #define sf(n) scanf("%lld", &n) #define sff(n, m) scanf("%lld %lld", &n, &m) #define sfff(n, m, r) scanf("%lld %lld %lld", &n, &m, &r) #define sfs(n) scanf("%s", n) #define pf(n) printf("%lld\n", n) #define pff(n, m) printf("%lld %lld\n", n, m) #define pfff(n, m, r) printf("%lld %lld %lld\n", n, m, r) #define pfs(n) printf("%s\n", n) #define pfcs(i, n) printf("Case %lld: %lld\n", i, n) #define pb push_back #define prf printf #define inf 2e18 #define low -1000000000000 #define PI acos(-1.0) #define rep1(i, n) for (i = 1; i < n; i++) #define rep0(i, n) for (i = 0; i < n; i++) #define rep(i, a, n) for (i = a; i < n; i++) #define repe1(i, n) for (i = 1; i <= n; i++) #define repe0(i, n) for (i = 0; i <= n; i++) #define repe(i, a, n) for (i = a; i <= n; i++) #define endl "\n" #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define fr freopen("input.txt", "r", stdin) #define fw freopen("output.txt", "w", stdout) ll dp[3009][3009]; string a, b; ll LCS(ll i, ll j) { if (dp[i][j] != -1) { return dp[i][j]; } if (i == 0 || j == 0) { return dp[i][j] = 0; } if (a[i - 1] == b[j - 1]) { return dp[i][j] = 1 + LCS(i - 1, j - 1); } else { return dp[i][j] = max(LCS(i - 1, j), LCS(i, j - 1)); } } void path(ll i, ll j) { if (i == 0 || j == 0) return; if (a[i - 1] == b[j - 1]) { path(i - 1, j - 1); cout << a[i - 1]; } else { if (dp[i][j] == dp[i - 1][j]) { path(i - 1, j); } else path(i, j - 1); } } int main() { ll i, n, t, k, j, x = 0, y = 0, m; cin >> a >> b; memset(dp, -1, sizeof dp); n = a.size(); m = b.size(); x = LCS(n, m); path(n, m); return 0; }
insert
47
47
47
50
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { string s, t; cin >> s >> t; ll a[s.length() + 1][t.length() + 1], i, j; memset(a, 0, sizeof(a)); for (i = 0; i <= s.length(); i++) { for (j = 0; j <= t.length(); j++) { if (i == 0 || j == 0) { a[i][j] = 0; } else { if (s[i - 1] == t[j - 1]) { a[i][j] = a[i - 1][j - 1] + 1; } else { a[i][j] = max(a[i - 1][j], a[i][j - 1]); } } } } string s1 = ""; for (i = s.length(), j = t.length(); i > 0, j > 0;) { if (s[i - 1] == t[j - 1]) { s1 += s[i - 1]; i--, j--; } else { if (a[i - 1][j] == a[i][j]) { i--; } else { j--; } } } reverse(s1.begin(), s1.end()); cout << s1 << endl; // cout<<a[s.length()-1][t.length()-1]<<endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { string s, t; cin >> s >> t; ll a[s.length() + 1][t.length() + 1], i, j; memset(a, 0, sizeof(a)); for (i = 0; i <= s.length(); i++) { for (j = 0; j <= t.length(); j++) { if (i == 0 || j == 0) { a[i][j] = 0; } else { if (s[i - 1] == t[j - 1]) { a[i][j] = a[i - 1][j - 1] + 1; } else { a[i][j] = max(a[i - 1][j], a[i][j - 1]); } } } } string s1 = ""; for (i = s.length(), j = t.length(); i > 0 && j > 0;) { if (s[i - 1] == t[j - 1]) { s1 += s[i - 1]; i--, j--; } else { if (a[i - 1][j] == a[i][j]) { i--; } else { j--; } } } reverse(s1.begin(), s1.end()); cout << s1 << endl; // cout<<a[s.length()-1][t.length()-1]<<endl; }
replace
24
25
24
25
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef pair<int, int> P; string s, t; string dp2[2][3030] = {}; int main() { cin >> s >> t; int n = s.size(), m = t.size(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s.at(i - 1) == t.at(j - 1)) { string tmp = dp2[(i - 1) & 1][j - 1]; tmp.push_back(t.at(j - 1)); dp2[i & 1][j] = tmp; } else { if (dp2[(i - 1) & 1][j].size() > dp2[i & 1][j - 1].size()) { dp2[i & 1][j] = dp2[(i - 1) & 1][j]; } else { dp2[i & 1][j] = dp2[i & 1][j - 1]; } } } } cout << dp2[n & 1][m] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef pair<int, int> P; string s, t; string dp2[2][3030] = {}; int main() { cin >> s >> t; int n = s.size(), m = t.size(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s.at(i - 1) == t.at(j - 1)) { dp2[i & 1][j] = dp2[(i - 1) & 1][j - 1]; dp2[i & 1][j].push_back(t.at(j - 1)); } else { if (dp2[(i - 1) & 1][j].size() > dp2[i & 1][j - 1].size()) { dp2[i & 1][j] = dp2[(i - 1) & 1][j]; } else { dp2[i & 1][j] = dp2[i & 1][j - 1]; } } } } cout << dp2[n & 1][m] << endl; return 0; }
replace
13
16
13
15
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; string s, t; int dp[3010][3010]; int main() { cin >> s >> t; int n = s.length(), m = t.length(); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) dp[i][j] = 0; if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); } } } string ans; int i = n, j = m; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ans.push_back(s[i - 1]); i--, j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } reverse(ans.begin(), ans.end()); cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; string s, t; int dp[3010][3010]; int main() { cin >> s >> t; int n = s.length(), m = t.length(); 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 (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); } } } string ans; int i = n, j = m; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ans.push_back(s[i - 1]); i--, j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } reverse(ans.begin(), ans.end()); cout << ans << '\n'; }
replace
16
17
16
17
0
p03165
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <functional> #include <iostream> #include <set> #include <utility> #include <vector> using namespace std; typedef unsigned long long ull; typedef pair<ull, ull> p; #define mp make_pair #define append push_back vector<ull> v; set<ull> s; multiset<ull> ms; ull prime[10000000]; #define f(i, a, b) for (int i = a; i < (b); i++) #define print cout << void boost() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } vector<ull> visited(10000000, 0); vector<ull> adj[10000]; struct node { ull data; node *left; node *right; }; node *getNode(ull val) { node *newNode = new node(); newNode->data = val; newNode->left = newNode->right = NULL; return newNode; } node *findNode(node *root, ull data) { if (root->data == data) return root; if (root->left) findNode(root->left, data); if (root->right) findNode(root->right, data); } void display(node *root) { // displaying the tree queue<node *> Q; Q.push(root); while (!Q.empty()) { node *x = Q.front(); Q.pop(); if (x->left) Q.push(x->left); if (x->right) Q.push(x->right); cout << x->data << " "; } cout << endl; } node *copy(node *root) { if (!root) return root; node *temp = new node(); temp->data = root->data; temp->left = copy(root->left); temp->right = copy(root->right); return temp; } void mirror(node *root) { if (root) { mirror(root->left); mirror(root->right); node *temp = root->left; root->left = root->right; root->right = temp; } return; } int findMirror(int target, node *left, node *right) { if (left == NULL || right == NULL) return 0; if (left->data == target) return right->data; if (right->data == target) return left->data; int mirrorValue = findMirror(target, left->left, right->right); if (mirrorValue) return mirrorValue; return findMirror(target, left->right, right->left); } ull ncrmodp(ull num, ull r, ull mod) { ull C[r + 1]; ull i, j; memset(C, 0, sizeof(C)); C[0] = 1; for (i = 1; i <= num; i++) { for (j = min(i, r); j > 0; j--) { C[j] = ((C[j]) % mod + (C[j - 1]) % mod) % mod; } } return C[r]; } ull multiply(ull n, ull m) { ull prod = 1; f(i, 0, n) { prod *= v[i]; prod = prod % m; } return prod; } ull gcd(ull a, ull b) { return b == 0 ? a : gcd(b, a % b); } ull power(ull x, ull y, ull p) { ull res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } void dfs(int k) { visited[k] = 1; for (int i = 0; i < adj[k].size(); ++i) { if (visited[adj[k][i]] == 0) { dfs(adj[k][i]); } } } void sieve(ull ending) { for (int i = 2; i < ending; i++) prime[i] = 1; for (int i = 2; i * i <= ending; i++) { if (prime[i] == 1) { for (int j = 2 * i; j <= ending; j = j + i) { prime[j] = 0; } } } prime[1] = 0; } int main() { boost(); ull i, j; ull t, n; string a, b; cin >> a >> b; int dp[1000][1000]; for (i = 0; i <= a.length(); i++) { for (j = 0; j <= b.length(); 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]); } } char ans[100000]; ull ind = dp[a.length()][b.length()]; i = a.length(); j = b.length(); while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { ans[ind - 1] = a[i - 1]; ind--; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } cout << ans << endl; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <functional> #include <iostream> #include <set> #include <utility> #include <vector> using namespace std; typedef unsigned long long ull; typedef pair<ull, ull> p; #define mp make_pair #define append push_back vector<ull> v; set<ull> s; multiset<ull> ms; ull prime[10000000]; #define f(i, a, b) for (int i = a; i < (b); i++) #define print cout << void boost() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } vector<ull> visited(10000000, 0); vector<ull> adj[10000]; struct node { ull data; node *left; node *right; }; node *getNode(ull val) { node *newNode = new node(); newNode->data = val; newNode->left = newNode->right = NULL; return newNode; } node *findNode(node *root, ull data) { if (root->data == data) return root; if (root->left) findNode(root->left, data); if (root->right) findNode(root->right, data); } void display(node *root) { // displaying the tree queue<node *> Q; Q.push(root); while (!Q.empty()) { node *x = Q.front(); Q.pop(); if (x->left) Q.push(x->left); if (x->right) Q.push(x->right); cout << x->data << " "; } cout << endl; } node *copy(node *root) { if (!root) return root; node *temp = new node(); temp->data = root->data; temp->left = copy(root->left); temp->right = copy(root->right); return temp; } void mirror(node *root) { if (root) { mirror(root->left); mirror(root->right); node *temp = root->left; root->left = root->right; root->right = temp; } return; } int findMirror(int target, node *left, node *right) { if (left == NULL || right == NULL) return 0; if (left->data == target) return right->data; if (right->data == target) return left->data; int mirrorValue = findMirror(target, left->left, right->right); if (mirrorValue) return mirrorValue; return findMirror(target, left->right, right->left); } ull ncrmodp(ull num, ull r, ull mod) { ull C[r + 1]; ull i, j; memset(C, 0, sizeof(C)); C[0] = 1; for (i = 1; i <= num; i++) { for (j = min(i, r); j > 0; j--) { C[j] = ((C[j]) % mod + (C[j - 1]) % mod) % mod; } } return C[r]; } ull multiply(ull n, ull m) { ull prod = 1; f(i, 0, n) { prod *= v[i]; prod = prod % m; } return prod; } ull gcd(ull a, ull b) { return b == 0 ? a : gcd(b, a % b); } ull power(ull x, ull y, ull p) { ull res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } void dfs(int k) { visited[k] = 1; for (int i = 0; i < adj[k].size(); ++i) { if (visited[adj[k][i]] == 0) { dfs(adj[k][i]); } } } void sieve(ull ending) { for (int i = 2; i < ending; i++) prime[i] = 1; for (int i = 2; i * i <= ending; i++) { if (prime[i] == 1) { for (int j = 2 * i; j <= ending; j = j + i) { prime[j] = 0; } } } prime[1] = 0; } int main() { boost(); ull i, j; ull t, n; string a, b; cin >> a >> b; int dp[3001][3001]; for (i = 0; i <= a.length(); i++) { for (j = 0; j <= b.length(); 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]); } } char ans[100000]; ull ind = dp[a.length()][b.length()]; i = a.length(); j = b.length(); while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { ans[ind - 1] = a[i - 1]; ind--; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } cout << ans << endl; return 0; }
replace
167
168
167
168
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03165
C++
Runtime Error
// It gets easier day by day, but the hard part is to........ // "I am here." ~ All Might! #pragma optimise GCC(-O2) #include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define pii pair<ll, ll> #define ve(x) vector<x> #define f(a, b, c) for (ll a = b; a < c; a++) #define foto(x, v) for (auto x : v) #define read(t) \ ll t; \ cin >> t; #define reads(t) \ string t; \ cin >> t; #define readarr(arr, n) \ ll arr[n]; \ f(i, 0, n) cin >> arr[i]; #define ln endl #define Endl endl #define dbg(x) cout << #x << " = " << x << ln; #define dbg2(x, y) cout << #x << " = " << x << " & " << #y << " = " << y << ln; #define dbgarr(v, s, e) \ cout << #v << " = "; \ f(i, s, e) cout << v[i] << ", "; \ cout << ln; #define addEdge(tr, k) \ f(i, 0, k) { \ ll x, y; \ cin >> x >> y, tr[x].push_back(y), tr[y].push_back(x); \ } #define all(v) v.begin(), v.end() mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); } // GLOBAL //VARS ll INF = LLONG_MAX; const ll M = 1000000007, M2 = 998244353, M3 = 2013265921; // USEFUL //FUNCTIONS ll powm(ll, ll, ll); ll modI(ll, ll); //------------------------------------------------------DSU // vector<int> parent, rank; // void make_set(int v) { parent[v] = v; rank[v] = 0;} // int find_set(int v) { if (v == parent[v]) return v; return parent[v] = // find_set(parent[v]);} void union_sets(int a, int b) { a = find_set(a); b = // find_set(b); if (a != b) { if (rank[a] < rank[b]) swap(a, b); parent[b] = a; // if (rank[a] == rank[b]) rank[a]++; }} //------------------------------------------------------LCA ** l=ceil(log2(n)) //dfs_lca(root,root) // ll dp[200005][21] ; ve(ll) tin(200005), tout(200005); ll tim = 0 , l; // void dfs_lca(int n , int par ){ tin[n] = ++tim; dp[n][0] = par; f(i,1,l+1) // dp[n][i] = dp[dp[n][i-1]][i-1]; // foto(x,tr[n]) if(x==par) continue; else dfs_lca(x , n); tout[n] = // ++tim; } // bool is_ancestor(ll u , ll v) { return tin[u]<=tin[v] && tout[u]>=tout[v] ;} // ll lca(ll u , ll v){ if(is_ancestor(u,v)) return u ; if(is_ancestor(v,u)) // return v; // for(int j = l ; j>=0 ; j--) if(!is_ancestor(dp[u][j] , v)) u = dp[u][j]; // return dp[u][0];} struct FenwickTree { vector<int> bit; int n; FenwickTree(int n) { this->n = n; bit.assign(n, 0); } int sum(int r) { int ret = 0; for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r]; return ret; } int sum(int l, int r) { return sum(r) - sum(l - 1); } void add(int idx, int delta) { for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta; } }; //-------------------------------------------------NTT modulo m primroot //x=(m,0)-------------------- // ll PrimRoot(ll p, ll x); // finds primitive root of prime p greater than x(If it // doesnt exist, returns 0) void fft(ve(ll) & a, ll n, bool invert, ll m, ll x); void PolyMult(ve(ll) & a, ve(ll) & b, ve(ll) & v, ll m, ll x); ll dp[3005][3005]; // MAIN int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); reads(s) reads(t) f(i, 0, 3005) f(j, 0, 3005) dp[i][j] = 0; f(i, 0, s.length()) { f(j, 0, t.length()) { if (s[i] == t[j]) dp[i + 1][j + 1] = 1 + dp[i][j]; else dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); } } string e = ""; if (dp[s.length()][t.length()] == 0) return cout << e, 0; int i = s.length(), j = t.length(); while (i or j) { // dbg2(i,j) // dbg2(dp[i-1][j],dp[i][j-1]) if (i >= 0 and j >= 0 and s[i - 1] == t[j - 1]) { e += s[i - 1]; i--, j--; continue; } if (i >= 1 and dp[i][j - 1] >= dp[i - 1][j]) j--; else if (i == 0) j--; else if (j >= 1 and dp[i][j - 1] <= dp[i - 1][j]) i--; else i--; } reverse(all(e)); cout << e << endl; } // --------------- Oooo I wont let you go -------------- // lcs(i , j) = (s[i]==t[j]) ? lcs(i-1,j-1) + 1 : max(lcs(i-1,j),lcs(i,j-1)) ; // FUNCTIONS //DECLARATIONS ll powm(ll a, ll b, ll c = M) { ll res = 1; while (b) { if (b & 1) res = (res * a) % c; a = (a * a) % c; b >>= 1; } return res; } ll modI(ll a, ll m = M) { ll m0 = m, y = 0, x = 1; if (m == 1) return 0; while (a > 1) { ll q = a / m; ll t = m; m = a % m; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } ll PrimRoot(ll p, ll x) // finds primitive root of prime p greater than x(If it // doesnt exist, returns 0) { ve(ll) v; ll t = p - 1; for (int i = 2; i * i <= t; i++) if (t % i == 0) { v.push_back((p - 1) / i); while (t % i == 0) t /= i; } if (t > 1) v.push_back((p - 1) / t); f(i, x + 1, p) { ll flag = 0; foto(x, v) if (powm(i, x, p) == 1) { flag = 1; break; } if (flag == 0) return i; } return 0; } void fft(ve(ll) & a, ll n, bool invert, ll m, ll x) { ll lg_n = __builtin_ctzll(n); for (ll i = 0, y = 0; i < n; i++) { f(j, 0, lg_n) if ((1LL << j) & i) y |= (1LL << (lg_n - j - 1)); if (y > i) swap(a[i], a[y]); } if (invert) x = modI(x, m); for (ll s = 2; s <= n; s <<= 1) { ll y = powm(x, (n / s), m); f(j, 0, (n / s)) { ll r = 1; f(i, 0, s / 2) { ll u = a[i + j * s], v = (r * a[i + j * s + (s / 2)]) % m; a[i + j * s] = (u + v) % m; a[i + j * s + (s / 2)] = (u - v + m) % m; r = r * y % m; } } } if (invert) { ll invn = modI(n, m); f(i, 0, n) a[i] = (a[i] * invn) % m; } return; } void PolyMult(ve(ll) & a, ve(ll) & b, ve(ll) & v, ll m, ll x) // pass x = 1 for xor conv { ll n = 1; while (n < ((ll)a.size()) + ((ll)b.size())) n <<= 1; ve(ll) fa(a.begin(), a.end()); fa.resize(n, 0); ve(ll) fb(b.begin(), b.end()); fb.resize(n, 0); ll y = powm(x, (m - 1) / n, m); fft(fa, n, false, m, y); fft(fb, n, false, m, y); v.resize(n, 0); f(i, 0, n) v[i] = ((fa[i] * fb[i]) % m); fft(v, n, true, m, y); return; }
// It gets easier day by day, but the hard part is to........ // "I am here." ~ All Might! #pragma optimise GCC(-O2) #include <bits/stdc++.h> using namespace std; #define int long long #define ll long long #define ld long double #define pii pair<ll, ll> #define ve(x) vector<x> #define f(a, b, c) for (ll a = b; a < c; a++) #define foto(x, v) for (auto x : v) #define read(t) \ ll t; \ cin >> t; #define reads(t) \ string t; \ cin >> t; #define readarr(arr, n) \ ll arr[n]; \ f(i, 0, n) cin >> arr[i]; #define ln endl #define Endl endl #define dbg(x) cout << #x << " = " << x << ln; #define dbg2(x, y) cout << #x << " = " << x << " & " << #y << " = " << y << ln; #define dbgarr(v, s, e) \ cout << #v << " = "; \ f(i, s, e) cout << v[i] << ", "; \ cout << ln; #define addEdge(tr, k) \ f(i, 0, k) { \ ll x, y; \ cin >> x >> y, tr[x].push_back(y), tr[y].push_back(x); \ } #define all(v) v.begin(), v.end() mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); } // GLOBAL //VARS ll INF = LLONG_MAX; const ll M = 1000000007, M2 = 998244353, M3 = 2013265921; // USEFUL //FUNCTIONS ll powm(ll, ll, ll); ll modI(ll, ll); //------------------------------------------------------DSU // vector<int> parent, rank; // void make_set(int v) { parent[v] = v; rank[v] = 0;} // int find_set(int v) { if (v == parent[v]) return v; return parent[v] = // find_set(parent[v]);} void union_sets(int a, int b) { a = find_set(a); b = // find_set(b); if (a != b) { if (rank[a] < rank[b]) swap(a, b); parent[b] = a; // if (rank[a] == rank[b]) rank[a]++; }} //------------------------------------------------------LCA ** l=ceil(log2(n)) //dfs_lca(root,root) // ll dp[200005][21] ; ve(ll) tin(200005), tout(200005); ll tim = 0 , l; // void dfs_lca(int n , int par ){ tin[n] = ++tim; dp[n][0] = par; f(i,1,l+1) // dp[n][i] = dp[dp[n][i-1]][i-1]; // foto(x,tr[n]) if(x==par) continue; else dfs_lca(x , n); tout[n] = // ++tim; } // bool is_ancestor(ll u , ll v) { return tin[u]<=tin[v] && tout[u]>=tout[v] ;} // ll lca(ll u , ll v){ if(is_ancestor(u,v)) return u ; if(is_ancestor(v,u)) // return v; // for(int j = l ; j>=0 ; j--) if(!is_ancestor(dp[u][j] , v)) u = dp[u][j]; // return dp[u][0];} struct FenwickTree { vector<int> bit; int n; FenwickTree(int n) { this->n = n; bit.assign(n, 0); } int sum(int r) { int ret = 0; for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r]; return ret; } int sum(int l, int r) { return sum(r) - sum(l - 1); } void add(int idx, int delta) { for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta; } }; //-------------------------------------------------NTT modulo m primroot //x=(m,0)-------------------- // ll PrimRoot(ll p, ll x); // finds primitive root of prime p greater than x(If it // doesnt exist, returns 0) void fft(ve(ll) & a, ll n, bool invert, ll m, ll x); void PolyMult(ve(ll) & a, ve(ll) & b, ve(ll) & v, ll m, ll x); ll dp[3005][3005]; // MAIN int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); reads(s) reads(t) f(i, 0, 3005) f(j, 0, 3005) dp[i][j] = 0; f(i, 0, s.length()) { f(j, 0, t.length()) { if (s[i] == t[j]) dp[i + 1][j + 1] = 1 + dp[i][j]; else dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); } } string e = ""; if (dp[s.length()][t.length()] == 0) return cout << e, 0; int i = s.length(), j = t.length(); while (i and j) { // dbg2(i,j) // dbg2(dp[i-1][j],dp[i][j-1]) if (i >= 0 and j >= 0 and s[i - 1] == t[j - 1]) { e += s[i - 1]; i--, j--; continue; } if (i >= 1 and dp[i][j - 1] >= dp[i - 1][j]) j--; else if (i == 0) j--; else if (j >= 1 and dp[i][j - 1] <= dp[i - 1][j]) i--; else i--; } reverse(all(e)); cout << e << endl; } // --------------- Oooo I wont let you go -------------- // lcs(i , j) = (s[i]==t[j]) ? lcs(i-1,j-1) + 1 : max(lcs(i-1,j),lcs(i,j-1)) ; // FUNCTIONS //DECLARATIONS ll powm(ll a, ll b, ll c = M) { ll res = 1; while (b) { if (b & 1) res = (res * a) % c; a = (a * a) % c; b >>= 1; } return res; } ll modI(ll a, ll m = M) { ll m0 = m, y = 0, x = 1; if (m == 1) return 0; while (a > 1) { ll q = a / m; ll t = m; m = a % m; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } ll PrimRoot(ll p, ll x) // finds primitive root of prime p greater than x(If it // doesnt exist, returns 0) { ve(ll) v; ll t = p - 1; for (int i = 2; i * i <= t; i++) if (t % i == 0) { v.push_back((p - 1) / i); while (t % i == 0) t /= i; } if (t > 1) v.push_back((p - 1) / t); f(i, x + 1, p) { ll flag = 0; foto(x, v) if (powm(i, x, p) == 1) { flag = 1; break; } if (flag == 0) return i; } return 0; } void fft(ve(ll) & a, ll n, bool invert, ll m, ll x) { ll lg_n = __builtin_ctzll(n); for (ll i = 0, y = 0; i < n; i++) { f(j, 0, lg_n) if ((1LL << j) & i) y |= (1LL << (lg_n - j - 1)); if (y > i) swap(a[i], a[y]); } if (invert) x = modI(x, m); for (ll s = 2; s <= n; s <<= 1) { ll y = powm(x, (n / s), m); f(j, 0, (n / s)) { ll r = 1; f(i, 0, s / 2) { ll u = a[i + j * s], v = (r * a[i + j * s + (s / 2)]) % m; a[i + j * s] = (u + v) % m; a[i + j * s + (s / 2)] = (u - v + m) % m; r = r * y % m; } } } if (invert) { ll invn = modI(n, m); f(i, 0, n) a[i] = (a[i] * invn) % m; } return; } void PolyMult(ve(ll) & a, ve(ll) & b, ve(ll) & v, ll m, ll x) // pass x = 1 for xor conv { ll n = 1; while (n < ((ll)a.size()) + ((ll)b.size())) n <<= 1; ve(ll) fa(a.begin(), a.end()); fa.resize(n, 0); ve(ll) fb(b.begin(), b.end()); fb.resize(n, 0); ll y = powm(x, (m - 1) / n, m); fft(fa, n, false, m, y); fft(fb, n, false, m, y); v.resize(n, 0); f(i, 0, n) v[i] = ((fa[i] * fb[i]) % m); fft(v, n, true, m, y); return; }
replace
121
122
121
122
0
p03165
C++
Runtime Error
// #include"stdc++.h" #include <bits/stdc++.h> using namespace std; #define ll long long int #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define cases \ ll t; \ cin >> t; \ while (t--) #define f1(a, n) for (int i = a; i < n; i++) #define f2(b, n) for (int j = b; j < n; j++) #define INF 1000000000000009 #define pb push_back ll ar[100005]; #define MAX 1000005 int main() { fast string s1, s2; ll dp[1005][1005]; cin >> s1 >> s2; memset(dp, 0, sizeof(dp)); ll ans = 0; f1(1, s1.length() + 1) { f2(1, s2.length() + 1) { dp[i][j] = max(dp[i - 1][j - 1], max(dp[i - 1][j], dp[i][j - 1])); if (s1[i - 1] == s2[j - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); } } // cout<<dp[s1.size()][s2.size()]<<endl; string t; int i = s1.size(), j = s2.size(); while (dp[i][j]) { if (dp[i][j] == dp[i - 1][j]) i--; else if (dp[i][j] == dp[i][j - 1]) j--; else if (dp[i][j] > dp[i - 1][j - 1]) i--, j--, t += s1[i]; } reverse(t.begin(), t.end()); cout << t; return 0; }
// #include"stdc++.h" #include <bits/stdc++.h> using namespace std; #define ll long long int #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define cases \ ll t; \ cin >> t; \ while (t--) #define f1(a, n) for (int i = a; i < n; i++) #define f2(b, n) for (int j = b; j < n; j++) #define INF 1000000000000009 #define pb push_back ll ar[100005]; #define MAX 1000005 int main() { fast string s1, s2; ll dp[3005][3005]; cin >> s1 >> s2; memset(dp, 0, sizeof(dp)); ll ans = 0; f1(1, s1.length() + 1) { f2(1, s2.length() + 1) { dp[i][j] = max(dp[i - 1][j - 1], max(dp[i - 1][j], dp[i][j - 1])); if (s1[i - 1] == s2[j - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); } } // cout<<dp[s1.size()][s2.size()]<<endl; string t; int i = s1.size(), j = s2.size(); while (dp[i][j]) { if (dp[i][j] == dp[i - 1][j]) i--; else if (dp[i][j] == dp[i][j - 1]) j--; else if (dp[i][j] > dp[i - 1][j - 1]) i--, j--, t += s1[i]; } reverse(t.begin(), t.end()); cout << t; return 0; }
replace
20
21
20
21
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int #define INF 1000000000000000000 #define fn(i, n) for (int i = 0; i < n; i += 1) int w[100100], v[100100]; int dp[1001][1001] = {0}; signed main() { string s, t; cin >> s >> t; int m = s.size(); int n = t.size(); fn(i, m + 1) { fn(j, n + 1) { if (i == 0 || j == 0) dp[i][j] = 0; else 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 = m, j = n; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ans += s[i - 1]; i -= 1; j -= 1; } else if (dp[i - 1][j] > dp[i][j - 1]) i -= 1; else j -= 1; } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define INF 1000000000000000000 #define fn(i, n) for (int i = 0; i < n; i += 1) int dp[3001][3001] = {0}; signed main() { string s, t; cin >> s >> t; int m = s.size(); int n = t.size(); fn(i, m + 1) { fn(j, n + 1) { if (i == 0 || j == 0) dp[i][j] = 0; else 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 = m, j = n; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ans += s[i - 1]; i -= 1; j -= 1; } else if (dp[i - 1][j] > dp[i][j - 1]) i -= 1; else j -= 1; } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
replace
5
7
5
7
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int #define ull unsigned long long int #define Endl endl #define mod 998244353 #define mkp make_pair #define pb push_back #define cin(n) scanf("%d", &n) #define cout(n) printf("%d ", n) #define sf(n) scanf("%d", &n) #define pf(n) printf("%d ", n) #define bitget(n) __builtin_popcountll(n) ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; ll dy[] = {0, -1, -1, -1, 0, 1, 1, 1}; ll dxx[] = {1, -1, 0, 0}; ll dyy[] = {0, 0, -1, 1}; ll modexpo(ll x, ll y) { if (y == 0) return 1; if (y % 2) { ll viky = modexpo(x, y / 2); return (((x * viky) % mod) * viky) % mod; } else { ll viky = modexpo(x, y / 2); return (viky * viky) % mod; } } ll intpoint(ll x1, ll y1, ll x2, ll y2) { return (__gcd(abs(x1 - x2), abs(y1 - y2)) + 1); } bool isvalid(ll x, ll y, ll n, ll m) { return (x >= 0 && x < n && y >= 0 && y < m); } // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // int pp = uniform_int_distribution<int>(0, kk-1)(rng); // ---------------------///////////////--------------------------------- // ---------------------///////////////---------------------------------- // -------------/////////////////---------------------------------/////// ll dp[311][311]; int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll t, i, j, l, r, q, k, kk, m, n, p; string s, sp; cin >> s >> sp; n = s.size(); m = sp.size(); // ll dp[n+11][m+11]; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (s[i - 1] == sp[j - 1]) { dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i - 1][j]); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } string ans = ""; i = n; j = m; while (j != 0 && i != 0) { if (dp[i][j] == dp[i - 1][j]) { i--; } else if (dp[i][j] == dp[i][j - 1]) { j--; } else { i--; j--; ans.push_back(s[i]); } } reverse(ans.begin(), ans.end()); cout << ans; // for(i=n;i>=1;i--){ // for(j=m;j>=1;j--){ // if(dp[i][j] == dp[i-1][j]){ // } // } // } // cout<<dp[n][m]; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define ull unsigned long long int #define Endl endl #define mod 998244353 #define mkp make_pair #define pb push_back #define cin(n) scanf("%d", &n) #define cout(n) printf("%d ", n) #define sf(n) scanf("%d", &n) #define pf(n) printf("%d ", n) #define bitget(n) __builtin_popcountll(n) ll dx[] = {1, 1, 0, -1, -1, -1, 0, 1}; ll dy[] = {0, -1, -1, -1, 0, 1, 1, 1}; ll dxx[] = {1, -1, 0, 0}; ll dyy[] = {0, 0, -1, 1}; ll modexpo(ll x, ll y) { if (y == 0) return 1; if (y % 2) { ll viky = modexpo(x, y / 2); return (((x * viky) % mod) * viky) % mod; } else { ll viky = modexpo(x, y / 2); return (viky * viky) % mod; } } ll intpoint(ll x1, ll y1, ll x2, ll y2) { return (__gcd(abs(x1 - x2), abs(y1 - y2)) + 1); } bool isvalid(ll x, ll y, ll n, ll m) { return (x >= 0 && x < n && y >= 0 && y < m); } // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // int pp = uniform_int_distribution<int>(0, kk-1)(rng); // ---------------------///////////////--------------------------------- // ---------------------///////////////---------------------------------- // -------------/////////////////---------------------------------/////// ll dp[3011][3011]; int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll t, i, j, l, r, q, k, kk, m, n, p; string s, sp; cin >> s >> sp; n = s.size(); m = sp.size(); // ll dp[n+11][m+11]; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (s[i - 1] == sp[j - 1]) { dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i - 1][j]); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } string ans = ""; i = n; j = m; while (j != 0 && i != 0) { if (dp[i][j] == dp[i - 1][j]) { i--; } else if (dp[i][j] == dp[i][j - 1]) { j--; } else { i--; j--; ans.push_back(s[i]); } } reverse(ans.begin(), ans.end()); cout << ans; // for(i=n;i>=1;i--){ // for(j=m;j>=1;j--){ // if(dp[i][j] == dp[i-1][j]){ // } // } // } // cout<<dp[n][m]; return 0; }
replace
40
41
40
41
0
p03165
C++
Runtime Error
#include <iostream> using namespace std; string ar[2][3050] = {}; string a, b; string bg(string l, string r) { if (l.size() > r.size()) { return l; } else { return r; } } int main() { cin >> a >> b; for (int i = 1; i <= a.size(); i++) { for (int j = 1; j <= b.size(); j++) { ar[i % 2][j] = bg(ar[(i - 1) % 2][j], ar[i % 2][j - 1]); if (a[i - 1] == b[j - 1]) { ar[i % 2][j] = bg(ar[i % 2][j], ar[(i - 1) % 2][j - 1] + a[i - 1]); } } } cout << ar[a.size()][b.size()] << "\n"; return 0; }
#include <iostream> using namespace std; string ar[2][3050] = {}; string a, b; string bg(string l, string r) { if (l.size() > r.size()) { return l; } else { return r; } } int main() { cin >> a >> b; for (int i = 1; i <= a.size(); i++) { for (int j = 1; j <= b.size(); j++) { ar[i % 2][j] = bg(ar[(i - 1) % 2][j], ar[i % 2][j - 1]); if (a[i - 1] == b[j - 1]) { ar[i % 2][j] = bg(ar[i % 2][j], ar[(i - 1) % 2][j - 1] + a[i - 1]); } } } cout << ar[a.size() % 2][b.size()] << "\n"; return 0; }
replace
21
22
21
22
-11
p03165
C++
Runtime Error
#include <iostream> using namespace std; template <class T> bool max(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> int maxReturn(T a, T b) { if (a > b) { return a; } return b; } template <class T> bool min(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main(void) { string s, t; cin >> s; cin >> t; int slength = s.length(); int tlength = t.length(); int dp[1000][1000]; for (int i = 1; i <= slength; i++) { for (int a = 1; a <= tlength; a++) { if (s[i - 1] == t[a - 1]) { dp[i][a] = dp[i - 1][a - 1] + 1; } else { dp[i][a] = maxReturn(dp[i - 1][a], dp[i][a - 1]); } } } int i = slength; int j = tlength; string res = ""; while (i > 0 && j > 0) { if (dp[i][j] == dp[i][j - 1]) { j--; } else if (dp[i][j] == dp[i - 1][j]) { i--; } else { res = s[i - 1] + res; i--; j--; } } cout << res << endl; }
#include <iostream> using namespace std; template <class T> bool max(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> int maxReturn(T a, T b) { if (a > b) { return a; } return b; } template <class T> bool min(T &a, T b) { if (a < b) { a = b; return true; } return false; } int main(void) { string s, t; cin >> s; cin >> t; int slength = s.length(); int tlength = t.length(); int dp[3100][3100] = {0}; for (int i = 1; i <= slength; i++) { for (int a = 1; a <= tlength; a++) { if (s[i - 1] == t[a - 1]) { dp[i][a] = dp[i - 1][a - 1] + 1; } else { dp[i][a] = maxReturn(dp[i - 1][a], dp[i][a - 1]); } } } int i = slength; int j = tlength; string res = ""; while (i > 0 && j > 0) { if (dp[i][j] == dp[i][j - 1]) { j--; } else if (dp[i][j] == dp[i - 1][j]) { i--; } else { res = s[i - 1] + res; i--; j--; } } cout << res << endl; }
replace
31
32
31
32
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; #define mod 1000000007 #define F first #define S second using namespace std; string a, b; ll memo[1005][1005]; ll solve(int i, int j) { if (i == a.size() || j == b.size()) return 0; if (~memo[i][j]) return memo[i][j]; ll ret = 0; if (a[i] == b[j]) ret = max(ret, solve(i + 1, j + 1) + 1); ret = max(ret, solve(i + 1, j)); ret = max(ret, solve(i, j + 1)); memo[i][j] = ret; return ret; } void print(int i, int j) { if (i == a.size() || j == b.size()) exit(0); ll ret = memo[i][j]; if (a[i] == b[j]) { cout << a[i]; print(i + 1, j + 1); } if (solve(i + 1, j) == ret) print(i + 1, j); print(i, j + 1); } int main() { cin >> a >> b; memset(memo, -1, sizeof memo); solve(0, 0); print(0, 0); return 0; }
#include <bits/stdc++.h> typedef long long ll; #define mod 1000000007 #define F first #define S second using namespace std; string a, b; ll memo[3005][3005]; ll solve(int i, int j) { if (i == a.size() || j == b.size()) return 0; if (~memo[i][j]) return memo[i][j]; ll ret = 0; if (a[i] == b[j]) ret = max(ret, solve(i + 1, j + 1) + 1); ret = max(ret, solve(i + 1, j)); ret = max(ret, solve(i, j + 1)); memo[i][j] = ret; return ret; } void print(int i, int j) { if (i == a.size() || j == b.size()) exit(0); ll ret = memo[i][j]; if (a[i] == b[j]) { cout << a[i]; print(i + 1, j + 1); } if (solve(i + 1, j) == ret) print(i + 1, j); print(i, j + 1); } int main() { cin >> a >> b; memset(memo, -1, sizeof memo); solve(0, 0); print(0, 0); return 0; }
replace
7
8
7
8
0
p03165
C++
Runtime Error
// g++ -std=gnu++14 a.cpp // #include <bits/stdc++.h> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) ll MOD = 1e9 + 7; int INF = 1 << 30; ll INFL = 1LL << 60; int main() { string S, T; cin >> S >> T; int long_s = S.size(); int long_t = T.size(); vector<vector<ll>> dp(3010, vector<ll>(3010, 0)); for (int i = 1; i <= long_s; i++) { for (int j = 1; j <= long_t; j++) { if (S.at(i - 1) == T.at(j - 1)) { dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]); } dp[i][j] = max(dp[i][j], dp[i - 1][j]); dp[i][j] = max(dp[i][j], dp[i][j - 1]); } } /* string ans = ""; for(int i = 1;i <= long_s;i++){ for(int j = 1;j <= long_t;j++){ if(dp[i-1][j-1]+1 == dp[i][j] && dp[i][j] == dp[i-1][j]+1 && dp[i][j] == dp[i][j-1]+1){ ans += S.at(i-1); } } } */ int i = long_s; int j = long_t; string ans = ""; while (i >= 0 && j >= 0) { if (dp[i][j] == dp[i][j - 1]) { j--; } else if (dp[i][j] == dp[i - 1][j]) { i--; } else { ans = ans + S.at(i - 1); i--; j--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
// g++ -std=gnu++14 a.cpp // #include <bits/stdc++.h> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; #define ll long long #define rep(i, n) for (ll i = 0; i < (n); i++) ll MOD = 1e9 + 7; int INF = 1 << 30; ll INFL = 1LL << 60; int main() { string S, T; cin >> S >> T; int long_s = S.size(); int long_t = T.size(); vector<vector<ll>> dp(3010, vector<ll>(3010, 0)); for (int i = 1; i <= long_s; i++) { for (int j = 1; j <= long_t; j++) { if (S.at(i - 1) == T.at(j - 1)) { dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]); } dp[i][j] = max(dp[i][j], dp[i - 1][j]); dp[i][j] = max(dp[i][j], dp[i][j - 1]); } } /* string ans = ""; for(int i = 1;i <= long_s;i++){ for(int j = 1;j <= long_t;j++){ if(dp[i-1][j-1]+1 == dp[i][j] && dp[i][j] == dp[i-1][j]+1 && dp[i][j] == dp[i][j-1]+1){ ans += S.at(i-1); } } } */ int i = long_s; int j = long_t; string ans = ""; while (i > 0 && j > 0) { if (dp[i][j] == dp[i][j - 1]) { j--; } else if (dp[i][j] == dp[i - 1][j]) { i--; } else { ans = ans + S.at(i - 1); i--; j--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
58
59
58
59
-11
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; string s, t; ll dp[3007][3007]; int main() { cin >> s >> t; s += ';'; t += '.'; for (ll i = 0; i < s.length(); ++i) { for (ll j = 0; j < t.length(); ++j) { if (s[i] == t[j]) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1); dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]); } } ll i = s.length() - 1, j = t.length() - 1; string ans = ""; while (i + j) { ll cur = dp[i][j]; if (dp[i - 1][j] == cur) i--; else if (dp[i][j - 1] == cur) j--; else { i--; j--; ans += s[i]; } } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; string s, t; ll dp[3007][3007]; int main() { cin >> s >> t; s += ';'; t += '.'; for (ll i = 0; i < s.length(); ++i) { for (ll j = 0; j < t.length(); ++j) { if (s[i] == t[j]) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1); dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); dp[i][j + 1] = max(dp[i][j], dp[i][j + 1]); } } ll i = s.length() - 1, j = t.length() - 1; string ans = ""; while (i && j) { ll cur = dp[i][j]; if (dp[i - 1][j] == cur) i--; else if (dp[i][j - 1] == cur) j--; else { i--; j--; ans += s[i]; } } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
replace
24
25
24
25
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int dir[1001][1001]; int dp[1001][1001]; string s1, s2; int solve(int i, int j) { if (dp[i][j] != -1) return dp[i][j]; if (i == s1.size() || j == s2.size()) { dir[i][j] = -1; return dp[i][j] = 0; } int anss = 0; int dik = -1; if (s1[i] == s2[j]) { int ans = 1 + solve(i + 1, j + 1); if (anss < ans) { dik = 0; anss = ans; } } int ans = solve(i, j + 1); if (ans > anss) { dik = 1; anss = ans; } ans = solve(i + 1, j); if (ans > anss) { dik = 2; anss = ans; } dp[i][j] = anss; dir[i][j] = dik; return dp[i][j]; } string s; void backtrack(int i, int j) { if (i == s1.size() || j == s2.size()) { return; } if (dir[i][j] == 0) { s.push_back(s1[i]); backtrack(i + 1, j + 1); } else if (dir[i][j] == 1) { backtrack(i, j + 1); } else if (dir[i][j] == 2) { backtrack(i + 1, j); } } int main() { memset(dp, -1, sizeof dp); cin >> s1 >> s2; int ans = solve(0, 0); // cout<<ans<<endl; backtrack(0, 0); cout << s << endl; }
#include <bits/stdc++.h> using namespace std; int dir[3001][3001]; int dp[3001][3001]; string s1, s2; int solve(int i, int j) { if (dp[i][j] != -1) return dp[i][j]; if (i == s1.size() || j == s2.size()) { dir[i][j] = -1; return dp[i][j] = 0; } int anss = 0; int dik = -1; if (s1[i] == s2[j]) { int ans = 1 + solve(i + 1, j + 1); if (anss < ans) { dik = 0; anss = ans; } } int ans = solve(i, j + 1); if (ans > anss) { dik = 1; anss = ans; } ans = solve(i + 1, j); if (ans > anss) { dik = 2; anss = ans; } dp[i][j] = anss; dir[i][j] = dik; return dp[i][j]; } string s; void backtrack(int i, int j) { if (i == s1.size() || j == s2.size()) { return; } if (dir[i][j] == 0) { s.push_back(s1[i]); backtrack(i + 1, j + 1); } else if (dir[i][j] == 1) { backtrack(i, j + 1); } else if (dir[i][j] == 2) { backtrack(i + 1, j); } } int main() { memset(dp, -1, sizeof dp); cin >> s1 >> s2; int ans = solve(0, 0); // cout<<ans<<endl; backtrack(0, 0); cout << s << endl; }
replace
2
4
2
4
0
p03165
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> using namespace std; #define float double const int LMT = 1000000000 + 5; #define int long long #define lp(i, n) for (i = 0; i < n; i++) const int N = 1005, M = 1005; int n, m, w[N], v[N], i, j; string a, b; int dp[N][M]; bool vis[N][M]; void solve(int n, int m) { for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { if (a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> a >> b; a = '@' + a; b = '@' + b; memset(dp, 0, sizeof(dp)); solve(n = a.length(), m = b.length()); // cout<<dp[n-1][m-1]<<endl; i = n - 1; j = m - 1; string ans = ""; /*for(int i=0;i<n;i++){cout<<endl; for(int j=0;j<m;j++) cout<<dp[i][j]<<" "; }*/ while (i > 0 && j > 0) { // cout<<ans<<endl; // cout<<i<<" "<<j<<" "; int curr = dp[i][j], ca = dp[i - 1][j - 1], cl = dp[i][j - 1], cu = dp[i - 1][j]; int mx = max(max(cl, cu), ca); // cout<<"mx="<<mx<<endl; if (mx == ca) { if (mx < dp[i][j]) ans += a[i]; i--; j--; continue; } if (mx == cl) { if (mx < dp[i][j]) ans += a[i]; j--; continue; } else { if (mx < dp[i][j]) ans += a[i]; i--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
#include <algorithm> #include <bits/stdc++.h> using namespace std; #define float double const int LMT = 1000000000 + 5; #define int long long #define lp(i, n) for (i = 0; i < n; i++) const int N = 3005, M = 3005; int n, m, w[N], v[N], i, j; string a, b; int dp[N][M]; bool vis[N][M]; void solve(int n, int m) { for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { if (a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cin >> a >> b; a = '@' + a; b = '@' + b; memset(dp, 0, sizeof(dp)); solve(n = a.length(), m = b.length()); // cout<<dp[n-1][m-1]<<endl; i = n - 1; j = m - 1; string ans = ""; /*for(int i=0;i<n;i++){cout<<endl; for(int j=0;j<m;j++) cout<<dp[i][j]<<" "; }*/ while (i > 0 && j > 0) { // cout<<ans<<endl; // cout<<i<<" "<<j<<" "; int curr = dp[i][j], ca = dp[i - 1][j - 1], cl = dp[i][j - 1], cu = dp[i - 1][j]; int mx = max(max(cl, cu), ca); // cout<<"mx="<<mx<<endl; if (mx == ca) { if (mx < dp[i][j]) ans += a[i]; i--; j--; continue; } if (mx == cl) { if (mx < dp[i][j]) ans += a[i]; j--; continue; } else { if (mx < dp[i][j]) ans += a[i]; i--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
7
8
7
8
0
p03165
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; const int maxn = 3005; int dp[maxn][maxn]; pair<int, int> dir[maxn][maxn]; string a, b; int main(int argc, char const *argv[]) { // freopen("in.txt", "r", stdin); cin >> a >> b; int n = a.size(), m = b.size(); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; dir[i][j] = make_pair(i, j); } else if (a[i - 1] == b[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; dir[i][j] = make_pair(i - 1, j - 1); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (dp[i][j] == dp[i - 1][j]) { dir[i][j] = make_pair(i - 1, j); } else { dir[i][j] = make_pair(i, j - 1); } } } } string ans; pair<int, int> cur = make_pair(n, m); while (cur.first != 0 || cur.second != 0) { int i = cur.first, j = cur.second; if (dir[i][j] == make_pair(i - 1, j - 1)) { ans += a[i - 1]; cur = make_pair(i - 1, j - 1); } else if (dir[i][j] == make_pair(i - 1, j)) { cur = make_pair(i - 1, j); } else { cur = make_pair(i, j - 1); } } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
#include "bits/stdc++.h" using namespace std; const int maxn = 3005; int dp[maxn][maxn]; pair<int, int> dir[maxn][maxn]; string a, b; int main(int argc, char const *argv[]) { // freopen("in.txt", "r", stdin); cin >> a >> b; int n = a.size(), m = b.size(); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; dir[i][j] = make_pair(i, j); } else if (a[i - 1] == b[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; dir[i][j] = make_pair(i - 1, j - 1); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (dp[i][j] == dp[i - 1][j]) { dir[i][j] = make_pair(i - 1, j); } else { dir[i][j] = make_pair(i, j - 1); } } } } string ans; pair<int, int> cur = make_pair(n, m); while (cur.first != 0 && cur.second != 0) { int i = cur.first, j = cur.second; if (dir[i][j] == make_pair(i - 1, j - 1)) { ans += a[i - 1]; cur = make_pair(i - 1, j - 1); } else if (dir[i][j] == make_pair(i - 1, j)) { cur = make_pair(i - 1, j); } else { cur = make_pair(i, j - 1); } } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
replace
32
33
32
33
-11
p03165
C++
Runtime Error
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define all(v) v.begin(), v.end() using in = int64_t; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++) const double PI = 3.14159265358979323846; const in MOD = 1000000007; const in INF = 1 << 30; #define vec2(a, y, x) vector<vector<int>> a(y, vector<int>(x, 0)) #define vec1(a, n) \ vector<int> a(n); \ rep(i, n) cin >> a[i] using P = pair<int, int>; vector<int> dx = {0, 1, -1, 0}; vector<int> dy = {1, 0, 0, -1}; bool chmax(int &a, int b) { if (a < b) { a = b; return 0; } return 1; } bool chmin(int &a, int b) { if (a > b) { a = b; return 0; } return 1; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s, t; cin >> s >> t; int xs = s.size(), xt = t.size(); vec2(dp, xs + 1, xt + 1); // dp[i][j]:=sのi,tのjまでの共通部分列の長さ rep(i, xs + 1) rep(j, xt + 1) { if (i > 0) chmax(dp[i][j], dp[i - 1][j]); if (j > 0) chmax(dp[i][j], dp[i][j - 1]); if (i > 0 && j > 0 && s[i - 1] == t[j - 1]) chmax(dp[i][j], dp[i - 1][j - 1] + 1); } cout << dp[4][5] << endl; string ans = ""; int cnt = dp[xs][xt]; int i = xs, j = xt; while (cnt > 0) { if (dp[i][j] == dp[i][j - 1]) j--; else if (dp[i][j] == dp[i - 1][j]) i--; else { i--; ans = s[i] + ans; j--; cnt--; } } cout << ans << '\n'; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define all(v) v.begin(), v.end() using in = int64_t; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++) const double PI = 3.14159265358979323846; const in MOD = 1000000007; const in INF = 1 << 30; #define vec2(a, y, x) vector<vector<int>> a(y, vector<int>(x, 0)) #define vec1(a, n) \ vector<int> a(n); \ rep(i, n) cin >> a[i] using P = pair<int, int>; vector<int> dx = {0, 1, -1, 0}; vector<int> dy = {1, 0, 0, -1}; bool chmax(int &a, int b) { if (a < b) { a = b; return 0; } return 1; } bool chmin(int &a, int b) { if (a > b) { a = b; return 0; } return 1; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s, t; cin >> s >> t; int xs = s.size(), xt = t.size(); vec2(dp, xs + 1, xt + 1); // dp[i][j]:=sのi,tのjまでの共通部分列の長さ rep(i, xs + 1) rep(j, xt + 1) { if (i > 0) chmax(dp[i][j], dp[i - 1][j]); if (j > 0) chmax(dp[i][j], dp[i][j - 1]); if (i > 0 && j > 0 && s[i - 1] == t[j - 1]) chmax(dp[i][j], dp[i - 1][j - 1] + 1); } // cout<<dp[4][5]<<endl; string ans = ""; int cnt = dp[xs][xt]; int i = xs, j = xt; while (cnt > 0) { if (dp[i][j] == dp[i][j - 1]) j--; else if (dp[i][j] == dp[i - 1][j]) i--; else { i--; ans = s[i] + ans; j--; cnt--; } } cout << ans << '\n'; }
replace
49
50
49
50
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int dp[1007][1007]; int main() { memset(dp, 0, sizeof(dp)); string s, t; cin >> s >> t; for (int x = 1; x <= s.size(); x++) { for (int j = 1; j <= t.size(); j++) { if (s[x - 1] == t[j - 1]) dp[x][j] = 1 + dp[x - 1][j - 1]; else dp[x][j] = max(dp[x - 1][j], dp[x][j - 1]); } } int i = (int)s.size(), j = (int)t.size(); string ans; 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 if (dp[i][j] == 1 + dp[i - 1][j - 1]) { ans.push_back(s[i - 1]); i--; j--; } } reverse(ans.begin(), ans.end()); cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[3007][3007]; int main() { memset(dp, 0, sizeof(dp)); string s, t; cin >> s >> t; for (int x = 1; x <= s.size(); x++) { for (int j = 1; j <= t.size(); j++) { if (s[x - 1] == t[j - 1]) dp[x][j] = 1 + dp[x - 1][j - 1]; else dp[x][j] = max(dp[x - 1][j], dp[x][j - 1]); } } int i = (int)s.size(), j = (int)t.size(); string ans; 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 if (dp[i][j] == 1 + dp[i - 1][j - 1]) { ans.push_back(s[i - 1]); i--; j--; } } reverse(ans.begin(), ans.end()); cout << ans << "\n"; return 0; }
replace
4
5
4
5
0
p03165
C++
Runtime Error
#include <iostream> using namespace std; int dp[3000][3000]; void lcs(string s1, string 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] = dp[i - 1][j - 1] + 1; } else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int main() { string s1; string s2; getline(cin, s1); getline(cin, s2); lcs(s1, s2); int i = s1.size(); int j = s2.size(); string s3 = ""; while (i > 0 && j > 0) { if (s1[i - 1] == s2[j - 1]) { s3 = s1[i - 1] + s3; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } cout << s3 << '\n'; return 0; }
#include <iostream> using namespace std; int dp[5000][5000]; void lcs(string s1, string 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] = dp[i - 1][j - 1] + 1; } else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int main() { string s1; string s2; getline(cin, s1); getline(cin, s2); lcs(s1, s2); int i = s1.size(); int j = s2.size(); string s3 = ""; while (i > 0 && j > 0) { if (s1[i - 1] == s2[j - 1]) { s3 = s1[i - 1] + s3; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } cout << s3 << '\n'; return 0; }
replace
4
5
4
5
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int dp[s.size() + 1][t.size() + 1]; for (int i = 0; i <= s.size(); i++) { for (int j = 0; j <= t.size(); j++) { if (i == 0 || j == 0) { dp[i][j] = 0; continue; } 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 = ""; for (int i = s.size(), j = t.size(); i > 0 || j > 0;) { // if(s[i-1] == t[j-1]){ if (dp[i][j] != max(dp[i - 1][j], dp[i][j - 1])) { ans += s[i - 1]; i--, j--; continue; } if (dp[i][j] == dp[i - 1][j]) { i--; continue; } else { j--; continue; } } // cout<<dp[s.size()][t.size()]; 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 dp[s.size() + 1][t.size() + 1]; for (int i = 0; i <= s.size(); i++) { for (int j = 0; j <= t.size(); j++) { if (i == 0 || j == 0) { dp[i][j] = 0; continue; } 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 = ""; for (int i = s.size(), j = t.size(); i > 0 && j > 0;) { if (s[i - 1] == t[j - 1]) { // if(dp[i][j] != max(dp[i-1][j], dp[i][j-1])) { ans += s[i - 1]; i--, j--; continue; } if (dp[i][j] == dp[i - 1][j]) { i--; continue; } else { j--; continue; } } // cout<<dp[s.size()][t.size()]; reverse(ans.begin(), ans.end()); cout << ans; return 0; }
replace
19
22
19
22
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define PI 3.14159265359 #define INF 1000100100 #define all(x) (x).begin(), (x).end() #define P pair<int, int> typedef long long ll; using namespace std; int main() { string s, t; cin >> s >> t; int sl = s.length(); int tl = t.length(); s = '#' + s; t = '#' + t; int dp[sl + 1][tl + 1]; rep(i, sl + 1) dp[i][0] = 0; rep(i, tl + 1) dp[0][i] = 0; for (int i = 1; i <= sl; i++) { for (int j = 1; j <= tl; j++) { if (s[i] == t[j]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int len = dp[sl][tl]; string ans = ""; int nowi = sl; int nowj = tl; while (len) { if (s[nowi] == t[nowj]) { ans = s[nowi] + ans; nowi--; nowj--; len--; } else if (dp[nowi - 1][nowj] == dp[nowi][nowj - 1]) nowi--; else nowj--; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define PI 3.14159265359 #define INF 1000100100 #define all(x) (x).begin(), (x).end() #define P pair<int, int> typedef long long ll; using namespace std; int main() { string s, t; cin >> s >> t; int sl = s.length(); int tl = t.length(); s = '#' + s; t = '#' + t; int dp[sl + 1][tl + 1]; rep(i, sl + 1) dp[i][0] = 0; rep(i, tl + 1) dp[0][i] = 0; for (int i = 1; i <= sl; i++) { for (int j = 1; j <= tl; j++) { if (s[i] == t[j]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int len = dp[sl][tl]; string ans = ""; int nowi = sl; int nowj = tl; while (len) { if (s[nowi] == t[nowj]) { ans = s[nowi] + ans; nowi--; nowj--; len--; } else if (dp[nowi][nowj] == dp[nowi - 1][nowj]) nowi--; else nowj--; } cout << ans << endl; return 0; }
replace
39
40
39
40
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define PB push_back #define MP make_pair #define MT make_tuple #define N 311111 #define A 1111111 #define BLOCK 555 // ~sqrt(N) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; const int INF = 1e9 + 5; ll bin_search(ll arr[], ll x, ll l, ll r) { while (l < r) { ll mid = (l + r + 1) / 2; ll power = (pow(2, mid + 1) * x) - (arr[mid]); // cout<<l<<" "<<r<<" "<<mid<<" "<<power<<endl; if (power > 0) { l = mid; } else if (power <= 0) { r = mid - 1; } } return l; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; // cin>>t; t = 1; while (t--) { int n, q; string a, b; string ans = ""; cin >> a >> b; int dp[a.length() + 1][b.length() + 1]; for (int i = 0; i <= a.length(); i++) { dp[i][0] = 0; } for (int j = 0; j <= b.length(); j++) { dp[0][j] = 0; } for (int i = 1; i <= a.length(); i++) { for (int j = 1; j <= b.length(); 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 = a.length(); int j = b.length(); while (i >= 1 || j >= 1) { if (a[i - 1] == b[j - 1]) { ans = a[i - 1] + ans; i--; j--; } else { if (dp[i - 1][j] == dp[i][j]) { i--; } else { j--; } } } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define PB push_back #define MP make_pair #define MT make_tuple #define N 311111 #define A 1111111 #define BLOCK 555 // ~sqrt(N) typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; const int INF = 1e9 + 5; ll bin_search(ll arr[], ll x, ll l, ll r) { while (l < r) { ll mid = (l + r + 1) / 2; ll power = (pow(2, mid + 1) * x) - (arr[mid]); // cout<<l<<" "<<r<<" "<<mid<<" "<<power<<endl; if (power > 0) { l = mid; } else if (power <= 0) { r = mid - 1; } } return l; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; // cin>>t; t = 1; while (t--) { int n, q; string a, b; string ans = ""; cin >> a >> b; int dp[a.length() + 1][b.length() + 1]; for (int i = 0; i <= a.length(); i++) { dp[i][0] = 0; } for (int j = 0; j <= b.length(); j++) { dp[0][j] = 0; } for (int i = 1; i <= a.length(); i++) { for (int j = 1; j <= b.length(); 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 = a.length(); int j = b.length(); while (i >= 1 && j >= 1) { if (a[i - 1] == b[j - 1]) { ans = a[i - 1] + ans; i--; j--; } else { if (dp[i - 1][j] == dp[i][j]) { i--; } else { j--; } } } cout << ans << endl; } }
replace
64
65
64
65
0
p03165
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define MAX 300 int d[MAX][MAX]; string a, b; int dp(int l, int r) { if (r < 0 || l < 0) return 0; if (d[l][r] == -1) { if (a[l] == b[r]) d[l][r] = dp(l - 1, r - 1) + 1; else d[l][r] = max({dp(l - 1, r), dp(l, r - 1)}); } return d[l][r]; } string dp2(int l, int r) { if (l == 0) { string s = ""; if (d[l][r]) return s + a[l]; else return ""; } if (r == 0) { string s = ""; if (d[l][r]) return s + b[r]; else return ""; } if (a[l] == b[r]) { return dp2(l - 1, r - 1) + a[l]; } else { int m = max(d[l - 1][r], d[l][r - 1]); if (m == d[l - 1][r]) return dp2(l - 1, r); else return dp2(l, r - 1); } } int main() { cin >> a >> b; for (int i = 0; i < a.length(); i++) for (int j = 0; j < b.length(); j++) d[i][j] = -1; dp(a.length() - 1, b.length() - 1); cout << dp2(a.length() - 1, b.length() - 1) << endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define MAX 3000 int d[MAX][MAX]; string a, b; int dp(int l, int r) { if (r < 0 || l < 0) return 0; if (d[l][r] == -1) { if (a[l] == b[r]) d[l][r] = dp(l - 1, r - 1) + 1; else d[l][r] = max({dp(l - 1, r), dp(l, r - 1)}); } return d[l][r]; } string dp2(int l, int r) { if (l == 0) { string s = ""; if (d[l][r]) return s + a[l]; else return ""; } if (r == 0) { string s = ""; if (d[l][r]) return s + b[r]; else return ""; } if (a[l] == b[r]) { return dp2(l - 1, r - 1) + a[l]; } else { int m = max(d[l - 1][r], d[l][r - 1]); if (m == d[l - 1][r]) return dp2(l - 1, r); else return dp2(l, r - 1); } } int main() { cin >> a >> b; for (int i = 0; i < a.length(); i++) for (int j = 0; j < b.length(); j++) d[i][j] = -1; dp(a.length() - 1, b.length() - 1); cout << dp2(a.length() - 1, b.length() - 1) << endl; return 0; }
replace
2
3
2
3
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define optimize \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define INF 2000000020LL #define ALL(x) x.begin(), x.end() #define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin()) #define REP(i, a, b) for (int i = (a); i <= (b); i++) #define POS(n, x) (lower_bound(ALL(n), x) - (n).begin()) #define ll long long #define ld long double #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define PB push_back #define EB emplace_back #define MOD 1000000007 #define PRIME 101 #define MAXN 3030 #define MAXL 23 #define endl '\n' using namespace std; // (1 + sqrt(5)) / 2 const ld GOLDEN_RATIO = 1.6180339887498949025257388711906969547271728515625; const ld PI = 2.0 * acos(0.0); const int BLOCK = 333; int dp[MAXN][MAXN]; string S, T; int solve(int i, int j) { if (i < 0 || j < 0) return 0; if (dp[i][j] != -1) return dp[i][j]; return dp[i][j] = (S[i] == T[j] ? 1 + solve(i - 1, j - 1) : max(solve(i - 1, j), solve(i, j - 1))); } int main(int argc, char const *argv[]) { optimize; memset(dp, -1, sizeof dp); cin >> S >> T; int N = S.length() - 1; int M = T.length() - 1; solve(N, M); string ans = ""; while (N >= 0 && M >= 0) { if (S[N] == T[M]) { ans += S[N]; N--; M--; } else { if (dp[N - 1][M] > dp[N][M - 1]) N--; else M--; } } reverse(ALL(ans)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define optimize \ ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define INF 2000000020LL #define ALL(x) x.begin(), x.end() #define UNIQUE(c) (c).resize(unique(ALL(c)) - (c).begin()) #define REP(i, a, b) for (int i = (a); i <= (b); i++) #define POS(n, x) (lower_bound(ALL(n), x) - (n).begin()) #define ll long long #define ld long double #define pii pair<int, int> #define vi vector<int> #define vii vector<pii> #define PB push_back #define EB emplace_back #define MOD 1000000007 #define PRIME 101 #define MAXN 3030 #define MAXL 23 #define endl '\n' using namespace std; // (1 + sqrt(5)) / 2 const ld GOLDEN_RATIO = 1.6180339887498949025257388711906969547271728515625; const ld PI = 2.0 * acos(0.0); const int BLOCK = 333; int dp[MAXN][MAXN]; string S, T; int solve(int i, int j) { if (i < 0 || j < 0) return 0; if (dp[i][j] != -1) return dp[i][j]; return dp[i][j] = (S[i] == T[j] ? 1 + solve(i - 1, j - 1) : max(solve(i - 1, j), solve(i, j - 1))); } int main(int argc, char const *argv[]) { optimize; memset(dp, -1, sizeof dp); cin >> S >> T; int N = S.length() - 1; int M = T.length() - 1; solve(N, M); string ans = ""; while (N >= 0 && M >= 0) { if (S[N] == T[M]) { ans += S[N]; N--; M--; } else { if (!N) M--; else if (!M) N--; else if (dp[N - 1][M] > dp[N][M - 1]) N--; else M--; } } reverse(ALL(ans)); cout << ans << endl; return 0; }
replace
65
66
65
70
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long #define ld long double #define pb push_back const ll mod = 1e9 + 7; template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans % m; } const ll N = 1e6 + 8; const ll INF = 1e18; string a, b; ll dp[3005][3005]; ll LCS(ll i, ll j) { if (i == 0 || j == 0) return 0; if (dp[i][j] != -1) return dp[i][j]; ll ans = 0; if (a[i - 1] == b[j - 1]) { ans = 1 + LCS(i - 1, j - 1); } ans = max(ans, LCS(i - 1, j)); ans = max(ans, LCS(i, j - 1)); return dp[i][j] = ans; } int main() { IOS; cin >> a >> b; string s = ""; memset(dp, -1, sizeof(dp)); ll l = LCS(a.length(), b.length()); ll n = a.length(), m = b.length(); while (n > 0 || m > 0 || l != 0) { if (a[n - 1] == b[m - 1] && LCS(n, m) == l) { s += a[n - 1]; // cout<<a[n-1]; n--; m--; l--; } else if (LCS(n - 1, m) == l) { n--; } else m--; } reverse(s.begin(), s.end()); cout << s << endl; }
#include <bits/stdc++.h> using namespace std; #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long #define ld long double #define pb push_back const ll mod = 1e9 + 7; template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans % m; } const ll N = 1e6 + 8; const ll INF = 1e18; string a, b; ll dp[3005][3005]; ll LCS(ll i, ll j) { if (i == 0 || j == 0) return 0; if (dp[i][j] != -1) return dp[i][j]; ll ans = 0; if (a[i - 1] == b[j - 1]) { ans = 1 + LCS(i - 1, j - 1); } ans = max(ans, LCS(i - 1, j)); ans = max(ans, LCS(i, j - 1)); return dp[i][j] = ans; } int main() { IOS; cin >> a >> b; string s = ""; memset(dp, -1, sizeof(dp)); ll l = LCS(a.length(), b.length()); ll n = a.length(), m = b.length(); while (n > 0 && m > 0 && l != 0) { if (a[n - 1] == b[m - 1] && LCS(n, m) == l) { s += a[n - 1]; // cout<<a[n-1]; n--; m--; l--; } else if (LCS(n - 1, m) == l) { n--; } else m--; } reverse(s.begin(), s.end()); cout << s << endl; }
replace
44
45
44
45
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define ull unsigned long long int #define ll long long #define int long long #define ld long double #define endl '\n' const int mod = 1e9 + 7; #define F first #define S second #define pb push_back #define mp make_pair #define trace(x) cerr << #x << " is " << x << endl; #define all(x) x.begin(), x.end() #define reset(x, val) memset(x, val, sizeof(x)) #define flush fflush(stdout) #define sz(a) a.size() #define pii pair<int, int> using namespace std; const int N = 1e3 + 3; int dp[N][N], n, m; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; n = sz(s); m = sz(t); string ans = ""; 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]); } } int i = n, j = m; while (i > 0 && j > 0) { // trace(ans); if (s[i - 1] == t[j - 1]) { ans = s[i - 1] + ans; i--; j--; } else { if (dp[i - 1][j] == dp[i][j]) i--; else j--; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ull unsigned long long int #define ll long long #define int long long #define ld long double #define endl '\n' const int mod = 1e9 + 7; #define F first #define S second #define pb push_back #define mp make_pair #define trace(x) cerr << #x << " is " << x << endl; #define all(x) x.begin(), x.end() #define reset(x, val) memset(x, val, sizeof(x)) #define flush fflush(stdout) #define sz(a) a.size() #define pii pair<int, int> using namespace std; const int N = 3005; int dp[N][N], n, m; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; n = sz(s); m = sz(t); string ans = ""; 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]); } } int i = n, j = m; while (i > 0 && j > 0) { // trace(ans); if (s[i - 1] == t[j - 1]) { ans = s[i - 1] + ans; i--; j--; } else { if (dp[i - 1][j] == dp[i][j]) i--; else j--; } } cout << ans << endl; return 0; }
replace
18
19
18
19
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { string s1; string s2; cin >> s1 >> s2; int l1 = s1.length(); int l2 = s2.length(); int ai[l1][l2]; if (s1[0] == s2[0]) ai[0][0] = 1; else ai[0][0] = 0; for (int i = 1; i < l2; i++) { if (s1[0] == s2[i]) ai[0][i] = 1; else ai[0][i] = ai[0][i - 1]; } for (int i = 1; i < l1; i++) { if (s1[i] == s2[0]) ai[i][0] = 1; else ai[i][0] = ai[i - 1][0]; } for (int n1 = 1; n1 < l1; n1++) { for (int n2 = 1; n2 < l2; n2++) { if (s1[n1] == s2[n2]) ai[n1][n2] = ai[n1 - 1][n2 - 1] + 1; else ai[n1][n2] = (ai[n1 - 1][n2] > ai[n1][n2 - 1]) ? ai[n1 - 1][n2] : ai[n1][n2 - 1]; } } string s = ""; int t = 0; int i = l1 - 1; int j = l2 - 1; while (t < 1) { if (s1[i] == s2[j]) { s = string(1, s1[i]) + s; i--; j--; } else { if (i > 0) { if (ai[i][j] == ai[i - 1][j]) i--; else if (j > 0) { if (ai[i][j] == ai[i][j - 1]) j--; } } else if (j > 0) { if (ai[i][j] == ai[i][j - 1]) j--; } } if (i < 0 || j < 0) t++; } cout << s; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s1; string s2; cin >> s1 >> s2; int l1 = s1.length(); int l2 = s2.length(); int ai[l1][l2]; if (s1[0] == s2[0]) ai[0][0] = 1; else ai[0][0] = 0; for (int i = 1; i < l2; i++) { if (s1[0] == s2[i]) ai[0][i] = 1; else ai[0][i] = ai[0][i - 1]; } for (int i = 1; i < l1; i++) { if (s1[i] == s2[0]) ai[i][0] = 1; else ai[i][0] = ai[i - 1][0]; } for (int n1 = 1; n1 < l1; n1++) { for (int n2 = 1; n2 < l2; n2++) { if (s1[n1] == s2[n2]) ai[n1][n2] = ai[n1 - 1][n2 - 1] + 1; else ai[n1][n2] = (ai[n1 - 1][n2] > ai[n1][n2 - 1]) ? ai[n1 - 1][n2] : ai[n1][n2 - 1]; } } string s = ""; int t = 0; int i = l1 - 1; int j = l2 - 1; while (t < 1) { if (s1[i] == s2[j]) { s = string(1, s1[i]) + s; i--; j--; } else { if (i > 0) { if (ai[i][j] == ai[i - 1][j]) i--; else if (j > 0) { if (ai[i][j] == ai[i][j - 1]) j--; } } else if (j > 0) { if (ai[i][j] == ai[i][j - 1]) j--; } } if (i < 0 || j < 0) t++; if ((i == 0 && j == 0) && s1[i] != s2[i]) t++; } cout << s; return 0; }
insert
58
58
58
60
TLE
p03165
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define loop(n, i, a) for (ll i = a; i < n; i++) #define loopR(n, i, a) for (ll i = n - 1; i >= a; i--) #define all(arr, n) arr, arr + n #define allv(v) (v).begin(), (v).end() #define rallv(v) (v).rbegin(), (v).rend() #define m_p make_pair #define ll long long #define pii pair<ll, ll> #define vi vector<int> #define vll vector<ll> #define vii vector<pair<ll, ll>> #define sz(x) (int)x.size() #define pb push_back #define endl "\n" #define Endl "\n" #define f first #define s second #define mem(dp, n) memset(dp, n, sizeof dp) #define test(i) cout << i << endl int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; int dy[] = {0, 0, 1, -1, -1, 1, -1, 1}; void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } const int mxN = 2e3 + 5, oo = 0x3f3f3f3f, mod = 1e9 + 7; string s, t; int dp[mxN][mxN]; int go(int i, int j) { if (i == sz(s) || j == sz(t)) return 0; int &ret = dp[i][j]; if (~ret) return ret; if (s[i] == t[j]) return ret = go(i + 1, j + 1) + 1; return ret = max(go(i + 1, j), go(i, j + 1)); } void build(int i, int j) { if (i == sz(s) || j == sz(t)) return; if (s[i] == t[j]) cout << s[i], build(i + 1, j + 1); else if (go(i, j) == go(i, j + 1)) build(i, j + 1); else build(i + 1, j); } void solve() { cin >> s >> t; mem(dp, -1); build(0, 0); } int main() { fast(); solve(); }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define loop(n, i, a) for (ll i = a; i < n; i++) #define loopR(n, i, a) for (ll i = n - 1; i >= a; i--) #define all(arr, n) arr, arr + n #define allv(v) (v).begin(), (v).end() #define rallv(v) (v).rbegin(), (v).rend() #define m_p make_pair #define ll long long #define pii pair<ll, ll> #define vi vector<int> #define vll vector<ll> #define vii vector<pair<ll, ll>> #define sz(x) (int)x.size() #define pb push_back #define endl "\n" #define Endl "\n" #define f first #define s second #define mem(dp, n) memset(dp, n, sizeof dp) #define test(i) cout << i << endl int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; int dy[] = {0, 0, 1, -1, -1, 1, -1, 1}; void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } const int mxN = 3e3 + 5, oo = 0x3f3f3f3f, mod = 1e9 + 7; string s, t; int dp[mxN][mxN]; int go(int i, int j) { if (i == sz(s) || j == sz(t)) return 0; int &ret = dp[i][j]; if (~ret) return ret; if (s[i] == t[j]) return ret = go(i + 1, j + 1) + 1; return ret = max(go(i + 1, j), go(i, j + 1)); } void build(int i, int j) { if (i == sz(s) || j == sz(t)) return; if (s[i] == t[j]) cout << s[i], build(i + 1, j + 1); else if (go(i, j) == go(i, j + 1)) build(i, j + 1); else build(i + 1, j); } void solve() { cin >> s >> t; mem(dp, -1); build(0, 0); } int main() { fast(); solve(); }
replace
47
48
47
48
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long signed main() { cin.tie(0); ios::sync_with_stdio(false); string s, t; cin >> s >> t; vector<vector<int>> dp(3000, vector<int>(3000, 0)); for (int i = 0; i < s.size(); i++) { for (int j = 0; j < t.size(); j++) { 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; int i = s.size(), j = t.size(); while (ans.size() != dp[s.size()][t.size()]) { if (dp[i - 1][j] == dp[i][j]) i--; else if (dp[i][j - 1] == dp[i][j]) j--; else { i--, j--; ans += s[i]; } } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long signed main() { cin.tie(0); ios::sync_with_stdio(false); string s, t; cin >> s >> t; vector<vector<int>> dp(3001, vector<int>(3001, 0)); for (int i = 0; i < s.size(); i++) { for (int j = 0; j < t.size(); j++) { 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; int i = s.size(), j = t.size(); while (ans.size() != dp[s.size()][t.size()]) { if (dp[i - 1][j] == dp[i][j]) i--; else if (dp[i][j - 1] == dp[i][j]) j--; else { i--, j--; ans += s[i]; } } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
replace
10
11
10
11
0
p03165
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; #define assem99 \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long #define ull unsigned long long #define ld long double #define ii pair<int, int> #define vii vector<ii> #define f(i, a, b) for (int i = a; i < b; i++) #define F first #define S second #define sz(a) (int)(a).size() #define ReadFile freopen("input.txt", "r", stdin) #define WriteFile freopen("output.txt", "w", stdout) int x[] = {0, -1, 0, 1, -1, 1, -1, 1}; int y[] = {-1, 0, 1, 0, 1, -1, -1, 1}; const int MOD = 998244353, N = 2e5 + 5; const double pi = acos(-1); int dp[3005][3005]; int main() { assem99; string s, t; cin >> s >> t; f(i, 0, sz(s)) { f(j, 0, sz(t)) { if (s[i] == t[j]) dp[i + 1][j + 1] = dp[i][j] + 1; else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } int cur = dp[sz(s)][sz(t)]; string ans = ""; int i = sz(s), j = sz(t); while (i >= 0 && j >= 0) { if (dp[i][j] != dp[i - 1][j] && dp[i][j] != dp[i][j - 1]) { ans += s[i - 1]; i--, j--; } else if (dp[i][j] == dp[i - 1][j]) i--; else j--; } for (int i = sz(ans) - 1; i >= 0; i--) cout << ans[i]; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <vector> using namespace std; #define assem99 \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long #define ull unsigned long long #define ld long double #define ii pair<int, int> #define vii vector<ii> #define f(i, a, b) for (int i = a; i < b; i++) #define F first #define S second #define sz(a) (int)(a).size() #define ReadFile freopen("input.txt", "r", stdin) #define WriteFile freopen("output.txt", "w", stdout) int x[] = {0, -1, 0, 1, -1, 1, -1, 1}; int y[] = {-1, 0, 1, 0, 1, -1, -1, 1}; const int MOD = 998244353, N = 2e5 + 5; const double pi = acos(-1); int dp[3005][3005]; int main() { assem99; string s, t; cin >> s >> t; f(i, 0, sz(s)) { f(j, 0, sz(t)) { if (s[i] == t[j]) dp[i + 1][j + 1] = dp[i][j] + 1; else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } int cur = dp[sz(s)][sz(t)]; string ans = ""; int i = sz(s), j = sz(t); while (i > 0 && j > 0) { if (dp[i][j] != dp[i - 1][j] && dp[i][j] != dp[i][j - 1]) { ans += s[i - 1]; i--, j--; } else if (dp[i][j] == dp[i - 1][j]) i--; else j--; } for (int i = sz(ans) - 1; i >= 0; i--) cout << ans[i]; return 0; }
replace
54
55
54
55
0
p03165
C++
Runtime Error
#include "algorithm" #include "cstring" #include "iostream" #include "math.h" #include "string" #include "vector" using namespace std; typedef long long ll; string s, t; int dpsize[3010][3010]; // i,jでのlcs int main() { cin >> s >> t; int size_s = s.size(); int size_t = t.size(); for (int i = 0; i < 3010; i++) { dpsize[0][i] = 0; dpsize[i][0] = 0; } for (int i = 0; i < size_s; i++) { for (int j = 0; j < size_t; j++) { if (s[i] == t[j]) { dpsize[i + 1][j + 1] = dpsize[i][j] + 1; } else if (dpsize[i + 1][j] > dpsize[i][j + 1]) { dpsize[i + 1][j + 1] = dpsize[i + 1][j]; } else { dpsize[i + 1][j + 1] = dpsize[i][j + 1]; } } } string ans = ""; int i = size_s; int j = size_t; while (i > 0 || j > 0) { if (dpsize[i][j] == dpsize[i - 1][j]) { i--; } if (dpsize[i][j] == dpsize[i][j - 1]) { j--; } if (dpsize[i][j] > dpsize[i - 1][j] && dpsize[i][j] > dpsize[i][j - 1]) { ans += s[i - 1]; i--; j--; } } reverse(ans.begin(), ans.end()); cout << ans << "\n"; }
#include "algorithm" #include "cstring" #include "iostream" #include "math.h" #include "string" #include "vector" using namespace std; typedef long long ll; string s, t; int dpsize[3010][3010]; // i,jでのlcs int main() { cin >> s >> t; int size_s = s.size(); int size_t = t.size(); for (int i = 0; i < 3010; i++) { dpsize[0][i] = 0; dpsize[i][0] = 0; } for (int i = 0; i < size_s; i++) { for (int j = 0; j < size_t; j++) { if (s[i] == t[j]) { dpsize[i + 1][j + 1] = dpsize[i][j] + 1; } else if (dpsize[i + 1][j] > dpsize[i][j + 1]) { dpsize[i + 1][j + 1] = dpsize[i + 1][j]; } else { dpsize[i + 1][j + 1] = dpsize[i][j + 1]; } } } string ans = ""; int i = size_s; int j = size_t; while (dpsize[i][j] > 0) { if (dpsize[i][j] == dpsize[i - 1][j]) { i--; } if (dpsize[i][j] == dpsize[i][j - 1]) { j--; } if (dpsize[i][j] > dpsize[i - 1][j] && dpsize[i][j] > dpsize[i][j - 1]) { ans += s[i - 1]; i--; j--; } } reverse(ans.begin(), ans.end()); cout << ans << "\n"; }
replace
36
37
36
37
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG // これつけるとA[N]でもいいらしい // for文のマクロ #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define big 1000000007 #define all(a) sort((a).begin(), (a).end()) // ソートのマクロ #define Re(a) reverse((a).begin(), (a).end()) #define YN(a) \ if (a) { \ cout << "Yes" << endl; \ } else \ cout << "No" << endl; // 条件によってYes、Noを出力する int main() { // LCS string s, t; cin >> s >> t; int n = s.length(), m = t.length(); int dp[301][301] = {}; // dp[i][j]=i-1文字目まで見た時のLCSの長さ 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][j - 1], dp[i - 1][j]); } } } /* rep(i,n+1){ rep(j,m+1){ cout<<dp[i][j]<<" "; } cout<<endl; } */ string ans = ""; int l = n, r = m; // line,raw while (dp[l][r] != 0) { if (dp[l - 1][r] < dp[l][r] && dp[l][r - 1] < dp[l][r]) { // 両方減る ans += s[l - 1]; l--, r--; } else if (dp[l - 1][r] == dp[l][r] && dp[l][r - 1] < dp[l][r]) { // 上は減らない、左は減る l--; } else if (dp[l - 1][r] < dp[l][r] && dp[l][r - 1] == dp[l][r]) { r--; } else { l--; } } Re(ans); // cout<<dp[n][m]<<endl; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG // これつけるとA[N]でもいいらしい // for文のマクロ #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define big 1000000007 #define all(a) sort((a).begin(), (a).end()) // ソートのマクロ #define Re(a) reverse((a).begin(), (a).end()) #define YN(a) \ if (a) { \ cout << "Yes" << endl; \ } else \ cout << "No" << endl; // 条件によってYes、Noを出力する int main() { // LCS string s, t; cin >> s >> t; int n = s.length(), m = t.length(); int dp[3001][3001] = {}; // dp[i][j]=i-1文字目まで見た時のLCSの長さ s[i]==t[i]だったらdp[i-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][j - 1], dp[i - 1][j]); } } } /* rep(i,n+1){ rep(j,m+1){ cout<<dp[i][j]<<" "; } cout<<endl; } */ string ans = ""; int l = n, r = m; // line,raw while (dp[l][r] != 0) { if (dp[l - 1][r] < dp[l][r] && dp[l][r - 1] < dp[l][r]) { // 両方減る ans += s[l - 1]; l--, r--; } else if (dp[l - 1][r] == dp[l][r] && dp[l][r - 1] < dp[l][r]) { // 上は減らない、左は減る l--; } else if (dp[l - 1][r] < dp[l][r] && dp[l][r - 1] == dp[l][r]) { r--; } else { l--; } } Re(ans); // cout<<dp[n][m]<<endl; cout << ans << endl; }
replace
18
20
18
20
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long string lcs(string s1, string s2) { ll int n = s1.length(); ll int m = s2.length(); if (n == 0 || m == 0) { return ""; } vector<vector<ll int>> dp(n + 1, vector<ll int>(m + 1, 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 s = n, e = m; string ans = ""; while (s >= 0 && e >= 0) { if (s1[s - 1] == s2[e - 1]) { ans += s1[s - 1]; s--; e--; } else { if (dp[s][e - 1] > dp[s - 1][e]) { e--; } else { s--; } } } reverse(ans.begin(), ans.end()); return ans; } int main() { string s1, s2; cin >> s1 >> s2; cout << lcs(s1, s2); }
#include <bits/stdc++.h> using namespace std; #define ll long long string lcs(string s1, string s2) { ll int n = s1.length(); ll int m = s2.length(); if (n == 0 || m == 0) { return ""; } vector<vector<ll int>> dp(n + 1, vector<ll int>(m + 1, 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 s = n, e = m; string ans = ""; while (s > 0 && e > 0) { if (s1[s - 1] == s2[e - 1]) { ans += s1[s - 1]; s--; e--; } else { if (dp[s][e - 1] > dp[s - 1][e]) { e--; } else { s--; } } } reverse(ans.begin(), ans.end()); return ans; } int main() { string s1, s2; cin >> s1 >> s2; cout << lcs(s1, s2); }
replace
33
34
33
34
0
p03165
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll, int> plli; typedef pair<int, pii> pipii; typedef vector<vector<int>> mati; typedef vector<vector<double>> matd; typedef vector<ll> vll; typedef vector<vector<ll>> vvll; #define rep(i, x, y) for (int i = (x); i < (y); ++i) template <typename T> void vec_print(vector<T> VEC) { rep(i, 0, VEC.size()) { cout << VEC[i] << " "; } cout << "\n"; }; template <typename T> void mat_print(vector<vector<T>> MAT){ rep(i, 0, MAT.size()){rep(j, 0, MAT[i].size()){cout << MAT[i][j] << " "; } cout << "\n"; } } ; template <typename CLASS1, typename CLASS2> class HOGE { public: CLASS1 key; CLASS2 value; HOGE(ll key, ll value) { this->key = index; this->value = value; }; ~HOGE(void) { return; }; void print(void) { cout << "key : " << key << ", value : " << value << "\n"; return; }; bool operator==(const HOGE &obj) { return (this->value == obj.value); }; bool operator<(const HOGE &obj) { return (this->value < obj.value); }; bool operator>(const HOGE &obj) { return (this->value > obj.value); }; }; constexpr int INF = (1 << 30); constexpr ll INFLL = 1LL << 62; constexpr long double EPS = 1e-12; typedef pair<vector<int>, int> P; /* void long_search(const string& s, const string& t, const int st_start, vector<string>& dp1, vector<string>& dp2){ if(st_start+1 <= s.size() + t.size() + 2) long_search(s, t, st_start+1, dp1, dp2); int hight = min(st_start+1, (int)s.size()); for(int s_start=max(0, st_start-(int)t.size()+1); s_start<hight; ++s_start){ int t_start = st_start-s_start; if(s[s_start] == t[t_start]){ dp2[s_start] = s[s_start] + dp2[s_start+1]; }else if(dp1[s_start].size() > dp1[s_start+1].size()){ dp2[s_start] = dp1[s_start]; }else{ dp2[s_start] = dp1[s_start+1]; } swap(dp1[s_start], dp2[s_start]); } swap(dp1[hight], dp2[hight]); return; } */ void long_search2(const string &s, const string &t, const int s_start, const int t_start, vector<vector<short int>> &dp) { if (s_start > s.size() || t_start > t.size()) return; if (s_start == s.size() || t_start == t.size()) { dp[s_start][t_start] = 0; return; } if (s[s_start] == t[t_start]) { long_search2(s, t, s_start + 1, t_start + 1, dp); dp[s_start][t_start] = dp[s_start + 1][t_start + 1] + 1; } else { long_search2(s, t, s_start + 1, t_start, dp); long_search2(s, t, s_start, t_start + 1, dp); dp[s_start][t_start] = max(dp[s_start + 1][t_start], dp[s_start][t_start + 1]); } return; } int main() { cin.tie(0); // cut the cin and cout (default, std::flush is performed after // std::cin) ios::sync_with_stdio( false); // cut the iostream and stdio (DON'T endl; BUT "\n";) string s, t; cin >> s >> t; if (s.size() > t.size()) swap(s, t); /* vector<string> dp1(s.size()+2, ""); vector<string> dp2(s.size()+2, ""); long_search(s, t, 0, dp1, dp2); */ vector<vector<short int>> dp(s.size() + 1, vector<short int>(t.size() + 1, -1)); long_search2(s, t, 0, 0, dp); string ans = ""; int s_start = 0, t_start = 0; while (true) { if (s_start >= s.size() || t_start >= t.size()) break; if (s[s_start] == t[t_start]) { ans += s[s_start]; s_start++; t_start++; } else if (dp[s_start + 1][t_start] > dp[s_start][t_start + 1]) { s_start++; } else { t_start++; } } cout << ans << "\n"; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll, int> plli; typedef pair<int, pii> pipii; typedef vector<vector<int>> mati; typedef vector<vector<double>> matd; typedef vector<ll> vll; typedef vector<vector<ll>> vvll; #define rep(i, x, y) for (int i = (x); i < (y); ++i) template <typename T> void vec_print(vector<T> VEC) { rep(i, 0, VEC.size()) { cout << VEC[i] << " "; } cout << "\n"; }; template <typename T> void mat_print(vector<vector<T>> MAT){ rep(i, 0, MAT.size()){rep(j, 0, MAT[i].size()){cout << MAT[i][j] << " "; } cout << "\n"; } } ; template <typename CLASS1, typename CLASS2> class HOGE { public: CLASS1 key; CLASS2 value; HOGE(ll key, ll value) { this->key = index; this->value = value; }; ~HOGE(void) { return; }; void print(void) { cout << "key : " << key << ", value : " << value << "\n"; return; }; bool operator==(const HOGE &obj) { return (this->value == obj.value); }; bool operator<(const HOGE &obj) { return (this->value < obj.value); }; bool operator>(const HOGE &obj) { return (this->value > obj.value); }; }; constexpr int INF = (1 << 30); constexpr ll INFLL = 1LL << 62; constexpr long double EPS = 1e-12; typedef pair<vector<int>, int> P; /* void long_search(const string& s, const string& t, const int st_start, vector<string>& dp1, vector<string>& dp2){ if(st_start+1 <= s.size() + t.size() + 2) long_search(s, t, st_start+1, dp1, dp2); int hight = min(st_start+1, (int)s.size()); for(int s_start=max(0, st_start-(int)t.size()+1); s_start<hight; ++s_start){ int t_start = st_start-s_start; if(s[s_start] == t[t_start]){ dp2[s_start] = s[s_start] + dp2[s_start+1]; }else if(dp1[s_start].size() > dp1[s_start+1].size()){ dp2[s_start] = dp1[s_start]; }else{ dp2[s_start] = dp1[s_start+1]; } swap(dp1[s_start], dp2[s_start]); } swap(dp1[hight], dp2[hight]); return; } */ void long_search2(const string &s, const string &t, const int s_start, const int t_start, vector<vector<short int>> &dp) { if (s_start > s.size() || t_start > t.size()) return; if (dp[s_start][t_start] >= 0) return; if (s_start == s.size() || t_start == t.size()) { dp[s_start][t_start] = 0; return; } if (s[s_start] == t[t_start]) { long_search2(s, t, s_start + 1, t_start + 1, dp); dp[s_start][t_start] = dp[s_start + 1][t_start + 1] + 1; } else { long_search2(s, t, s_start + 1, t_start, dp); long_search2(s, t, s_start, t_start + 1, dp); dp[s_start][t_start] = max(dp[s_start + 1][t_start], dp[s_start][t_start + 1]); } return; } int main() { cin.tie(0); // cut the cin and cout (default, std::flush is performed after // std::cin) ios::sync_with_stdio( false); // cut the iostream and stdio (DON'T endl; BUT "\n";) string s, t; cin >> s >> t; if (s.size() > t.size()) swap(s, t); /* vector<string> dp1(s.size()+2, ""); vector<string> dp2(s.size()+2, ""); long_search(s, t, 0, dp1, dp2); */ vector<vector<short int>> dp(s.size() + 1, vector<short int>(t.size() + 1, -1)); long_search2(s, t, 0, 0, dp); string ans = ""; int s_start = 0, t_start = 0; while (true) { if (s_start >= s.size() || t_start >= t.size()) break; if (s[s_start] == t[t_start]) { ans += s[s_start]; s_start++; t_start++; } else if (dp[s_start + 1][t_start] > dp[s_start][t_start + 1]) { s_start++; } else { t_start++; } } cout << ans << "\n"; return 0; }
insert
98
98
98
101
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int using namespace std; ll dp[3000][3000]; int main() { string p, q; cin >> p >> q; for (int i = 0; i <= p.length(); i++) { for (int j = 0; j <= q.length(); j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (p[i - 1] == q[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } // cout<<dp[p.length()][q.length()]; char ans[dp[p.length()][q.length()] + 1]; ans[dp[p.length()][q.length()]] = '\0'; int i = p.length(); int j = q.length(); int k = dp[p.length()][q.length()] - 1; while (i > 0 && j > 0) { if (p[i - 1] == q[j - 1]) { ans[k] = p[i - 1]; k--; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } cout << ans; }
#include <bits/stdc++.h> #define ll long long int using namespace std; ll dp[3001][3001]; int main() { string p, q; cin >> p >> q; for (int i = 0; i <= p.length(); i++) { for (int j = 0; j <= q.length(); j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (p[i - 1] == q[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } // cout<<dp[p.length()][q.length()]; char ans[dp[p.length()][q.length()] + 1]; ans[dp[p.length()][q.length()]] = '\0'; int i = p.length(); int j = q.length(); int k = dp[p.length()][q.length()] - 1; while (i > 0 && j > 0) { if (p[i - 1] == q[j - 1]) { ans[k] = p[i - 1]; k--; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } cout << ans; }
replace
3
4
3
4
0
p03165
C++
Runtime Error
/* * 未だ解けていない */ #include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; const int MAX = 3000 + 10; int dp[MAX][MAX]; int main() { string s1; string s2; cin >> s1 >> s2; // vector<vector<int>> dp(s1.length() + 10, vector<int>(s2.length() + 10, 0)); for (int i = 1; i <= s1.length(); ++i) { for (int j = 1; j <= s2.length(); ++j) { int si = i - 1; int sj = j - 1; if (s1[si] == s2[sj] && dp[i - 1][j - 1] + 1 > dp[i - 1][j]) dp[i][j] = dp[i - 1][j - 1] + 1; else { int aboveSize = dp[i - 1][j]; int leftSize = dp[i][j - 1]; if (aboveSize > leftSize) dp[i][j] = dp[i - 1][j]; else dp[i][j] = dp[i][j - 1]; } } } /* for(int i = 0; i <= s1.length(); ++i) { if(i != 0) cout << s1[i - 1]; for(int j = 0; j <= s2.length(); ++j) { if(i == 0) { if(j == 0) cout << "\t"; else cout << s2[j - 1] << "\t"; } else { if(j == 0) cout << ""; else cout << "\t" << dp[i][j]; } } cout << endl; } */ string s; int i = s1.length(), j = s2.length(); while (i != 0 && j != 0) { while (j != 0 && dp[i][j - 1] == dp[i][j]) j--; while (i != 0 && dp[i - 1][j] == dp[i][j]) i--; s = s2[j - 1] + s; // cout << s2[j - 1] << endl; i--; j--; } cout << s << endl; // cout << dp[s1.length()][s2.length()] << endl; return 0; }
/* * 未だ解けていない */ #include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; const int MAX = 3000 + 10; int dp[MAX][MAX]; int main() { string s1; string s2; cin >> s1 >> s2; // vector<vector<int>> dp(s1.length() + 10, vector<int>(s2.length() + 10, 0)); for (int i = 1; i <= s1.length(); ++i) { for (int j = 1; j <= s2.length(); ++j) { int si = i - 1; int sj = j - 1; if (s1[si] == s2[sj] && dp[i - 1][j - 1] + 1 > dp[i - 1][j]) dp[i][j] = dp[i - 1][j - 1] + 1; else { int aboveSize = dp[i - 1][j]; int leftSize = dp[i][j - 1]; if (aboveSize > leftSize) dp[i][j] = dp[i - 1][j]; else dp[i][j] = dp[i][j - 1]; } } } /* for(int i = 0; i <= s1.length(); ++i) { if(i != 0) cout << s1[i - 1]; for(int j = 0; j <= s2.length(); ++j) { if(i == 0) { if(j == 0) cout << "\t"; else cout << s2[j - 1] << "\t"; } else { if(j == 0) cout << ""; else cout << "\t" << dp[i][j]; } } cout << endl; } */ string s; int i = s1.length(), j = s2.length(); while (i != 0 && j != 0) { while (j != 0 && dp[i][j - 1] == dp[i][j]) j--; while (i != 0 && dp[i - 1][j] == dp[i][j]) i--; if (j == 0) break; s = s2[j - 1] + s; // cout << s2[j - 1] << endl; i--; j--; } cout << s << endl; // cout << dp[s1.length()][s2.length()] << endl; return 0; }
insert
78
78
78
80
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int int mod = INT_MAX; int memo[3005][3005]; int n; string LCSstr(string &s1, string &s2, int i, int j, int len) { // base cases if ((i >= s1.size()) or (j >= s2.size()) or (len == 0)) return ""; // selfwork string ans; while (len > 0) { if (s1[i] == s2[j]) { ans.push_back(s1[i]); i++; j++; len--; } else { if (memo[i + 1][j] > memo[i][j + 1]) i++; else j++; } } return ans; } int LCSlen(string &s1, string &s2, int i, int j) // pass string by reference { // base cases if ((i >= s1.size()) or (j >= s2.size())) return 0; int &ans = memo[i][j]; // memoizatiion if (ans != -1) return ans; // self work if (s1[i] == s2[j]) return ans = 1 + LCSlen(s1, s2, i + 1, j + 1); else return ans = max(LCSlen(s1, s2, i + 1, j), LCSlen(s1, s2, i, j + 1)); } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(memo, -1, sizeof(memo)); string s1, s2; cin >> s1 >> s2; int len = LCSlen(s1, s2, 0, 0); // cout << len << endl; cout << LCSstr(s1, s2, 0, 0, len); return 0; } // 3 8 // 3 30 // 4 50 // 5 60
#include <bits/stdc++.h> using namespace std; #define int long long int int mod = INT_MAX; int memo[3005][3005]; int n; string LCSstr(string &s1, string &s2, int i, int j, int len) { // base cases if ((i >= s1.size()) or (j >= s2.size()) or (len == 0)) return ""; // selfwork string ans; while (len > 0) { if (s1[i] == s2[j]) { ans.push_back(s1[i]); i++; j++; len--; } else { if (memo[i + 1][j] > memo[i][j + 1]) i++; else j++; } } return ans; } int LCSlen(string &s1, string &s2, int i, int j) // pass string by reference { // base cases if ((i >= s1.size()) or (j >= s2.size())) return 0; int &ans = memo[i][j]; // memoizatiion if (ans != -1) return ans; // self work if (s1[i] == s2[j]) return ans = 1 + LCSlen(s1, s2, i + 1, j + 1); else return ans = max(LCSlen(s1, s2, i + 1, j), LCSlen(s1, s2, i, j + 1)); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(memo, -1, sizeof(memo)); string s1, s2; cin >> s1 >> s2; int len = LCSlen(s1, s2, 0, 0); // cout << len << endl; cout << LCSstr(s1, s2, 0, 0, len); return 0; } // 3 8 // 3 30 // 4 50 // 5 60
delete
44
48
44
44
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAXN = 3007; int dp[MAXN][MAXN]; char s[MAXN], t[MAXN]; int main() { scanf(" %s", s + 1); scanf(" %s", t + 1); int n = strlen(s + 1), m = strlen(t + 1); for (int i = 1; i <= n / 2; i++) swap(s[i], s[n + 1 - i]); for (int j = 1; j <= m / 2; j++) swap(t[j], t[m + 1 - j]); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) dp[i][j] = s[i] == t[j] ? dp[i - 1][j - 1] + 1 : max(dp[i][j - 1], dp[i - 1][j]); } for (int i = n, lst = m; i >= 1; --i) for (int j = lst; j >= 1; --j) { if (dp[i][j] == dp[i - 1][j - 1] + 1) { assert(s[i] == t[j]); putchar(t[j]); lst = j - 1; break; } } puts(""); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 3007; int dp[MAXN][MAXN]; char s[MAXN], t[MAXN]; int main() { scanf(" %s", s + 1); scanf(" %s", t + 1); int n = strlen(s + 1), m = strlen(t + 1); for (int i = 1; i <= n / 2; i++) swap(s[i], s[n + 1 - i]); for (int j = 1; j <= m / 2; j++) swap(t[j], t[m + 1 - j]); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) dp[i][j] = s[i] == t[j] ? dp[i - 1][j - 1] + 1 : max(dp[i][j - 1], dp[i - 1][j]); } int i = n, j = m; while (i && j) { if (s[i] == t[j]) putchar(t[j]), i--, j--; else dp[i - 1][j] > dp[i][j - 1] ? i-- : j--; } puts(""); return 0; }
replace
18
27
18
25
-6
d2fe1de6-9a29-42b1-9b91-8c5d4a81d0ce.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03165/C++/s668438060.cpp:22: int main(): Assertion `s[i] == t[j]' failed.
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; // typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> pbds; #define int long long #define pb push_back #define inf 1e9 #define mod 1000000007 const int maxn = 3010; int dp[maxn][maxn]; string getLCS(string &s, string &t, int len) { string ans; int i = 0, j = 0; while (len > 0) { if (s[i] == t[j]) { ans.push_back(s[i]); i++; j++; len--; } else { if (dp[i][j + 1] > dp[i + 1][j]) j++; else i++; } } return ans; } int lenLCS(string s, string t, int i, int j) { if (i >= s.length() or j >= t.length()) { return 0; // one of the string is empty } 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 + 1, j), lenLCS(s, t, i, j + 1)); } } string fun(string s, string t) { memset(dp, -1, sizeof(dp)); int len = lenLCS(s, t, 0, 0); return getLCS(s, t, len); } void solve() { string s, t; cin >> s >> t; cout << fun(s, t); } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; // typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> pbds; #define int long long #define pb push_back #define inf 1e9 #define mod 1000000007 const int maxn = 3010; int dp[maxn][maxn]; string getLCS(string &s, string &t, int len) { string ans; int i = 0, j = 0; while (len > 0) { if (s[i] == t[j]) { ans.push_back(s[i]); i++; j++; len--; } else { if (dp[i][j + 1] > dp[i + 1][j]) j++; else i++; } } return ans; } int lenLCS(string &s, string &t, int i, int j) { if (i >= s.length() or j >= t.length()) { return 0; // one of the string is empty } 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 + 1, j), lenLCS(s, t, i, j + 1)); } } string fun(string s, string t) { memset(dp, -1, sizeof(dp)); int len = lenLCS(s, t, 0, 0); return getLCS(s, t, len); } void solve() { string s, t; cin >> s >> t; cout << fun(s, t); } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }
replace
33
34
33
34
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back using namespace std; int32_t main() { string a, b; cin >> a >> b; int n = a.length(); int m = b.length(); vector<vector<int>> d(n + 1, vector<int>(m + 1)); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { d[i][j] = 0; } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { d[i][j] = 0; if (i == 0 || j == 0) d[i][j] = 0; else if (a[i - 1] == b[j - 1]) { d[i][j] += d[i - 1][j - 1] + 1; } else { if (d[i - 1][j] >= d[i][j - 1]) d[i][j] += d[i - 1][j]; else d[i][j] += d[i][j - 1]; } } } int ind = d[n][m]; char ans[ind + 1]; int i = n; int j = m; ans[ind] = '\0'; while (i >= 0 && j >= 0) { if (a[i - 1] == b[j - 1]) { ans[ind - 1] = a[i - 1]; ind--; i--; j--; } else if (d[i - 1][j] > d[i][j - 1]) i--; else j--; } cout << ans; }
#include <bits/stdc++.h> #define pb push_back using namespace std; int32_t main() { string a, b; cin >> a >> b; int n = a.length(); int m = b.length(); vector<vector<int>> d(n + 1, vector<int>(m + 1)); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { d[i][j] = 0; } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { d[i][j] = 0; if (i == 0 || j == 0) d[i][j] = 0; else if (a[i - 1] == b[j - 1]) { d[i][j] += d[i - 1][j - 1] + 1; } else { if (d[i - 1][j] >= d[i][j - 1]) d[i][j] += d[i - 1][j]; else d[i][j] += d[i][j - 1]; } } } int ind = d[n][m]; char ans[ind + 1]; int i = n; int j = m; ans[ind] = '\0'; while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { ans[ind - 1] = a[i - 1]; ind--; i--; j--; } else if (d[i - 1][j] > d[i][j - 1]) i--; else j--; } cout << ans; }
replace
39
40
39
40
0
p03165
C++
Time Limit Exceeded
/* * * * * * * * * * * ** * * * saurabh8522 * * I will handle * * IT. * * * * * * * * * * * * * * **/ #include <bits/stdc++.h> #define mp(a, b) make_pair(a, b) #define pb push_back #define FastRead \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl '\n' #define ld long double #define zero(a) memset((a), 0, sizeof((a))) #define one(a) memset((a), 1, sizeof((a))) #define minus(a) memset((a), -1, sizeof((a))) #define all(g) g.begin(), g.end() #define ppb pop_back using namespace std; typedef long long int ll; #define MOD 1000000007 ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } else { int g = extgcd(b, a % b, y, x); y -= a / b * x; return g; } } ll modpow(ll a, ll b) { ll res = 1; a %= MOD; for (; b; b >>= 1) { if (b & 1) res = res * a % MOD; a = a * a % MOD; } return res; } ll numdigit(ll n) { return floor(log10(n)) + 1; } bool isPowerTwo(ll x) { return x && (!(x & (x - 1))); } // ll dp[3001][3001]; // ll find(string &s,string &t,ll i,ll j){ // if(i<0||j<0) return 0; // ll &ans=dp[i][j]; // if(ans!=-1) return ans; // ans=0; // if(s[i]==t[j]){ // ans=1+find(s,t,i-1,j-1); // } // else ans=max(ans,max(find(s,t,i,j-1),find(s,t,i-1,j))); // return ans; // } int main() { FastRead; ll t = 1; // cin>>t; while (t--) { string s, u; cin >> s >> u; // cout<<s<<" "<<u<<endl; ll n = s.size(), m = u.size(); // cout<<n<<" "<<m<<endl; ll dp[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { dp[i][j] = 0; } } // minus(dp); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i] == u[j]) { dp[i][j] = (i == 0 || j == 0) ? 1 : dp[i - 1][j - 1] + 1; } else { dp[i][j] = max((i == 0) ? 0 : dp[i - 1][j], (j == 0) ? 0 : dp[i][j - 1]); } // cout<<i<<" "<<j<<endl; } } // find(s,u,n-1,m-1); string ans = ""; int i = n - 1, j = m - 1; // cout<<dp[n-1][m-1]<<endl; while (i >= 0 && j >= 0) { if (s[i] == u[j]) { ans += s[i]; i--; j--; continue; } if (i == 0 && j == 0) { break; } if (i > 0) { if (dp[i][j] == dp[i - 1][j]) { i--; } } else { j--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; } }
/* * * * * * * * * * * ** * * * saurabh8522 * * I will handle * * IT. * * * * * * * * * * * * * * **/ #include <bits/stdc++.h> #define mp(a, b) make_pair(a, b) #define pb push_back #define FastRead \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define endl '\n' #define ld long double #define zero(a) memset((a), 0, sizeof((a))) #define one(a) memset((a), 1, sizeof((a))) #define minus(a) memset((a), -1, sizeof((a))) #define all(g) g.begin(), g.end() #define ppb pop_back using namespace std; typedef long long int ll; #define MOD 1000000007 ll extgcd(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } else { int g = extgcd(b, a % b, y, x); y -= a / b * x; return g; } } ll modpow(ll a, ll b) { ll res = 1; a %= MOD; for (; b; b >>= 1) { if (b & 1) res = res * a % MOD; a = a * a % MOD; } return res; } ll numdigit(ll n) { return floor(log10(n)) + 1; } bool isPowerTwo(ll x) { return x && (!(x & (x - 1))); } // ll dp[3001][3001]; // ll find(string &s,string &t,ll i,ll j){ // if(i<0||j<0) return 0; // ll &ans=dp[i][j]; // if(ans!=-1) return ans; // ans=0; // if(s[i]==t[j]){ // ans=1+find(s,t,i-1,j-1); // } // else ans=max(ans,max(find(s,t,i,j-1),find(s,t,i-1,j))); // return ans; // } int main() { FastRead; ll t = 1; // cin>>t; while (t--) { string s, u; cin >> s >> u; // cout<<s<<" "<<u<<endl; ll n = s.size(), m = u.size(); // cout<<n<<" "<<m<<endl; ll dp[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { dp[i][j] = 0; } } // minus(dp); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i] == u[j]) { dp[i][j] = (i == 0 || j == 0) ? 1 : dp[i - 1][j - 1] + 1; } else { dp[i][j] = max((i == 0) ? 0 : dp[i - 1][j], (j == 0) ? 0 : dp[i][j - 1]); } // cout<<i<<" "<<j<<endl; } } // find(s,u,n-1,m-1); string ans = ""; int i = n - 1, j = m - 1; // cout<<dp[n-1][m-1]<<endl; while (i >= 0 && j >= 0) { if (s[i] == u[j]) { ans += s[i]; i--; j--; continue; } if (i == 0 && j == 0) { break; } // if(i>0){ if (dp[i][j] == dp[i - 1][j] && i > 0) { i--; } // } else { j--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; } }
replace
102
107
102
108
TLE
p03165
C++
Memory Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstdint> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <sys/timeb.h> #include <vector> using namespace std; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define reprrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define reprev(i, n) reprrev(i, 0, n) #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define chmin(mi, value) mi = min(mi, value) #define chmax(ma, value) ma = max(ma, value) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define mp make_pair #define mt make_tuple #define INF 1050000000 #define INFR INT_MAX #define INFL (long long)(4e18) #define INFLR LLONG_MAX #define EPS (1e-10) #define MOD 1000000007 // #define MOD 998244353 #define PI 3.141592653589793238 #define RMAX 4294967295 using vi = vector<int>; using vvi = vector<vector<int>>; using vvvi = vector<vector<vector<int>>>; using vvvvi = vector<vector<vector<vector<int>>>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using vvvll = vector<vector<vector<ll>>>; using vd = vector<double>; using vvd = vector<vector<double>>; using vb = vector<bool>; using vvb = vector<vector<bool>>; using vc = vector<char>; using vvc = vector<vector<char>>; using vs = vector<string>; using vvs = vector<vector<string>>; using Pi = pair<int, int>; using vPi = vector<Pi>; using vvPi = vector<vector<Pi>>; using vvvPi = vector<vector<vector<Pi>>>; using vvvvPi = vector<vector<vector<vector<Pi>>>>; using Pll = pair<ll, ll>; using vPll = vector<Pll>; using Pd = pair<double, double>; using vPd = vector<Pd>; template <class T> using vec = vector<T>; template <class T> using pql = priority_queue<T, vector<T>, greater<T>>; using Comp = complex<double>; // vvvvvvvvvvvvvvvvvvvvvvv debug output vvvvvvvvvvvvvvvvvvvvvvv // vector input template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // pair template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // vector template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // deque template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // map template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &map_var) { os << "{"; repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } // multiset template <typename T> ostream &operator<<(ostream &os, const multiset<T> &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif // ^^^^^^^^^^^^^^^^^^^^^^^ debug output ^^^^^^^^^^^^^^^^^^^^^^^ string YN(bool y, int id = 0) { if (id) cout << id; return (y ? "YES" : "NO"); } string yn(bool y, int id = 0) { if (id) cout << id; return (y ? "Yes" : "No"); } string ON(bool y, int id = 0) { if (id) cout << id; return (y ? "OK" : "NG"); } int dir4[4][2] = {{0, -1}, {-1, 0}, {1, 0}, {0, 1}}; int dir8[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}}; char dirchar[4] = {'<', '^', '>', 'v'}; // [a,b) int irand(int a, int b) { static mt19937 Rand(static_cast<unsigned int>(time(nullptr))); uniform_int_distribution<int> dist(a, b - 1); return dist(Rand); } // [a,b) double drand(int a, int b) { static mt19937 Rand(static_cast<unsigned int>(time(nullptr))); uniform_real_distribution<double> dist(a, b); return dist(Rand); } // https://qiita.com/IgnorantCoder/items/3101d6276e9bdddf872c template <typename A, typename F> inline auto transform(const A &v, F &&f) { using result_type = decltype(std::declval<F>()(std::declval<typename A::value_type>())); vector<result_type> y(v.size()); std::transform(std::cbegin(v), std::cend(v), std::begin(y), f); return y; } // generate vector which has multiple dimension template <class T> vector<T> make_v(size_t size, const T &init) { return vector<T>(size, init); } template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); } template <typename T> T Max(vector<T> a) { return *max_element(all(a)); } template <typename T> T Min(vector<T> a) { return *min_element(all(a)); } template <typename T> T Sum(vector<T> a) { return accumulate(all(a), (T)0); } // for counting using map template <typename T> void Add(map<T, int> &m, T item) { if (m.find(item) == m.end()) { m[item] = 1; } else { m[item]++; } } // for counting using map template <typename T> void Erase(map<T, int> &m, T item) { if (m.find(item) == m.end()) { } else { if (m[item] == 1) { m.erase(item); } else { m[item]--; } } } // get method for map with default value template <typename T, typename U> U Get(map<T, U> m, T key, U def) { if (m.find(key) == m.end()) { return def; } else { return m[key]; } } template <typename T> inline bool Contains(const set<T> &t, const T &key) { return t.find(key) != t.end(); } template <typename T, typename U> inline bool Contains(const map<T, U> &t, const T &key) { return t.find(key) != t.end(); } template <class T> struct Edge { int from, to; T cost; Edge(int f, int t, T c) : from(f), to(t), cost(c) {} }; template <class T> bool operator<(const Edge<T> e1, const Edge<T> e2) { return e1.cost < e2.cost || (e1.cost == e2.cost && e1.from < e2.from) || (e1.cost == e2.cost && e1.from == e2.from && e1.to < e2.to); } template <class T> ostream &operator<<(ostream &os, const Edge<T> &edge) { os << "(" << edge.from << "->" << edge.to << ":" << edge.cost << ")"; return os; } template <class T = int> class Graph { int n; bool directed; vector<vector<Edge<T>>> edges; public: Graph(int n, bool directed) : n(n), directed(directed), edges(vector<vector<Edge<T>>>(n)) {} void add_edge(int s, int t, T cost) { edges[s].emplace_back(s, t, cost); if (!directed) { edges[t].emplace_back(t, s, cost); } } Graph() {} vector<Edge<T>> &operator[](size_t i) { return edges[i]; } int size() const { return n; } }; //====================================================== int main() { string S, T; cin >> S >> T; int NS = S.size(), NT = T.size(); vvs dp(NS + 1, vs(NT + 1, "")); repr(i, 1, NS + 1) { repr(j, 1, NT + 1) { if (S[i - 1] == T[j - 1]) { dp[i][j] = dp[i - 1][j]; if (dp[i][j - 1].size() > dp[i][j].size()) { dp[i][j] = dp[i][j - 1]; } if (dp[i - 1][j - 1].size() + 1 > dp[i][j].size()) { dp[i][j] = dp[i - 1][j - 1] + S[i - 1]; } } else { dp[i][j] = dp[i - 1][j]; if (dp[i][j - 1].size() > dp[i][j].size()) { dp[i][j] = dp[i][j - 1]; } } } } cout << dp[NS][NT] << endl; return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstdint> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <sys/timeb.h> #include <vector> using namespace std; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define reprrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define reprev(i, n) reprrev(i, 0, n) #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define chmin(mi, value) mi = min(mi, value) #define chmax(ma, value) ma = max(ma, value) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define mp make_pair #define mt make_tuple #define INF 1050000000 #define INFR INT_MAX #define INFL (long long)(4e18) #define INFLR LLONG_MAX #define EPS (1e-10) #define MOD 1000000007 // #define MOD 998244353 #define PI 3.141592653589793238 #define RMAX 4294967295 using vi = vector<int>; using vvi = vector<vector<int>>; using vvvi = vector<vector<vector<int>>>; using vvvvi = vector<vector<vector<vector<int>>>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using vvvll = vector<vector<vector<ll>>>; using vd = vector<double>; using vvd = vector<vector<double>>; using vb = vector<bool>; using vvb = vector<vector<bool>>; using vc = vector<char>; using vvc = vector<vector<char>>; using vs = vector<string>; using vvs = vector<vector<string>>; using Pi = pair<int, int>; using vPi = vector<Pi>; using vvPi = vector<vector<Pi>>; using vvvPi = vector<vector<vector<Pi>>>; using vvvvPi = vector<vector<vector<vector<Pi>>>>; using Pll = pair<ll, ll>; using vPll = vector<Pll>; using Pd = pair<double, double>; using vPd = vector<Pd>; template <class T> using vec = vector<T>; template <class T> using pql = priority_queue<T, vector<T>, greater<T>>; using Comp = complex<double>; // vvvvvvvvvvvvvvvvvvvvvvv debug output vvvvvvvvvvvvvvvvvvvvvvv // vector input template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } // pair template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // vector template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // deque template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // map template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &map_var) { os << "{"; repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } // multiset template <typename T> ostream &operator<<(ostream &os, const multiset<T> &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif // ^^^^^^^^^^^^^^^^^^^^^^^ debug output ^^^^^^^^^^^^^^^^^^^^^^^ string YN(bool y, int id = 0) { if (id) cout << id; return (y ? "YES" : "NO"); } string yn(bool y, int id = 0) { if (id) cout << id; return (y ? "Yes" : "No"); } string ON(bool y, int id = 0) { if (id) cout << id; return (y ? "OK" : "NG"); } int dir4[4][2] = {{0, -1}, {-1, 0}, {1, 0}, {0, 1}}; int dir8[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}}; char dirchar[4] = {'<', '^', '>', 'v'}; // [a,b) int irand(int a, int b) { static mt19937 Rand(static_cast<unsigned int>(time(nullptr))); uniform_int_distribution<int> dist(a, b - 1); return dist(Rand); } // [a,b) double drand(int a, int b) { static mt19937 Rand(static_cast<unsigned int>(time(nullptr))); uniform_real_distribution<double> dist(a, b); return dist(Rand); } // https://qiita.com/IgnorantCoder/items/3101d6276e9bdddf872c template <typename A, typename F> inline auto transform(const A &v, F &&f) { using result_type = decltype(std::declval<F>()(std::declval<typename A::value_type>())); vector<result_type> y(v.size()); std::transform(std::cbegin(v), std::cend(v), std::begin(y), f); return y; } // generate vector which has multiple dimension template <class T> vector<T> make_v(size_t size, const T &init) { return vector<T>(size, init); } template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); } template <typename T> T Max(vector<T> a) { return *max_element(all(a)); } template <typename T> T Min(vector<T> a) { return *min_element(all(a)); } template <typename T> T Sum(vector<T> a) { return accumulate(all(a), (T)0); } // for counting using map template <typename T> void Add(map<T, int> &m, T item) { if (m.find(item) == m.end()) { m[item] = 1; } else { m[item]++; } } // for counting using map template <typename T> void Erase(map<T, int> &m, T item) { if (m.find(item) == m.end()) { } else { if (m[item] == 1) { m.erase(item); } else { m[item]--; } } } // get method for map with default value template <typename T, typename U> U Get(map<T, U> m, T key, U def) { if (m.find(key) == m.end()) { return def; } else { return m[key]; } } template <typename T> inline bool Contains(const set<T> &t, const T &key) { return t.find(key) != t.end(); } template <typename T, typename U> inline bool Contains(const map<T, U> &t, const T &key) { return t.find(key) != t.end(); } template <class T> struct Edge { int from, to; T cost; Edge(int f, int t, T c) : from(f), to(t), cost(c) {} }; template <class T> bool operator<(const Edge<T> e1, const Edge<T> e2) { return e1.cost < e2.cost || (e1.cost == e2.cost && e1.from < e2.from) || (e1.cost == e2.cost && e1.from == e2.from && e1.to < e2.to); } template <class T> ostream &operator<<(ostream &os, const Edge<T> &edge) { os << "(" << edge.from << "->" << edge.to << ":" << edge.cost << ")"; return os; } template <class T = int> class Graph { int n; bool directed; vector<vector<Edge<T>>> edges; public: Graph(int n, bool directed) : n(n), directed(directed), edges(vector<vector<Edge<T>>>(n)) {} void add_edge(int s, int t, T cost) { edges[s].emplace_back(s, t, cost); if (!directed) { edges[t].emplace_back(t, s, cost); } } Graph() {} vector<Edge<T>> &operator[](size_t i) { return edges[i]; } int size() const { return n; } }; //====================================================== int main() { string S, T; cin >> S >> T; int NS = S.size(), NT = T.size(); vvs dp(NS + 1, vs(NT + 1, "")); repr(i, 1, NS + 1) { if (i >= 2) { dp[i - 2].clear(); } repr(j, 1, NT + 1) { if (S[i - 1] == T[j - 1]) { dp[i][j] = dp[i - 1][j]; if (dp[i][j - 1].size() > dp[i][j].size()) { dp[i][j] = dp[i][j - 1]; } if (dp[i - 1][j - 1].size() + 1 > dp[i][j].size()) { dp[i][j] = dp[i - 1][j - 1] + S[i - 1]; } } else { dp[i][j] = dp[i - 1][j]; if (dp[i][j - 1].size() > dp[i][j].size()) { dp[i][j] = dp[i][j - 1]; } } } } cout << dp[NS][NT] << endl; return 0; }
insert
314
314
314
317
MLE
p03165
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> std::string s, t; int dp[3001][3001]; int main() { std::cin >> s >> t; int n = s.length(); int m = t.length(); 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] = std::max(dp[i - 1][j], dp[i][j - 1]); } } std::string ans; while (n | m) { if (s[n - 1] == t[m - 1]) ans.push_back(s[n - 1]), n--, m--; else if (dp[n - 1][m] > dp[n][m - 1]) n--; else m--; } std::reverse(ans.begin(), ans.end()); std::cout << ans << std::endl; return 0; }
#include <algorithm> #include <iostream> #include <string> std::string s, t; int dp[3001][3001]; int main() { std::cin >> s >> t; int n = s.length(); int m = t.length(); 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] = std::max(dp[i - 1][j], dp[i][j - 1]); } } std::string ans; while (n && m) { if (s[n - 1] == t[m - 1]) ans.push_back(s[n - 1]), n--, m--; else if (dp[n - 1][m] > dp[n][m - 1]) n--; else m--; } std::reverse(ans.begin(), ans.end()); std::cout << ans << std::endl; return 0; }
replace
23
24
23
24
0
p03165
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <stack> #include <string> #include <vector> #define Rep(i, n) for (int i = 0; i < (n); ++i) #define Repd(i, n) for (int i = (n)-1; i >= 0; --i) #define For(i, a, b) for (int i = (a); i <= (b); ++i) #define Ford(i, a, b) for (int i = (a); i >= (b); --i) #define ALL(a) (a).begin(), (a).end() #define taomap map<ll, int> mmap #define Upper(k) upper_bound(a, a + n, (k)) #define Lower(k) lower_bound(a, a + n, (k)) #define pb(i) push_back((i)) #define NAME "TEST" #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define READ freopen(NAME ".INP", "r", stdin); #define WRITE freopen(NAME ".OUT", "w", stdout); using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef float fl; typedef double db; const int inf = 1e6; const int Linf = 1e9; const ll LLinf = (ll)1e18; const int maxn = 1e3 + 5; /* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Be amazed ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */ int a[maxn][maxn], i, n, j, m; string x, y; stack<char> dapso; int commonlength() { for (int i = 0; i <= x.size(); ++i) { for (int j = 0; j <= y.size(); ++j) { if (i == 0 || j == 0) a[i][j] = 0; else a[i][j] = (x[i - 1] == y[j - 1]) ? a[i - 1][j - 1] + 1 : max(a[i][j - 1], a[i - 1][j]); } } // cout << a[x.size()][y.size()] << endl; return a[x.size()][y.size()]; } int main() { FAST; // READ;WRITE; int k; int n, m; // cin >> n >> m >> k; cin >> x >> y; n = commonlength(); // return cout << n , 0; i = x.size(); j = y.size(); // cout << n << endl; // return 0; while (i > 0 && j > 0) { if (x[i - 1] == y[j - 1]) { dapso.push(x[i - 1]); i--; j--; } else { if (a[i - 1][j] > a[i][j - 1]) i--; else j--; } } while (!dapso.empty()) cout << dapso.top(), dapso.pop(); }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <stack> #include <string> #include <vector> #define Rep(i, n) for (int i = 0; i < (n); ++i) #define Repd(i, n) for (int i = (n)-1; i >= 0; --i) #define For(i, a, b) for (int i = (a); i <= (b); ++i) #define Ford(i, a, b) for (int i = (a); i >= (b); --i) #define ALL(a) (a).begin(), (a).end() #define taomap map<ll, int> mmap #define Upper(k) upper_bound(a, a + n, (k)) #define Lower(k) lower_bound(a, a + n, (k)) #define pb(i) push_back((i)) #define NAME "TEST" #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define READ freopen(NAME ".INP", "r", stdin); #define WRITE freopen(NAME ".OUT", "w", stdout); using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef float fl; typedef double db; const int inf = 1e6; const int Linf = 1e9; const ll LLinf = (ll)1e18; const int maxn = 3e3 + 5; /* ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Be amazed ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */ int a[maxn][maxn], i, n, j, m; string x, y; stack<char> dapso; int commonlength() { for (int i = 0; i <= x.size(); ++i) { for (int j = 0; j <= y.size(); ++j) { if (i == 0 || j == 0) a[i][j] = 0; else a[i][j] = (x[i - 1] == y[j - 1]) ? a[i - 1][j - 1] + 1 : max(a[i][j - 1], a[i - 1][j]); } } // cout << a[x.size()][y.size()] << endl; return a[x.size()][y.size()]; } int main() { FAST; // READ;WRITE; int k; int n, m; // cin >> n >> m >> k; cin >> x >> y; n = commonlength(); // return cout << n , 0; i = x.size(); j = y.size(); // cout << n << endl; // return 0; while (i > 0 && j > 0) { if (x[i - 1] == y[j - 1]) { dapso.push(x[i - 1]); i--; j--; } else { if (a[i - 1][j] > a[i][j - 1]) i--; else j--; } } while (!dapso.empty()) cout << dapso.top(), dapso.pop(); }
replace
38
39
38
39
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r+", stdin); // for writing output to output.txt freopen("output.txt", "w+", stdout); #endif string a, b; cin >> a >> b; int n = a.length(); int m = b.length(); ll dp[n + 1][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 (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]); } } } // for(int i=0;i<=n;i++) { // for(int j=0;j<=m;j++) { // cout << dp[i][j] << " "; // } // cout << '\n'; // } // cout << dp[n][m] << '\n'; string c = ""; while (n > 0 and m > 0) { // cout << n << " " << m << '\n'; if (a[n - 1] == b[m - 1]) { c = a[n - 1] + c; m--; n--; // continue; } else if (dp[n - 1][m] > dp[n][m - 1]) { n--; // continue; } else { m--; // continue; } } cout << c; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair int main() { ios_base::sync_with_stdio(0); cin.tie(0); // #ifndef ONLINE_JUDGE // // for getting input from input.txt // freopen("input.txt", "r+", stdin); // // for writing output to output.txt // freopen("output.txt", "w+", stdout); // #endif string a, b; cin >> a >> b; int n = a.length(); int m = b.length(); ll dp[n + 1][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 (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]); } } } // for(int i=0;i<=n;i++) { // for(int j=0;j<=m;j++) { // cout << dp[i][j] << " "; // } // cout << '\n'; // } // cout << dp[n][m] << '\n'; string c = ""; while (n > 0 and m > 0) { // cout << n << " " << m << '\n'; if (a[n - 1] == b[m - 1]) { c = a[n - 1] + c; m--; n--; // continue; } else if (dp[n - 1][m] > dp[n][m - 1]) { n--; // continue; } else { m--; // continue; } } cout << c; }
replace
11
17
11
17
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++) #define brep(i, a, b) for (ll i = (a), i##_len = (b); i <= i##_len; i++) #define rrep(i, n) \ ll i = (n); \ ll now = -1; \ while (i-- && (now++ || 1)) #define xrep(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; i++) #define Yes(n) cout << ((n) ? YES : NO) << '\n' #define rYes(n) \ cout << ((n) ? YES : NO) << '\n'; \ return #define co(n) cout << (n) << '\n' #define rco(n) \ cout << (n) << '\n'; \ return #define Sort(v) sort(all(v)) #define rSort(v) \ sort(all(v)); \ reverse(all(v)) #define Unique(v) (v).erase(unique(all(v)), (v).end()) #define eb emplace_back using namespace std; using ll = long long; using vll = vector<ll>; using vbl = vector<bool>; using vint = vector<int>; using vvll = vector<vll>; using vstr = vector<string>; const ll INF = 1e18; // --- functions which take 1 argument --- static inline void ignore_ret(int x) { x++; } template <class T> inline int sgn(T x) { return (x > 0) - (x < 0); } template <class T> inline ll factorial(T n) { if (n == 0) return 1; ll r = 1; for (T i = 2; i <= n; i++) r *= i; return r; } template <class T> inline map<T, T> factorize(T n) { map<T, T> r; for (T i = 2; i * i <= n; i++) { while (n % i == 0) { r[i]++; n /= i; } } if (n != 1) r[n] = 1; return r; } template <class T> inline vector<T> divisor(T n) { vector<T> r; for (T i = 1; i * i <= n; i++) { if (!(n % i)) { r.eb(i); if (i * i != n) r.eb(n / i); } } Sort(r); return r; } // --- functions which take 2 arguments --- template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline T nPr(T n, T k) { if (n < k || n < 0 || k < 0) return 0; T r = 1; for (T i = n - k + 1; i <= n; i++) r *= i; return r; } template <class T> inline T nCr(T n, T k) { if (n < k || n < 0 || k < 0) return 0; T r = 1; k = min(k, n - k); for (T i = n - k + 1; i <= n; i++) r *= i; return r / factorial(k); } // --- functions which take vector(s) as argument(s) --- template <class T> inline void print(T v) { cout << "[ "; for (auto x : v) cout << x << ' '; cout << "]\n"; } template <class T> inline void print_d(T v) { for (auto x : v) cout << x; cout << '\n'; } template <class T> inline void print_all(T v) { for (auto x : v) cout << x << '\n'; } template <class T> inline T min(vector<T> v) { return *min_element(all(v)); } template <class T> inline T max(vector<T> v) { return *max_element(all(v)); } template <class T> inline ll sum(T v) { return reduce(all(v), 0LL); } template <class T> inline T gcd(vector<T> v) { T r = v[0]; for (T i = 1; i < v.size(); i++) r = gcd(r, v[i]); return r; } template <class T> inline T lcm(vector<T> v) { T r = v[0]; for (T i = 1; i < v.size(); i++) r = lcm(r, v[i]); return r; } template <class T> inline double abs(vector<T> v) { return sqrt(reduce(all(v), 0.0, [](T s, T v) { return s += v * v; })); } template <class T> inline T vector_add(T u, T v) { T r; rep(i, u.size()) r.eb(u[i] + v[i]); return r; } template <class T> inline T vector_subtract(T u, T v) { T r; rep(i, u.size()) r.eb(u[i] - v[i]); return r; } template <class T> inline T dot_product(vector<T> u, vector<T> v) { if (u.size() < v.size()) return 0; T r = 0; for (T i = 0; i < u.size(); i++) r += u[i] * v[i]; return r; } template <class T> inline T cross_product(T u, T v) { T r(3); r[0] = u[1] * v[2] - u[2] * v[1]; r[1] = u[2] * v[0] - u[0] * v[2]; r[2] = u[0] * v[1] - u[1] * v[0]; return r; } // --- functions which take set(s) as argument(s) --- template <class T> inline T min(set<T> v) { return *min_element(all(v)); } template <class T> inline T max(set<T> v) { return *max_element(all(v)); } template <class T> inline T gcd(set<T> v) { T r = 0; for (T x : v) r = (r == 0) ? x : gcd(r, x); return r; } template <class T> inline T lcm(set<T> v) { T r = 0; for (T x : v) r = (r == 0) ? x : lcm(r, x); return r; } // --- Template ends here --- void solve(string s, string t) { vvll dp(15, vll(15, 0)); brep(i, 1, s.size()) { brep(j, 1, t.size()) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (s[i - 1] == t[j - 1]) { chmax(dp[i][j], dp[i - 1][j - 1] + 1); } } } // for (auto x: dp) print(x); string ans = ""; ll i = s.size(), j = t.size(); while (i >= 1 && j >= 1) { 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--; } } reverse(all(ans)); cout << ans << '\n'; } int main() { string s; cin >> s; string t; cin >> t; solve(s, t); return 0; }
#include <bits/stdc++.h> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++) #define brep(i, a, b) for (ll i = (a), i##_len = (b); i <= i##_len; i++) #define rrep(i, n) \ ll i = (n); \ ll now = -1; \ while (i-- && (now++ || 1)) #define xrep(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; i++) #define Yes(n) cout << ((n) ? YES : NO) << '\n' #define rYes(n) \ cout << ((n) ? YES : NO) << '\n'; \ return #define co(n) cout << (n) << '\n' #define rco(n) \ cout << (n) << '\n'; \ return #define Sort(v) sort(all(v)) #define rSort(v) \ sort(all(v)); \ reverse(all(v)) #define Unique(v) (v).erase(unique(all(v)), (v).end()) #define eb emplace_back using namespace std; using ll = long long; using vll = vector<ll>; using vbl = vector<bool>; using vint = vector<int>; using vvll = vector<vll>; using vstr = vector<string>; const ll INF = 1e18; // --- functions which take 1 argument --- static inline void ignore_ret(int x) { x++; } template <class T> inline int sgn(T x) { return (x > 0) - (x < 0); } template <class T> inline ll factorial(T n) { if (n == 0) return 1; ll r = 1; for (T i = 2; i <= n; i++) r *= i; return r; } template <class T> inline map<T, T> factorize(T n) { map<T, T> r; for (T i = 2; i * i <= n; i++) { while (n % i == 0) { r[i]++; n /= i; } } if (n != 1) r[n] = 1; return r; } template <class T> inline vector<T> divisor(T n) { vector<T> r; for (T i = 1; i * i <= n; i++) { if (!(n % i)) { r.eb(i); if (i * i != n) r.eb(n / i); } } Sort(r); return r; } // --- functions which take 2 arguments --- template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline T nPr(T n, T k) { if (n < k || n < 0 || k < 0) return 0; T r = 1; for (T i = n - k + 1; i <= n; i++) r *= i; return r; } template <class T> inline T nCr(T n, T k) { if (n < k || n < 0 || k < 0) return 0; T r = 1; k = min(k, n - k); for (T i = n - k + 1; i <= n; i++) r *= i; return r / factorial(k); } // --- functions which take vector(s) as argument(s) --- template <class T> inline void print(T v) { cout << "[ "; for (auto x : v) cout << x << ' '; cout << "]\n"; } template <class T> inline void print_d(T v) { for (auto x : v) cout << x; cout << '\n'; } template <class T> inline void print_all(T v) { for (auto x : v) cout << x << '\n'; } template <class T> inline T min(vector<T> v) { return *min_element(all(v)); } template <class T> inline T max(vector<T> v) { return *max_element(all(v)); } template <class T> inline ll sum(T v) { return reduce(all(v), 0LL); } template <class T> inline T gcd(vector<T> v) { T r = v[0]; for (T i = 1; i < v.size(); i++) r = gcd(r, v[i]); return r; } template <class T> inline T lcm(vector<T> v) { T r = v[0]; for (T i = 1; i < v.size(); i++) r = lcm(r, v[i]); return r; } template <class T> inline double abs(vector<T> v) { return sqrt(reduce(all(v), 0.0, [](T s, T v) { return s += v * v; })); } template <class T> inline T vector_add(T u, T v) { T r; rep(i, u.size()) r.eb(u[i] + v[i]); return r; } template <class T> inline T vector_subtract(T u, T v) { T r; rep(i, u.size()) r.eb(u[i] - v[i]); return r; } template <class T> inline T dot_product(vector<T> u, vector<T> v) { if (u.size() < v.size()) return 0; T r = 0; for (T i = 0; i < u.size(); i++) r += u[i] * v[i]; return r; } template <class T> inline T cross_product(T u, T v) { T r(3); r[0] = u[1] * v[2] - u[2] * v[1]; r[1] = u[2] * v[0] - u[0] * v[2]; r[2] = u[0] * v[1] - u[1] * v[0]; return r; } // --- functions which take set(s) as argument(s) --- template <class T> inline T min(set<T> v) { return *min_element(all(v)); } template <class T> inline T max(set<T> v) { return *max_element(all(v)); } template <class T> inline T gcd(set<T> v) { T r = 0; for (T x : v) r = (r == 0) ? x : gcd(r, x); return r; } template <class T> inline T lcm(set<T> v) { T r = 0; for (T x : v) r = (r == 0) ? x : lcm(r, x); return r; } // --- Template ends here --- void solve(string s, string t) { vvll dp(3300, vll(3300, 0)); brep(i, 1, s.size()) { brep(j, 1, t.size()) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (s[i - 1] == t[j - 1]) { chmax(dp[i][j], dp[i - 1][j - 1] + 1); } } } // for (auto x: dp) print(x); string ans = ""; ll i = s.size(), j = t.size(); while (i >= 1 && j >= 1) { 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--; } } reverse(all(ans)); cout << ans << '\n'; } int main() { string s; cin >> s; string t; cin >> t; solve(s, t); return 0; }
replace
184
185
184
185
0
p03165
C++
Runtime Error
#pragma region template #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++) #define brep(i, a, b) for (ll i = (a), i##_len = (b); i <= i##_len; i++) #define rrep(i, n) \ ll i = (n); \ ll now = -1; \ while (i-- && (now++ || 1)) #define xrep(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; i++) #define Yes(n) cout << ((n) ? YES : NO) << '\n' #define rYes(n) \ cout << ((n) ? YES : NO) << '\n'; \ return #define co(n) cout << (n) << '\n' #define rco(n) \ cout << (n) << '\n'; \ return #define Unique(v) (v).erase(unique(all(v)), (v).end()) #define eb emplace_back using namespace std; using ll = long long; using vll = vector<ll>; using vbl = vector<bool>; using vint = vector<int>; using vvll = vector<vll>; using vstr = vector<string>; const ll INF = 1e18; // --- functions which take 1 argument --- static inline void ignore_ret(int x) { x++; } template <class T> inline int sgn(T x) { return (x > 0) - (x < 0); } template <class T> inline ll factorial(T n) { if (n == 0) return 1; ll r = 1; for (T i = 2; i <= n; i++) r *= i; return r; } template <class T> inline map<T, T> factorize(T n) { map<T, T> r; for (T i = 2; i * i <= n; i++) { while (n % i == 0) { r[i]++; n /= i; } } if (n != 1) r[n] = 1; return r; } template <class T> inline vector<T> divisor(T n) { vector<T> r; for (T i = 1; i * i <= n; i++) { if (!(n % i)) { r.eb(i); if (i * i != n) r.eb(n / i); } } Sort(r); return r; } // --- functions which take 2 arguments --- template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline T nPr(T n, T k) { if (n < k || n < 0 || k < 0) return 0; T r = 1; for (T i = n - k + 1; i <= n; i++) r *= i; return r; } template <class T> inline T nCr(T n, T k) { if (n < k || n < 0 || k < 0) return 0; T r = 1; k = min(k, n - k); for (T i = n - k + 1; i <= n; i++) r *= i; return r / factorial(k); } // --- functions which take vector(s) as argument(s) --- template <class T> inline void print(T v) { cout << "[ "; for (auto x : v) cout << x << ' '; cout << "]\n"; } template <class T> inline void print_2d(vector<T> v) { for (T x : v) print<T>(x); } template <class T> inline void print_d(T v, string d) { rep(i, (ll)v.size() - 1) cout << v[i] << d; cout << v[(ll)v.size() - 1] << '\n'; } template <class T> inline void print_2d_d(vector<T> v, string d) { for (T x : v) print_d<T>(x, d); } template <class T> inline void Sort(vector<T> &v) { sort(all(v)); } template <class T> inline void rSort(vector<T> &v) { sort(all(v), greater<T>()); } template <class T> inline T min(vector<T> v) { return *min_element(all(v)); } template <class T> inline T max(vector<T> v) { return *max_element(all(v)); } template <class T> inline ll sum(T v) { return reduce(all(v), 0LL); } template <class T> inline T gcd(vector<T> v) { T r = v[0]; for (T i = 1; i < v.size(); i++) r = gcd(r, v[i]); return r; } template <class T> inline T lcm(vector<T> v) { T r = v[0]; for (T i = 1; i < v.size(); i++) r = lcm(r, v[i]); return r; } template <class T> inline double abs(vector<T> v) { return sqrt(reduce(all(v), 0.0, [](T s, T v) { return s += v * v; })); } template <class T> inline T vector_add(T u, T v) { T r; rep(i, u.size()) r.eb(u[i] + v[i]); return r; } template <class T> inline T vector_subtract(T u, T v) { T r; rep(i, u.size()) r.eb(u[i] - v[i]); return r; } template <class T> inline T dot_product(vector<T> u, vector<T> v) { if (u.size() < v.size()) return 0; T r = 0; for (T i = 0; i < u.size(); i++) r += u[i] * v[i]; return r; } template <class T> inline T cross_product(T u, T v) { T r(3); r[0] = u[1] * v[2] - u[2] * v[1]; r[1] = u[2] * v[0] - u[0] * v[2]; r[2] = u[0] * v[1] - u[1] * v[0]; return r; } // --- functions which take set(s) as argument(s) --- template <class T> inline T min(set<T> v) { return *min_element(all(v)); } template <class T> inline T max(set<T> v) { return *max_element(all(v)); } template <class T> inline T gcd(set<T> v) { T r = 0; for (T x : v) r = (r == 0) ? x : gcd(r, x); return r; } template <class T> inline T lcm(set<T> v) { T r = 0; for (T x : v) r = (r == 0) ? x : lcm(r, x); return r; } // --- Template ends here --- #pragma endregion template void solve(string s, string t) { vector<vstr> dp(s.size() + 1, vstr(t.size() + 1, "")); for (ll s_index = 1; s_index <= s.size(); s_index++) { for (ll t_index = 1; t_index <= t.size(); t_index++) { dp[s_index][t_index] = (dp[s_index - 1][t_index].size() > dp[s_index][t_index - 1].size()) ? dp[s_index - 1][t_index] : dp[s_index][t_index - 1]; if (s[s_index - 1] == t[t_index - 1]) dp[s_index][t_index] = dp[s_index - 1][t_index - 1] + s[s_index - 1]; } } cout << dp[s.size()][t.size()] << endl; } int main() { string s; cin >> s; string t; cin >> t; solve(s, t); return 0; }
#pragma region template #define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; i++) #define brep(i, a, b) for (ll i = (a), i##_len = (b); i <= i##_len; i++) #define rrep(i, n) \ ll i = (n); \ ll now = -1; \ while (i-- && (now++ || 1)) #define xrep(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; i++) #define Yes(n) cout << ((n) ? YES : NO) << '\n' #define rYes(n) \ cout << ((n) ? YES : NO) << '\n'; \ return #define co(n) cout << (n) << '\n' #define rco(n) \ cout << (n) << '\n'; \ return #define Unique(v) (v).erase(unique(all(v)), (v).end()) #define eb emplace_back using namespace std; using ll = long long; using vll = vector<ll>; using vbl = vector<bool>; using vint = vector<int>; using vvll = vector<vll>; using vstr = vector<string>; const ll INF = 1e18; // --- functions which take 1 argument --- static inline void ignore_ret(int x) { x++; } template <class T> inline int sgn(T x) { return (x > 0) - (x < 0); } template <class T> inline ll factorial(T n) { if (n == 0) return 1; ll r = 1; for (T i = 2; i <= n; i++) r *= i; return r; } template <class T> inline map<T, T> factorize(T n) { map<T, T> r; for (T i = 2; i * i <= n; i++) { while (n % i == 0) { r[i]++; n /= i; } } if (n != 1) r[n] = 1; return r; } template <class T> inline vector<T> divisor(T n) { vector<T> r; for (T i = 1; i * i <= n; i++) { if (!(n % i)) { r.eb(i); if (i * i != n) r.eb(n / i); } } Sort(r); return r; } // --- functions which take 2 arguments --- template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline T nPr(T n, T k) { if (n < k || n < 0 || k < 0) return 0; T r = 1; for (T i = n - k + 1; i <= n; i++) r *= i; return r; } template <class T> inline T nCr(T n, T k) { if (n < k || n < 0 || k < 0) return 0; T r = 1; k = min(k, n - k); for (T i = n - k + 1; i <= n; i++) r *= i; return r / factorial(k); } // --- functions which take vector(s) as argument(s) --- template <class T> inline void print(T v) { cout << "[ "; for (auto x : v) cout << x << ' '; cout << "]\n"; } template <class T> inline void print_2d(vector<T> v) { for (T x : v) print<T>(x); } template <class T> inline void print_d(T v, string d) { rep(i, (ll)v.size() - 1) cout << v[i] << d; cout << v[(ll)v.size() - 1] << '\n'; } template <class T> inline void print_2d_d(vector<T> v, string d) { for (T x : v) print_d<T>(x, d); } template <class T> inline void Sort(vector<T> &v) { sort(all(v)); } template <class T> inline void rSort(vector<T> &v) { sort(all(v), greater<T>()); } template <class T> inline T min(vector<T> v) { return *min_element(all(v)); } template <class T> inline T max(vector<T> v) { return *max_element(all(v)); } template <class T> inline ll sum(T v) { return reduce(all(v), 0LL); } template <class T> inline T gcd(vector<T> v) { T r = v[0]; for (T i = 1; i < v.size(); i++) r = gcd(r, v[i]); return r; } template <class T> inline T lcm(vector<T> v) { T r = v[0]; for (T i = 1; i < v.size(); i++) r = lcm(r, v[i]); return r; } template <class T> inline double abs(vector<T> v) { return sqrt(reduce(all(v), 0.0, [](T s, T v) { return s += v * v; })); } template <class T> inline T vector_add(T u, T v) { T r; rep(i, u.size()) r.eb(u[i] + v[i]); return r; } template <class T> inline T vector_subtract(T u, T v) { T r; rep(i, u.size()) r.eb(u[i] - v[i]); return r; } template <class T> inline T dot_product(vector<T> u, vector<T> v) { if (u.size() < v.size()) return 0; T r = 0; for (T i = 0; i < u.size(); i++) r += u[i] * v[i]; return r; } template <class T> inline T cross_product(T u, T v) { T r(3); r[0] = u[1] * v[2] - u[2] * v[1]; r[1] = u[2] * v[0] - u[0] * v[2]; r[2] = u[0] * v[1] - u[1] * v[0]; return r; } // --- functions which take set(s) as argument(s) --- template <class T> inline T min(set<T> v) { return *min_element(all(v)); } template <class T> inline T max(set<T> v) { return *max_element(all(v)); } template <class T> inline T gcd(set<T> v) { T r = 0; for (T x : v) r = (r == 0) ? x : gcd(r, x); return r; } template <class T> inline T lcm(set<T> v) { T r = 0; for (T x : v) r = (r == 0) ? x : lcm(r, x); return r; } // --- Template ends here --- #pragma endregion template void solve(string s, string t) { vvll dp(s.size() + 1, vll(t.size() + 1, 0)); for (ll s_i = 1; s_i <= s.size(); s_i++) { for (ll t_i = 1; t_i <= t.size(); t_i++) { dp[s_i][t_i] = max(dp[s_i - 1][t_i], dp[s_i][t_i - 1]); if (s[s_i - 1] == t[t_i - 1]) { dp[s_i][t_i] = dp[s_i - 1][t_i - 1] + 1; } } } string ans = ""; ll s_index = s.size(), t_index = t.size(); while (s_index > 0 && t_index > 0) { if (dp[s_index][t_index] == dp[s_index - 1][t_index]) s_index--; else if (dp[s_index][t_index] == dp[s_index][t_index - 1]) t_index--; else { ans += s[s_index - 1]; s_index--; t_index--; } } reverse(all(ans)); cout << ans << '\n'; } int main() { string s; cin >> s; string t; cin >> t; solve(s, t); return 0; }
replace
190
204
190
218
0
p03165
C++
Runtime Error
#include <algorithm> #include <iostream> #include <string> #include <vector> #define FOR(i, i0, n) for (int i = (i0); i < (n); i++) #define COUT(val) cout << (val) << "\n"; using namespace std; using ll = long long int; int main(void) { string s, t; cin >> s >> t; int ns = s.size(), nt = t.size(); vector<vector<int>> dp(ns + 1, vector<int>(nt + 1, -1)); FOR(i, 0, ns + 1) dp[i][0] = 0; FOR(j, 0, nt + 1) dp[0][j] = 0; FOR(i, 1, ns + 1) FOR(j, 1, nt + 1) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (s[i - 1] == t[j - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); } string ans = ""; while (ns > 0 || nt > 0) { while (dp[ns][nt] == dp[ns - 1][nt]) ns--; while (dp[ns][nt] == dp[ns][nt - 1]) nt--; ans.push_back(s[ns - 1]); ns--; nt--; } reverse(ans.begin(), ans.end()); COUT(ans); }
#include <algorithm> #include <iostream> #include <string> #include <vector> #define FOR(i, i0, n) for (int i = (i0); i < (n); i++) #define COUT(val) cout << (val) << "\n"; using namespace std; using ll = long long int; int main(void) { string s, t; cin >> s >> t; int ns = s.size(), nt = t.size(); vector<vector<int>> dp(ns + 1, vector<int>(nt + 1, -1)); FOR(i, 0, ns + 1) dp[i][0] = 0; FOR(j, 0, nt + 1) dp[0][j] = 0; FOR(i, 1, ns + 1) FOR(j, 1, nt + 1) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (s[i - 1] == t[j - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); } string ans = ""; while (dp[ns][nt] > 0) { while (dp[ns][nt] == dp[ns - 1][nt]) ns--; while (dp[ns][nt] == dp[ns][nt - 1]) nt--; ans.push_back(s[ns - 1]); ns--; nt--; } reverse(ans.begin(), ans.end()); COUT(ans); }
replace
25
26
25
26
0
p03165
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <unordered_map> #include <vector> #define ll long long #define ld long double #define ull unsigned long long #define endl "\n" #define MohamedMotaz \ ios::sync_with_stdio(0); \ cin.tie(0); \ ios_base::sync_with_stdio(0); #define f(a, b, c) for (int a = b; a < c; a++) #define fr(a, b, c) for (int a = b; a >= c; a--) using namespace std; // ll fact[1000001]; ll inv[1000001]; // ll primes[100007]; // ll arr[1000007]; // ll modPower(ll b, ll p) { // if (p == 0) // return 1; // // ll halfpow = modPower(b, p / 2); // ll toReturn = (halfpow * halfpow) % mod; // if (p % 2) // toReturn = (toReturn * b) % mod; // // return toReturn; // } // // ll fastPower(ll b, ll p) { // if (p == 0) // return 1; // ll ans = fastPower(b, p / 2); // ans = (ans * ans); // if (p % 2 != 0) // ans = (ans * b); // return ans; // } // ll GcdRecursive(ll a, ll b) { // if (b == 0) return a; // return GcdRecursive(b, a % b); // } // ll modLCM(ll a, ll b) { // ll val = GcdRecursive(a, b); // ll tmp = ((a % mod) * (b % mod)) % mod; // ll finalVal = ((tmp % mod) * (arr[val] % mod)) % mod; // return finalVal; // // } // ll LCM(ll a, ll b) { // return (a * b) / GcdRecursive(a, b); // } // void move1step(ll& a, ll& b, ll q) { // a and b by reference // ll c = a - q * b; // a = b; // b = c; // } // ll GcdIterative(ll a, ll b) { // while (b) move1step(a, b, a / b); // return a; // } // // void pre(ll n) { // // fact[0] = 1; // inv[0] = 1; // // for (ll i = 1; i <= n; i++) { // fact[i] = (i * fact[i - 1]) % mod; // inv[i] = modPower(fact[i], mod - 2); // arr[i] = modPower(i, mod - 2); // } // } // // ll npr(ll n, ll r) { // return ((fact[n] * inv[n - r]) % mod); // } // // ll ncr(ll n, ll r) { // return ((((fact[n] * inv[n - r]) % mod) * inv[r]) % mod); // } // // void sieve(ll val) { // memset(primes, 1, sizeof primes); // primes[0] = primes[1] = false; // for (int i = 2; i <= val; i++) { // if (primes[i]) { // for (int j = i * i; j <= val; j += i) { // primes[j] = 0; // } // } // } // // } const ll mod = 1e9 + 7; const ll N = 2e3 + 7; const ll inf = 1e9 + 5; string s, t; ll dp[N][N]; int main() { MohamedMotaz; cin >> s >> t; memset(dp, 0, sizeof(dp)); f(i, 1, s.size() + 1) { f(j, 1, t.size() + 1) { 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]); } } } // f(i,1,s.size() + 1) {f(j,1,t.size() + 1) cout << dp[i][j] << " "; cout << // endl;} ll a = s.size(), b = t.size(); string ans = ""; while (a && b) { if (dp[a][b] == dp[a - 1][b]) a--; else if (dp[a][b] == dp[a][b - 1]) b--; else { ans.push_back(s[a - 1]); a--; b--; } } reverse(ans.begin(), ans.end()); cout << ans; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <unordered_map> #include <vector> #define ll long long #define ld long double #define ull unsigned long long #define endl "\n" #define MohamedMotaz \ ios::sync_with_stdio(0); \ cin.tie(0); \ ios_base::sync_with_stdio(0); #define f(a, b, c) for (int a = b; a < c; a++) #define fr(a, b, c) for (int a = b; a >= c; a--) using namespace std; // ll fact[1000001]; ll inv[1000001]; // ll primes[100007]; // ll arr[1000007]; // ll modPower(ll b, ll p) { // if (p == 0) // return 1; // // ll halfpow = modPower(b, p / 2); // ll toReturn = (halfpow * halfpow) % mod; // if (p % 2) // toReturn = (toReturn * b) % mod; // // return toReturn; // } // // ll fastPower(ll b, ll p) { // if (p == 0) // return 1; // ll ans = fastPower(b, p / 2); // ans = (ans * ans); // if (p % 2 != 0) // ans = (ans * b); // return ans; // } // ll GcdRecursive(ll a, ll b) { // if (b == 0) return a; // return GcdRecursive(b, a % b); // } // ll modLCM(ll a, ll b) { // ll val = GcdRecursive(a, b); // ll tmp = ((a % mod) * (b % mod)) % mod; // ll finalVal = ((tmp % mod) * (arr[val] % mod)) % mod; // return finalVal; // // } // ll LCM(ll a, ll b) { // return (a * b) / GcdRecursive(a, b); // } // void move1step(ll& a, ll& b, ll q) { // a and b by reference // ll c = a - q * b; // a = b; // b = c; // } // ll GcdIterative(ll a, ll b) { // while (b) move1step(a, b, a / b); // return a; // } // // void pre(ll n) { // // fact[0] = 1; // inv[0] = 1; // // for (ll i = 1; i <= n; i++) { // fact[i] = (i * fact[i - 1]) % mod; // inv[i] = modPower(fact[i], mod - 2); // arr[i] = modPower(i, mod - 2); // } // } // // ll npr(ll n, ll r) { // return ((fact[n] * inv[n - r]) % mod); // } // // ll ncr(ll n, ll r) { // return ((((fact[n] * inv[n - r]) % mod) * inv[r]) % mod); // } // // void sieve(ll val) { // memset(primes, 1, sizeof primes); // primes[0] = primes[1] = false; // for (int i = 2; i <= val; i++) { // if (primes[i]) { // for (int j = i * i; j <= val; j += i) { // primes[j] = 0; // } // } // } // // } const ll mod = 1e9 + 7; const ll N = 3e3 + 7; const ll inf = 1e9 + 5; string s, t; ll dp[N][N]; int main() { MohamedMotaz; cin >> s >> t; memset(dp, 0, sizeof(dp)); f(i, 1, s.size() + 1) { f(j, 1, t.size() + 1) { 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]); } } } // f(i,1,s.size() + 1) {f(j,1,t.size() + 1) cout << dp[i][j] << " "; cout << // endl;} ll a = s.size(), b = t.size(); string ans = ""; while (a && b) { if (dp[a][b] == dp[a - 1][b]) a--; else if (dp[a][b] == dp[a][b - 1]) b--; else { ans.push_back(s[a - 1]); a--; b--; } } reverse(ans.begin(), ans.end()); cout << ans; }
replace
111
112
111
112
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; string s, t; int dp[3000][3000]; int main() { cin >> s >> t; int ls = s.length(); int lt = t.length(); for (int i = 1; i <= ls; i++) { for (int j = 1; j <= lt; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } dp[i][j] = max(dp[i][j], dp[i - 1][j]); dp[i][j] = max(dp[i][j], dp[i][j - 1]); } } int i = ls; int j = lt; string out; 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 { out = out + s[i - 1]; i--; j--; } } int lo = out.length(); for (int i = lo - 1; i >= 0; i--) { cout << out[i]; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string s, t; int dp[4000][4000]; int main() { cin >> s >> t; int ls = s.length(); int lt = t.length(); for (int i = 1; i <= ls; i++) { for (int j = 1; j <= lt; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } dp[i][j] = max(dp[i][j], dp[i - 1][j]); dp[i][j] = max(dp[i][j], dp[i][j - 1]); } } int i = ls; int j = lt; string out; 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 { out = out + s[i - 1]; i--; j--; } } int lo = out.length(); for (int i = lo - 1; i >= 0; i--) { cout << out[i]; } cout << endl; return 0; }
replace
5
6
5
6
0
p03165
C++
Runtime Error
#define rep(i, n) REP(i, 0, n) #define REP(i, a, b) for (int i = a; i < (int)(b); i++) #include <bits/stdc++.h> typedef long long ll; using namespace std; template <class T> inline void chmax(T &a, const T &b) { if (a < b) a = b; } template <class T> inline void chmin(T &a, const T &b) { if (a > b) a = b; } const ll MAXN = (ll)3e3 + 10; const long long INF = 1LL << 60; ll dp[MAXN][MAXN]; int main(void) { string s, t; cin >> s; cin >> t; rep(i, MAXN) rep(j, MAXN) dp[i][j] = 0; rep(i, s.size()) rep(j, t.size()) { if (s[i] == t[j]) chmax(dp[i + 1][j + 1], dp[i][j] + 1); else { chmax(dp[i + 1][j + 1], dp[i][j + 1]); chmax(dp[i + 1][j + 1], dp[i + 1][j]); } } // cout<<dp[s.size()][t.size()]<<endl; int i = s.size(); int j = t.size(); stack<char> ans; while (1) { if (i == 0 && j == 0) break; if (dp[i][j] == dp[i - 1][j]) i--; else if (dp[i][j - 1] == dp[i][j]) j--; else { i--; j--; // cout<<s[i]<<endl; ans.push(s[i]); } } while (ans.size()) { cout << ans.top(); ans.pop(); } cout << endl; return 0; }
#define rep(i, n) REP(i, 0, n) #define REP(i, a, b) for (int i = a; i < (int)(b); i++) #include <bits/stdc++.h> typedef long long ll; using namespace std; template <class T> inline void chmax(T &a, const T &b) { if (a < b) a = b; } template <class T> inline void chmin(T &a, const T &b) { if (a > b) a = b; } const ll MAXN = (ll)3e3 + 10; const long long INF = 1LL << 60; ll dp[MAXN][MAXN]; int main(void) { string s, t; cin >> s; cin >> t; rep(i, MAXN) rep(j, MAXN) dp[i][j] = 0; rep(i, s.size()) rep(j, t.size()) { if (s[i] == t[j]) chmax(dp[i + 1][j + 1], dp[i][j] + 1); else { chmax(dp[i + 1][j + 1], dp[i][j + 1]); chmax(dp[i + 1][j + 1], dp[i + 1][j]); } } // cout<<dp[s.size()][t.size()]<<endl; int i = s.size(); int j = t.size(); stack<char> ans; while (1) { if (i == 0 || j == 0) break; if (dp[i][j] == dp[i - 1][j]) i--; else if (dp[i][j - 1] == dp[i][j]) j--; else { i--; j--; // cout<<s[i]<<endl; ans.push(s[i]); } } while (ans.size()) { cout << ans.top(); ans.pop(); } cout << endl; return 0; }
replace
38
39
38
39
0
p03165
C++
Runtime Error
#include <cstring> #include <fstream> #include <iostream> #define nmax 3001 using namespace std; ifstream fin("p.in"); ofstream fout("p.out"); int dist[nmax][nmax]; int main() { char s[nmax], t[nmax]; cin >> s >> t; int n = strlen(s), m = strlen(t); for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) if (i == 0 || j == 0) dist[i][j] = 0; else if (s[i - 1] == t[j - 1]) dist[i][j] = 1 + dist[i - 1][j - 1]; else dist[i][j] = max(dist[i - 1][j], dist[i][j - 1]); /*for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) fout<<dist[i][j]<<" "; fout<<endl; }*/ char sol[nmax]; int ct = 0; int i = n, j = m; while (i >= 0 && j >= 0) { if (s[i - 1] == t[j - 1]) { sol[ct++] = s[i - 1]; i--; j--; } else if (dist[i - 1][j] > dist[i][j - 1]) i--; else j--; } sol[ct] = NULL; for (int i = ct - 1; i >= 0; i--) cout << sol[i]; return 0; }
#include <cstring> #include <fstream> #include <iostream> #define nmax 3001 using namespace std; ifstream fin("p.in"); ofstream fout("p.out"); int dist[nmax][nmax]; int main() { char s[nmax], t[nmax]; cin >> s >> t; int n = strlen(s), m = strlen(t); for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) if (i == 0 || j == 0) dist[i][j] = 0; else if (s[i - 1] == t[j - 1]) dist[i][j] = 1 + dist[i - 1][j - 1]; else dist[i][j] = max(dist[i - 1][j], dist[i][j - 1]); /*for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) fout<<dist[i][j]<<" "; fout<<endl; }*/ char sol[nmax]; int ct = 0; int i = n, j = m; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { sol[ct++] = s[i - 1]; i--; j--; } else if (dist[i - 1][j] > dist[i][j - 1]) i--; else j--; } sol[ct] = NULL; for (int i = ct - 1; i >= 0; i--) cout << sol[i]; return 0; }
replace
36
37
36
37
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define PI acos(-1) #define mx 110 int fx[] = {0, 0, -1, 1}; int fy[] = {1, -1, 0, 0}; char s1[mx], s2[mx]; char symbol[mx][mx]; int i, j, m, n, larr[mx][mx]; void print(int i, int j) { if (i == 0 || j == 0) return; if (symbol[i][j] == 'c') { print(i - 1, j - 1); printf("%c", s1[i - 1]); } else if (symbol[i][j] == 'u') { print(i - 1, j); } else { print(i, j - 1); } } void Lcs() { m = strlen(s1); n = strlen(s2); for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { if (s1[i - 1] == s2[j - 1]) { larr[i][j] = larr[i - 1][j - 1] + 1; symbol[i][j] = 'c'; // c -> diagonal } else if (larr[i - 1][j] >= larr[i][j - 1]) { larr[i][j] = larr[i - 1][j]; symbol[i][j] = 'u'; // c -> upper } else { larr[i][j] = larr[i][j - 1]; symbol[i][j] = 'l'; // c -> left } } } } int main() { scanf("%s", s1); scanf("%s", s2); Lcs(); print(m, n); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define PI acos(-1) #define mx 3005 int fx[] = {0, 0, -1, 1}; int fy[] = {1, -1, 0, 0}; char s1[mx], s2[mx]; char symbol[mx][mx]; int i, j, m, n, larr[mx][mx]; void print(int i, int j) { if (i == 0 || j == 0) return; if (symbol[i][j] == 'c') { print(i - 1, j - 1); printf("%c", s1[i - 1]); } else if (symbol[i][j] == 'u') { print(i - 1, j); } else { print(i, j - 1); } } void Lcs() { m = strlen(s1); n = strlen(s2); for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { if (s1[i - 1] == s2[j - 1]) { larr[i][j] = larr[i - 1][j - 1] + 1; symbol[i][j] = 'c'; // c -> diagonal } else if (larr[i - 1][j] >= larr[i][j - 1]) { larr[i][j] = larr[i - 1][j]; symbol[i][j] = 'u'; // c -> upper } else { larr[i][j] = larr[i][j - 1]; symbol[i][j] = 'l'; // c -> left } } } } int main() { scanf("%s", s1); scanf("%s", s2); Lcs(); print(m, n); printf("\n"); return 0; }
replace
4
5
4
5
0
p03165
C++
Runtime Error
#include <algorithm> #include <cmath> #include <iostream> int main() { std::string s, t, result; std::cin >> s >> t; int DP[301][301]; for (int i = 0; i < (int)s.length(); i++) DP[i][0] = 0; for (int i = 0; i < (int)t.length(); i++) DP[0][i] = 0; for (int i = 1; i <= (int)s.length(); i++) { for (int j = 1; j <= (int)t.length(); j++) { if (s[i - 1] == t[j - 1]) DP[i][j] = DP[i - 1][j - 1] + 1; else DP[i][j] = std::max(DP[i - 1][j], DP[i][j - 1]); } } int i = s.length(), j = t.length(); while (i != 0 && j != 0) { if (s[i - 1] == t[j - 1]) { result = s[i - 1] + result; i--; j--; } else if (DP[i][j] == DP[i - 1][j]) i = i - 1; else j = j - 1; } std::cout << result; return 0; }
#include <algorithm> #include <cmath> #include <iostream> int main() { std::string s, t, result; std::cin >> s >> t; int DP[3001][3001]; for (int i = 0; i < (int)s.length(); i++) DP[i][0] = 0; for (int i = 0; i < (int)t.length(); i++) DP[0][i] = 0; for (int i = 1; i <= (int)s.length(); i++) { for (int j = 1; j <= (int)t.length(); j++) { if (s[i - 1] == t[j - 1]) DP[i][j] = DP[i - 1][j - 1] + 1; else DP[i][j] = std::max(DP[i - 1][j], DP[i][j - 1]); } } int i = s.length(), j = t.length(); while (i != 0 && j != 0) { if (s[i - 1] == t[j - 1]) { result = s[i - 1] + result; i--; j--; } else if (DP[i][j] == DP[i - 1][j]) i = i - 1; else j = j - 1; } std::cout << result; return 0; }
replace
7
8
7
8
0
p03165
C++
Time Limit Exceeded
// made by capeta160 #include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; #define etm \ cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n' #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define ll long long int #define ull unsigned long long int #define doub long double #define mp make_pair #define pb push_back #define pii pair<int, int> #define pdd pair<double, double> #define pll pair<long long int, long long int> #define vpl vector<pll> #define vll vector<ll> #define vb vector<bool> #define vvbb vector<vb> #define vi vector<int> #define mi map<int, int> #define mull map<ull, ull> #define stp setprecision(20) #define N 100005 #define rep(i, a, b, c) for (ll i = (a); i <= (b); i += (c)) #define repb(i, a, b, c) for (ll i = (a); i >= (b); i -= (c)) #define MOD 1000000007 #define ld long double #define inf 1e18 #define mp make_pair #define vpll vector<pair<ll, ll>> #define vvpll vector<vector<pair<ll, ll>>> #define vvll vector<vector<ll>> #define vvii vector<vector<int>> #define all(x) x.begin(), x.end() #define fi first #define se second #define test \ ll T; \ cin >> T; \ while (T--) #define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') #define show(w, size) \ for (ll i = 0; i < size; i++) \ cout << w[i] << " "; #define print(a) cout << a << "\n"; #define pqll priority_queue<ll> #define mset(dp, no) memset(dp, no, sizeof(dp)) #define umll unordered_map<ll, ll> #define mll map<ll, ll> #define input(a, n) \ ll I; \ rep(I, 0, n - 1, 1) cin >> a[I]; #define countbit __builtin_popcount // Number of set bits . #define fbo(k) find_by_order // K-th element in a set (counting from zero) . #define ook(k) order_of_key // Number of items strictly smaller than k . #define lb lower_bound #define up upper_bound #define in insert // #define db(x) cout <<#x<<": "<<x<<'\n'; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> using namespace std; #define db(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << '\n'; } 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...); } ll gcd(ll a, ll b) { if (a < b) return gcd(b, a); else if (b == 0) return a; else return gcd(b, a % b); } ll isPrime(ll n) { ll p = (ll)sqrt(n); rep(i, 2, p, 1) if (n % i == 0) return 0; return 1; } // reuturn 1 if prime ll pow(ll b, ll e) { if (e == 0) return 1; else if (e % 2 == 0) { ll a = pow(b, e / 2); return a * a; } else { ll a = pow(b, e / 2); return b * a * a; } } ll powm(ll x, ll y, ll m = MOD) { x = x % m; ll res = 1; while (y) { if (y & 1) res = res * x; res %= m; y = y >> 1; x = x * x; x %= m; } return res; } ll ceil(long double a) { ll b = a; if (a == b) { return b; } else { return b + 1; } } ll floor(long double a) { ll b = a; return b; } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } ll modInverse(ll a, ll m) { return powm(a, m - 2, m); } bool issq(ll n) { ll p = sqrt(n); return p * p == n; } vll prime; // if i==prime[i] then prime otherwise smallest prime factor of that // number void sieve(ll n) { prime.resize(n + 1, 1); prime[0] = 0, prime[1] = 0; for (ll i = 2; i * i <= n; i++) if (prime[i]) for (ll j = i * i; j <= n; j += i) prime[j] = 0; } ll extended_GCD(ll a, ll b, ll &x, ll &y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll gcd = extended_GCD(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } ll mulInv(ll a, ll mod = 26) { ll x, y; extended_GCD(a, mod, x, y); if (x < 0) x += mod; return x; } ll find(ll num[], ll rem[], ll k, ll prod) { // Compute product of all numbers ll result = 0; // Apply above formula for (ll i = 0; i < k; i++) { ll pp = prod / num[i]; result += rem[i] * mulInv(pp, num[i]) * pp; } return result % prod; } ll nCr(ll n, ll k) { ll res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (ll i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } class DSU { public: vll parent, size; public: DSU(ll n) { parent.resize(n + 1); size.resize(n + 1); for (ll i = 1; i <= n; i++) { parent[i] = i; size[i] = 1; } } public: ll find_set(ll x) { if (parent[x] == x) { return x; } return parent[x] = find_set(parent[x]); } public: void union_set(ll x, ll y) { x = find_set(x); y = find_set(y); if (x != y) { parent[y] = x; } } }; bool cmp(pair<ll, ll> &p1, pair<ll, ll> &p2) { if (p1.fi == p2.fi) { return p1.se > p2.se; } return p1.fi > p2.fi; } bool isPalindrome(string s) { ll i, j; for (i = 0, j = s.length() - 1; i <= j; i++, j--) { if (s[i] != s[j]) { return 0; } } return 1; } ll ToInt(char ch) { return ch - '0'; } char ToChar(ll a) { return a + '0'; } bool isSubSeq(string str1, string str2, ll m, ll n) { // Base Cases if (m == 0) return true; if (n == 0) return false; // If last characters of two strings are matching if (str1[m - 1] == str2[n - 1]) return isSubSeq(str1, str2, m - 1, n - 1); // If last characters are not matching return isSubSeq(str1, str2, m, n - 1); } void printVectorPair(vpll v) { for (ll i = 0; i < v.size(); i++) { cout << v[i].fi << " " << v[i].se << "\n"; } } void modBigNumber(string num, ll m) { // Store the modulus of big number vector<int> vec; ll mod = 0; // Do step by step division for (int i = 0; i < num.size(); i++) { int digit = num[i] - '0'; mod = mod * 10 + digit; int quo = mod / m; if ((vec.size() != 0) || (quo != 0)) // to remove initiale zeros vec.push_back(quo); mod = mod % m; } // cout << "\nRemainder : " << mod << "\n"; // cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n"; return; } struct SegmentTree { ll n; vll v; SegmentTree(ll size) { n = 4 * size + 1; v.resize(n, 0); } void build(ll ar[], ll ipos, ll fpos, ll pos = 1) { if (ipos > fpos) return; if (ipos == fpos) v[pos] = ar[ipos]; else { ll mid = (ipos + fpos) / 2; build(ar, ipos, mid, pos * 2); build(ar, mid + 1, fpos, pos * 2 + 1); v[pos] = v[pos * 2] + v[pos * 2 + 1]; } } void update(ll index, ll val, ll ipos, ll fpos, ll pos = 1) { if (ipos > fpos) return; if (ipos == fpos) v[pos] += val; else { v[pos] += val; ll mid = (ipos + fpos) / 2; if (mid >= index) update(index, val, ipos, mid, pos * 2); else update(index, val, mid + 1, fpos, pos * 2 + 1); } } ll get_sum(ll l, ll r, ll ipos, ll fpos, ll pos = 1) { if (ipos > fpos) return 0; if (l > r) return 0; ll mid = (ipos + fpos) / 2; if ((l == ipos) && (r == fpos)) return v[pos]; else return get_sum(l, min(mid, r), ipos, mid, pos * 2) + get_sum(max(mid + 1, l), r, mid + 1, fpos, pos * 2 + 1); } }; struct BIT { vector<ll> bitree; ll n; BIT(ll n) { this->n = n; bitree.resize(n + 1, 0); } void update(ll idx, ll val) { idx++; while (idx <= n) { bitree[idx] += val; idx += idx & (-idx); } } ll Sum(ll idx) { ll sum = 0; idx++; while (idx > 0) { sum += bitree[idx]; idx -= idx & (-idx); } return sum; } }; ll sumofdigits(ll a) { ll val = 0; while (a > 0) { val += a % 10; a /= 10; } return val; } int main() { fastio; string s, t; cin >> s >> t; // i-->pointer for s and j-->pointer for t // dp[i][j]=max(dp[i+1][j], dp[i][j+1]);-->if i and j dont match // dp[i][j]=dp[i+1][j+1]; if they match vector<vector<string>> dp(s.length() + 1, vector<string>(t.length() + 1, "")); ll ans = 0; rep(i, 1, s.length(), 1) { rep(j, 1, t.length(), 1) { if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + s[i - 1]; else { if (dp[i - 1][j].length() > dp[i][j - 1].length()) dp[i][j] = dp[i - 1][j]; else dp[i][j] = dp[i][j - 1]; } } } cout << dp[s.length()][t.length()] << endl; return 0; }
// made by capeta160 #include <algorithm> #include <climits> #include <cmath> #include <cstring> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; #define etm \ cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n' #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define ll long long int #define ull unsigned long long int #define doub long double #define mp make_pair #define pb push_back #define pii pair<int, int> #define pdd pair<double, double> #define pll pair<long long int, long long int> #define vpl vector<pll> #define vll vector<ll> #define vb vector<bool> #define vvbb vector<vb> #define vi vector<int> #define mi map<int, int> #define mull map<ull, ull> #define stp setprecision(20) #define N 100005 #define rep(i, a, b, c) for (ll i = (a); i <= (b); i += (c)) #define repb(i, a, b, c) for (ll i = (a); i >= (b); i -= (c)) #define MOD 1000000007 #define ld long double #define inf 1e18 #define mp make_pair #define vpll vector<pair<ll, ll>> #define vvpll vector<vector<pair<ll, ll>>> #define vvll vector<vector<ll>> #define vvii vector<vector<int>> #define all(x) x.begin(), x.end() #define fi first #define se second #define test \ ll T; \ cin >> T; \ while (T--) #define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') #define show(w, size) \ for (ll i = 0; i < size; i++) \ cout << w[i] << " "; #define print(a) cout << a << "\n"; #define pqll priority_queue<ll> #define mset(dp, no) memset(dp, no, sizeof(dp)) #define umll unordered_map<ll, ll> #define mll map<ll, ll> #define input(a, n) \ ll I; \ rep(I, 0, n - 1, 1) cin >> a[I]; #define countbit __builtin_popcount // Number of set bits . #define fbo(k) find_by_order // K-th element in a set (counting from zero) . #define ook(k) order_of_key // Number of items strictly smaller than k . #define lb lower_bound #define up upper_bound #define in insert // #define db(x) cout <<#x<<": "<<x<<'\n'; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> using namespace std; #define db(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << '\n'; } 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...); } ll gcd(ll a, ll b) { if (a < b) return gcd(b, a); else if (b == 0) return a; else return gcd(b, a % b); } ll isPrime(ll n) { ll p = (ll)sqrt(n); rep(i, 2, p, 1) if (n % i == 0) return 0; return 1; } // reuturn 1 if prime ll pow(ll b, ll e) { if (e == 0) return 1; else if (e % 2 == 0) { ll a = pow(b, e / 2); return a * a; } else { ll a = pow(b, e / 2); return b * a * a; } } ll powm(ll x, ll y, ll m = MOD) { x = x % m; ll res = 1; while (y) { if (y & 1) res = res * x; res %= m; y = y >> 1; x = x * x; x %= m; } return res; } ll ceil(long double a) { ll b = a; if (a == b) { return b; } else { return b + 1; } } ll floor(long double a) { ll b = a; return b; } ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); } ll modInverse(ll a, ll m) { return powm(a, m - 2, m); } bool issq(ll n) { ll p = sqrt(n); return p * p == n; } vll prime; // if i==prime[i] then prime otherwise smallest prime factor of that // number void sieve(ll n) { prime.resize(n + 1, 1); prime[0] = 0, prime[1] = 0; for (ll i = 2; i * i <= n; i++) if (prime[i]) for (ll j = i * i; j <= n; j += i) prime[j] = 0; } ll extended_GCD(ll a, ll b, ll &x, ll &y) { if (a == 0) { x = 0; y = 1; return b; } ll x1, y1; ll gcd = extended_GCD(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } ll mulInv(ll a, ll mod = 26) { ll x, y; extended_GCD(a, mod, x, y); if (x < 0) x += mod; return x; } ll find(ll num[], ll rem[], ll k, ll prod) { // Compute product of all numbers ll result = 0; // Apply above formula for (ll i = 0; i < k; i++) { ll pp = prod / num[i]; result += rem[i] * mulInv(pp, num[i]) * pp; } return result % prod; } ll nCr(ll n, ll k) { ll res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (ll i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } class DSU { public: vll parent, size; public: DSU(ll n) { parent.resize(n + 1); size.resize(n + 1); for (ll i = 1; i <= n; i++) { parent[i] = i; size[i] = 1; } } public: ll find_set(ll x) { if (parent[x] == x) { return x; } return parent[x] = find_set(parent[x]); } public: void union_set(ll x, ll y) { x = find_set(x); y = find_set(y); if (x != y) { parent[y] = x; } } }; bool cmp(pair<ll, ll> &p1, pair<ll, ll> &p2) { if (p1.fi == p2.fi) { return p1.se > p2.se; } return p1.fi > p2.fi; } bool isPalindrome(string s) { ll i, j; for (i = 0, j = s.length() - 1; i <= j; i++, j--) { if (s[i] != s[j]) { return 0; } } return 1; } ll ToInt(char ch) { return ch - '0'; } char ToChar(ll a) { return a + '0'; } bool isSubSeq(string str1, string str2, ll m, ll n) { // Base Cases if (m == 0) return true; if (n == 0) return false; // If last characters of two strings are matching if (str1[m - 1] == str2[n - 1]) return isSubSeq(str1, str2, m - 1, n - 1); // If last characters are not matching return isSubSeq(str1, str2, m, n - 1); } void printVectorPair(vpll v) { for (ll i = 0; i < v.size(); i++) { cout << v[i].fi << " " << v[i].se << "\n"; } } void modBigNumber(string num, ll m) { // Store the modulus of big number vector<int> vec; ll mod = 0; // Do step by step division for (int i = 0; i < num.size(); i++) { int digit = num[i] - '0'; mod = mod * 10 + digit; int quo = mod / m; if ((vec.size() != 0) || (quo != 0)) // to remove initiale zeros vec.push_back(quo); mod = mod % m; } // cout << "\nRemainder : " << mod << "\n"; // cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n"; return; } struct SegmentTree { ll n; vll v; SegmentTree(ll size) { n = 4 * size + 1; v.resize(n, 0); } void build(ll ar[], ll ipos, ll fpos, ll pos = 1) { if (ipos > fpos) return; if (ipos == fpos) v[pos] = ar[ipos]; else { ll mid = (ipos + fpos) / 2; build(ar, ipos, mid, pos * 2); build(ar, mid + 1, fpos, pos * 2 + 1); v[pos] = v[pos * 2] + v[pos * 2 + 1]; } } void update(ll index, ll val, ll ipos, ll fpos, ll pos = 1) { if (ipos > fpos) return; if (ipos == fpos) v[pos] += val; else { v[pos] += val; ll mid = (ipos + fpos) / 2; if (mid >= index) update(index, val, ipos, mid, pos * 2); else update(index, val, mid + 1, fpos, pos * 2 + 1); } } ll get_sum(ll l, ll r, ll ipos, ll fpos, ll pos = 1) { if (ipos > fpos) return 0; if (l > r) return 0; ll mid = (ipos + fpos) / 2; if ((l == ipos) && (r == fpos)) return v[pos]; else return get_sum(l, min(mid, r), ipos, mid, pos * 2) + get_sum(max(mid + 1, l), r, mid + 1, fpos, pos * 2 + 1); } }; struct BIT { vector<ll> bitree; ll n; BIT(ll n) { this->n = n; bitree.resize(n + 1, 0); } void update(ll idx, ll val) { idx++; while (idx <= n) { bitree[idx] += val; idx += idx & (-idx); } } ll Sum(ll idx) { ll sum = 0; idx++; while (idx > 0) { sum += bitree[idx]; idx -= idx & (-idx); } return sum; } }; ll sumofdigits(ll a) { ll val = 0; while (a > 0) { val += a % 10; a /= 10; } return val; } int main() { fastio; string s, t; cin >> s >> t; // i-->pointer for s and j-->pointer for t // dp[i][j]=max(dp[i+1][j], dp[i][j+1]);-->if i and j dont match // dp[i][j]=dp[i+1][j+1]; if they match vvll dp(3005, vll(3005, 0)); rep(i, 1, 3000, 1) { rep(j, 1, 3000, 1) { if (i <= s.length() and j <= t.length() and 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<<dp[s.length()][t.length()]<<endl; ll i = s.length(), j = t.length(); string ans; while (i > 0 and j > 0) { if (s[i - 1] == t[j - 1]) { ans = s[i - 1] + ans; i--; j--; } else { if (dp[i][j] == dp[i - 1][j]) i--; else j--; } } cout << ans << endl; return 0; }
replace
365
380
365
390
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = int64_t; int main() { string s, t; cin >> s >> t; vector<vector<ll>> dp(s.size() + 1, vector<ll>(t.size() + 1, 0)); vector<vector<pair<ll, ll>>> pre( s.size() + 1, vector<pair<ll, ll>>(t.size() + 1, {-1, -1})); ll max_value = -1; for (ll i = 0; i < s.size(); i++) { for (ll j = 0; j < t.size(); j++) { if (s[i] == t[j]) { if (dp[i][j] + 1 > dp[i + 1][j + 1]) { max_value = max(max_value, dp[i + 1][j + 1] = dp[i][j] + 1); pre[i + 1][j + 1] = {i, j}; } } if (dp[i][j] > dp[i + 1][j]) { dp[i + 1][j] = dp[i][j]; pre[i + 1][j] = {i, j}; } if (dp[i][j] > dp[i][j + 1]) { dp[i][j + 1] = dp[i][j]; pre[i][j + 1] = {i, j}; } } } string ans; for (ll i = 0; i <= s.size(); i++) { for (ll j = 0; j <= t.size(); j++) { if (dp[i][j] == max_value) { while (pre[i][j].first != -1) { auto p = pre[i][j]; if (0 <= p.first && p.first < i && 0 <= p.second && p.second < j && s[p.first] == t[p.second]) { ans += s[p.first]; max_value--; } i = p.first; j = p.second; } i = s.size() + 1; j = t.size() + 1; } } } assert(max_value == 0); reverse(ans.begin(), ans.end()); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = int64_t; int main() { string s, t; cin >> s >> t; vector<vector<ll>> dp(s.size() + 1, vector<ll>(t.size() + 1, 0)); vector<vector<pair<ll, ll>>> pre( s.size() + 1, vector<pair<ll, ll>>(t.size() + 1, {-1, -1})); ll max_value = -1; for (ll i = 0; i < s.size(); i++) { for (ll j = 0; j < t.size(); j++) { if (s[i] == t[j]) { if (dp[i][j] + 1 > dp[i + 1][j + 1]) { max_value = max(max_value, dp[i + 1][j + 1] = dp[i][j] + 1); pre[i + 1][j + 1] = {i, j}; } } if (dp[i][j] > dp[i + 1][j]) { dp[i + 1][j] = dp[i][j]; pre[i + 1][j] = {i, j}; } if (dp[i][j] > dp[i][j + 1]) { dp[i][j + 1] = dp[i][j]; pre[i][j + 1] = {i, j}; } } } string ans; for (ll i = 0; i <= s.size(); i++) { for (ll j = 0; j <= t.size(); j++) { if (dp[i][j] == max_value) { while (pre[i][j].first != -1) { auto p = pre[i][j]; if (0 <= p.first && p.first < i && 0 <= p.second && p.second < j && s[p.first] == t[p.second]) { ans += s[p.first]; max_value--; } i = p.first; j = p.second; } i = s.size() + 1; j = t.size() + 1; } } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
delete
53
54
53
53
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; const long long int INF = 1LL << 60; int main() { string s, t; cin >> s; cin >> t; vector<vector<int>> dp(s.length() + 1, vector<int>(t.length(), 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; } else { dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } } string res = ""; int i = s.length(), j = t.length(); 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 { res = s[i - 1] + res; i--; j--; } } cout << res << endl; return 0; }
#include <bits/stdc++.h> #include <iostream> using namespace std; const long long int INF = 1LL << 60; int main() { string s, t; cin >> s; cin >> t; vector<vector<int>> dp(s.length() + 1, vector<int>(t.length() + 1, 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; } else { dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]); } } } string res = ""; int i = s.length(), j = t.length(); 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 { res = s[i - 1] + res; i--; j--; } } cout << res << endl; return 0; }
replace
12
13
12
13
0
p03165
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; int f[3005][3005], l1, l2, p[3005][3005]; char s1[3005], s2[3005]; inline void print(int x, int y) { if (!x && !y) return; if (p[x][y] == 1) { print(x - 1, y - 1); printf("%c", s1[x]); } else if (p[x][y] == 3) print(x, y - 1); else print(x - 1, y); } int main() { scanf("%s%s", s1 + 1, s2 + 1); l1 = strlen(s1 + 1), l2 = strlen(s2 + 1); for (int i = 1; i <= l1; i++) for (int j = 1; j <= l2; j++) { if (s1[i] == s2[j]) f[i][j] = f[i - 1][j - 1] + 1, p[i][j] = 1; if (f[i - 1][j] > f[i][j]) f[i][j] = f[i - 1][j], p[i][j] = 2; if (f[i][j - 1] > f[i][j]) f[i][j] = f[i][j - 1], p[i][j] = 3; } print(l1, l2); }
#include <algorithm> #include <cstdio> #include <cstring> using namespace std; int f[3005][3005], l1, l2, p[3005][3005]; char s1[3005], s2[3005]; inline void print(int x, int y) { if (!x && !y) return; if (p[x][y] == 1) { print(x - 1, y - 1); printf("%c", s1[x]); } else if (p[x][y] == 3) print(x, y - 1); else print(x - 1, y); } int main() { scanf("%s%s", s1 + 1, s2 + 1); l1 = strlen(s1 + 1), l2 = strlen(s2 + 1); for (int i = 1; i <= l1; i++) p[i][0] = 2; for (int j = 1; j <= l2; j++) p[0][j] = 3; for (int i = 1; i <= l1; i++) for (int j = 1; j <= l2; j++) { if (s1[i] == s2[j]) f[i][j] = f[i - 1][j - 1] + 1, p[i][j] = 1; if (f[i - 1][j] > f[i][j]) f[i][j] = f[i - 1][j], p[i][j] = 2; if (f[i][j - 1] > f[i][j]) f[i][j] = f[i][j - 1], p[i][j] = 3; } print(l1, l2); }
insert
21
21
21
25
0
p03165
C++
Runtime Error
#include <iostream> #include <string> using namespace std; int main() { string s1, s2, ans = ""; cin >> s1 >> s2; int l1 = s1.size(), l2 = s2.size(); int dp[l2 + 1][l1 + 1]; for (int i = 0; i < l1 + 1; ++i) for (int j = 0; j < l2 + 1; ++j) dp[i][j] = 0; for (int i = 1; i < l2 + 1; ++i) for (int j = 1; j < l1 + 1; ++j) if (s2[i - 1] == s1[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 ci = l2, cj = l1; while (dp[ci][cj] > 0) { if (dp[ci][cj] == dp[ci][cj - 1]) --cj; else if (dp[ci][cj] == dp[ci - 1][cj]) --ci; else { ans = s1[cj - 1] + ans; --ci; --cj; } } cout << ans; }
#include <iostream> #include <string> using namespace std; int main() { string s1, s2, ans = ""; cin >> s1 >> s2; int l1 = s1.size(), l2 = s2.size(); int dp[l2 + 1][l1 + 1]; for (int i = 0; i < l2 + 1; ++i) for (int j = 0; j < l1 + 1; ++j) dp[i][j] = 0; for (int i = 1; i < l2 + 1; ++i) for (int j = 1; j < l1 + 1; ++j) if (s2[i - 1] == s1[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 ci = l2, cj = l1; while (dp[ci][cj] > 0) { if (dp[ci][cj] == dp[ci][cj - 1]) --cj; else if (dp[ci][cj] == dp[ci - 1][cj]) --ci; else { ans = s1[cj - 1] + ans; --ci; --cj; } } cout << ans; }
replace
9
11
9
11
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define int long long int #define IOS ios_base::sync_with_stdio(false), cin.tie(NULL); #define fr(i, a, b) for (int i = a; i <= b; i++) #define rf(j, b, a) for (int j = b; j >= a; j--) #define frr(k, a, b) for (int k = a; k <= b; k++) #define mp make_pair #define pb push_back #define MOD 1000000007 using namespace std; typedef pair<int, int> pii; typedef pair<pii, int> ppii; signed main() { IOS #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif string s1, s2; cin >> s1; cin >> s2; int l1 = s1.size(); int l2 = s2.size(); int dp[l1 + 1][l2 + 1]; for (int i = 0; i <= l1; i++) { for (int j = 0; j <= l2; j++) { 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 = ""; int i = l1, j = l2; while (j > 0 && i > 0) { if (s1[i - 1] == s2[j - 1]) { ans = s1[i - 1] + ans; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } cout << ans << endl; }
#include <bits/stdc++.h> #define int long long int #define IOS ios_base::sync_with_stdio(false), cin.tie(NULL); #define fr(i, a, b) for (int i = a; i <= b; i++) #define rf(j, b, a) for (int j = b; j >= a; j--) #define frr(k, a, b) for (int k = a; k <= b; k++) #define mp make_pair #define pb push_back #define MOD 1000000007 using namespace std; typedef pair<int, int> pii; typedef pair<pii, int> ppii; signed main() { IOS /* #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif*/ string s1, s2; cin >> s1; cin >> s2; int l1 = s1.size(); int l2 = s2.size(); int dp[l1 + 1][l2 + 1]; for (int i = 0; i <= l1; i++) { for (int j = 0; j <= l2; j++) { 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 = ""; int i = l1, j = l2; while (j > 0 && i > 0) { if (s1[i - 1] == s2[j - 1]) { ans = s1[i - 1] + ans; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } cout << ans << endl; }
replace
14
19
14
20
0
p03165
C++
Runtime Error
/* Coded by peyha X */ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int NMAX = 1e3 + 1, AUCUN = 0; string s, t; char used[NMAX][NMAX]; int n, m, dp[NMAX][NMAX]; pair<int, int> last[NMAX][NMAX]; void retrieve(int k, int l) { if (k == 0 || l == 0) { return; } int nextK = last[k][l].first, nextL = last[k][l].second; retrieve(nextK, nextL); if (used[k][l] != AUCUN) { cout << used[k][l]; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s >> t; n = s.size(); m = t.size(); for (int k = 0; k <= n; k++) { used[k][0] = AUCUN; } for (int l = 0; l <= m; l++) { used[0][l] = AUCUN; } for (int k = 1; k <= n; k++) { for (int l = 1; l <= m; l++) { // cout << k << ' ' << l << endl; if (dp[k - 1][l] > dp[k][l]) { dp[k][l] = dp[k - 1][l]; used[k][l] = AUCUN; last[k][l] = make_pair(k - 1, l); } if (dp[k][l - 1] > dp[k][l]) { dp[k][l] = dp[k][l - 1]; used[k][l] = AUCUN; last[k][l] = make_pair(k, l - 1); } if (s[k - 1] == t[l - 1] && dp[k - 1][l - 1] + 1 > dp[k][l]) { dp[k][l] = dp[k - 1][l - 1] + 1; used[k][l] = s[k - 1]; last[k][l] = make_pair(k - 1, l - 1); } } } retrieve(n, m); cout << '\n'; }
/* Coded by peyha X */ #include <bits/stdc++.h> using namespace std; typedef long long ll; const int NMAX = 3 * 1e3 + 2, AUCUN = 0; string s, t; char used[NMAX][NMAX]; int n, m, dp[NMAX][NMAX]; pair<int, int> last[NMAX][NMAX]; void retrieve(int k, int l) { if (k == 0 || l == 0) { return; } int nextK = last[k][l].first, nextL = last[k][l].second; retrieve(nextK, nextL); if (used[k][l] != AUCUN) { cout << used[k][l]; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s >> t; n = s.size(); m = t.size(); for (int k = 0; k <= n; k++) { used[k][0] = AUCUN; } for (int l = 0; l <= m; l++) { used[0][l] = AUCUN; } for (int k = 1; k <= n; k++) { for (int l = 1; l <= m; l++) { // cout << k << ' ' << l << endl; if (dp[k - 1][l] > dp[k][l]) { dp[k][l] = dp[k - 1][l]; used[k][l] = AUCUN; last[k][l] = make_pair(k - 1, l); } if (dp[k][l - 1] > dp[k][l]) { dp[k][l] = dp[k][l - 1]; used[k][l] = AUCUN; last[k][l] = make_pair(k, l - 1); } if (s[k - 1] == t[l - 1] && dp[k - 1][l - 1] + 1 > dp[k][l]) { dp[k][l] = dp[k - 1][l - 1] + 1; used[k][l] = s[k - 1]; last[k][l] = make_pair(k - 1, l - 1); } } } retrieve(n, m); cout << '\n'; }
replace
12
13
12
13
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define forn(i, n) for (int i = 0; i < (int)n; ++i) #define rfor(i, n) for (int i = n - 1; i >= 0; --i) #define rmod(x, y) (((x % y) + y) % y) using namespace std; typedef vector<int>::iterator vit; typedef unsigned int uint; typedef unsigned short ushort; typedef long long ll; const int MAXN = 1024; // OPERATORS // GLOBALS // FUNCTIONS int arr[3001][3001]; int main() { ios::sync_with_stdio(0); cin.tie(0); string a, b; cin >> a >> b; for (int i = 0; i < 3001; i++) { arr[0][i] = 0; } for (int i = 0; i < 3001; i++) { arr[i][0] = 0; } // 1 \ 2| 3 _ for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { if (a[i] == b[j]) { arr[i + 1][j + 1] = arr[i][j] + 1; } else { arr[i + 1][j + 1] = max(arr[i][j + 1], arr[i + 1][j]); } } } int ii, jj; int mx = 0; for (int i = 0; i < a.size() + 1; i++) { for (int j = 0; j < b.size() + 1; j++) { if (arr[i + 1][j + 1] > mx) { ii = i + 1; jj = j + 1; mx = arr[i + 1][j + 1]; } } } string f = ""; while (arr[ii][jj]) { if (a[ii - 1] == b[jj - 1]) { f += a[ii - 1]; ii--; jj--; } else { if (arr[ii - 1][jj] > arr[ii][jj - 1]) { ii = ii - 1; } else { jj = jj - 1; } } } reverse(f.begin(), f.end()); cout << f << endl; return 0; }
#include <bits/stdc++.h> #define forn(i, n) for (int i = 0; i < (int)n; ++i) #define rfor(i, n) for (int i = n - 1; i >= 0; --i) #define rmod(x, y) (((x % y) + y) % y) using namespace std; typedef vector<int>::iterator vit; typedef unsigned int uint; typedef unsigned short ushort; typedef long long ll; const int MAXN = 1024; // OPERATORS // GLOBALS // FUNCTIONS int arr[3001][3001]; int main() { ios::sync_with_stdio(0); cin.tie(0); string a, b; cin >> a >> b; for (int i = 0; i < 3001; i++) { arr[0][i] = 0; } for (int i = 0; i < 3001; i++) { arr[i][0] = 0; } // 1 \ 2| 3 _ for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { if (a[i] == b[j]) { arr[i + 1][j + 1] = arr[i][j] + 1; } else { arr[i + 1][j + 1] = max(arr[i][j + 1], arr[i + 1][j]); } } } int ii, jj; int mx = 0; for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { if (arr[i + 1][j + 1] > mx) { ii = i + 1; jj = j + 1; mx = arr[i + 1][j + 1]; } } } string f = ""; while (arr[ii][jj]) { if (a[ii - 1] == b[jj - 1]) { f += a[ii - 1]; ii--; jj--; } else { if (arr[ii - 1][jj] > arr[ii][jj - 1]) { ii = ii - 1; } else { jj = jj - 1; } } } reverse(f.begin(), f.end()); cout << f << endl; return 0; }
replace
41
43
41
43
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; using namespace std; #define MX 1003 int dp[MX][MX]; int main() { string s, t; 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] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } stack<int> st; int i = s.size(); int j = t.size(); while (dp[i][j] != 0) { if (dp[i][j] == dp[i - 1][j]) i = i - 1; else if (dp[i][j] == dp[i][j - 1]) j = j - 1; else { st.push(i); i = i - 1; j = j - 1; } } while (!st.empty()) cout << s[st.top() - 1], st.pop(); cout << endl; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; #define MX 3003 int dp[MX][MX]; int main() { string s, t; 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] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } stack<int> st; int i = s.size(); int j = t.size(); while (dp[i][j] != 0) { if (dp[i][j] == dp[i - 1][j]) i = i - 1; else if (dp[i][j] == dp[i][j - 1]) j = j - 1; else { st.push(i); i = i - 1; j = j - 1; } } while (!st.empty()) cout << s[st.top() - 1], st.pop(); cout << endl; }
replace
3
4
3
4
0
p03165
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> using namespace std; #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define endl "\n" #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define F first #define S second #define bitcnt(v) __builtin_popcount(v) #define all(v) v.begin(), v.end() void solution() { string s, t; cin >> s >> t; int m = s.size(), n = t.size(); pii dp[m + 1][n + 1]; for (int i = 0; i <= m; i++) { dp[i][0] = {0, 0}; } for (int i = 0; i <= n; i++) { dp[0][i] = {0, 0}; } pii cur = {-1, -1}; int best = 0; for (int i = 1; i <= (int)s.size(); i++) { for (int j = 1; j <= (int)t.size(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = {1 + dp[i - 1][j - 1].F, 3}; } else { if (dp[i][j - 1].F < dp[i - 1][j].F) { dp[i][j] = {dp[i - 1][j].F, 2}; } else { dp[i][j] = {dp[i][j - 1].F, 1}; } } if (best <= dp[i][j].F) { best = dp[i][j].F; cur = {i, j}; } } } string ans = ""; while (cur.F > 0 || cur.S > 0) { pii p = dp[cur.F][cur.S]; if (p.S == 3) { ans.push_back(s[cur.F - 1]); cur.F--; cur.S--; } else if (p.S == 1) cur.S--; else cur.F--; } reverse(all(ans)); cout << ans << endl; } int main() { fio; // fast input output int t; t = 1; // cin >> t; for (int i = 1; i <= t; i++) { // GOOGLE // // cout << "Case #" << i << ": "; solution(); } return 0; }
#include <algorithm> #include <bits/stdc++.h> using namespace std; #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define endl "\n" #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define F first #define S second #define bitcnt(v) __builtin_popcount(v) #define all(v) v.begin(), v.end() void solution() { string s, t; cin >> s >> t; int m = s.size(), n = t.size(); pii dp[m + 1][n + 1]; for (int i = 0; i <= m; i++) { dp[i][0] = {0, 0}; } for (int i = 0; i <= n; i++) { dp[0][i] = {0, 0}; } pii cur = {-1, -1}; int best = 0; for (int i = 1; i <= (int)s.size(); i++) { for (int j = 1; j <= (int)t.size(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = {1 + dp[i - 1][j - 1].F, 3}; } else { if (dp[i][j - 1].F < dp[i - 1][j].F) { dp[i][j] = {dp[i - 1][j].F, 2}; } else { dp[i][j] = {dp[i][j - 1].F, 1}; } } if (best <= dp[i][j].F) { best = dp[i][j].F; cur = {i, j}; } } } string ans = ""; while (cur.F > 0 && cur.S > 0) { pii p = dp[cur.F][cur.S]; if (p.S == 3) { ans.push_back(s[cur.F - 1]); cur.F--; cur.S--; } else if (p.S == 1) cur.S--; else cur.F--; } reverse(all(ans)); cout << ans << endl; } int main() { fio; // fast input output int t; t = 1; // cin >> t; for (int i = 1; i <= t; i++) { // GOOGLE // // cout << "Case #" << i << ": "; solution(); } return 0; }
replace
55
56
55
56
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; int m = s1.length(), n = s2.length(); int lookup[m + 1][n + 1]; memset(lookup, 0, sizeof lookup); int i, j; for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { if (s1[i - 1] == s2[j - 1]) lookup[i][j] = 1 + lookup[i - 1][j - 1]; else lookup[i][j] = max(lookup[i][j - 1], lookup[i - 1][j]); } } // printf("%d\n", lookup[m][n]); int count = 0; i = m, j = n; string ans = ""; while (count <= lookup[m][n]) { if (s1[i - 1] == s2[j - 1]) { ans += s1[i - 1]; i--; j--; count++; } else { if (lookup[i][j - 1] > lookup[i - 1][j]) j--; else i--; } } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; int m = s1.length(), n = s2.length(); int lookup[m + 1][n + 1]; memset(lookup, 0, sizeof lookup); int i, j; for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { if (s1[i - 1] == s2[j - 1]) lookup[i][j] = 1 + lookup[i - 1][j - 1]; else lookup[i][j] = max(lookup[i][j - 1], lookup[i - 1][j]); } } // printf("%d\n", lookup[m][n]); int count = 0; i = m, j = n; string ans = ""; while (count < lookup[m][n]) { if (s1[i - 1] == s2[j - 1]) { ans += s1[i - 1]; i--; j--; count++; } else { if (lookup[i][j - 1] > lookup[i - 1][j]) j--; else i--; } } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
replace
25
26
25
26
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string i1; string i2; cin >> i1 >> i2; long int Arr[i1.length() + 1][i2.length() + 1]; for (long int i = 0; i <= i2.length(); i++) { for (long int j = 0; j <= i1.length(); j++) { if (i == 0 || j == 0) { Arr[i][j] = 0; } else if (i2[i - 1] == i1[j - 1]) { Arr[i][j] = Arr[i - 1][j - 1] + 1; } else { Arr[i][j] = max(Arr[i - 1][j], Arr[i][j - 1]); } } } long int a = i1.length(), b = i2.length(); if (Arr[b][a] > 0) { int e = Arr[b][a] - 1; int d = Arr[b][a]; char Brr[d]; while (true) { if (a == 0 || b == 0) break; if (Arr[b][a] == Arr[b][a - 1]) { a--; } else if (Arr[b][a] == Arr[b - 1][a]) { b--; } else { Brr[e] = i1[a - 1]; b--; a--; e--; } } for (int h = 0; h < d; h++) { cout << Brr[h]; } } }
#include <bits/stdc++.h> using namespace std; int main() { string i1; string i2; cin >> i1 >> i2; long int Arr[i2.length() + 1][i1.length() + 1]; for (long int i = 0; i <= i2.length(); i++) { for (long int j = 0; j <= i1.length(); j++) { if (i == 0 || j == 0) { Arr[i][j] = 0; } else if (i2[i - 1] == i1[j - 1]) { Arr[i][j] = Arr[i - 1][j - 1] + 1; } else { Arr[i][j] = max(Arr[i - 1][j], Arr[i][j - 1]); } } } long int a = i1.length(), b = i2.length(); if (Arr[b][a] > 0) { int e = Arr[b][a] - 1; int d = Arr[b][a]; char Brr[d]; while (true) { if (a == 0 || b == 0) break; if (Arr[b][a] == Arr[b][a - 1]) { a--; } else if (Arr[b][a] == Arr[b - 1][a]) { b--; } else { Brr[e] = i1[a - 1]; b--; a--; e--; } } for (int h = 0; h < d; h++) { cout << Brr[h]; } } }
replace
6
7
6
7
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, t, res = ""; cin >> s >> t; int n = s.size(), m = t.size(), i, j; int b[n + 2][m + 2]; for (i = 0; i <= n; i++) { b[i][0] = 0; } for (i = 0; i <= m; i++) { b[0][i] = 0; } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) b[i][j] = b[i - 1][j - 1] + 1; else b[i][j] = max(b[i - 1][j], b[i][j - 1]); // cout<<b[i][j]<<" "; } // cout<<"\n"; } // cout<<b[n][m]; i = n; j = m; while (i >= 0 || j >= 0) { if (b[i][j] > max(b[i - 1][j], b[i][j - 1])) { res += t[j - 1]; i = i - 1; j = j - 1; } else { if (b[i - 1][j] > b[i][j - 1]) { i = i - 1; j = j; } else { i = i; j = j - 1; } } } reverse(res.begin(), res.end()); cout << res << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t, res = ""; cin >> s >> t; int n = s.size(), m = t.size(), i, j; int b[n + 2][m + 2]; for (i = 0; i <= n; i++) { b[i][0] = 0; } for (i = 0; i <= m; i++) { b[0][i] = 0; } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) b[i][j] = b[i - 1][j - 1] + 1; else b[i][j] = max(b[i - 1][j], b[i][j - 1]); // cout<<b[i][j]<<" "; } // cout<<"\n"; } // cout<<b[n][m]; i = n; j = m; while (i >= 0 && j >= 0) { if (b[i][j] > max(b[i - 1][j], b[i][j - 1])) { res += t[j - 1]; i = i - 1; j = j - 1; } else { if (b[i - 1][j] > b[i][j - 1]) { i = i - 1; j = j; } else { i = i; j = j - 1; } } } reverse(res.begin(), res.end()); cout << res << "\n"; return 0; }
replace
26
27
26
27
0
p03165
C++
Runtime Error
#include <iostream> #include <string> using namespace std; // define maximum possible length of string X and Y #define MAX 20 // lookup[i][j] stores the length of LCS of substring X[0..i-1], Y[0..j-1] int lookup[MAX][MAX]; // Recursive Function to find Longest Common Subsequence of // string X[0..m-1] and Y[0..n-1] string LCS(string X, string Y, int m, int n) { // return empty string if we have reached the end of // either sequence if (m == 0 || n == 0) return string(""); // if last character of X and Y matches if (X[m - 1] == Y[n - 1]) { // append current character (X[m-1] or Y[n-1]) to LCS of // substring X[0..m-2] and Y[0..n-2] return LCS(X, Y, m - 1, n - 1) + X[m - 1]; } // else when the last character of X and Y are different // if top cell of current cell has more value than the left // cell, then drop current character of string X and find LCS // of substring X[0..m-2], Y[0..n-1] if (lookup[m - 1][n] > lookup[m][n - 1]) return LCS(X, Y, m - 1, n); else // if left cell of current cell has more value than the top // cell, then drop current character of string Y and find LCS // of substring X[0..m-1], Y[0..n-2] return LCS(X, Y, m, n - 1); } // Function to fill lookup table by finding the length of LCS // of substring X[0..m-1] and Y[0..n-1] void LCSLength(string X, string Y, int m, int n) { // first row and first column of the lookup table // are already 0 as lookup[][] is globally declared // fill the lookup table in bottom-up manner for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { // if current character of X and Y matches if (X[i - 1] == Y[j - 1]) lookup[i][j] = lookup[i - 1][j - 1] + 1; // else if current character of X and Y don't match else lookup[i][j] = max(lookup[i - 1][j], lookup[i][j - 1]); } } } int main() { string X, Y; cin >> X >> Y; int m = X.length(), n = Y.length(); // fill lookup table LCSLength(X, Y, m, n); // find longest common sequence cout << LCS(X, Y, m, n); return 0; }
#include <iostream> #include <string> using namespace std; // define maximum possible length of string X and Y #define MAX 3003 // lookup[i][j] stores the length of LCS of substring X[0..i-1], Y[0..j-1] int lookup[MAX][MAX]; // Recursive Function to find Longest Common Subsequence of // string X[0..m-1] and Y[0..n-1] string LCS(string X, string Y, int m, int n) { // return empty string if we have reached the end of // either sequence if (m == 0 || n == 0) return string(""); // if last character of X and Y matches if (X[m - 1] == Y[n - 1]) { // append current character (X[m-1] or Y[n-1]) to LCS of // substring X[0..m-2] and Y[0..n-2] return LCS(X, Y, m - 1, n - 1) + X[m - 1]; } // else when the last character of X and Y are different // if top cell of current cell has more value than the left // cell, then drop current character of string X and find LCS // of substring X[0..m-2], Y[0..n-1] if (lookup[m - 1][n] > lookup[m][n - 1]) return LCS(X, Y, m - 1, n); else // if left cell of current cell has more value than the top // cell, then drop current character of string Y and find LCS // of substring X[0..m-1], Y[0..n-2] return LCS(X, Y, m, n - 1); } // Function to fill lookup table by finding the length of LCS // of substring X[0..m-1] and Y[0..n-1] void LCSLength(string X, string Y, int m, int n) { // first row and first column of the lookup table // are already 0 as lookup[][] is globally declared // fill the lookup table in bottom-up manner for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { // if current character of X and Y matches if (X[i - 1] == Y[j - 1]) lookup[i][j] = lookup[i - 1][j - 1] + 1; // else if current character of X and Y don't match else lookup[i][j] = max(lookup[i - 1][j], lookup[i][j - 1]); } } } int main() { string X, Y; cin >> X >> Y; int m = X.length(), n = Y.length(); // fill lookup table LCSLength(X, Y, m, n); // find longest common sequence cout << LCS(X, Y, m, n); return 0; }
replace
5
6
5
6
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; // constant const ll MOD = 1e+9 + 7; const double EPS = 1e-10; #define REP(i, n) for (ll i = 0; i < n; i++) #define FOR(i, s, e) for (ll i = s; i < e; i++) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() // debug #define DUMP(x) cerr << #x << " = " << (x) << endl; #define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl; // view vector template <typename T> void view(T e) { std::cout << e << std::endl; } template <typename T> void view(const std::vector<T> &v) { for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void view(const std::vector<std::vector<T>> &vv) { for (const auto &v : vv) { view(v); } } // DP 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; } // *************************************** int main(void) { string S, T; cin >> S >> T; vector<vector<int>> dp(3000, vector<int>(3000)); REP(i, S.size()) { REP(j, T.size()) { if (S[i] == T[j]) chmax<int>(dp[i + 1][j + 1], dp[i][j] + 1); chmax<int>(dp[i + 1][j + 1], dp[i][j + 1]); chmax<int>(dp[i + 1][j + 1], dp[i + 1][j]); } } int i = S.size(); int j = T.size(); string ans{}; 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] + ans; i--; j--; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; // constant const ll MOD = 1e+9 + 7; const double EPS = 1e-10; #define REP(i, n) for (ll i = 0; i < n; i++) #define FOR(i, s, e) for (ll i = s; i < e; i++) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() // debug #define DUMP(x) cerr << #x << " = " << (x) << endl; #define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl; // view vector template <typename T> void view(T e) { std::cout << e << std::endl; } template <typename T> void view(const std::vector<T> &v) { for (const auto &e : v) { std::cout << e << " "; } std::cout << std::endl; } template <typename T> void view(const std::vector<std::vector<T>> &vv) { for (const auto &v : vv) { view(v); } } // DP 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; } // *************************************** int main(void) { string S, T; cin >> S >> T; vector<vector<int>> dp(3001, vector<int>(3001)); REP(i, S.size()) { REP(j, T.size()) { if (S[i] == T[j]) chmax<int>(dp[i + 1][j + 1], dp[i][j] + 1); chmax<int>(dp[i + 1][j + 1], dp[i][j + 1]); chmax<int>(dp[i + 1][j + 1], dp[i + 1][j]); } } int i = S.size(); int j = T.size(); string ans{}; 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] + ans; i--; j--; } } cout << ans << endl; return 0; }
replace
55
56
55
56
0
p03165
C++
Runtime Error
// Code by : Abhishek Tiwari // codechef.com/users/devil_within || hackerearth.com/@devil_within #include <bits/stdc++.h> #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define int long long int #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define repr(i, a, b) for (int i = (a); i > (b); --i) #define mp make_pair #define pb push_back #define pf push_front #define pii pair<int, int> #define ff first #define ss second #define mod 1000000007 #define inf 1000000000 #define gcd __gcd #define lcm(a, b) ((a) * (b)) / gcd(a, b) using namespace std; /*******************************************************************************************/ /*******************************************************************************************/ template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; for (int i = 0; i < v.size() - 1; ++i) os << v[i] << ", "; os << v[v.size() - 1] << "]\n"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "{"; for (auto it : v) { os << it; if (it != *v.rbegin()) os << ", "; } os << "}\n"; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &v) { os << "{"; for (auto it : v) { os << "(" << it.first << " : " << it.second << ")"; if (it != *v.rbegin()) os << ", "; } os << "}\n"; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << "("; os << v.first << ", " << v.second << ")"; return os; } template <typename T> T maxi(T a, T b) { return a > b ? a : b; } template <typename T> T mini(T a, T b) { return a < b ? a : b; } /*******************************************************************************************/ /*******************************************************************************************/ int fpow(int x, int y, int p); vector<vector<int>> dp; int Lcs(int i, int j, string &s, string &t) { 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 + Lcs(i + 1, j + 1, s, t); else dp[i][j] = maxi(Lcs(i + 1, j, s, t), Lcs(i, j + 1, s, t)); return dp[i][j]; } string findLcs(int i, int j, string &s, string &t) { if (i >= s.length() || j >= t.length()) return ""; if (s[i] == t[j]) return s[i] + findLcs(i + 1, j + 1, s, t); else if (dp[i + 1][j] > dp[i][j + 1]) return findLcs(i + 1, j, s, t); else return findLcs(i, j + 1, s, t); } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif fio; int tcases = 1; // cin>>tcases; for (int tcase = 1; tcase <= tcases; ++tcase) { string s, t; cin >> s >> t; dp.resize(s.length() + 1); rep(i, 0, s.length() + 1) dp[i].resize(t.length() + 1, -1); Lcs(0, 0, s, t); // for(auto i : dp) cout<<i; cout << findLcs(0, 0, s, t); // cout<<"Case #"<<tcase<<": "; } return 0; } // Functions int fpow(int x, int y, int p) { int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }
// Code by : Abhishek Tiwari // codechef.com/users/devil_within || hackerearth.com/@devil_within #include <bits/stdc++.h> #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define int long long int #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define repr(i, a, b) for (int i = (a); i > (b); --i) #define mp make_pair #define pb push_back #define pf push_front #define pii pair<int, int> #define ff first #define ss second #define mod 1000000007 #define inf 1000000000 #define gcd __gcd #define lcm(a, b) ((a) * (b)) / gcd(a, b) using namespace std; /*******************************************************************************************/ /*******************************************************************************************/ template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "["; for (int i = 0; i < v.size() - 1; ++i) os << v[i] << ", "; os << v[v.size() - 1] << "]\n"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "{"; for (auto it : v) { os << it; if (it != *v.rbegin()) os << ", "; } os << "}\n"; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &v) { os << "{"; for (auto it : v) { os << "(" << it.first << " : " << it.second << ")"; if (it != *v.rbegin()) os << ", "; } os << "}\n"; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << "("; os << v.first << ", " << v.second << ")"; return os; } template <typename T> T maxi(T a, T b) { return a > b ? a : b; } template <typename T> T mini(T a, T b) { return a < b ? a : b; } /*******************************************************************************************/ /*******************************************************************************************/ int fpow(int x, int y, int p); vector<vector<int>> dp; int Lcs(int i, int j, string &s, string &t) { 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 + Lcs(i + 1, j + 1, s, t); else dp[i][j] = maxi(Lcs(i + 1, j, s, t), Lcs(i, j + 1, s, t)); return dp[i][j]; } string findLcs(int i, int j, string &s, string &t) { if (i >= s.length() || j >= t.length()) return ""; if (s[i] == t[j]) return s[i] + findLcs(i + 1, j + 1, s, t); else if (dp[i + 1][j] > dp[i][j + 1]) return findLcs(i + 1, j, s, t); else return findLcs(i, j + 1, s, t); } int32_t main() { #ifndef ONLINE_JUDGE // freopen("input.txt" , "r" , stdin);freopen("output.txt" , "w" , // stdout);freopen("error.txt" , "w" , stderr); #endif fio; int tcases = 1; // cin>>tcases; for (int tcase = 1; tcase <= tcases; ++tcase) { string s, t; cin >> s >> t; dp.resize(s.length() + 1); rep(i, 0, s.length() + 1) dp[i].resize(t.length() + 1, -1); Lcs(0, 0, s, t); // for(auto i : dp) cout<<i; cout << findLcs(0, 0, s, t); // cout<<"Case #"<<tcase<<": "; } return 0; } // Functions int fpow(int x, int y, int p) { int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }
replace
87
90
87
89
0
p03165
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; const int MAX = 1010; string a, b; int dp[MAX][MAX]; int sol(int m, int n) { if (!m or !n) return 0; int &ans = dp[m][n]; if (ans != -1) return ans; if (a[m - 1] == b[n - 1]) ans = 1 + sol(m - 1, n - 1); else ans = max(sol(m, n - 1), sol(m - 1, n)); return ans; } int main() { cin >> a >> b; memset(dp, -1, sizeof dp); sol(a.size(), b.size()); string ans = ""; int i = a.size(), j = b.size(); while (i > 0 and j > 0) { if (a[i - 1] == b[j - 1]) { i--, j--; ans.push_back(a[i]); } 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; const int MAX = 3010; string a, b; int dp[MAX][MAX]; int sol(int m, int n) { if (!m or !n) return 0; int &ans = dp[m][n]; if (ans != -1) return ans; if (a[m - 1] == b[n - 1]) ans = 1 + sol(m - 1, n - 1); else ans = max(sol(m, n - 1), sol(m - 1, n)); return ans; } int main() { cin >> a >> b; memset(dp, -1, sizeof dp); sol(a.size(), b.size()); string ans = ""; int i = a.size(), j = b.size(); while (i > 0 and j > 0) { if (a[i - 1] == b[j - 1]) { i--, j--; ans.push_back(a[i]); } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
3
4
3
4
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAX 1010 string a, b; int dp[MAX][MAX]; int get(int i = 0, int j = 0) { if (i == a.size() || j == b.size()) return 0; int &ret = dp[i][j]; if (ret + 1) return ret; ret = 0; if (a[i] == b[j]) return ret = 1 + get(i + 1, j + 1); return ret = max(get(i + 1, j), get(i, j + 1)); } void getPath(int i = 0, int j = 0) { if (i == a.size() || j == b.size()) { cout << '\n'; return; } int ret = get(i, j); if (a[i] == b[j]) { cout << a[i]; getPath(i + 1, j + 1); return; } if (ret == get(i + 1, j)) getPath(i + 1, j); else getPath(i, j + 1); } int main() { cin >> a >> b; memset(dp, -1, sizeof dp); getPath(0, 0); return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX 3010 string a, b; int dp[MAX][MAX]; int get(int i = 0, int j = 0) { if (i == a.size() || j == b.size()) return 0; int &ret = dp[i][j]; if (ret + 1) return ret; ret = 0; if (a[i] == b[j]) return ret = 1 + get(i + 1, j + 1); return ret = max(get(i + 1, j), get(i, j + 1)); } void getPath(int i = 0, int j = 0) { if (i == a.size() || j == b.size()) { cout << '\n'; return; } int ret = get(i, j); if (a[i] == b[j]) { cout << a[i]; getPath(i + 1, j + 1); return; } if (ret == get(i + 1, j)) getPath(i + 1, j); else getPath(i, j + 1); } int main() { cin >> a >> b; memset(dp, -1, sizeof dp); getPath(0, 0); return 0; }
replace
3
4
3
4
0