problem_id
stringlengths
6
6
language
stringclasses
2 values
original_status
stringclasses
3 values
original_src
stringlengths
19
243k
changed_src
stringlengths
19
243k
change
stringclasses
3 values
i1
int64
0
8.44k
i2
int64
0
8.44k
j1
int64
0
8.44k
j2
int64
0
8.44k
error
stringclasses
270 values
stderr
stringlengths
0
226k
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define N (int)3e2 + 5 #define f1 first #define s2 second #define INF 9999999 #define pb push_back #define ll long long #define ull unsigned long long; #define all(a) a.begin(), a.end() #define fri(a) freopen(a, "r", stdin); #define fro(a) freopen(a, "w", stdout); string str1, str2, ans; int dp[N][N], n, m; void lcs() { 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 (str1[i - 1] == str2[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 main() { // fri("in.txt"); // fro("out.txt"); cin >> str1 >> str2; n = str1.length(); m = str2.length(); memset(dp, -1, sizeof(dp)); lcs(); while (n > 0 && m > 0) { if (str1[n - 1] == str2[m - 1]) { ans.pb(str1[n - 1]); 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> using namespace std; #define N (int)3e3 + 5 #define f1 first #define s2 second #define INF 9999999 #define pb push_back #define ll long long #define ull unsigned long long; #define all(a) a.begin(), a.end() #define fri(a) freopen(a, "r", stdin); #define fro(a) freopen(a, "w", stdout); string str1, str2, ans; int dp[N][N], n, m; void lcs() { 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 (str1[i - 1] == str2[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 main() { // fri("in.txt"); // fro("out.txt"); cin >> str1 >> str2; n = str1.length(); m = str2.length(); memset(dp, -1, sizeof(dp)); lcs(); while (n > 0 && m > 0) { if (str1[n - 1] == str2[m - 1]) { ans.pb(str1[n - 1]); n--; m--; } else if (dp[n - 1][m] > dp[n][m - 1]) { n--; } else m--; } reverse(all(ans)); cout << ans << endl; return 0; }
replace
3
4
3
4
0
p03165
C++
Runtime Error
// _/\\\\____________/\\\\______/\\\\\\\\\______/\\\________/\\\___/\\\\\\\\/\\____/\\\\\\\\\\_| // _\/\\\\\\________/\\\\\\____/\\\\\\\\\\\\\___\/\\\________/\\\__\/\\\____\/\\\__\////\\\///__| // _\/\\\//\\\____/\\\//\\\___/\\\/////////\\\__\/\\\________/\\\__\/\\\____\/\\\\____\/\\\_____| // _\/\\\\///\\\/\\\/_\/\\\__\/\\\_______\/\\\__\/\\\\\\\\\\\\\\\__\/\\\____\/\\\\____\/\\\_____| // _\/\\\__\///\\\/___\/\\\__\/\\\\\\\\\\\\\\\__\/\\\/////////\\\__\/\\\____\/\\\\____\/\\\_____| // _\/\\\____\///_____\/\\\__\/\\\/////////\\\__\/\\\_______\/\\\__\/\\\____\/\\\_____\/\\\_____| // _\/\\\_____________\/\\\__\/\\\_______\/\\\__\/\\\_______\/\\\__\/\\\___/\\\\______\/\\\_____| // _\/\\\_____________\/\\\__\/\\\_______\/\\\__\/\\\_______\/\\\__\/\\\\\\/\\_____/\\\\\\\\\\__| // _\///______________\///___\///________\///___\///________\///___\/////////_____\//////////___| #include <bits/stdc++.h> using namespace std; typedef string str; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<char> vc; typedef vector<string> vs; typedef vector<long long> vl; typedef vector<pair<int, int>> vpi; #define hurry \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define setp setprecision #define pop pop_back() #define pb push_back #define mp make_pair #define beg begin() #define sc second #define sz size() #define fr first #define endl '\n' //======================================================================================================================= // global int dp[1003][1002]; pair<int, int> par[1008][1009]; vc v; //======================================================================================================================= // functions void lcs(str s, str t) { for (int i = 1; i <= s.sz; i++) { for (int j = 1; j <= t.sz; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; par[i][j].fr = i - 1; par[i][j].sc = j - 1; } else { if (dp[i - 1][j] >= dp[i][j - 1]) { dp[i][j] = dp[i - 1][j]; par[i][j].fr = i - 1; par[i][j].sc = j; } else { dp[i][j] = dp[i][j - 1]; par[i][j].fr = i; par[i][j].sc = j - 1; } } } } } //======================================================================================================================= int main() { str s, t; cin >> s >> t; lcs(s, t); int n = s.sz; int m = t.sz; while (n > 0 and m > 0) { if (par[n][m].fr == n - 1 and par[n][m].sc == m - 1) v.pb(s[n - 1]); pair<int, int> tmp = par[n][m]; n = tmp.fr; m = tmp.sc; } for (int i = v.sz - 1; i >= 0; i--) { cout << v[i]; } } //======================================================================================================================== // 4 // 1 4 2 3 // 4 1 2 3 // 1 2 4 3
// _/\\\\____________/\\\\______/\\\\\\\\\______/\\\________/\\\___/\\\\\\\\/\\____/\\\\\\\\\\_| // _\/\\\\\\________/\\\\\\____/\\\\\\\\\\\\\___\/\\\________/\\\__\/\\\____\/\\\__\////\\\///__| // _\/\\\//\\\____/\\\//\\\___/\\\/////////\\\__\/\\\________/\\\__\/\\\____\/\\\\____\/\\\_____| // _\/\\\\///\\\/\\\/_\/\\\__\/\\\_______\/\\\__\/\\\\\\\\\\\\\\\__\/\\\____\/\\\\____\/\\\_____| // _\/\\\__\///\\\/___\/\\\__\/\\\\\\\\\\\\\\\__\/\\\/////////\\\__\/\\\____\/\\\\____\/\\\_____| // _\/\\\____\///_____\/\\\__\/\\\/////////\\\__\/\\\_______\/\\\__\/\\\____\/\\\_____\/\\\_____| // _\/\\\_____________\/\\\__\/\\\_______\/\\\__\/\\\_______\/\\\__\/\\\___/\\\\______\/\\\_____| // _\/\\\_____________\/\\\__\/\\\_______\/\\\__\/\\\_______\/\\\__\/\\\\\\/\\_____/\\\\\\\\\\__| // _\///______________\///___\///________\///___\///________\///___\/////////_____\//////////___| #include <bits/stdc++.h> using namespace std; typedef string str; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<char> vc; typedef vector<string> vs; typedef vector<long long> vl; typedef vector<pair<int, int>> vpi; #define hurry \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define setp setprecision #define pop pop_back() #define pb push_back #define mp make_pair #define beg begin() #define sc second #define sz size() #define fr first #define endl '\n' //======================================================================================================================= // global int dp[3003][3002]; pair<int, int> par[3008][3009]; vc v; //======================================================================================================================= // functions void lcs(str s, str t) { for (int i = 1; i <= s.sz; i++) { for (int j = 1; j <= t.sz; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; par[i][j].fr = i - 1; par[i][j].sc = j - 1; } else { if (dp[i - 1][j] >= dp[i][j - 1]) { dp[i][j] = dp[i - 1][j]; par[i][j].fr = i - 1; par[i][j].sc = j; } else { dp[i][j] = dp[i][j - 1]; par[i][j].fr = i; par[i][j].sc = j - 1; } } } } } //======================================================================================================================= int main() { str s, t; cin >> s >> t; lcs(s, t); int n = s.sz; int m = t.sz; while (n > 0 and m > 0) { if (par[n][m].fr == n - 1 and par[n][m].sc == m - 1) v.pb(s[n - 1]); pair<int, int> tmp = par[n][m]; n = tmp.fr; m = tmp.sc; } for (int i = v.sz - 1; i >= 0; i--) { cout << v[i]; } } //======================================================================================================================== // 4 // 1 4 2 3 // 4 1 2 3 // 1 2 4 3
replace
34
36
34
36
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define pb push_back #define MOD 2019 #define inf 3e18 #define vi vector<int> #define vld vector<ld> #define pii pair<int, int> #define mii map<int, int> #define fi first #define se second #define fastIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #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...); } void inp_out() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("debug.txt", "w", stderr); #endif } int dp[3000][3000]; string s, t; int fun(int idx_s, int idx_t) { // db(idx_s,idx_t); if ((idx_s == s.size()) || (idx_t == t.size())) return dp[idx_s][idx_t] = 0; if (dp[idx_s][idx_t] != -1) return dp[idx_s][idx_t]; int &ans = dp[idx_s][idx_t]; if (s[idx_s] == t[idx_t]) ans = 1 + fun(idx_s + 1, idx_t + 1); ans = max(ans, fun(idx_s + 1, idx_t)); ans = max(ans, fun(idx_s, idx_t + 1)); return ans; } int32_t main() { fastIO // inp_out(); // string s,t; memset(dp, -1, sizeof(dp)); cin >> s >> t; fun(0, 0); string ans = ""; int idx_s = 0, idx_t = 0; while (idx_s < s.size() and idx_t < t.size()) { if ((dp[idx_s + 1][idx_t + 1] == dp[idx_s][idx_t] - 1) and (s[idx_s] == t[idx_t])) { ans += s[idx_s]; idx_s += 1; idx_t += 1; } else if (dp[idx_s][idx_t] == dp[idx_s + 1][idx_t]) idx_s += 1; else idx_t += 1; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define pb push_back #define MOD 2019 #define inf 3e18 #define vi vector<int> #define vld vector<ld> #define pii pair<int, int> #define mii map<int, int> #define fi first #define se second #define fastIO \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #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...); } void inp_out() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("debug.txt", "w", stderr); #endif } int dp[3005][3005]; string s, t; int fun(int idx_s, int idx_t) { // db(idx_s,idx_t); if ((idx_s == s.size()) || (idx_t == t.size())) return dp[idx_s][idx_t] = 0; if (dp[idx_s][idx_t] != -1) return dp[idx_s][idx_t]; int &ans = dp[idx_s][idx_t]; if (s[idx_s] == t[idx_t]) ans = 1 + fun(idx_s + 1, idx_t + 1); ans = max(ans, fun(idx_s + 1, idx_t)); ans = max(ans, fun(idx_s, idx_t + 1)); return ans; } int32_t main() { fastIO // inp_out(); // string s,t; memset(dp, -1, sizeof(dp)); cin >> s >> t; fun(0, 0); string ans = ""; int idx_s = 0, idx_t = 0; while (idx_s < s.size() and idx_t < t.size()) { if ((dp[idx_s + 1][idx_t + 1] == dp[idx_s][idx_t] - 1) and (s[idx_s] == t[idx_t])) { ans += s[idx_s]; idx_s += 1; idx_t += 1; } else if (dp[idx_s][idx_t] == dp[idx_s + 1][idx_t]) idx_s += 1; else idx_t += 1; } cout << ans; return 0; }
replace
36
37
36
37
0
p03165
C++
Runtime Error
/* lakshaygpt28 Lakshay Gupta */ #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; using ll = long long; using db = double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vll = vector<ll>; template <typename T> using OrderedSet = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>; #ifndef ONLINE_JUDGE #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std ::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define debug(...) #endif #define fast_io() \ ios_base ::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define eb emplace_back #define mp make_pair #define pb push_back #define mt make_tuple #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const db PI = acos(-1); const ll LINF = LLONG_MAX; const int INF = INT_MAX, MOD = 1e9 + 7, N = 1e3 + 10; ll dp[N][N]; int main() { fast_io(); string s, t; cin >> s >> t; ll len1 = s.length(), len2 = t.length(); s = "." + s; t = "." + t; for (ll i = 1; i <= len1; i++) { for (ll j = 1; j <= len2; j++) { dp[i][j] = max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (s[i] == t[j])}); } } string ans; ll i = len1, j = len2; while (i > 0 and j > 0) { if (s[i] == t[j]) { ans = s[i] + ans; i--, j--; } else if (dp[i - 1][j] >= dp[i][j - 1]) { i--; } else { j--; } } cout << ans << "\n"; // cout << dp[len1][len2] << "\n"; return 0; }
/* lakshaygpt28 Lakshay Gupta */ #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; using ll = long long; using db = double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vll = vector<ll>; template <typename T> using OrderedSet = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> using MinPriorityQueue = priority_queue<T, vector<T>, greater<T>>; #ifndef ONLINE_JUDGE #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << std ::endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define debug(...) #endif #define fast_io() \ ios_base ::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define eb emplace_back #define mp make_pair #define pb push_back #define mt make_tuple #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const db PI = acos(-1); const ll LINF = LLONG_MAX; const int INF = INT_MAX, MOD = 1e9 + 7, N = 3e3 + 10; ll dp[N][N]; int main() { fast_io(); string s, t; cin >> s >> t; ll len1 = s.length(), len2 = t.length(); s = "." + s; t = "." + t; for (ll i = 1; i <= len1; i++) { for (ll j = 1; j <= len2; j++) { dp[i][j] = max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (s[i] == t[j])}); } } string ans; ll i = len1, j = len2; while (i > 0 and j > 0) { if (s[i] == t[j]) { ans = s[i] + ans; i--, j--; } else if (dp[i - 1][j] >= dp[i][j - 1]) { i--; } else { j--; } } cout << ans << "\n"; // cout << dp[len1][len2] << "\n"; return 0; }
replace
51
52
51
52
0
p03165
C++
Runtime Error
// HTTF.cpp : このファイルには 'main' // 関数が含まれています。プログラム実行の開始と終了がそこで行われます。 // #include "bits/stdc++.h" #define YES "YES" #define NO "NO" #define YESNO OUT(three(solve(), YES, NO)) #define ECHO OUT(solve()) #define three(A, B, C) ((A) ? (B) : (C)) #define FOR(i, a, b) for (LL i = (a); i < (LL)(b); i++) #define EFOR(i, a, b) for (LL i = (a); i <= (LL)(b); i++) #define RFOR(i, a, b) for (LL i = (a); i >= (LL)(b); i--) #define REP(i, b) FOR(i, zero, b) #define EREP(i, b) EFOR(i, zero, b) #define RREP(i, b) RFOR(i, b, zero) #define ALL(c) c.begin(), c.end() #define UNIQUE(c) \ sort(ALL(c)); \ c.erase(unique(ALL(c)), c.end()) #define MAX(c) (*max_element(ALL(c))) #define MIN(c) (*min_element(ALL(c))) #define MP make_pair #define FI first #define SE second #define SI(x) (LL(x.size())) #define PB push_back #define DEBUG(a) OUT(a) #define DEBUG2(a, b) OUT2(a, b) #define cat cout << __LINE__ << endl #define OUT(a) cout << (a) << endl #define OUT2(a, b) cout << (a) << " " << (b) << endl #define zero 0LL #define int LL #define pb emplace_back #define eb pb using namespace std; template <typename T> inline void maximize(T &a, T b) { a = max(a, b); } template <typename T> inline void minimize(T &a, T b) { a = min(a, b); } template <typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; } template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } typedef long long LL; typedef double ld; typedef int ut; typedef vector<ut> VI; typedef vector<VI> VII; typedef pair<ut, ut> pr; typedef pair<ut, pr> ppr; typedef vector<pr> Vpr; typedef vector<ppr> Vppr; typedef priority_queue<pr, Vpr, greater<pr>> PQ; inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; } OUT(""); } const int SIZE1 = 5e5 + 1000; const int SIZE2 = 3010; const int SIZE3 = 330; const int SIZE = SIZE1; const int MAPSIZE = 40; const LL p = 7 + 1e9; const LL INF = 1LL << 60; const long double EPS = 1e-7; typedef pair<ld, ut> pld; #define endl "\n" // インタラクティブでは消す ut N, M, K, L, Q, D, H, W; // ut A,B,C,D,E,F,G,H,I,J,O,P,Q,R,T,U; VI edges[SIZE]; LL vals[SIZE], maps2[SIZE2][SIZE2], answer = zero; LL maps[SIZE2][SIZE2]; LL DP[SIZE2][SIZE2]; bool finished[101][SIZE]; LL DP2[SIZE2][SIZE2]; LL DP3[SIZE3][SIZE3][SIZE3]; LL A[SIZE][3]; LL WW[SIZE]; LL VV[SIZE]; string s, t; LL solve2(LL a, LL b) { if (a < 0 or b < 0) return 0; if (finished[a][b]) return DP[a][b]; finished[a][b] = true; DP[a][b] = max(solve2(a - 1, b), solve2(a, b - 1)); DP[a][b] = max(DP[a][b], solve2(a - 1, b - 1) + (s[a] == t[b])); return DP[a][b]; } string solve3(LL a, LL b) { if (a < 0 or b < 0) return ""; if (DP[a][b] == DP[a - 1][b]) return solve3(a - 1, b); if (DP[a][b] == DP[a][b - 1]) return solve3(a, b - 1); if (DP[a][b] == DP[a - 1][b - 1]) return solve3(a - 1, b - 1); return solve3(a - 1, b - 1) + s[a]; } LL solve() { cin >> s >> t; solve2(s.size() - 1, t.size() - 1); cout << solve3(s.size() - 1, t.size() - 1) << endl; return 0; } //!!!!!!!!!!!!!!!!!!!実装を詰める!!!!!!!!!!!!!!!!!!!!!!!!! signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(12); solve(); cin >> N; return 0; }
// HTTF.cpp : このファイルには 'main' // 関数が含まれています。プログラム実行の開始と終了がそこで行われます。 // #include "bits/stdc++.h" #define YES "YES" #define NO "NO" #define YESNO OUT(three(solve(), YES, NO)) #define ECHO OUT(solve()) #define three(A, B, C) ((A) ? (B) : (C)) #define FOR(i, a, b) for (LL i = (a); i < (LL)(b); i++) #define EFOR(i, a, b) for (LL i = (a); i <= (LL)(b); i++) #define RFOR(i, a, b) for (LL i = (a); i >= (LL)(b); i--) #define REP(i, b) FOR(i, zero, b) #define EREP(i, b) EFOR(i, zero, b) #define RREP(i, b) RFOR(i, b, zero) #define ALL(c) c.begin(), c.end() #define UNIQUE(c) \ sort(ALL(c)); \ c.erase(unique(ALL(c)), c.end()) #define MAX(c) (*max_element(ALL(c))) #define MIN(c) (*min_element(ALL(c))) #define MP make_pair #define FI first #define SE second #define SI(x) (LL(x.size())) #define PB push_back #define DEBUG(a) OUT(a) #define DEBUG2(a, b) OUT2(a, b) #define cat cout << __LINE__ << endl #define OUT(a) cout << (a) << endl #define OUT2(a, b) cout << (a) << " " << (b) << endl #define zero 0LL #define int LL #define pb emplace_back #define eb pb using namespace std; template <typename T> inline void maximize(T &a, T b) { a = max(a, b); } template <typename T> inline void minimize(T &a, T b) { a = min(a, b); } template <typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; } template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } typedef long long LL; typedef double ld; typedef int ut; typedef vector<ut> VI; typedef vector<VI> VII; typedef pair<ut, ut> pr; typedef pair<ut, pr> ppr; typedef vector<pr> Vpr; typedef vector<ppr> Vppr; typedef priority_queue<pr, Vpr, greater<pr>> PQ; inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; } OUT(""); } const int SIZE1 = 5e5 + 1000; const int SIZE2 = 3010; const int SIZE3 = 330; const int SIZE = SIZE1; const int MAPSIZE = 40; const LL p = 7 + 1e9; const LL INF = 1LL << 60; const long double EPS = 1e-7; typedef pair<ld, ut> pld; #define endl "\n" // インタラクティブでは消す ut N, M, K, L, Q, D, H, W; // ut A,B,C,D,E,F,G,H,I,J,O,P,Q,R,T,U; VI edges[SIZE]; LL vals[SIZE], maps2[SIZE2][SIZE2], answer = zero; LL maps[SIZE2][SIZE2]; LL DP[SIZE2][SIZE2]; bool finished[SIZE2][SIZE2]; LL A[SIZE]; string s, t; LL solve2(LL a, LL b) { if (a < 0 or b < 0) return 0; if (finished[a][b]) return DP[a][b]; finished[a][b] = true; DP[a][b] = max(solve2(a - 1, b), solve2(a, b - 1)); DP[a][b] = max(DP[a][b], solve2(a - 1, b - 1) + (s[a] == t[b])); return DP[a][b]; } string solve3(LL a, LL b) { if (a < 0 or b < 0) return ""; if (DP[a][b] == DP[a - 1][b]) return solve3(a - 1, b); if (DP[a][b] == DP[a][b - 1]) return solve3(a, b - 1); if (DP[a][b] == DP[a - 1][b - 1]) return solve3(a - 1, b - 1); return solve3(a - 1, b - 1) + s[a]; } LL solve() { cin >> s >> t; solve2(s.size() - 1, t.size() - 1); cout << solve3(s.size() - 1, t.size() - 1) << endl; return 0; } //!!!!!!!!!!!!!!!!!!!実装を詰める!!!!!!!!!!!!!!!!!!!!!!!!! signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(12); solve(); cin >> N; return 0; }
replace
80
86
80
82
-11
p03165
C++
Runtime Error
/* Dynamic Programming implementation of LCS problem */ #include <bits/stdc++.h> using namespace std; // Maximum string length #define N 100 int dp[N][N]; string printLCS(string X, int m, int n) { string s = ""; while (0 < m and 0 < n) { if (dp[m][n] == dp[m][n - 1]) n--; // daaner tar soman hole else if (dp[m][n] == dp[m - 1][n]) m--; // uporer tar soman hole else { s += X[m - 1]; // or i can add s[n-1], mane eikhane char 2 ta milche. eikhane // diagonal er sathe 1 add hoiche. ar ekhn e else ken? karon ei // table e value kokhonoi kome na. same thake or bare. m--, n--; } } reverse(s.begin(), s.end()); // hisab ta kora hoy last theke, tai reverse kora return s; } // Returns length of LCS for X[0..m-1], Y[0..n-1]; building the table, also LCS // size can be found from this table void LCStable(string X, string Y, int m, int n) { // Build L[m+1][n+1] in bottom up fashion for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) // i/j 0 hole 0-1 index bole to kichu nai. tai 0 hobe. dp[i][j] = 0; else if (X[i - 1] == Y[j - 1]) // char mile gele diagonal er sathe 1 + hobe dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); // na mille daner ba uporer tar moddhe max ta } } // return dp[m][n]; // number of the last row and last column ekdom nicer // daner value ta. the max value in the table! } /* Driver program to test above function */ int main() { string X, Y; cin >> X >> Y; int m = X.length(); int n = Y.length(); LCStable(X, Y, m, n); cout << printLCS(X, m, n) << endl; return 0; }
/* Dynamic Programming implementation of LCS problem */ #include <bits/stdc++.h> using namespace std; // Maximum string length #define N 3000 + 1 int dp[N][N]; string printLCS(string X, int m, int n) { string s = ""; while (0 < m and 0 < n) { if (dp[m][n] == dp[m][n - 1]) n--; // daaner tar soman hole else if (dp[m][n] == dp[m - 1][n]) m--; // uporer tar soman hole else { s += X[m - 1]; // or i can add s[n-1], mane eikhane char 2 ta milche. eikhane // diagonal er sathe 1 add hoiche. ar ekhn e else ken? karon ei // table e value kokhonoi kome na. same thake or bare. m--, n--; } } reverse(s.begin(), s.end()); // hisab ta kora hoy last theke, tai reverse kora return s; } // Returns length of LCS for X[0..m-1], Y[0..n-1]; building the table, also LCS // size can be found from this table void LCStable(string X, string Y, int m, int n) { // Build L[m+1][n+1] in bottom up fashion for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) // i/j 0 hole 0-1 index bole to kichu nai. tai 0 hobe. dp[i][j] = 0; else if (X[i - 1] == Y[j - 1]) // char mile gele diagonal er sathe 1 + hobe dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); // na mille daner ba uporer tar moddhe max ta } } // return dp[m][n]; // number of the last row and last column ekdom nicer // daner value ta. the max value in the table! } /* Driver program to test above function */ int main() { string X, Y; cin >> X >> Y; int m = X.length(); int n = Y.length(); LCStable(X, Y, m, n); cout << printLCS(X, m, n) << endl; return 0; }
replace
5
6
5
6
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <unordered_map> #define M 1000000007 #define T 998244353 #define PI 3.142 #define ll long long using namespace std; int ldp[3001][3001]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string s, t; cin >> s >> t; int i, j; for (i = 0; i < 3001; i++) { ldp[0][i] = 0; ldp[i][0] = 0; } for (i = 1; i <= s.length(); i++) { for (j = 1; j <= t.length(); j++) { if (s[i - 1] == t[j - 1]) { ldp[i][j] = ldp[i - 1][j - 1] + 1; } else { if (ldp[i - 1][j] > ldp[i][j - 1]) { ldp[i][j] = ldp[i - 1][j]; } else { ldp[i][j] = ldp[i][j - 1]; } } } } i = s.length(); j = t.length(); int ml = ldp[i][j]; char d[ml]; int ind = ml - 1; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { d[ind] = s[i - 1]; i--; j--; ind--; } else if (ldp[i - 1][j] > ldp[i][j - 1]) { i--; } else { j--; } } for (i = 0; i < ml; i++) { cout << d[i]; } cout << endl; return 0; }
#include <bits/stdc++.h> #include <unordered_map> #define M 1000000007 #define T 998244353 #define PI 3.142 #define ll long long using namespace std; int ldp[3001][3001]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; int i, j; for (i = 0; i < 3001; i++) { ldp[0][i] = 0; ldp[i][0] = 0; } for (i = 1; i <= s.length(); i++) { for (j = 1; j <= t.length(); j++) { if (s[i - 1] == t[j - 1]) { ldp[i][j] = ldp[i - 1][j - 1] + 1; } else { if (ldp[i - 1][j] > ldp[i][j - 1]) { ldp[i][j] = ldp[i - 1][j]; } else { ldp[i][j] = ldp[i][j - 1]; } } } } i = s.length(); j = t.length(); int ml = ldp[i][j]; char d[ml]; int ind = ml - 1; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { d[ind] = s[i - 1]; i--; j--; ind--; } else if (ldp[i - 1][j] > ldp[i][j - 1]) { i--; } else { j--; } } for (i = 0; i < ml; i++) { cout << d[i]; } cout << endl; return 0; }
delete
14
19
14
14
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ff first #define ss second #define quick \ ios::sync_with_stdio(false); \ cin.tie(0); #define time cerr << (0.1 * clock()) / CLOCKS_PER_SEC << endl; #define mod 1000000007 typedef long long ll; typedef pair<ll, ll> pl; #define forn(n) for (ll i = 0; i < ll(n); i++) void solve() { string s, t; cin >> s; cin >> t; int n = s.length(), m = t.length(); vector<vector<int>> v(n, vector<int>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { v[i][j] = (s[i] == t[j]); if (i and j) { v[i][j] = (s[i] == t[j]) + v[i - 1][j - 1]; } if (i) { v[i][j] = max(v[i][j], v[i - 1][j]); } if (j) { v[i][j] = max(v[i][j], v[i][j - 1]); } } } int in = n - 1, jn = m - 1; bool pr = true; string ans = ""; // for (auto i : v) { // for (auto j : i) { // cout << j << " "; // } // cout << '\n'; // } while (in >= 0 and jn >= 0) { // cout << in << " "/ << jn << '\n'; if (jn) { if (v[in][jn] == v[in][jn - 1]) { jn--; continue; } } if (in) { if (v[in][jn] == v[in - 1][jn]) { in--; continue; } } if (s[in] == t[jn]) { ans += s[in]; in--; jn--; } } reverse(ans.begin(), ans.end()); cout << ans << '\n'; } int main() { quick; solve(); // ll t; // cin >> t; // while (t--) { // solve(); // } }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define ff first #define ss second #define quick \ ios::sync_with_stdio(false); \ cin.tie(0); #define time cerr << (0.1 * clock()) / CLOCKS_PER_SEC << endl; #define mod 1000000007 typedef long long ll; typedef pair<ll, ll> pl; #define forn(n) for (ll i = 0; i < ll(n); i++) void solve() { string s, t; cin >> s; cin >> t; int n = s.length(), m = t.length(); vector<vector<int>> v(n, vector<int>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { v[i][j] = (s[i] == t[j]); if (i and j) { v[i][j] = (s[i] == t[j]) + v[i - 1][j - 1]; } if (i) { v[i][j] = max(v[i][j], v[i - 1][j]); } if (j) { v[i][j] = max(v[i][j], v[i][j - 1]); } } } int in = n - 1, jn = m - 1; bool pr = true; string ans = ""; // for (auto i : v) { // for (auto j : i) { // cout << j << " "; // } // cout << '\n'; // } while (in >= 0 and jn >= 0) { // cout << in << " "/ << jn << '\n'; if (jn) { if (v[in][jn] == v[in][jn - 1]) { jn--; continue; } } if (in) { if (v[in][jn] == v[in - 1][jn]) { in--; continue; } } if (s[in] == t[jn]) { ans += s[in]; in--; jn--; } else { break; } } reverse(ans.begin(), ans.end()); cout << ans << '\n'; } int main() { quick; solve(); // ll t; // cin >> t; // while (t--) { // solve(); // } }
insert
61
61
61
63
TLE
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) typedef int64_t Int; int main() { string S, T; cin >> S >> T; S = " " + S; T = " " + T; vector<string> now(3010, ""), before(3010, ""); rep1(i, S.length() - 1) { rep1(j, T.length() - 1) { if (S[i] == T[j]) { now[j] = before[j - 1] + S[i]; } else { int L1 = before[j].length(); int L2 = now[j - 1].length(); if (L1 < L2) now[j] = now[j - 1]; else now[j] = before[j]; } } swap(now, before); } cout << before[T.length() - 1] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) typedef int64_t Int; int main() { string S, T; cin >> S >> T; S = " " + S; T = " " + T; vector<string> now(3010, ""), before(3010, ""); rep1(i, S.length() - 1) { rep1(j, T.length() - 1) { if (S[i] == T[j]) { now[j] = before[j - 1] + S[i]; } else { int L1 = before[j].length(); int L2 = now[j - 1].length(); if (L1 < L2) now[j] = now[j - 1]; else now[j] = before[j]; } } before = now; } cout << before[T.length() - 1] << endl; }
replace
28
29
28
30
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s; cin >> t; int n = s.length(); int m = t.length(); vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0)); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int i = n; string ans; int j = m; if (dp[n][m] == 0) cout << " "; else { 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; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s; cin >> t; int n = s.length(); int m = t.length(); vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0)); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int i = n; string ans; int j = m; if (dp[n][m] == 0) cout << " "; else { while (i - 1 >= 0 && j - 1 >= 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; } return 0; }
replace
26
27
26
27
0
p03165
C++
Runtime Error
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <time.h> #include <type_traits> #include <typeinfo> #include <unordered_map> #include <vector> using namespace std; #define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i) #define rep_skip(i, N, M, ...) \ for (ll i = N, i##_len = (M); i < i##_len; i += (skip)) #define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i) #define pb push_back typedef pair<double, double> pd; typedef long long ll; typedef pair<ll, ll> pll; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 62; template <int n> struct tll_impl { using type = decltype(tuple_cat(tuple<ll>(), declval<typename tll_impl<n - 1>::type>())); }; template <> struct tll_impl<1> { using type = tuple<ll>; }; template <int n> using tll = typename tll_impl<n>::type; template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); }; template <int n, typename T> struct vec_t_impl { using type = vector<typename vec_t_impl<n - 1, T>::type>; }; template <typename T> struct vec_t_impl<1, T> { using type = vector<T>; }; template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type; // check static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, ""); // decompose vector into basetype and dimension. template <typename T> struct vec_dec { static constexpr int dim = 0; using type = T; }; template <typename T> struct vec_dec<vector<T>> { static constexpr int dim = vec_dec<T>::dim + 1; using type = typename vec_dec<T>::type; }; static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, ""); static_assert(vec_dec<vec_t<3, ll>>::dim == 3, ""); template <typename T = ll> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T = ll, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } // ex: auto dp = make_v<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5)); // check if T is vector template <typename T> struct is_vector : std::false_type {}; template <typename T> struct is_vector<vector<T>> : std::true_type {}; template <typename T, typename V> typename enable_if<!is_vector<T>::value>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_vector<T>::value>::type fill_v(T &t, const V &v) { for (auto &&x : t) fill_v(x, v); } // ex: fill_v(dp, INF); typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<pll> vpll; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; template <typename T> using pq_greater = priority_queue<T, vector<T>, greater<T>>; #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define perm(c) \ sort(all(c)); \ for (bool c##perm = 1; c##perm; c##perm = next_permutation(all(c))) template <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } vll seq(ll i, ll j) { vll res(j - i); rep(k, i, j) res[k] = i + k; return res; } constexpr ll POW_(ll n, ll m) { ll res = 1; rep(i, 0, m) { res *= n; } return res; } template <ll mod = 0> constexpr ll POW(ll x, ll n) { if (x == 2) { return (1LL << n) % mod; } if (n == 0) return 1; if (n == 1) return x % mod; if (n % 2 == 0) return POW_(POW<mod>(x, n / 2), 2LL) % mod; return ((POW_(POW<mod>(x, n / 2), 2LL) % mod) * (x % mod)) % mod; } template <> constexpr ll POW<0>(ll x, ll n) { if (x == 2) { return 1LL << n; } if (n == 0) return 1; if (n == 1) return x; if (n % 2 == 0) return POW_(POW(x, n / 2), 2); return (POW_(POW(x, n / 2), 2)) * x; } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void sort_by(Inputs &inputs, Functor f) { std::sort(std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void stable_sort_by(Inputs &inputs, Functor f) { std::stable_sort( std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } // ============================ Header ================================= int main() { cin.tie(0); ios::sync_with_stdio(false); string s, t; cin >> s >> t; auto dp = make_v(s.size() + 1, t.size() + 1); auto dps = make_v<string>(s.size() + 1, t.size() + 1); rep(i, 0, s.size()) rep(j, 0, t.size()) { dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); dps[i + 1][j + 1] = (dp[i + 1][j] > dp[i][j + 1] ? dps[i + 1][j] : dps[i][j + 1]); if (s[i] == t[j]) { if (dp[i][j] + 1 > max(dp[i + 1][j], dp[i][j + 1])) { dp[i + 1][j + 1] = dp[i][j] + 1; dps[i + 1][j + 1] = dps[i][j] + s[i]; } } dps[i][j].shrink_to_fit(); } cout << dps[s.size()][t.size()] << endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <time.h> #include <type_traits> #include <typeinfo> #include <unordered_map> #include <vector> using namespace std; #define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i) #define rep_skip(i, N, M, ...) \ for (ll i = N, i##_len = (M); i < i##_len; i += (skip)) #define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i) #define pb push_back typedef pair<double, double> pd; typedef long long ll; typedef pair<ll, ll> pll; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 62; template <int n> struct tll_impl { using type = decltype(tuple_cat(tuple<ll>(), declval<typename tll_impl<n - 1>::type>())); }; template <> struct tll_impl<1> { using type = tuple<ll>; }; template <int n> using tll = typename tll_impl<n>::type; template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); }; template <int n, typename T> struct vec_t_impl { using type = vector<typename vec_t_impl<n - 1, T>::type>; }; template <typename T> struct vec_t_impl<1, T> { using type = vector<T>; }; template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type; // check static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, ""); // decompose vector into basetype and dimension. template <typename T> struct vec_dec { static constexpr int dim = 0; using type = T; }; template <typename T> struct vec_dec<vector<T>> { static constexpr int dim = vec_dec<T>::dim + 1; using type = typename vec_dec<T>::type; }; static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, ""); static_assert(vec_dec<vec_t<3, ll>>::dim == 3, ""); template <typename T = ll> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T = ll, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } // ex: auto dp = make_v<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5)); // check if T is vector template <typename T> struct is_vector : std::false_type {}; template <typename T> struct is_vector<vector<T>> : std::true_type {}; template <typename T, typename V> typename enable_if<!is_vector<T>::value>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_vector<T>::value>::type fill_v(T &t, const V &v) { for (auto &&x : t) fill_v(x, v); } // ex: fill_v(dp, INF); typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<pll> vpll; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; template <typename T> using pq_greater = priority_queue<T, vector<T>, greater<T>>; #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define perm(c) \ sort(all(c)); \ for (bool c##perm = 1; c##perm; c##perm = next_permutation(all(c))) template <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } vll seq(ll i, ll j) { vll res(j - i); rep(k, i, j) res[k] = i + k; return res; } constexpr ll POW_(ll n, ll m) { ll res = 1; rep(i, 0, m) { res *= n; } return res; } template <ll mod = 0> constexpr ll POW(ll x, ll n) { if (x == 2) { return (1LL << n) % mod; } if (n == 0) return 1; if (n == 1) return x % mod; if (n % 2 == 0) return POW_(POW<mod>(x, n / 2), 2LL) % mod; return ((POW_(POW<mod>(x, n / 2), 2LL) % mod) * (x % mod)) % mod; } template <> constexpr ll POW<0>(ll x, ll n) { if (x == 2) { return 1LL << n; } if (n == 0) return 1; if (n == 1) return x; if (n % 2 == 0) return POW_(POW(x, n / 2), 2); return (POW_(POW(x, n / 2), 2)) * x; } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void sort_by(Inputs &inputs, Functor f) { std::sort(std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void stable_sort_by(Inputs &inputs, Functor f) { std::stable_sort( std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } // ============================ Header ================================= int main() { cin.tie(0); ios::sync_with_stdio(false); string s, t; cin >> s >> t; auto dp = make_v(s.size() + 1, t.size() + 1); auto dps = make_v<string>(s.size() + 1, t.size() + 1); rep(i, 0, s.size()) rep(j, 0, t.size()) { dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); dps[i + 1][j + 1] = (dp[i + 1][j] > dp[i][j + 1] ? dps[i + 1][j] : dps[i][j + 1]); if (s[i] == t[j]) { if (dp[i][j] + 1 > max(dp[i + 1][j], dp[i][j + 1])) { dp[i + 1][j + 1] = dp[i][j] + 1; dps[i + 1][j + 1] = dps[i][j] + s[i]; } } dps[i][j].clear(); dps[i][j].shrink_to_fit(); } cout << dps[s.size()][t.size()] << endl; return 0; }
insert
195
195
195
196
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long int ll k; int dp[3001][3001]; string final; int lcs(string x, string y, int n, int m) { if (n == 0 || m == 0) { return 0; } if (dp[n][m] != -1) { // cout<<"here"<<endl; return dp[n][m]; } if (x[n - 1] == y[m - 1]) { // final.push_back(x[n-1]); dp[n][m] = 1 + lcs(x, y, n - 1, m - 1); return dp[n][m]; } else { dp[n][m] = max(lcs(x, y, n - 1, m), lcs(x, y, n, m - 1)); return dp[n][m]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(dp, -1, sizeof(dp)); string x; cin >> x; string y; cin >> y; int n, m; n = x.length(); m = y.length(); k = lcs(x, y, n, m); int i = n, j = m; while (i > 0 && j > 0) { if (x[i - 1] == y[j - 1]) { final.push_back(x[i - 1]); i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } if (final.length() > 0) { for (int i = final.length() - 1; i >= 0; i--) { cout << final[i]; } } // cout<<endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long int ll k; int dp[3001][3001]; string final; int lcs(string &x, string &y, int n, int m) { if (n == 0 || m == 0) { return 0; } if (dp[n][m] != -1) { // cout<<"here"<<endl; return dp[n][m]; } if (x[n - 1] == y[m - 1]) { // final.push_back(x[n-1]); dp[n][m] = 1 + lcs(x, y, n - 1, m - 1); return dp[n][m]; } else { dp[n][m] = max(lcs(x, y, n - 1, m), lcs(x, y, n, m - 1)); return dp[n][m]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(dp, -1, sizeof(dp)); string x; cin >> x; string y; cin >> y; int n, m; n = x.length(); m = y.length(); k = lcs(x, y, n, m); int i = n, j = m; while (i > 0 && j > 0) { if (x[i - 1] == y[j - 1]) { final.push_back(x[i - 1]); i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } if (final.length() > 0) { for (int i = final.length() - 1; i >= 0; i--) { cout << final[i]; } } // cout<<endl; }
replace
6
7
6
7
TLE
p03165
C++
Runtime Error
#include <cstring> #include <iostream> #include <string> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; int **dp = new int *[s2.length() + 1]; for (int i = 0; i <= s2.length(); i++) { dp[i] = new int[s1.length()]; memset(dp[i], 0, s1.length() * sizeof(int)); } for (int i = 1; i <= s2.length(); i++) { for (int j = 1; j <= s1.length(); j++) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (s2[i - 1] == s1[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } } } string revOut = ""; int len = dp[s2.length()][s1.length()]; int row = s2.length(), col = s1.length(); while (row > 0 && col > 0) { if (dp[row][col] == dp[row - 1][col]) { row--; } else if (dp[row][col] == dp[row][col - 1]) { col--; } else if (dp[row][col] == dp[row - 1][col - 1] + 1) { revOut += s2[row - 1]; row--; col--; // len--; } } /* for(int i=1;i<=s2.length();i++) { for(int j=1;j<=s1.length();j++) { cout<<dp[i][j]<<" "; } cout<<endl; } */ for (int i = revOut.length() - 1; i >= 0; i--) { cout << revOut[i]; } cout << endl; // cout<<dp[s2.length()][s1.length()]<<endl; return 0; }
#include <cstring> #include <iostream> #include <string> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; int **dp = new int *[s2.length() + 1]; for (int i = 0; i <= s2.length(); i++) { dp[i] = new int[s1.length() + 1]; memset(dp[i], 0, (s1.length() + 1) * sizeof(int)); } for (int i = 1; i <= s2.length(); i++) { for (int j = 1; j <= s1.length(); j++) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (s2[i - 1] == s1[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } } } string revOut = ""; int len = dp[s2.length()][s1.length()]; int row = s2.length(), col = s1.length(); while (row > 0 && col > 0) { if (dp[row][col] == dp[row - 1][col]) { row--; } else if (dp[row][col] == dp[row][col - 1]) { col--; } else if (dp[row][col] == dp[row - 1][col - 1] + 1) { revOut += s2[row - 1]; row--; col--; // len--; } } /* for(int i=1;i<=s2.length();i++) { for(int j=1;j<=s1.length();j++) { cout<<dp[i][j]<<" "; } cout<<endl; } */ for (int i = revOut.length() - 1; i >= 0; i--) { cout << revOut[i]; } cout << endl; // cout<<dp[s2.length()][s1.length()]<<endl; return 0; }
replace
9
11
9
11
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; int main() { string s, t; cin >> s >> t; int ls = s.size(); int lt = t.size(); vector<vector<int>> dp(ls + 1, vector<int>(lt + 1)); rep(i, ls) { rep(j, lt) { 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]); } } } int p = ls, q = lt; string ans; while (p > 0 || q > 0) { if (dp[p][q] == dp[p - 1][q]) p--; else if (dp[p][q] == dp[p][q - 1]) q--; else { p--; q--; ans = s[p] + ans; } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; using ll = long long; int main() { string s, t; cin >> s >> t; int ls = s.size(); int lt = t.size(); vector<vector<int>> dp(ls + 1, vector<int>(lt + 1)); rep(i, ls) { rep(j, lt) { 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]); } } } int p = ls, q = lt; string ans; while (p > 0 && q > 0) { if (dp[p][q] == dp[p - 1][q]) p--; else if (dp[p][q] == dp[p][q - 1]) q--; else { p--; q--; ans = s[p] + ans; } } cout << ans << endl; }
replace
24
25
24
25
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 100; int n, W; string a, b; int dp[maxn], l[maxn][maxn]; stack<char> S; int main() { cin >> a >> b; for (int j = 0; j < max(a.length(), b.length()); j++) for (int i = 0; i < b.length(); i++) l[j][i] = -1; for (int i = 0; i < a.length(); i++) { int maxans = 0, maxi = -1; for (int j = 0; j < b.length(); j++) { int maxans2 = maxans, maxi2 = maxi; if (dp[j] > maxans2) { maxans2 = dp[j]; maxi2 = j; } if (a[i] == b[j]) { if (dp[j] < maxans + 1) { dp[j] = maxans + 1; l[dp[j]][j] = maxi; } } maxans = maxans2; maxi = maxi2; } } int maxans = 0, maxi = -1; for (int i = 0; i < b.length(); i++) { if (dp[i] > maxans) { maxans = dp[i]; maxi = i; } } while (maxi != -1) { // cout<<maxi<<endl; S.push(b[maxi]); maxi = l[maxans--][maxi]; } // cout<<maxans<<endl; while (!S.empty()) { cout << S.top(); S.pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 3e3 + 100; int n, W; string a, b; int dp[maxn], l[maxn][maxn]; stack<char> S; int main() { cin >> a >> b; for (int j = 0; j < max(a.length(), b.length()); j++) for (int i = 0; i < b.length(); i++) l[j][i] = -1; for (int i = 0; i < a.length(); i++) { int maxans = 0, maxi = -1; for (int j = 0; j < b.length(); j++) { int maxans2 = maxans, maxi2 = maxi; if (dp[j] > maxans2) { maxans2 = dp[j]; maxi2 = j; } if (a[i] == b[j]) { if (dp[j] < maxans + 1) { dp[j] = maxans + 1; l[dp[j]][j] = maxi; } } maxans = maxans2; maxi = maxi2; } } int maxans = 0, maxi = -1; for (int i = 0; i < b.length(); i++) { if (dp[i] > maxans) { maxans = dp[i]; maxi = i; } } while (maxi != -1) { // cout<<maxi<<endl; S.push(b[maxi]); maxi = l[maxans--][maxi]; } // cout<<maxans<<endl; while (!S.empty()) { cout << S.top(); S.pop(); } return 0; }
replace
2
3
2
3
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll int const ll N = 3e3 + 5; string s, t, dp[2][N]; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> s >> t; for (ll i = 0; i < s.size(); i++) for (ll j = 0; j < t.size(); j++) if (s[i] == t[j]) dp[i % 2][j + 1] = dp[!(i % 2)][j] + s[i]; else if (dp[i % 2][j].size() > dp[!(i % 2)][j + 1].size()) dp[i % 2][j + 1] = dp[i % 2][j]; else dp[i % 2][j + 1] = dp[!(i % 2)][j + 1]; cout << dp[!(s.size() % 2)][t.size()]; }
#include <bits/stdc++.h> using namespace std; #define ll int const ll N = 3e3 + 5; string s, t, dp[2][N]; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> s >> t; for (ll i = 0; i < s.size(); i++) for (ll j = 0; j < t.size(); j++) if (s[i] == t[j]) dp[i % 2][j + 1] = dp[!(i % 2)][j], dp[i % 2][j + 1].push_back(s[i]); else if (dp[i % 2][j].size() > dp[!(i % 2)][j + 1].size()) dp[i % 2][j + 1] = dp[i % 2][j]; else dp[i % 2][j + 1] = dp[!(i % 2)][j + 1]; cout << dp[!(s.size() % 2)][t.size()]; }
replace
11
12
11
12
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { // your code goes here string s, t; cin >> s >> t; int l1 = s.length(); int l2 = t.length(); vector<vector<int>> dp(l1 + 1, vector<int>(l2 + 1, 0)); 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 (s[i - 1] == t[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); } } int i = l1, j = l2; string ans = ""; while (i >= 0 && j >= 0) { if (s[i - 1] == t[j - 1]) { ans.push_back(s[i - 1]); i--; j--; } else if (dp[i][j - 1] >= dp[i - 1][j]) { j--; } else i--; } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { // your code goes here string s, t; cin >> s >> t; int l1 = s.length(); int l2 = t.length(); vector<vector<int>> dp(l1 + 1, vector<int>(l2 + 1, 0)); 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 (s[i - 1] == t[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); } } int i = l1, j = l2; string ans = ""; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ans.push_back(s[i - 1]); i--; j--; } else if (dp[i][j - 1] >= dp[i - 1][j]) { j--; } else i--; } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
replace
24
25
24
25
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define sc second #define fr first #define mp make_pair const int N = 2e3 + 10, Max = 50 + 10; const ll MOD = 998244353, OO = 1e14 + 2; int dp[N][N]; string a, b; vector<char> sol; int solve(int i, int j) { if (i == a.size() || j == b.size()) return 0; int &ret = dp[i][j]; if (ret != -1) return ret; int c1 = solve(i + 1, j); int c2 = solve(i, j + 1); int c3 = 0; if (a[i] == b[j]) { c3 = 1 + solve(i + 1, j + 1); } return ret = max(c1, max(c2, c3)); } void path(int i, int j) { if (i == a.size() || j == b.size()) return; int optimal = solve(i, j); int c1 = solve(i + 1, j); int c2 = solve(i, j + 1); int c3 = 0; if (a[i] == b[i]) { c3 = 1 + solve(i + 1, j + 1); } if (optimal == c1) { path(i + 1, j); } else if (optimal == c2) { path(i, j + 1); } else { sol.pb(a[i]); path(i + 1, j + 1); } } int main() { memset(dp, -1, sizeof(dp)); cin >> a >> b; path(0, 0); for (int i = 0; i < sol.size(); ++i) cout << sol[i]; cout << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define sc second #define fr first #define mp make_pair const int N = 3e3 + 10, Max = 50 + 10; const ll MOD = 998244353, OO = 1e14 + 2; int dp[N][N]; string a, b; vector<char> sol; int solve(int i, int j) { if (i == a.size() || j == b.size()) return 0; int &ret = dp[i][j]; if (ret != -1) return ret; int c1 = solve(i + 1, j); int c2 = solve(i, j + 1); int c3 = 0; if (a[i] == b[j]) { c3 = 1 + solve(i + 1, j + 1); } return ret = max(c1, max(c2, c3)); } void path(int i, int j) { if (i == a.size() || j == b.size()) return; int optimal = solve(i, j); int c1 = solve(i + 1, j); int c2 = solve(i, j + 1); int c3 = 0; if (a[i] == b[i]) { c3 = 1 + solve(i + 1, j + 1); } if (optimal == c1) { path(i + 1, j); } else if (optimal == c2) { path(i, j + 1); } else { sol.pb(a[i]); path(i + 1, j + 1); } } int main() { memset(dp, -1, sizeof(dp)); cin >> a >> b; path(0, 0); for (int i = 0; i < sol.size(); ++i) cout << sol[i]; cout << endl; }
replace
7
8
7
8
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int #define X first #define Y second const int MAXN = 1e5 + 5; string s, t; int n, m; int dp[105][105]; int main() { cin >> s >> t; n = s.length(), 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] = max(dp[i - 1][j], dp[i][j - 1]); } } string ans = ""; while (n > 0 && m > 0) { if (dp[n][m] == dp[n - 1][m]) n--; else if (dp[n][m] == dp[n][m - 1]) m--; else n--, m--, ans.push_back(s[n]); } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define X first #define Y second const int MAXN = 1e5 + 5; string s, t; int n, m; int dp[3005][3005]; int main() { cin >> s >> t; n = s.length(), 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] = max(dp[i - 1][j], dp[i][j - 1]); } } string ans = ""; while (n > 0 && m > 0) { if (dp[n][m] == dp[n - 1][m]) n--; else if (dp[n][m] == dp[n][m - 1]) m--; else n--, m--, ans.push_back(s[n]); } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
replace
9
10
9
10
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int INF = 1145141919; const long long INFL = 1LL << 60; 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 dp[3 * 1000][3 * 1000] = {}; int main() { string s, t; cin >> s >> t; s = "@" + s; t = "@" + t; for (int i = 1; i < s.size(); ++i) { for (int j = 1; j < t.size(); ++j) { 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[s.size() - 1][t.size() - 1]; int i = s.size() - 1; int j = t.size() - 1; string ans; while (len > 0) { if (s[i] == t[j]) { ans += s[i]; --i; --j; --len; } else if (dp[i][j] == dp[i - 1][j]) { --i; } else { --j; } } for (int i = 0; i < ans.size(); ++i) { cout << ans[ans.size() - 1 - i]; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1145141919; const long long INFL = 1LL << 60; 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 dp[3 * 1100][3 * 1100] = {}; int main() { string s, t; cin >> s >> t; s = "@" + s; t = "@" + t; for (int i = 1; i < s.size(); ++i) { for (int j = 1; j < t.size(); ++j) { 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[s.size() - 1][t.size() - 1]; int i = s.size() - 1; int j = t.size() - 1; string ans; while (len > 0) { if (s[i] == t[j]) { ans += s[i]; --i; --j; --len; } else if (dp[i][j] == dp[i - 1][j]) { --i; } else { --j; } } for (int i = 0; i < ans.size(); ++i) { cout << ans[ans.size() - 1 - i]; } cout << endl; return 0; }
replace
22
23
22
23
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1001; int dp[N][N]; int main() { string s, t; cin >> s; cin >> t; for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = max(dp[i][j - 1], 1 + dp[i - 1][j - 1]); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } if (dp[s.size()][t.size()] == 0) { cout << ""; return 0; } // branch and bound int i = s.size(), j = t.size(); string ans = ""; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ans = ans + 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; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 10001; int dp[N][N]; int main() { string s, t; cin >> s; cin >> t; for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = max(dp[i][j - 1], 1 + dp[i - 1][j - 1]); } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } if (dp[s.size()][t.size()] == 0) { cout << ""; return 0; } // branch and bound int i = s.size(), j = t.size(); string ans = ""; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ans = ans + 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; return 0; }
replace
2
3
2
3
0
p03165
C++
Memory Limit Exceeded
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <time.h> #include <type_traits> #include <typeinfo> #include <unordered_map> #include <vector> using namespace std; #define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i) #define rep_skip(i, N, M, ...) \ for (ll i = N, i##_len = (M); i < i##_len; i += (skip)) #define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i) #define pb push_back typedef pair<double, double> pd; typedef long long ll; typedef pair<ll, ll> pll; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 62; template <int n> struct tll_impl { using type = decltype(tuple_cat(tuple<ll>(), declval<typename tll_impl<n - 1>::type>())); }; template <> struct tll_impl<1> { using type = tuple<ll>; }; template <int n> using tll = typename tll_impl<n>::type; template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); }; template <int n, typename T> struct vec_t_impl { using type = vector<typename vec_t_impl<n - 1, T>::type>; }; template <typename T> struct vec_t_impl<1, T> { using type = vector<T>; }; template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type; // check static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, ""); // decompose vector into basetype and dimension. template <typename T> struct vec_dec { static constexpr int dim = 0; using type = T; }; template <typename T> struct vec_dec<vector<T>> { static constexpr int dim = vec_dec<T>::dim + 1; using type = typename vec_dec<T>::type; }; static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, ""); static_assert(vec_dec<vec_t<3, ll>>::dim == 3, ""); template <typename T = ll> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T = ll, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } // ex: auto dp = make_v<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5)); // check if T is vector template <typename T> struct is_vector : std::false_type {}; template <typename T> struct is_vector<vector<T>> : std::true_type {}; template <typename T, typename V> typename enable_if<!is_vector<T>::value>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_vector<T>::value>::type fill_v(T &t, const V &v) { for (auto &&x : t) fill_v(x, v); } // ex: fill_v(dp, INF); typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<pll> vpll; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; template <typename T> using pq_greater = priority_queue<T, vector<T>, greater<T>>; #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define perm(c) \ sort(all(c)); \ for (bool c##perm = 1; c##perm; c##perm = next_permutation(all(c))) template <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } vll seq(ll i, ll j) { vll res(j - i); rep(k, i, j) res[k] = i + k; return res; } constexpr ll POW_(ll n, ll m) { ll res = 1; rep(i, 0, m) { res *= n; } return res; } template <ll mod = 0> constexpr ll POW(ll x, ll n) { if (x == 2) { return (1LL << n) % mod; } if (n == 0) return 1; if (n == 1) return x % mod; if (n % 2 == 0) return POW_(POW<mod>(x, n / 2), 2LL) % mod; return ((POW_(POW<mod>(x, n / 2), 2LL) % mod) * (x % mod)) % mod; } template <> constexpr ll POW<0>(ll x, ll n) { if (x == 2) { return 1LL << n; } if (n == 0) return 1; if (n == 1) return x; if (n % 2 == 0) return POW_(POW(x, n / 2), 2); return (POW_(POW(x, n / 2), 2)) * x; } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void sort_by(Inputs &inputs, Functor f) { std::sort(std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void stable_sort_by(Inputs &inputs, Functor f) { std::stable_sort( std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } // ============================ Header ================================= int main() { cin.tie(0); ios::sync_with_stdio(false); string s, t; cin >> s >> t; auto dp = make_v(s.size() + 1, t.size() + 1); auto dps = make_v<string>(s.size() + 1, t.size() + 1); rep(i, 0, s.size()) rep(j, 0, t.size()) { dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); dps[i + 1][j + 1] = (dp[i + 1][j] > dp[i][j + 1] ? dps[i + 1][j] : dps[i][j + 1]); if (s[i] == t[j]) { if (dp[i][j] + 1 > max(dp[i + 1][j], dp[i][j + 1])) { dp[i + 1][j + 1] = dp[i][j] + 1; dps[i + 1][j + 1] = dps[i][j] + s[i]; } } dps[i][j] = ""; } cout << dps[s.size()][t.size()] << endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <stack> #include <string> #include <time.h> #include <type_traits> #include <typeinfo> #include <unordered_map> #include <vector> using namespace std; #define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i) #define rep_skip(i, N, M, ...) \ for (ll i = N, i##_len = (M); i < i##_len; i += (skip)) #define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i) #define pb push_back typedef pair<double, double> pd; typedef long long ll; typedef pair<ll, ll> pll; constexpr ll MOD = 1000000007; constexpr ll INF = 1LL << 62; template <int n> struct tll_impl { using type = decltype(tuple_cat(tuple<ll>(), declval<typename tll_impl<n - 1>::type>())); }; template <> struct tll_impl<1> { using type = tuple<ll>; }; template <int n> using tll = typename tll_impl<n>::type; template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); }; template <int n, typename T> struct vec_t_impl { using type = vector<typename vec_t_impl<n - 1, T>::type>; }; template <typename T> struct vec_t_impl<1, T> { using type = vector<T>; }; template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type; // check static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, ""); // decompose vector into basetype and dimension. template <typename T> struct vec_dec { static constexpr int dim = 0; using type = T; }; template <typename T> struct vec_dec<vector<T>> { static constexpr int dim = vec_dec<T>::dim + 1; using type = typename vec_dec<T>::type; }; static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, ""); static_assert(vec_dec<vec_t<3, ll>>::dim == 3, ""); template <typename T = ll> vector<T> make_v(size_t a) { return vector<T>(a); } template <typename T = ll, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } // ex: auto dp = make_v<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5)); // check if T is vector template <typename T> struct is_vector : std::false_type {}; template <typename T> struct is_vector<vector<T>> : std::true_type {}; template <typename T, typename V> typename enable_if<!is_vector<T>::value>::type fill_v(T &t, const V &v) { t = v; } template <typename T, typename V> typename enable_if<is_vector<T>::value>::type fill_v(T &t, const V &v) { for (auto &&x : t) fill_v(x, v); } // ex: fill_v(dp, INF); typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<pll> vpll; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; template <typename T> using pq_greater = priority_queue<T, vector<T>, greater<T>>; #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define perm(c) \ sort(all(c)); \ for (bool c##perm = 1; c##perm; c##perm = next_permutation(all(c))) template <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } vll seq(ll i, ll j) { vll res(j - i); rep(k, i, j) res[k] = i + k; return res; } constexpr ll POW_(ll n, ll m) { ll res = 1; rep(i, 0, m) { res *= n; } return res; } template <ll mod = 0> constexpr ll POW(ll x, ll n) { if (x == 2) { return (1LL << n) % mod; } if (n == 0) return 1; if (n == 1) return x % mod; if (n % 2 == 0) return POW_(POW<mod>(x, n / 2), 2LL) % mod; return ((POW_(POW<mod>(x, n / 2), 2LL) % mod) * (x % mod)) % mod; } template <> constexpr ll POW<0>(ll x, ll n) { if (x == 2) { return 1LL << n; } if (n == 0) return 1; if (n == 1) return x; if (n % 2 == 0) return POW_(POW(x, n / 2), 2); return (POW_(POW(x, n / 2), 2)) * x; } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void sort_by(Inputs &inputs, Functor f) { std::sort(std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void stable_sort_by(Inputs &inputs, Functor f) { std::stable_sort( std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } // ============================ Header ================================= int main() { cin.tie(0); ios::sync_with_stdio(false); string s, t; cin >> s >> t; auto dp = make_v(s.size() + 1, t.size() + 1); auto dps = make_v<string>(s.size() + 1, t.size() + 1); rep(i, 0, s.size()) rep(j, 0, t.size()) { dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]); dps[i + 1][j + 1] = (dp[i + 1][j] > dp[i][j + 1] ? dps[i + 1][j] : dps[i][j + 1]); if (s[i] == t[j]) { if (dp[i][j] + 1 > max(dp[i + 1][j], dp[i][j + 1])) { dp[i + 1][j + 1] = dp[i][j] + 1; dps[i + 1][j + 1] = dps[i][j] + s[i]; } } string().swap(dps[i][j]); } cout << dps[s.size()][t.size()] << endl; return 0; }
replace
195
196
195
196
MLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; string a, b; int S[1001][1001]; vector<char> ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> a >> b; for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { if (a[i] == b[j]) { S[i + 1][j + 1] = S[i][j] + 1; } else { S[i + 1][j + 1] = max(S[i + 1][j], S[i][j + 1]); } } } int k = S[a.size()][b.size()]; int i = a.size(); int j = b.size(); // cout<<k<<"\n"; while (k) { // 대칭성이 있다는게 큰 장점인듯. 그러면 n^2/2로 줄이는것도 되겠네 while (j && S[i][j] == k) j--; j++; while (i && S[i][j] == k) i--; i++; ans.push_back(b[j - 1]); k--; i--; j--; } for (int i = ans.size() - 1; i >= 0; i--) { cout << ans[i]; } }
#include <bits/stdc++.h> using namespace std; string a, b; int S[3001][3001]; vector<char> ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> a >> b; for (int i = 0; i < a.size(); i++) { for (int j = 0; j < b.size(); j++) { if (a[i] == b[j]) { S[i + 1][j + 1] = S[i][j] + 1; } else { S[i + 1][j + 1] = max(S[i + 1][j], S[i][j + 1]); } } } int k = S[a.size()][b.size()]; int i = a.size(); int j = b.size(); // cout<<k<<"\n"; while (k) { // 대칭성이 있다는게 큰 장점인듯. 그러면 n^2/2로 줄이는것도 되겠네 while (j && S[i][j] == k) j--; j++; while (i && S[i][j] == k) i--; i++; ans.push_back(b[j - 1]); k--; i--; j--; } for (int i = ans.size() - 1; i >= 0; i--) { cout << ans[i]; } }
replace
3
4
3
4
0
p03165
C++
Runtime Error
#include <algorithm> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define int long long #define All(v) (v).begin(), (v).end() int dy[4] = {-1, 0, 1, 0}; int dx[4] = {0, 1, 0, -1}; int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int mod = 1000000007; const int inf = mod * mod; const int d5 = 100100; int dp[3000][3000]; signed main() { ios::sync_with_stdio(false); cin.tie(0); string s, t; cin >> s >> t; int n = s.size(), m = t.size(); // dp[i][j]:sをi番目まで,tをj番目まで見た時のLCS for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { dp[i][j] = 0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (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 = ""; while (n && m) { if (dp[n][m] == dp[n - 1][m]) n--; else if (dp[n][m] == dp[n][m - 1]) m--; else { res += s[n - 1]; n--; m--; } } reverse(All(res)); cout << res << endl; }
#include <algorithm> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define int long long #define All(v) (v).begin(), (v).end() int dy[4] = {-1, 0, 1, 0}; int dx[4] = {0, 1, 0, -1}; int Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int mod = 1000000007; const int inf = mod * mod; const int d5 = 100100; int dp[3030][3030]; signed main() { ios::sync_with_stdio(false); cin.tie(0); string s, t; cin >> s >> t; int n = s.size(), m = t.size(); // dp[i][j]:sをi番目まで,tをj番目まで見た時のLCS for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { dp[i][j] = 0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (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 = ""; while (n && m) { if (dp[n][m] == dp[n - 1][m]) n--; else if (dp[n][m] == dp[n][m - 1]) m--; else { res += s[n - 1]; n--; m--; } } reverse(All(res)); cout << res << endl; }
replace
25
26
25
26
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int #define ull unsigned long long int #define mod 1000000007 using namespace std; int main(int argc, char const *argv[]) { #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 = 1; // cin>>t; while (t--) { string inp1, inp2; cin >> inp1 >> inp2; ll n = inp1.size(), m = inp2.size(); ll dp[n + 1][m + 1]; for (ll i = 0; i <= n; i++) { for (ll j = 0; j <= m; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (inp1[i - 1] == inp2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); } } } vector<char> v; ll i = n, j = m; while (i > 0 && j > 0) { if (inp1[i - 1] == inp2[j - 1]) { v.push_back(inp1[i - 1]); i--, j--; } else if (dp[i - 1][j] >= dp[i][j - 1]) { i--; } else j--; } reverse(v.begin(), v.end()); for (auto elem : v) { cout << elem; } cout << endl; } return 0; }
#include <bits/stdc++.h> #define ll long long int #define ull unsigned long long int #define mod 1000000007 using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t = 1; // cin>>t; while (t--) { string inp1, inp2; cin >> inp1 >> inp2; ll n = inp1.size(), m = inp2.size(); ll dp[n + 1][m + 1]; for (ll i = 0; i <= n; i++) { for (ll j = 0; j <= m; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (inp1[i - 1] == inp2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); } } } vector<char> v; ll i = n, j = m; while (i > 0 && j > 0) { if (inp1[i - 1] == inp2[j - 1]) { v.push_back(inp1[i - 1]); i--, j--; } else if (dp[i - 1][j] >= dp[i][j - 1]) { i--; } else j--; } reverse(v.begin(), v.end()); for (auto elem : v) { cout << elem; } cout << endl; } return 0; }
replace
6
11
6
8
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long const int SIZE = 1e3 + 5; int DP[SIZE][SIZE]; char z[SIZE][SIZE]; string B = "0", A = "0", K = "0", P, Q; void read() { cin >> P >> Q; A += P; B += Q; } void COUNT_LCS() { for (int i = 1; i <= A.size() - 1; i++) for (int j = 1; j <= B.size() - 1; j++) { if (A[i] == B[j]) { DP[i][j] += DP[i - 1][j - 1] + 1; z[i][j] = A[i]; } else { if (DP[i - 1][j] > DP[i][j - 1]) { DP[i][j] += DP[i - 1][j]; z[i][j] = '#'; } else { DP[i][j] += DP[i][j - 1]; z[i][j] = '$'; } } } } void odzyskaj() { int j = B.size() - 1; for (int i = A.size() - 1; i >= 1;) { if (z[i][j] == '#') { i--; continue; } if (z[i][j] == '$') { j--; continue; } if (z[i][j] != 0) { K += z[i][j]; i--; j--; continue; } break; } } void RESULT() { for (int i = K.size() - 1; i >= 1; i--) cout << K[i]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); read(); COUNT_LCS(); odzyskaj(); RESULT(); }
#include <bits/stdc++.h> using namespace std; #define ll long long const int SIZE = 3e3 + 5; int DP[SIZE][SIZE]; char z[SIZE][SIZE]; string B = "0", A = "0", K = "0", P, Q; void read() { cin >> P >> Q; A += P; B += Q; } void COUNT_LCS() { for (int i = 1; i <= A.size() - 1; i++) for (int j = 1; j <= B.size() - 1; j++) { if (A[i] == B[j]) { DP[i][j] += DP[i - 1][j - 1] + 1; z[i][j] = A[i]; } else { if (DP[i - 1][j] > DP[i][j - 1]) { DP[i][j] += DP[i - 1][j]; z[i][j] = '#'; } else { DP[i][j] += DP[i][j - 1]; z[i][j] = '$'; } } } } void odzyskaj() { int j = B.size() - 1; for (int i = A.size() - 1; i >= 1;) { if (z[i][j] == '#') { i--; continue; } if (z[i][j] == '$') { j--; continue; } if (z[i][j] != 0) { K += z[i][j]; i--; j--; continue; } break; } } void RESULT() { for (int i = K.size() - 1; i >= 1; i--) cout << K[i]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); read(); COUNT_LCS(); odzyskaj(); RESULT(); }
replace
4
5
4
5
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; string s, t; int c[3000][3000] = {}; pair<int, int> w[3000][3000] = {}; string ans = ""; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> s >> t; c[0][0] = (s[0] == t[0]); for (int i = 1; i < s.length(); i++) { c[i][0] = (c[i - 1][0] | (s[i] == t[0])); w[i][0] = {-1, 0}; } for (int i = 1; i < t.length(); i++) { c[0][i] = (c[0][i - 1] | (s[0] == t[i])); w[0][i] = {0, -1}; } for (int i = 1; i < s.length(); i++) { for (int j = 1; j < t.length(); j++) { if (s[i] == t[j]) { c[i][j] = c[i - 1][j - 1] + 1; w[i][j] = {-1, -1}; } else { if (c[i - 1][j] > c[i][j - 1]) { c[i][j] = c[i - 1][j]; w[i][j] = {-1, 0}; } else { c[i][j] = c[i][j - 1]; w[i][j] = {0, -1}; } } } } int i = s.length() - 1; int j = t.length() - 1; while (1) { int x = w[i][j].first; int y = w[i][j].second; if ((s[i] == t[j] && c[i][j] == 1) || (x == -1 && y == -1)) ans += s[i]; if (s[i] == t[j] && c[i][j] == 1) break; i += x; j += y; } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; string s, t; int c[3000][3000] = {}; pair<int, int> w[3000][3000] = {}; string ans = ""; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> s >> t; c[0][0] = (s[0] == t[0]); for (int i = 1; i < s.length(); i++) { c[i][0] = (c[i - 1][0] | (s[i] == t[0])); w[i][0] = {-1, 0}; } for (int i = 1; i < t.length(); i++) { c[0][i] = (c[0][i - 1] | (s[0] == t[i])); w[0][i] = {0, -1}; } for (int i = 1; i < s.length(); i++) { for (int j = 1; j < t.length(); j++) { if (s[i] == t[j]) { c[i][j] = c[i - 1][j - 1] + 1; w[i][j] = {-1, -1}; } else { if (c[i - 1][j] > c[i][j - 1]) { c[i][j] = c[i - 1][j]; w[i][j] = {-1, 0}; } else { c[i][j] = c[i][j - 1]; w[i][j] = {0, -1}; } } } } int i = s.length() - 1; int j = t.length() - 1; if (c[i][j] == 0) return 0; while (1) { int x = w[i][j].first; int y = w[i][j].second; if ((s[i] == t[j] && c[i][j] == 1) || (x == -1 && y == -1)) ans += s[i]; if (s[i] == t[j] && c[i][j] == 1) break; i += x; j += y; } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
insert
46
46
46
49
TLE
p03165
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdint> #include <cstdlib> #include <iostream> #include <list> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep_r(i, n) for (int i = n - 1; i >= 0; i--) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define all(x) (x).begin(), (x).end() #define SZ(x) ((ll)(x).size()) #define bit(n) (1LL << (n)) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define INF bit(60) #define pb push_back #define mod 1000000007 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; } using namespace std; using uif = uint_fast64_t; using ll = long long int; int qp(int a, ll b) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % mod; a = 1ll * a * a % mod; } while (b >>= 1); return ans; } int qp(int a, ll b, int mo) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main(void) { string s, t; cin >> s; cin >> t; vector<vector<ll>> dp(3001, vector<ll>(3001, 0)); string lcs; rep(i, s.size() + 1) { rep(j, t.size() + 1) { if (s[i] == t[j]) { dp[i + 1][j + 1] = max({dp[i][j] + 1, dp[i][j + 1], dp[i + 1][j]}); } else { dp[i + 1][j + 1] = max({dp[i][j + 1], dp[i + 1][j]}); } } } rep(i, s.size() + 1) { rep(j, t.size() + 1) { // cout << dp[i][j] << " "; } // cout << endl; } ll i = s.size(), j = t.size(); ll cnt = dp[i][j]; if (cnt == 0) { cout << "" << endl; return 0; } while (i > 0 && j > 0) { while (cnt == dp[i][j - 1] && j > 1) j--; while (cnt == dp[i - 1][j] && i > 1) i--; lcs += s[i - 1]; // cout << i << "," << j << endl; // cout << s[i-1] << "," << t[j-1] << endl; i--; j--; cnt--; if (cnt == 0) break; } reverse(all(lcs)); cout << lcs << endl; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdint> #include <cstdlib> #include <iostream> #include <list> #include <queue> #include <set> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep_r(i, n) for (int i = n - 1; i >= 0; i--) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define all(x) (x).begin(), (x).end() #define SZ(x) ((ll)(x).size()) #define bit(n) (1LL << (n)) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); #define INF bit(60) #define pb push_back #define mod 1000000007 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; } using namespace std; using uif = uint_fast64_t; using ll = long long int; int qp(int a, ll b) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % mod; a = 1ll * a * a % mod; } while (b >>= 1); return ans; } int qp(int a, ll b, int mo) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main(void) { string s, t; cin >> s; cin >> t; vector<vector<ll>> dp(3002, vector<ll>(3002, 0)); string lcs; rep(i, s.size() + 1) { rep(j, t.size() + 1) { if (s[i] == t[j]) { dp[i + 1][j + 1] = max({dp[i][j] + 1, dp[i][j + 1], dp[i + 1][j]}); } else { dp[i + 1][j + 1] = max({dp[i][j + 1], dp[i + 1][j]}); } } } rep(i, s.size() + 1) { rep(j, t.size() + 1) { // cout << dp[i][j] << " "; } // cout << endl; } ll i = s.size(), j = t.size(); ll cnt = dp[i][j]; if (cnt == 0) { cout << "" << endl; return 0; } while (i > 0 && j > 0) { while (cnt == dp[i][j - 1] && j > 1) j--; while (cnt == dp[i - 1][j] && i > 1) i--; lcs += s[i - 1]; // cout << i << "," << j << endl; // cout << s[i-1] << "," << t[j-1] << endl; i--; j--; cnt--; if (cnt == 0) break; } reverse(all(lcs)); cout << lcs << endl; return 0; }
replace
73
74
73
74
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define fore(i, init, n) for (long long i = init; i < n; i++) #define FIN \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); typedef long long ll; const int MAXN = 1e4 + 5; int sn, tn, dp[MAXN][MAXN]; string s, t; bool vis[MAXN], v[MAXN]; void debug() { cout << endl << endl; fore(i, 0, sn) { fore(j, 0, tn) { cout << dp[i][j] << " "; } cout << s[i]; cout << endl; } fore(i, 0, tn + 1) cout << t[i] << " "; } int main() { FIN; cin >> s >> t; sn = s.size(); tn = t.size(); for (int i = sn - 1; i >= 0; i--) { for (int j = tn - 1; j >= 0; j--) { if (s[i] == t[j]) { dp[i][j] = 1 + dp[i + 1][j + 1]; } dp[i][j] = max({dp[i][j], dp[i + 1][j], dp[i][j + 1]}); } } int i = 0, j = 0; string res = ""; while (i < sn || j < tn) { if (s[i] == t[j]) { res += s[i]; i++; j++; } else { if (dp[i + 1][j] > dp[i][j + 1]) i++; else j++; } } cout << res << endl; // debug(); }
#include <bits/stdc++.h> using namespace std; #define fore(i, init, n) for (long long i = init; i < n; i++) #define FIN \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); typedef long long ll; const int MAXN = 1e4 + 5; int sn, tn, dp[MAXN][MAXN]; string s, t; bool vis[MAXN], v[MAXN]; void debug() { cout << endl << endl; fore(i, 0, sn) { fore(j, 0, tn) { cout << dp[i][j] << " "; } cout << s[i]; cout << endl; } fore(i, 0, tn + 1) cout << t[i] << " "; } int main() { FIN; cin >> s >> t; sn = s.size(); tn = t.size(); for (int i = sn - 1; i >= 0; i--) { for (int j = tn - 1; j >= 0; j--) { if (s[i] == t[j]) { dp[i][j] = 1 + dp[i + 1][j + 1]; } dp[i][j] = max({dp[i][j], dp[i + 1][j], dp[i][j + 1]}); } } int i = 0, j = 0; string res = ""; while (i < sn && j < tn) { if (s[i] == t[j]) { res += s[i]; i++; j++; } else { if (dp[i + 1][j] > dp[i][j + 1]) i++; else j++; } } cout << res << endl; // debug(); }
replace
43
44
43
44
-11
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define MAX 3000 using namespace std; int dp[MAX + 1][MAX + 1]; string print_lcs(string &s, string &t, int len) { string LCS = ""; int i = 0, j = 0; while (len > 0) { if (s[i] == t[j]) { LCS += s[i]; i++; j++; len--; } else { if (dp[i][j + 1] > dp[i + 1][j]) { j++; } else { i++; } } } return LCS; } int length_lcs(string &s, string &t, int i, int j) { if (i >= s.length() || j >= t.length()) { return 0; } if (s[i] == t[j]) { return dp[i][j] = 1 + length_lcs( s, t, i + 1, j + 1); /// here dp[i][j] represents length of lcs of string /// s from i to end and string t from j to end } else { return dp[i][j] = max(length_lcs(s, t, i + 1, j), length_lcs(s, t, i, j + 1)); } } int main() { string s, t; cin >> s >> t; memset(dp, -1, sizeof(dp)); int len = length_lcs(s, t, 0, 0); string LCS; LCS = print_lcs(s, t, len); cout << LCS << "\n"; return 0; }
#include <bits/stdc++.h> #define MAX 3000 using namespace std; int dp[MAX + 1][MAX + 1]; string print_lcs(string &s, string &t, int len) { string LCS = ""; int i = 0, j = 0; while (len > 0) { if (s[i] == t[j]) { LCS += s[i]; i++; j++; len--; } else { if (dp[i][j + 1] > dp[i + 1][j]) { j++; } else { i++; } } } return LCS; } int length_lcs(string &s, string &t, int i, int j) { if (i >= s.length() || j >= t.length()) { return 0; } if (dp[i][j] != -1) { return dp[i][j]; } if (s[i] == t[j]) { return dp[i][j] = 1 + length_lcs( s, t, i + 1, j + 1); /// here dp[i][j] represents length of lcs of string /// s from i to end and string t from j to end } else { return dp[i][j] = max(length_lcs(s, t, i + 1, j), length_lcs(s, t, i, j + 1)); } } int main() { string s, t; cin >> s >> t; memset(dp, -1, sizeof(dp)); int len = length_lcs(s, t, 0, 0); string LCS; LCS = print_lcs(s, t, len); cout << LCS << "\n"; return 0; }
insert
33
33
33
36
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int using namespace std; int main() { #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 s1; string s2; cin >> s1 >> s2; int n = s1.size(); int m = s2.size(); int 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 (s1[i - 1] == s2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int i = n, j = m; string ans = ""; while (i != 0 || j != 0) { if (s1[i - 1] == s2[j - 1]) { ans += s1[i - 1]; i--; j--; } else { if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
#include <bits/stdc++.h> #define ll long long int using namespace std; int main() { #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 s1; string s2; cin >> s1 >> s2; int n = s1.size(); int m = s2.size(); int 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 (s1[i - 1] == s2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int i = n, j = m; string ans = ""; while (i != 0 && j != 0) { if (s1[i - 1] == s2[j - 1]) { ans += s1[i - 1]; i--; j--; } else { if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
33
34
33
34
0
p03165
C++
Runtime Error
#include <algorithm> #include <bitset> #include <chrono> #include <climits> #include <cmath> #include <cstring> #include <deque> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> p64; typedef vector<ll> v64; const int N = 2 * 1e5 + 10; const ll inf = 1e18 + 100; const ll mod = 1e9 + 7; #define pb push_back #define mp make_pair #define ff first #define ss second #define rep(i, s, e) for (long long i = s; i <= e; i++) #define brep(i, s, e) for (long long i = s; i >= e; i--) #define all(x) x.begin(), x.end() #define mem(x, y) memset(x, y, sizeof(x)) #define DANGER \ std::ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) void solve() { string s, t; cin >> s >> t; ll 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; } 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]); } } } } // cout<<dp[s.size() +1][t.size() + 1]; string ans = ""; int row = s.size(), col = t.size(); while (row != 0 || col != 0) { if (dp[row][col] == dp[row - 1][col]) { row = row - 1; continue; } if (dp[row][col] == dp[row][col - 1]) { col = col - 1; continue; } ans += s[row - 1]; row--; col--; } reverse(ans.begin(), ans.end()); cout << ans; } int main() { ll t = 1; // cin>>t; while (t--) { solve(); } return 0; }
#include <algorithm> #include <bitset> #include <chrono> #include <climits> #include <cmath> #include <cstring> #include <deque> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> p64; typedef vector<ll> v64; const int N = 2 * 1e5 + 10; const ll inf = 1e18 + 100; const ll mod = 1e9 + 7; #define pb push_back #define mp make_pair #define ff first #define ss second #define rep(i, s, e) for (long long i = s; i <= e; i++) #define brep(i, s, e) for (long long i = s; i >= e; i--) #define all(x) x.begin(), x.end() #define mem(x, y) memset(x, y, sizeof(x)) #define DANGER \ std::ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) void solve() { string s, t; cin >> s >> t; ll 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; } 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]); } } } } // cout<<dp[s.size() +1][t.size() + 1]; string ans = ""; int row = s.size(), col = t.size(); while (row != 0 && col != 0) { if (dp[row][col] == dp[row - 1][col]) { row = row - 1; continue; } if (dp[row][col] == dp[row][col - 1]) { col = col - 1; continue; } ans += s[row - 1]; row--; col--; } reverse(ans.begin(), ans.end()); cout << ans; } int main() { ll t = 1; // cin>>t; while (t--) { solve(); } return 0; }
replace
61
62
61
62
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int recurse(int i, int j, vector<vector<int>> &dp, string s, string t) { if (i == 0 || j == 0) return 0; else { if (dp[i][j] != -1) return dp[i][j]; else { if (s[i - 1] == t[j - 1]) dp[i][j] = recurse(i - 1, j - 1, dp, s, t) + 1; else dp[i][j] = max(recurse(i - 1, j, dp, s, t), recurse(i, j - 1, dp, s, t)); return dp[i][j]; } } } int main(int argc, char const *argv[]) { string s, t; cin >> s >> t; int n = s.length(); int m = t.length(); vector<vector<int>> dp(n + 1, vector<int>(m + 1, -1)); dp[n][m] = recurse(n, m, dp, s, t); int i = n; int j = m; int k = 1; char lcs[dp[n][m] + 1]; lcs[dp[n][m]] = '\0'; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { lcs[dp[n][m] - k] = s[i - 1]; k++; i--; j--; } else { if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } } cout << lcs; return 0; }
#include <bits/stdc++.h> using namespace std; int recurse(int i, int j, vector<vector<int>> &dp, string &s, string &t) { if (i == 0 || j == 0) return 0; else { if (dp[i][j] != -1) return dp[i][j]; else { if (s[i - 1] == t[j - 1]) dp[i][j] = recurse(i - 1, j - 1, dp, s, t) + 1; else dp[i][j] = max(recurse(i - 1, j, dp, s, t), recurse(i, j - 1, dp, s, t)); return dp[i][j]; } } } int main(int argc, char const *argv[]) { string s, t; cin >> s >> t; int n = s.length(); int m = t.length(); vector<vector<int>> dp(n + 1, vector<int>(m + 1, -1)); dp[n][m] = recurse(n, m, dp, s, t); int i = n; int j = m; int k = 1; char lcs[dp[n][m] + 1]; lcs[dp[n][m]] = '\0'; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { lcs[dp[n][m] - k] = s[i - 1]; k++; i--; j--; } else { if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } } cout << lcs; return 0; }
replace
4
5
4
5
TLE
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long using namespace std; int dp[3005][3005]; int lcs(string a, string b, int n, int m) { if (n == 0 or m == 0) return 0; if (dp[n][m] != -1) return dp[n][m]; if (a[n - 1] == b[m - 1]) { return dp[n][m] = 1 + lcs(a, b, n - 1, m - 1); } int x = lcs(a, b, n - 1, m); int y = lcs(a, b, n, m - 1); return dp[n][m] = max(x, y); } void solve() { string a, b; cin >> a >> b; int n = a.size(), m = b.size(); memset(dp, -1, sizeof(dp)); int len = lcs(a, b, n, m); char ans[len + 1]; ans[len] = '\0'; int i = n, j = m; while (i > 0 and j > 0) { if (a[i - 1] == b[j - 1]) { ans[len - 1] = a[i - 1]; i--, j--, len--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } for (int k = 0; ans[k] != '\0'; ++k) cout << ans[k]; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; int dp[3005][3005]; int lcs(string &a, string &b, int n, int m) { if (n == 0 or m == 0) return 0; if (dp[n][m] != -1) return dp[n][m]; if (a[n - 1] == b[m - 1]) { return dp[n][m] = 1 + lcs(a, b, n - 1, m - 1); } int x = lcs(a, b, n - 1, m); int y = lcs(a, b, n, m - 1); return dp[n][m] = max(x, y); } void solve() { string a, b; cin >> a >> b; int n = a.size(), m = b.size(); memset(dp, -1, sizeof(dp)); int len = lcs(a, b, n, m); char ans[len + 1]; ans[len] = '\0'; int i = n, j = m; while (i > 0 and j > 0) { if (a[i - 1] == b[j - 1]) { ans[len - 1] = a[i - 1]; i--, j--, len--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } for (int k = 0; ans[k] != '\0'; ++k) cout << ans[k]; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; while (t--) solve(); return 0; }
replace
6
7
6
7
TLE
p03165
C++
Runtime Error
/* @author: @sancodemonster */ #include <bits/stdc++.h> #define flash ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define endl '\n' #define ll long long int #define ld long double #define pb push_back #define mp make_pair #define MOD 1000000007 #define f(i, a, b) for (long long i = a; i < b; i++) #define all(c) (c).begin(), (c).end() using namespace std; string s, t; ll dp[3010][3010]; ll parentDP[3010][3010] = {0}; string ansStr = ""; std::vector<ll> v; ll rec(ll i, ll j, string &s, string &t) { if (i == s.size() || j == t.size()) { return 0; } ll one = INT_MIN, two = INT_MIN, three = INT_MIN; if (dp[i][j] != -1) return dp[i][j]; if (s[i] == t[j]) { one = 1 + rec(i + 1, j + 1, s, t); } else { // if(i<s.size()) two = rec(i + 1, j, s, t); // if(j<t.size()) three = rec(i, j + 1, s, t); } ll maxi = INT_MIN; maxi = max(one, two); if (maxi == one) { parentDP[i][j] = 1; dp[i][j] = one; } else { parentDP[i][j] = 2; dp[i][j] = two; } maxi = max(maxi, three); if (maxi == three) { parentDP[i][j] = 3; dp[i][j] = three; } return dp[i][j]; } void backtrack(ll i, ll j, string &s, string &t) { if (i == s.size() || j == t.size()) { return; } if (parentDP[i][j] == 1) { v.push_back(i); backtrack(i + 1, j + 1, s, t); } if (parentDP[i][j] == 2) { backtrack(i + 1, j, s, t); } if (parentDP[i][j] == 3) backtrack(i, j + 1, s, t); } int main() { #ifndef ONLINE_JUDGE freopen("C:\\desktopp\\new coding problems\\in.txt", "r", stdin); freopen("C:\\desktopp\\new coding problems\\out.txt", "w", stdout); #endif flash; ///////////////////////////////////////////////////////////////////////////////////////////////////// cin >> s; cin >> t; ll i, j; f(i, 0, 3010) { f(j, 0, 3010) { dp[i][j] = -1; } } ll str = rec(0, 0, s, t); // cout<<str; backtrack(0, 0, s, t); f(i, 0, v.size()) { cout << s[v[i]]; } // cout<<ansStr; ///////////////////////////////////////////////////////////////////////////////////////////////////// return 0; }
/* @author: @sancodemonster */ #include <bits/stdc++.h> #define flash ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define endl '\n' #define ll long long int #define ld long double #define pb push_back #define mp make_pair #define MOD 1000000007 #define f(i, a, b) for (long long i = a; i < b; i++) #define all(c) (c).begin(), (c).end() using namespace std; string s, t; ll dp[3010][3010]; ll parentDP[3010][3010] = {0}; string ansStr = ""; std::vector<ll> v; ll rec(ll i, ll j, string &s, string &t) { if (i == s.size() || j == t.size()) { return 0; } ll one = INT_MIN, two = INT_MIN, three = INT_MIN; if (dp[i][j] != -1) return dp[i][j]; if (s[i] == t[j]) { one = 1 + rec(i + 1, j + 1, s, t); } else { // if(i<s.size()) two = rec(i + 1, j, s, t); // if(j<t.size()) three = rec(i, j + 1, s, t); } ll maxi = INT_MIN; maxi = max(one, two); if (maxi == one) { parentDP[i][j] = 1; dp[i][j] = one; } else { parentDP[i][j] = 2; dp[i][j] = two; } maxi = max(maxi, three); if (maxi == three) { parentDP[i][j] = 3; dp[i][j] = three; } return dp[i][j]; } void backtrack(ll i, ll j, string &s, string &t) { if (i == s.size() || j == t.size()) { return; } if (parentDP[i][j] == 1) { v.push_back(i); backtrack(i + 1, j + 1, s, t); } if (parentDP[i][j] == 2) { backtrack(i + 1, j, s, t); } if (parentDP[i][j] == 3) backtrack(i, j + 1, s, t); } int main() { flash; ///////////////////////////////////////////////////////////////////////////////////////////////////// cin >> s; cin >> t; ll i, j; f(i, 0, 3010) { f(j, 0, 3010) { dp[i][j] = -1; } } ll str = rec(0, 0, s, t); // cout<<str; backtrack(0, 0, s, t); f(i, 0, v.size()) { cout << s[v[i]]; } // cout<<ansStr; ///////////////////////////////////////////////////////////////////////////////////////////////////// return 0; }
delete
82
86
82
82
-11
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long #define Int int32_t #define all(c) c.begin(), c.end() #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL); #define pii pair<int, int> #define pb push_back #define F first #define S second #define endl "\n" #define bitcnt(n) __builtin_popcountll(n) #define setpre(n) cout << fixed << setprecision(n) #define tr(c) \ for (const auto &x : c) \ cout << x << " "; \ cout << "\n"; #define ol(c, s, e) \ for (int pos = s; pos < e; pos++) \ cout << c[pos] << " "; \ cout << "\n"; #define PI acos(-1LL) const int M = 1000000007; const int N = 2e5 + 5; const long long INF = 1e9 + 12; string a, b; int dp[3005][3005]; int fun(int i, int j) { if (i < 0 || j < 0) return 0; if (dp[i][j] != -1) return dp[i][j]; if (a[i] == b[j]) { return dp[i][j] = 1 + fun(i - 1, j - 1); } else { return dp[i][j] = max(fun(i - 1, j), fun(i, j - 1)); } } string ans; void path(int i, int j) { if (i < 0 || j < 0) return; if (a[i] == b[j]) { ans += a[i]; path(i - 1, j - 1); } else { if (dp[i][j] == dp[i - 1][j]) path(i - 1, j); else path(i, j - 1); } } void solve() { cin >> a >> b; memset(dp, -1, sizeof dp); fun(a.size() - 1, b.size() - 1); path(a.size() - 1, b.size() - 1); reverse(all(ans)); cout << ans; } signed main() { FAST int tc = 1; // TODO: check for tc // cin >> tc; for (int t = 1; t <= tc; t++) { solve(); } }
#include <bits/stdc++.h> using namespace std; #define int long long #define Int int32_t #define all(c) c.begin(), c.end() #define FAST ios_base::sync_with_stdio(false), cin.tie(NULL); #define pii pair<int, int> #define pb push_back #define F first #define S second #define endl "\n" #define bitcnt(n) __builtin_popcountll(n) #define setpre(n) cout << fixed << setprecision(n) #define tr(c) \ for (const auto &x : c) \ cout << x << " "; \ cout << "\n"; #define ol(c, s, e) \ for (int pos = s; pos < e; pos++) \ cout << c[pos] << " "; \ cout << "\n"; #define PI acos(-1LL) const int M = 1000000007; const int N = 2e5 + 5; const long long INF = 1e9 + 12; string a, b; int dp[3005][3005]; int fun(int i, int j) { if (i < 0 || j < 0) return 0; if (dp[i][j] != -1) return dp[i][j]; if (a[i] == b[j]) { return dp[i][j] = 1 + fun(i - 1, j - 1); } else { return dp[i][j] = max(fun(i - 1, j), fun(i, j - 1)); } } string ans; void path(int i, int j) { if (i < 0 || j < 0) return; if (a[i] == b[j]) { ans += a[i]; path(i - 1, j - 1); } else { if (fun(i, j) == fun(i - 1, j)) path(i - 1, j); else path(i, j - 1); } } void solve() { cin >> a >> b; memset(dp, -1, sizeof dp); fun(a.size() - 1, b.size() - 1); path(a.size() - 1, b.size() - 1); reverse(all(ans)); cout << ans; } signed main() { FAST int tc = 1; // TODO: check for tc // cin >> tc; for (int t = 1; t <= tc; t++) { solve(); } }
replace
48
49
48
49
-11
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define ALL(x) (x).begin(), (x).end() typedef long long ll; typedef pair<int, int> pii; const int INF = 1e9; const ll LINF = 1e15; const int MOD = 1000000007; const double PI = acos(-1); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int dp[3001][3001]; int main() { string s, t; cin >> s >> t; int n = s.size(); int m = t.size(); memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; 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]); } } } int length = dp[n][m]; int l = n; int r = m; string ans = ""; while (length >= 0) { if (s[l - 1] == t[r - 1]) { ans += s[l - 1]; l--; r--; length--; } else if (dp[l][r] == dp[l - 1][r]) l--; else r--; } reverse(ALL(ans)); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) #define ALL(x) (x).begin(), (x).end() typedef long long ll; typedef pair<int, int> pii; const int INF = 1e9; const ll LINF = 1e15; const int MOD = 1000000007; const double PI = acos(-1); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int dp[3001][3001]; int main() { string s, t; cin >> s >> t; int n = s.size(); int m = t.size(); memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; 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]); } } } int length = dp[n][m]; int l = n; int r = m; string ans = ""; while (length > 0) { if (s[l - 1] == t[r - 1]) { ans += s[l - 1]; l--; r--; length--; } else if (dp[l][r] == dp[l - 1][r]) l--; else r--; } reverse(ALL(ans)); cout << ans << endl; }
replace
35
36
35
36
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int llu; int dp[100][100]; /* Memoized LCS int lcs(string a,string b,int n,int m){ if(n==0 || m==0){ return 0; } if(dp[n][m]!= (-1)){ return dp[n][m]; } if(a[n-1]==b[m-1]){ return dp[n][m]=1+lcs(a,b,n-1,m-1); } else{ return dp[n][m] = max(lcs(a,b,n-1,m),lcs(a,b,n,m-1)); } } */ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string a, b; cin >> a >> b; int n, m; n = a.length(); m = b.length(); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i - 1] == b[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<<lcs(a,b,a.size(),b.size()); // length of lcs // cout<<dp[n][m]<<"\n"; // Printing lcs int i = n, j = m; string res = ""; while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { res.push_back(a[i - 1]); i--, j--; } else { if (dp[i][j - 1] >= dp[i - 1][j]) { j--; } else { i--; } } } reverse(res.begin(), res.end()); cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int llu; int dp[3001][3001]; /* Memoized LCS int lcs(string a,string b,int n,int m){ if(n==0 || m==0){ return 0; } if(dp[n][m]!= (-1)){ return dp[n][m]; } if(a[n-1]==b[m-1]){ return dp[n][m]=1+lcs(a,b,n-1,m-1); } else{ return dp[n][m] = max(lcs(a,b,n-1,m),lcs(a,b,n,m-1)); } } */ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string a, b; cin >> a >> b; int n, m; n = a.length(); m = b.length(); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i - 1] == b[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<<lcs(a,b,a.size(),b.size()); // length of lcs // cout<<dp[n][m]<<"\n"; // Printing lcs int i = n, j = m; string res = ""; while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { res.push_back(a[i - 1]); i--, j--; } else { if (dp[i][j - 1] >= dp[i - 1][j]) { j--; } else { i--; } } } reverse(res.begin(), res.end()); cout << res; return 0; }
replace
6
7
6
7
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, m) for (long long i = 0; i < m; i++) #define per(i, m) for (long long i = m - 1; i >= 0; i--) #define FOR(i, n, m) for (long long i = n; i < m; i++) #define ROF(i, n, m) for (long long i = m - 1; i >= n; i--) #define SORT(v, n) \ do { \ sort(v, v + n); \ reverse(v, v + n); \ } while (0) #define all(x) (x).begin(), (x).end() #define EPS (1e-7) #define INF (1e18) #define PI (acos(-1)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using namespace std; typedef long long ll; const ll MOD = 1000000007; typedef pair<ll, ll> LP; ll POW(ll x, ll n) { if (n == 0) return 1; if (n % 2 == 0) return POW(x * x, n / 2) % MOD; return x % MOD * POW(x, n - 1) % MOD; } ll POW2(ll x, ll n) { if (n == 0) return 1; if (n % 2 == 0) return POW2(x * x, n / 2); return x * POW2(x, n - 1); } ll POW3(ll x, ll n, ll m) { x %= m; if (n == 0) return 1; if (n % 2 == 0) return POW3(x * x, n / 2, m) % m; return x * POW3(x, n - 1, m) % m; } ll gcd(ll u, ll v) { ll r; while (0 != v) { r = u % v; u = v; v = r; } return u; } ll lcm(ll u, ll v) { return u * v / gcd(u, v); } ll KAI(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI(m - 1) % MOD; } ll KAI2(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI2(m - 1); } ll extGCD(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } inline ll mod(ll a, ll m) { return (a % m + m) % m; } ll modinv(ll a) { ll x, y; extGCD(a, MOD, x, y); return mod(x, MOD); } ll COM(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI(m) % MOD * modinv(KAI(n) % MOD * KAI(m - n) % MOD) % MOD; } ll COM2(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI2(m) / KAI2(n) / KAI2(m - n); } ll DEC(ll x, ll m, ll n) // xのm進数でのx^nの位の値 { return x % POW2(m, n + 1) / POW2(m, n); } ll keta(ll x, ll n) // xのn進数での桁数 { if (x == 0) return 0; return keta(x / n, n) + 1; } ll DIV(ll x, ll n) // x!のnで割り切れる回数 { if (x == 0) return 0; return x / n + DIV(x / n, n); } ll ORD(ll x, ll n) // xのnで割り切れる回数 { if (x == 0) return INF; if (x % n != 0) return 0; return 1 + ORD(x / n, n); } ll SGS(ll x, ll y, ll m) // 1+x+…+x^(y-1)をmで割った余り { if (y == 0) return 0; if (y % 2 == 0) { return (1 + POW3(x, y / 2, m)) * SGS(x, y / 2, m) % m; } return (1 + x * SGS(x, y - 1, m)) % m; } ll SSGS(ll x, ll y, ll m) // Σ[k=1→y](1+x+…+x^(k-1))をmで割った余り { if (y == 0) return 0; if (y == 1) return 1; if (y % 2 == 0) { return (SSGS(x, y / 2, m) * (POW3(x, y / 2, m) + 1) % m + SGS(x, y / 2, m) * y / 2 % m) % m; } return (SSGS(x, y - 1, m) * x % m + y) % m; } struct UnionFind { vector<int> par; vector<int> sizes; UnionFind(int n) : par(n), sizes(n, 1) { rep(i, n) par[i] = i; } int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sizes[x] < sizes[y]) swap(x, y); par[y] = x; sizes[x] += sizes[y]; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sizes[find(x)]; } }; int main() { string s, t; ll dp[320][320], m[320][320], a, b; stack<char> p; cin >> s >> t; rep(i, s.length()) { rep(j, t.length()) { if (s[i] == t[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; m[i + 1][j + 1] = 0; } dp[i + 1][j + 1] = max({dp[i + 1][j], dp[i][j + 1], dp[i + 1][j + 1]}); if (dp[i + 1][j + 1] == dp[i + 1][j]) { m[i + 1][j + 1] = 1; } if (dp[i + 1][j + 1] == dp[i][j + 1]) { m[i + 1][j + 1] = 2; } } } a = s.length(); b = t.length(); while (a >= 1 && b >= 1) { if (m[a][b] == 0) { p.push(s[a - 1]); a--; b--; } else if (m[a][b] == 1) b--; else a--; } while (p.size()) { cout << p.top(); p.pop(); } }
#include <bits/stdc++.h> #define rep(i, m) for (long long i = 0; i < m; i++) #define per(i, m) for (long long i = m - 1; i >= 0; i--) #define FOR(i, n, m) for (long long i = n; i < m; i++) #define ROF(i, n, m) for (long long i = m - 1; i >= n; i--) #define SORT(v, n) \ do { \ sort(v, v + n); \ reverse(v, v + n); \ } while (0) #define all(x) (x).begin(), (x).end() #define EPS (1e-7) #define INF (1e18) #define PI (acos(-1)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using namespace std; typedef long long ll; const ll MOD = 1000000007; typedef pair<ll, ll> LP; ll POW(ll x, ll n) { if (n == 0) return 1; if (n % 2 == 0) return POW(x * x, n / 2) % MOD; return x % MOD * POW(x, n - 1) % MOD; } ll POW2(ll x, ll n) { if (n == 0) return 1; if (n % 2 == 0) return POW2(x * x, n / 2); return x * POW2(x, n - 1); } ll POW3(ll x, ll n, ll m) { x %= m; if (n == 0) return 1; if (n % 2 == 0) return POW3(x * x, n / 2, m) % m; return x * POW3(x, n - 1, m) % m; } ll gcd(ll u, ll v) { ll r; while (0 != v) { r = u % v; u = v; v = r; } return u; } ll lcm(ll u, ll v) { return u * v / gcd(u, v); } ll KAI(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI(m - 1) % MOD; } ll KAI2(ll m) { if (m < 0) return 0; if (m == 0) return 1; return m * KAI2(m - 1); } ll extGCD(ll a, ll b, ll &x, ll &y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } inline ll mod(ll a, ll m) { return (a % m + m) % m; } ll modinv(ll a) { ll x, y; extGCD(a, MOD, x, y); return mod(x, MOD); } ll COM(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI(m) % MOD * modinv(KAI(n) % MOD * KAI(m - n) % MOD) % MOD; } ll COM2(ll m, ll n) { if (m < n) return 0; if (n < 0) return 0; if (n == 0) return 1; if (m == n) return 1; return KAI2(m) / KAI2(n) / KAI2(m - n); } ll DEC(ll x, ll m, ll n) // xのm進数でのx^nの位の値 { return x % POW2(m, n + 1) / POW2(m, n); } ll keta(ll x, ll n) // xのn進数での桁数 { if (x == 0) return 0; return keta(x / n, n) + 1; } ll DIV(ll x, ll n) // x!のnで割り切れる回数 { if (x == 0) return 0; return x / n + DIV(x / n, n); } ll ORD(ll x, ll n) // xのnで割り切れる回数 { if (x == 0) return INF; if (x % n != 0) return 0; return 1 + ORD(x / n, n); } ll SGS(ll x, ll y, ll m) // 1+x+…+x^(y-1)をmで割った余り { if (y == 0) return 0; if (y % 2 == 0) { return (1 + POW3(x, y / 2, m)) * SGS(x, y / 2, m) % m; } return (1 + x * SGS(x, y - 1, m)) % m; } ll SSGS(ll x, ll y, ll m) // Σ[k=1→y](1+x+…+x^(k-1))をmで割った余り { if (y == 0) return 0; if (y == 1) return 1; if (y % 2 == 0) { return (SSGS(x, y / 2, m) * (POW3(x, y / 2, m) + 1) % m + SGS(x, y / 2, m) * y / 2 % m) % m; } return (SSGS(x, y - 1, m) * x % m + y) % m; } struct UnionFind { vector<int> par; vector<int> sizes; UnionFind(int n) : par(n), sizes(n, 1) { rep(i, n) par[i] = i; } int find(int x) { if (x == par[x]) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sizes[x] < sizes[y]) swap(x, y); par[y] = x; sizes[x] += sizes[y]; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return sizes[find(x)]; } }; int main() { string s, t; ll dp[3200][3200], m[3200][3200], a, b; stack<char> p; cin >> s >> t; rep(i, s.length()) { rep(j, t.length()) { if (s[i] == t[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; m[i + 1][j + 1] = 0; } dp[i + 1][j + 1] = max({dp[i + 1][j], dp[i][j + 1], dp[i + 1][j + 1]}); if (dp[i + 1][j + 1] == dp[i + 1][j]) { m[i + 1][j + 1] = 1; } if (dp[i + 1][j + 1] == dp[i][j + 1]) { m[i + 1][j + 1] = 2; } } } a = s.length(); b = t.length(); while (a >= 1 && b >= 1) { if (m[a][b] == 0) { p.push(s[a - 1]); a--; b--; } else if (m[a][b] == 1) b--; else a--; } while (p.size()) { cout << p.top(); p.pop(); } }
replace
196
197
196
197
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main() { string s, d; cin >> s >> d; char c[1010]; int p = s.size(); int q = d.size(); int dp[p + 2][q + 2]; for (int i = 0; i <= p; i++) { for (int j = 0; j <= q; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (s[i - 1] == d[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][q] << endl; int i = p; int j = q; int k = 0; 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 { c[k] = s[i - 1]; i--; j--; k++; } } // cout << p << " " << q << endl; for (int i = k - 1; i >= 0; i--) cout << c[i]; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s, d; cin >> s >> d; char c[3010]; int p = s.size(); int q = d.size(); int dp[p + 2][q + 2]; for (int i = 0; i <= p; i++) { for (int j = 0; j <= q; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (s[i - 1] == d[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][q] << endl; int i = p; int j = q; int k = 0; 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 { c[k] = s[i - 1]; i--; j--; k++; } } // cout << p << " " << q << endl; for (int i = k - 1; i >= 0; i--) cout << c[i]; cout << endl; return 0; }
replace
6
7
6
7
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; const int maxn = 3010; int dp[maxn][maxn]; int main() { string s, t; cin >> s >> t; for (int i = 0; i < 3010; i++) { dp[i][0] = 0; dp[0][i] = 0; } 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]); } } string ans; int i = s.size(), j = t.size(); while (i > 0 || j > 0) { if (s[i - 1] == t[j - 1]) { ans += s[i - 1]; i--; j--; } else if (dp[i][j] == dp[i][j - 1]) { j--; } else if (dp[i][j] == dp[i - 1][j]) { i--; } } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
#include <bits/stdc++.h> #include <iostream> using namespace std; const int maxn = 3010; int dp[maxn][maxn]; int main() { string s, t; cin >> s >> t; for (int i = 0; i < 3010; i++) { dp[i][0] = 0; dp[0][i] = 0; } 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]); } } string ans; int i = s.size(), j = t.size(); while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ans += s[i - 1]; i--; j--; } else if (dp[i][j] == dp[i][j - 1]) { j--; } else if (dp[i][j] == dp[i - 1][j]) { i--; } } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
replace
24
25
24
25
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; char DIR[3000][3000]; void printLCS(int l1, int l2, string str) { if (l1 == 0 || l2 == 0) { return; } if (DIR[l1][l2] == 'A') { printLCS(l1 - 1, l2 - 1, str); cout << str[l1 - 1]; } else if (DIR[l1][l2] == 'U') { printLCS(l1 - 1, l2, str); } else { printLCS(l1, l2 - 1, str); } } void findLCS(string str1, string str2) { int l1 = str1.length(); int l2 = str2.length(); int M[l1 + 1][l2 + 1]; for (int i = 0; i <= l1; i++) { M[i][0] = 0; } for (int i = 0; i <= l2; i++) { M[0][i] = 0; } for (int i = 0; i <= l1; i++) { for (int j = 0; j <= l2; j++) { DIR[i][j] = 'N'; } } for (int i = 1; i <= l1; i++) { for (int j = 1; j <= l2; j++) { if (str1[i - 1] == str2[j - 1]) { M[i][j] = M[i - 1][j - 1] + 1; DIR[i][j] = 'A'; } else { if (M[i - 1][j] >= M[i][j - 1]) { M[i][j] = M[i - 1][j]; DIR[i][j] = 'U'; } else { M[i][j] = M[i][j - 1]; DIR[i][j] = 'L'; } } } } /* for(int i=0;i<=l1;i++){ for(int j=0;j<=l2;j++){ cout<<M[i][j]<<" "; } cout<<endl; }*/ printLCS(l1, l2, str1); } int main() { string str1, str2; cin >> str1 >> str2; findLCS(str1, str2); return 0; }
#include <bits/stdc++.h> using namespace std; char DIR[3005][3005]; void printLCS(int l1, int l2, string str) { if (l1 == 0 || l2 == 0) { return; } if (DIR[l1][l2] == 'A') { printLCS(l1 - 1, l2 - 1, str); cout << str[l1 - 1]; } else if (DIR[l1][l2] == 'U') { printLCS(l1 - 1, l2, str); } else { printLCS(l1, l2 - 1, str); } } void findLCS(string str1, string str2) { int l1 = str1.length(); int l2 = str2.length(); int M[l1 + 1][l2 + 1]; for (int i = 0; i <= l1; i++) { M[i][0] = 0; } for (int i = 0; i <= l2; i++) { M[0][i] = 0; } for (int i = 0; i <= l1; i++) { for (int j = 0; j <= l2; j++) { DIR[i][j] = 'N'; } } for (int i = 1; i <= l1; i++) { for (int j = 1; j <= l2; j++) { if (str1[i - 1] == str2[j - 1]) { M[i][j] = M[i - 1][j - 1] + 1; DIR[i][j] = 'A'; } else { if (M[i - 1][j] >= M[i][j - 1]) { M[i][j] = M[i - 1][j]; DIR[i][j] = 'U'; } else { M[i][j] = M[i][j - 1]; DIR[i][j] = 'L'; } } } } /* for(int i=0;i<=l1;i++){ for(int j=0;j<=l2;j++){ cout<<M[i][j]<<" "; } cout<<endl; }*/ printLCS(l1, l2, str1); } int main() { string str1, str2; cin >> str1 >> str2; findLCS(str1, str2); return 0; }
replace
4
5
4
5
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; string a, b; long long n, m; const long long N = 1e3 + 10; long long dp[N][N]; long long dfs(long long x, long long y) { if (x >= n) return 0; if (y >= m) return 0; if (dp[x][y] != -1) return dp[x][y]; long long choice1 = 0; long long choice2 = 0; long long choice3 = 0; if (a[x] == b[y]) { choice1 = 1 + dfs(x + 1, y + 1); } else { choice2 = dfs(x + 1, y); choice3 = dfs(x, y + 1); } return dp[x][y] = max({choice1, choice2, choice3}); } string ans = ""; void build(long long x, long long y) { if (x >= n) return; if (y >= m) return; // if ( dp[x][y] != -1 ) return dp[x][y] ; long long choice1 = 0; long long choice2 = 0; long long choice3 = 0; if (a[x] == b[y]) { choice1 = 1 + dfs(x + 1, y + 1); } else { choice2 = dfs(x + 1, y); choice3 = dfs(x, y + 1); } long long optimal = dfs(x, y); if (optimal == choice2) { build(x + 1, y); } else if (optimal == choice3) { build(x, y + 1); } else if (optimal == choice1) { ans += a[x]; build(x + 1, y + 1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); memset(dp, -1, sizeof dp); cin >> a >> b; n = (long long)a.size(); m = (long long)b.size(); dfs(0, 0); build(0, 0); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string a, b; long long n, m; const long long N = 3e3 + 10; long long dp[N][N]; long long dfs(long long x, long long y) { if (x >= n) return 0; if (y >= m) return 0; if (dp[x][y] != -1) return dp[x][y]; long long choice1 = 0; long long choice2 = 0; long long choice3 = 0; if (a[x] == b[y]) { choice1 = 1 + dfs(x + 1, y + 1); } else { choice2 = dfs(x + 1, y); choice3 = dfs(x, y + 1); } return dp[x][y] = max({choice1, choice2, choice3}); } string ans = ""; void build(long long x, long long y) { if (x >= n) return; if (y >= m) return; // if ( dp[x][y] != -1 ) return dp[x][y] ; long long choice1 = 0; long long choice2 = 0; long long choice3 = 0; if (a[x] == b[y]) { choice1 = 1 + dfs(x + 1, y + 1); } else { choice2 = dfs(x + 1, y); choice3 = dfs(x, y + 1); } long long optimal = dfs(x, y); if (optimal == choice2) { build(x + 1, y); } else if (optimal == choice3) { build(x, y + 1); } else if (optimal == choice1) { ans += a[x]; build(x + 1, y + 1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); memset(dp, -1, sizeof dp); cin >> a >> b; n = (long long)a.size(); m = (long long)b.size(); dfs(0, 0); build(0, 0); cout << ans << endl; return 0; }
replace
6
7
6
7
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int MAX_N = 3000; const int MAX_M = 3000; void LCS(string s, string t) { vector<vector<ll>> dp(MAX_N, vector<ll>(MAX_M)); vector<vector<pair<int, int>>> root(MAX_N, vector<pair<int, int>>(MAX_M)); rep(i, s.size()) { rep(j, t.size()) { if (s[i] == t[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; root[i + 1][j + 1].first = i; root[i + 1][j + 1].second = j; } else { if (dp[i + 1][j] > dp[i][j + 1]) { dp[i + 1][j + 1] = dp[i + 1][j]; root[i + 1][j + 1].first = i + 1; root[i + 1][j + 1].second = j; } else { dp[i + 1][j + 1] = dp[i][j + 1]; root[i + 1][j + 1].first = i; root[i + 1][j + 1].second = j + 1; } } } } string res; int rx = t.size(); int ry = s.size(); int ny = root[s.size()][t.size()].first; int nx = root[s.size()][t.size()].second; if ((rx - nx) == 1 && (ry - ny) == 1) { res = s[ny] + res; } while (nx != 0 && ny != 0) { rx = nx; ry = ny; ny = root[ry][rx].first; nx = root[ry][rx].second; if ((rx - nx) == 1 && (ry - ny) == 1) { res = s[ny] + res; } }; cout << res << endl; } int main() { string s; string t; cin >> s; cin >> t; LCS(s, t); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int MAX_N = 3000; const int MAX_M = 3000; void LCS(string s, string t) { vector<vector<ll>> dp(MAX_N + 1, vector<ll>(MAX_M + 1)); vector<vector<pair<int, int>>> root(MAX_N + 1, vector<pair<int, int>>(MAX_M + 1)); rep(i, s.size()) { rep(j, t.size()) { if (s[i] == t[j]) { dp[i + 1][j + 1] = dp[i][j] + 1; root[i + 1][j + 1].first = i; root[i + 1][j + 1].second = j; } else { if (dp[i + 1][j] > dp[i][j + 1]) { dp[i + 1][j + 1] = dp[i + 1][j]; root[i + 1][j + 1].first = i + 1; root[i + 1][j + 1].second = j; } else { dp[i + 1][j + 1] = dp[i][j + 1]; root[i + 1][j + 1].first = i; root[i + 1][j + 1].second = j + 1; } } } } string res; int rx = t.size(); int ry = s.size(); int ny = root[s.size()][t.size()].first; int nx = root[s.size()][t.size()].second; if ((rx - nx) == 1 && (ry - ny) == 1) { res = s[ny] + res; } while (nx != 0 && ny != 0) { rx = nx; ry = ny; ny = root[ry][rx].first; nx = root[ry][rx].second; if ((rx - nx) == 1 && (ry - ny) == 1) { res = s[ny] + res; } }; cout << res << endl; } int main() { string s; string t; cin >> s; cin >> t; LCS(s, t); return 0; }
replace
8
10
8
11
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03165
C++
Runtime Error
/* Author : Simanta Deb Turja */ #include <bits/stdc++.h> using namespace std; #define Professor using ll = long long; using i64 = unsigned long long; template <typename T> inline T Min(T a, T b, T c) { return min(a, min(b, c)); } template <typename T> inline T Min(T a, T b, T c, T d) { return min(a, min(b, min(c, d))); } template <typename T> inline T Max(T a, T b, T c) { return max(a, max(b, c)); } template <typename T> inline T Max(T a, T b, T c, T d) { return max(a, max(b, max(c, d))); } template <typename T> inline T Ceil(T a, T b) { return ((a % b == 0) ? (a / b) : (a / b + 1)); } template <typename T> inline T Floor(T a, T b) { return a / b; } template <typename T> inline T Power(T a, T p) { T res = 1, x = a; while (p) { if (p & 1) res = (res * x); x = (x * x); p >>= 1; } return res; } template <typename T> inline T BigMod(T a, T p, T M) { a %= M; T ret = 1; while (p) { if (p & 1) ret = (ret * a) % M; a = (a * a) % M; p = p >> 1; } return ret; } template <typename T> inline T InverseMod(T a, T M) { return BigMod(a, M - 2, M); } template <typename T> inline T gcd(T a, T b) { a = abs(a); b = abs(b); while (b) { a = a % b; swap(a, b); } return a; } template <typename T> inline T lcm(T x, T y) { return (((x) / gcd((x), (y))) * (y)); } #define endl '\n' #define eb emplace_back const int N = (int)4e5 + 10; const double EPS = 1e-7; const double PI = acos(-1.0); const ll LLINF = (ll)1e18 + 1; const int INF = (int)1e9 + 1; const ll MOD = (ll)1e9 + 7; template <typename T> bool cmp(const pair<T, T> &a, const pair<T, T> &b) { return a.first < b.first; } string ans; void print_result(string s, int i, int j, vector<vector<int>> dp_dir) { if (i == 0 || j == 0) return; if (dp_dir[i][j] == 3) { ans += s[i]; // cout << i << ' ' << j << endl; print_result(s, i - 1, j - 1, dp_dir); } else { if (dp_dir[i][j] == 2) { print_result(s, i, j - 1, dp_dir); } else { print_result(s, i - 1, j, dp_dir); } } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); auto Solve = [&]() { #ifdef __APPLE__ cout << "Running\n"; #endif string s, t; cin >> s >> t; int len_s = (int)s.length(), len_t = (int)t.length(); s = " " + s; t = " " + t; vector<vector<int>> dp(len_s + 1, vector<int>(len_t + 1, 0)); vector<vector<int>> dp_dir(len_s + 1, vector<int>(len_t + 1, 0)); for (int i = 1; i <= len_s; ++i) { for (int j = 1; j <= len_t; ++j) { if (s[i] == t[j]) { dp[i][j] = 1 + dp[i - 1][j - 1]; dp_dir[i][j] = 3; } else { if (dp[i - 1][j] >= dp[i][j - 1]) { dp[i][j] = dp[i - 1][j]; dp_dir[i][j] = 1; } else { dp[i][j] = dp[i][j - 1]; dp_dir[i][j] = 2; } } } } if (dp[len_s][len_t] == 0) { cout << endl; } else { print_result(s, len_s, len_t, dp_dir); reverse(ans.begin(), ans.end()); cout << ans << endl; } }; Solve(); return 0; }
/* Author : Simanta Deb Turja */ #include <bits/stdc++.h> using namespace std; #define Professor using ll = long long; using i64 = unsigned long long; template <typename T> inline T Min(T a, T b, T c) { return min(a, min(b, c)); } template <typename T> inline T Min(T a, T b, T c, T d) { return min(a, min(b, min(c, d))); } template <typename T> inline T Max(T a, T b, T c) { return max(a, max(b, c)); } template <typename T> inline T Max(T a, T b, T c, T d) { return max(a, max(b, max(c, d))); } template <typename T> inline T Ceil(T a, T b) { return ((a % b == 0) ? (a / b) : (a / b + 1)); } template <typename T> inline T Floor(T a, T b) { return a / b; } template <typename T> inline T Power(T a, T p) { T res = 1, x = a; while (p) { if (p & 1) res = (res * x); x = (x * x); p >>= 1; } return res; } template <typename T> inline T BigMod(T a, T p, T M) { a %= M; T ret = 1; while (p) { if (p & 1) ret = (ret * a) % M; a = (a * a) % M; p = p >> 1; } return ret; } template <typename T> inline T InverseMod(T a, T M) { return BigMod(a, M - 2, M); } template <typename T> inline T gcd(T a, T b) { a = abs(a); b = abs(b); while (b) { a = a % b; swap(a, b); } return a; } template <typename T> inline T lcm(T x, T y) { return (((x) / gcd((x), (y))) * (y)); } #define endl '\n' #define eb emplace_back const int N = (int)4e5 + 10; const double EPS = 1e-7; const double PI = acos(-1.0); const ll LLINF = (ll)1e18 + 1; const int INF = (int)1e9 + 1; const ll MOD = (ll)1e9 + 7; template <typename T> bool cmp(const pair<T, T> &a, const pair<T, T> &b) { return a.first < b.first; } string ans; void print_result(const string &s, int i, int j, const vector<vector<int>> &dp_dir) { if (i == 0 || j == 0) return; if (dp_dir[i][j] == 3) { ans += s[i]; // cout << i << ' ' << j << endl; print_result(s, i - 1, j - 1, dp_dir); } else { if (dp_dir[i][j] == 2) { print_result(s, i, j - 1, dp_dir); } else { print_result(s, i - 1, j, dp_dir); } } return; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); auto Solve = [&]() { #ifdef __APPLE__ cout << "Running\n"; #endif string s, t; cin >> s >> t; int len_s = (int)s.length(), len_t = (int)t.length(); s = " " + s; t = " " + t; vector<vector<int>> dp(len_s + 1, vector<int>(len_t + 1, 0)); vector<vector<int>> dp_dir(len_s + 1, vector<int>(len_t + 1, 0)); for (int i = 1; i <= len_s; ++i) { for (int j = 1; j <= len_t; ++j) { if (s[i] == t[j]) { dp[i][j] = 1 + dp[i - 1][j - 1]; dp_dir[i][j] = 3; } else { if (dp[i - 1][j] >= dp[i][j - 1]) { dp[i][j] = dp[i - 1][j]; dp_dir[i][j] = 1; } else { dp[i][j] = dp[i][j - 1]; dp_dir[i][j] = 2; } } } } if (dp[len_s][len_t] == 0) { cout << endl; } else { print_result(s, len_s, len_t, dp_dir); reverse(ans.begin(), ans.end()); cout << ans << endl; } }; Solve(); return 0; }
replace
77
78
77
79
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1010; string s1, s2; int n, m; int dp[N + 7][N + 7], dir[N + 7][N + 7]; int solve(int i, int j) { if (i >= n || j >= m) return 0; if (dp[i][j] != -1) return dp[i][j]; int ret1 = 0, ret2 = 0, ret3 = 0; if (s1[i] == s2[j]) { ret1 = solve(i + 1, j + 1) + 1; } else { ret2 = solve(i + 1, j); if (ret1 < ret2) { ret1 = ret2; } ret3 = solve(i, j + 1); if (ret1 < ret3) { ret1 = ret3; } } return dp[i][j] = ret1; } string s; void print(int i, int j) { if (i >= n || j >= m) return; int ret1 = 0, ret2 = 0, ret3 = 0; if (s1[i] == s2[j]) { s += s1[i]; print(i + 1, j + 1); } else { ret2 = solve(i + 1, j); ret3 = solve(i, j + 1); if (ret2 == dp[i][j] && ret3 == dp[i][j]) { if (s1[i] < s2[j]) { print(i + 1, j); } else { print(i, j + 1); } } else if (ret2 == dp[i][j]) { print(i + 1, j); } else if (ret3 == dp[i][j]) { print(i, j + 1); } } } int main() { cin >> s1 >> s2; n = (int)s1.length(), m = (int)s2.length(); memset(dp, -1, sizeof(dp)); solve(0, 0); print(0, 0); cout << s << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 3010; string s1, s2; int n, m; int dp[N + 7][N + 7], dir[N + 7][N + 7]; int solve(int i, int j) { if (i >= n || j >= m) return 0; if (dp[i][j] != -1) return dp[i][j]; int ret1 = 0, ret2 = 0, ret3 = 0; if (s1[i] == s2[j]) { ret1 = solve(i + 1, j + 1) + 1; } else { ret2 = solve(i + 1, j); if (ret1 < ret2) { ret1 = ret2; } ret3 = solve(i, j + 1); if (ret1 < ret3) { ret1 = ret3; } } return dp[i][j] = ret1; } string s; void print(int i, int j) { if (i >= n || j >= m) return; int ret1 = 0, ret2 = 0, ret3 = 0; if (s1[i] == s2[j]) { s += s1[i]; print(i + 1, j + 1); } else { ret2 = solve(i + 1, j); ret3 = solve(i, j + 1); if (ret2 == dp[i][j] && ret3 == dp[i][j]) { if (s1[i] < s2[j]) { print(i + 1, j); } else { print(i, j + 1); } } else if (ret2 == dp[i][j]) { print(i + 1, j); } else if (ret3 == dp[i][j]) { print(i, j + 1); } } } int main() { cin >> s1 >> s2; n = (int)s1.length(), m = (int)s2.length(); memset(dp, -1, sizeof(dp)); solve(0, 0); print(0, 0); cout << s << endl; return 0; }
replace
2
3
2
3
0
p03165
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stdlib.h> #include <string> #include <vector> using namespace std; #define ll long long vector<vector<int>> vv; string a, b; void ans(int i, int j) { if (vv[i][j] == vv[i - 1][j]) { ans(i - 1, j); return; } if (vv[i][j] == vv[i][j - 1]) { ans(i, j - 1); return; } if (vv[i - 1][j - 1]) ans(i - 1, j - 1); cout << a[i - 1]; return; } signed main() { cin >> a >> b; vv.resize(a.size() + 1, (vector<int>(b.size() + 1))); for (int i = 0; i < a.size(); i++) vv[i][0]; for (int i = 0; i < b.size(); i++) vv[0][i]; for (int i = 1; i <= a.size(); i++) for (int j = 1; j <= b.size(); j++) { if (a[i - 1] == b[j - 1]) vv[i][j] = vv[i - 1][j - 1] + 1; else vv[i][j] = max(vv[i - 1][j], vv[i][j - 1]); } ans(int(a.size()), int(b.size())); cin >> a; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stdlib.h> #include <string> #include <vector> using namespace std; #define ll long long vector<vector<int>> vv; string a, b; void ans(int i, int j) { if (!vv[i][j]) return; if (vv[i][j] == vv[i - 1][j]) { ans(i - 1, j); return; } if (vv[i][j] == vv[i][j - 1]) { ans(i, j - 1); return; } if (vv[i - 1][j - 1]) ans(i - 1, j - 1); cout << a[i - 1]; return; } signed main() { cin >> a >> b; vv.resize(a.size() + 1, (vector<int>(b.size() + 1))); for (int i = 0; i < a.size(); i++) vv[i][0]; for (int i = 0; i < b.size(); i++) vv[0][i]; for (int i = 1; i <= a.size(); i++) for (int j = 1; j <= b.size(); j++) { if (a[i - 1] == b[j - 1]) vv[i][j] = vv[i - 1][j - 1] + 1; else vv[i][j] = max(vv[i - 1][j], vv[i][j - 1]); } ans(int(a.size()), int(b.size())); cin >> a; }
insert
25
25
25
27
0
p03165
C++
Runtime Error
// #pragma GCC optimize("03") #include <bits/stdc++.h> #define ll long long #define fi first #define se second #define mod 1000000007 using namespace std; int n, m; int dp[3010][3010]; string a, b; vector<char> sol; ll getsum(int l, int r) { return dp[r] - dp[l - 1]; } int main() { // ifstream cin("tst.in"); // ofstream cout("tst.out"); ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> a >> b; n = a.size(); m = b.size(); a = '+' + a; b = '+' + b; 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]); while (n > 0 && m > 0) { while (dp[n - 1][m] == dp[n][m]) n--; while (dp[n][m - 1] == dp[n][m]) m--; sol.push_back(a[n]); n--; m--; } reverse(sol.begin(), sol.end()); for (auto it : sol) cout << it; return 0; }
// #pragma GCC optimize("03") #include <bits/stdc++.h> #define ll long long #define fi first #define se second #define mod 1000000007 using namespace std; int n, m; int dp[3010][3010]; string a, b; vector<char> sol; ll getsum(int l, int r) { return dp[r] - dp[l - 1]; } int main() { // ifstream cin("tst.in"); // ofstream cout("tst.out"); ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> a >> b; n = a.size(); m = b.size(); a = '+' + a; b = '+' + b; 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]); while (n > 0 && m > 0 && dp[n][m] > 0) { while (dp[n - 1][m] == dp[n][m]) n--; while (dp[n][m - 1] == dp[n][m]) m--; sol.push_back(a[n]); n--; m--; } reverse(sol.begin(), sol.end()); for (auto it : sol) cout << it; return 0; }
replace
36
37
36
37
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); typedef long long int ll; using namespace std; const int N = 3e3 + 5; #define fi first #define se second #define pb push_back #define endl "\n" ll ans[N][N]; int main() { string s, t; cin >> s >> t; ll n = s.length(), m = t.length(); for (ll i = 1; i <= n; ++i) { for (ll j = 1; j <= m; ++j) { if (s[i - 1] == t[j - 1]) { ans[i][j] = ans[i - 1][j - 1] + 1; } else ans[i][j] = max(ans[i][j - 1], ans[i - 1][j]); } } string res = ""; ll i = n, j = m; while (i || j) { if (ans[i][j] != ans[i][j - 1] && ans[i][j] != ans[i - 1][j]) { res += t[j - 1]; --i; --j; } else if (ans[i][j] == ans[i][j - 1]) { // res+=t[j-1]; --j; } else if (ans[i][j] == ans[i - 1][j]) { --i; } } ll u = res.size(); for (ll i = u - 1; i >= 0; --i) cout << res[i]; /* for(ll i=0;i<=n;++i) { for(ll j=0;j<=m;++j) { cout<<ans[i][j]<<" "; } cout<<endl; }*/ }
#include <bits/stdc++.h> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); typedef long long int ll; using namespace std; const int N = 3e3 + 5; #define fi first #define se second #define pb push_back #define endl "\n" ll ans[N][N]; int main() { string s, t; cin >> s >> t; ll n = s.length(), m = t.length(); for (ll i = 1; i <= n; ++i) { for (ll j = 1; j <= m; ++j) { if (s[i - 1] == t[j - 1]) { ans[i][j] = ans[i - 1][j - 1] + 1; } else ans[i][j] = max(ans[i][j - 1], ans[i - 1][j]); } } string res = ""; ll i = n, j = m; while (i && j) { if (ans[i][j] != ans[i][j - 1] && ans[i][j] != ans[i - 1][j]) { res += t[j - 1]; --i; --j; } else if (ans[i][j] == ans[i][j - 1]) { // res+=t[j-1]; --j; } else if (ans[i][j] == ans[i - 1][j]) { --i; } } ll u = res.size(); for (ll i = u - 1; i >= 0; --i) cout << res[i]; /* for(ll i=0;i<=n;++i) { for(ll j=0;j<=m;++j) { cout<<ans[i][j]<<" "; } cout<<endl; }*/ }
replace
32
33
32
33
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<ll, ll> pll; typedef vector<bool> vb; const ll oo = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-9; #define sz(c) ll((c).size()) #define all(c) begin(c), end(c) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--) #define mp make_pair #define pb push_back #define eb emplace_back #define xx first #define yy second #define TR(X) \ ({ \ if (1) \ cerr << "TR: " << (#X) << " = " << (X) << endl; \ }) string a, b; const ll N = 3010; ll dp[N][N]; ll rec(ll i, ll j) { if (i == sz(a) || j == sz(b)) return 0; if (dp[i][j] != -1) return dp[i][j]; if (a[i] == b[j]) return dp[i][j] = 1 + rec(i + 1, j + 1); return dp[i][j] = max(rec(i + 1, j), rec(i, j + 1)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> a >> b; memset(dp, -1, sizeof dp); ll i = 0, j = 0; while (i < sz(a) || j < sz(b)) { if (a[i] == b[j]) { cout << a[i]; i++, j++; } else { if (rec(i, j) == rec(i + 1, j)) i++; else j++; } } cout << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<ll, ll> pll; typedef vector<bool> vb; const ll oo = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-9; #define sz(c) ll((c).size()) #define all(c) begin(c), end(c) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORD(i, a, b) for (ll i = (b)-1; i >= (a); i--) #define mp make_pair #define pb push_back #define eb emplace_back #define xx first #define yy second #define TR(X) \ ({ \ if (1) \ cerr << "TR: " << (#X) << " = " << (X) << endl; \ }) string a, b; const ll N = 3010; ll dp[N][N]; ll rec(ll i, ll j) { if (i == sz(a) || j == sz(b)) return 0; if (dp[i][j] != -1) return dp[i][j]; if (a[i] == b[j]) return dp[i][j] = 1 + rec(i + 1, j + 1); return dp[i][j] = max(rec(i + 1, j), rec(i, j + 1)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> a >> b; memset(dp, -1, sizeof dp); ll i = 0, j = 0; while (i < sz(a) && j < sz(b)) { if (a[i] == b[j]) { cout << a[i]; i++, j++; } else { if (rec(i, j) == rec(i + 1, j)) i++; else j++; } } cout << endl; }
replace
48
49
48
49
0
p03165
C++
Runtime Error
#include <algorithm> #include <iostream> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { string s, t; cin >> s >> t; int s_len = s.length(), t_len = t.length(); int dp[305][305]; for (int i = 0; i <= s_len; i++) { for (int j = 0; j <= t_len; j++) { dp[i][j] = 0; } } for (int i = 0; i <= s_len; i++) { for (int j = 0; j <= t_len; j++) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]); if (i < s_len && j < t_len && s[i] == t[j]) { dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j + 1]); } } } #ifdef __DEBUG__ for (int i = 0; i <= s_len; i++) { for (int j = 0; j <= t_len; j++) { cout << dp[i][j] << " "; } cout << endl; } #endif string ret; ret.reserve(min(s_len, t_len)); int i = s_len, j = t_len; 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 { ret.push_back(s[i - 1]); i--; j--; } } reverse(ret.begin(), ret.end()); cout << ret << endl; }
#include <algorithm> #include <iostream> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; int main() { string s, t; cin >> s >> t; int s_len = s.length(), t_len = t.length(); int dp[3005][3005]; for (int i = 0; i <= s_len; i++) { for (int j = 0; j <= t_len; j++) { dp[i][j] = 0; } } for (int i = 0; i <= s_len; i++) { for (int j = 0; j <= t_len; j++) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]); if (i < s_len && j < t_len && s[i] == t[j]) { dp[i + 1][j + 1] = max(dp[i][j] + 1, dp[i + 1][j + 1]); } } } #ifdef __DEBUG__ for (int i = 0; i <= s_len; i++) { for (int j = 0; j <= t_len; j++) { cout << dp[i][j] << " "; } cout << endl; } #endif string ret; ret.reserve(min(s_len, t_len)); int i = s_len, j = t_len; 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 { ret.push_back(s[i - 1]); i--; j--; } } reverse(ret.begin(), ret.end()); cout << ret << endl; }
replace
13
14
13
14
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <string> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; // #define ordered_set tree<int, null_type,less<int>, // rb_tree_tag,tree_order_statistics_node_update> #define ordered_multiset // tree<int, null_type,less_equal<int>, // rb_tree_tag,tree_order_statistics_node_update> #define int long long #define jai_shree_ram \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define rep(i, a, b, d) for (int i = a; i <= b; i += d) #define brep(i, a, b, d) for (int i = a; i >= b; i -= d) #define pb push_back #define all(x) x.begin(), x.end() #define pii pair<int, int> #define endl '\n' const int MAX = 1e5 + 5; const int MOD = 1e9 + 7; //////////////////////////////// pair<int, pii> dp[3005][3005]; int32_t main() { jai_shree_ram string s, t; cin >> s >> t; int l1 = s.length(), l2 = t.length(); rep(i, 1, l1, 1) { rep(j, 1, l2, 1) { if (dp[i - 1][j].first >= dp[i][j - 1].first) { dp[i][j] = dp[i - 1][j]; } else dp[i][j] = dp[i][j - 1]; if (s[i - 1] == t[j - 1]) { dp[i][j] = {dp[i - 1][j - 1].first + 1, {i, j}}; } } } string ans = ""; int i = l1, j = l2; while (1) { int i1 = (dp[i][j].second).first; int j1 = (dp[i][j].second).second; // cout << i1 << " " << j1 << endl; ans += s[i1 - 1]; if (i1 == 1) break; i = i1 - 1; j = j1 - 1; } reverse(all(ans)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include <string> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; // #define ordered_set tree<int, null_type,less<int>, // rb_tree_tag,tree_order_statistics_node_update> #define ordered_multiset // tree<int, null_type,less_equal<int>, // rb_tree_tag,tree_order_statistics_node_update> #define int long long #define jai_shree_ram \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define rep(i, a, b, d) for (int i = a; i <= b; i += d) #define brep(i, a, b, d) for (int i = a; i >= b; i -= d) #define pb push_back #define all(x) x.begin(), x.end() #define pii pair<int, int> #define endl '\n' const int MAX = 1e5 + 5; const int MOD = 1e9 + 7; //////////////////////////////// pair<int, pii> dp[3005][3005]; int32_t main() { jai_shree_ram string s, t; cin >> s >> t; int l1 = s.length(), l2 = t.length(); rep(i, 1, l1, 1) { rep(j, 1, l2, 1) { if (dp[i - 1][j].first >= dp[i][j - 1].first) { dp[i][j] = dp[i - 1][j]; } else dp[i][j] = dp[i][j - 1]; if (s[i - 1] == t[j - 1]) { dp[i][j] = {dp[i - 1][j - 1].first + 1, {i, j}}; } } } string ans = ""; int i = l1, j = l2; while (1) { int i1 = (dp[i][j].second).first; int j1 = (dp[i][j].second).second; if (i1 == 0) break; ans += s[i1 - 1]; if (i1 == 1) break; i = i1 - 1; j = j1 - 1; } reverse(all(ans)); cout << ans << endl; return 0; }
replace
49
50
49
51
-11
p03165
C++
Runtime Error
#include "bits/stdc++.h" #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define double long double #define int long long #define pb push_back #define Z(c) (int)(c).size() #define L(c) c[Z(c) - 1] #define F first #define S second #define nl "\n" #define mii map<int, int> #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define rep(i, iv, n) for (int(i) = (iv); (i) < (n); ++(i)) #define FO fflush(stdout) #define D1(x) cerr << "At line " << __LINE__ << " " << #x << "=" << x << nl #define D2(x, y) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << nl #define D3(x, y, z) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << " " << #z << "=" << z << nl #define tr(it, ct) for (auto &(it) : (ct)) #define pi acos(-1) #define prt(ct) \ for (auto &(it) : (ct)) \ cerr << it << " "; \ cerr << nl #define prtp(ct) \ for (auto &(it) : (ct)) \ cerr << it.F << " " << it.S << nl; \ cerr << nl typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef vector<pii> vp; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> baap; void swap(char &a, char &b) { auto tm = a; a = b; b = tm; } void mina(int &a, int b) { a = (a < b ? a : b); } void maxa(int &a, int b) { a = (a > b ? a : b); } const int mod = 1000000007; const int mod2 = 998244353; const int INF = 1e16 + 18; const int N = 1e5 + 15; const int M = 1e3 + 13; int dp[M][M]; char ch[M][M]; string ans; void get_ans(int x, int y) { if (x == 0 || y == 0) return; if (ch[x][y] != '#') { ans += ch[x][y]; get_ans(x - 1, y - 1); } else { if (dp[x - 1][y] > dp[x][y - 1]) get_ans(x - 1, y); else get_ans(x, y - 1); } } void fft() { string s, t; cin >> s >> t; int n = Z(s), m = Z(t); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; ch[i][j] = s[i - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); ch[i][j] = '#'; } } } get_ans(n, m); reverse(all(ans)); cout << ans; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; // cin>>tc; rep(i, 1, tc + 1) { // cout<<"Case #"<<i<<": "; fft(); if (i < tc) cout << nl; } return 0; }
#include "bits/stdc++.h" #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define double long double #define int long long #define pb push_back #define Z(c) (int)(c).size() #define L(c) c[Z(c) - 1] #define F first #define S second #define nl "\n" #define mii map<int, int> #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define rep(i, iv, n) for (int(i) = (iv); (i) < (n); ++(i)) #define FO fflush(stdout) #define D1(x) cerr << "At line " << __LINE__ << " " << #x << "=" << x << nl #define D2(x, y) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << nl #define D3(x, y, z) \ cerr << "At line " << __LINE__ << " " << #x << "=" << x << " " << #y << "=" \ << y << " " << #z << "=" << z << nl #define tr(it, ct) for (auto &(it) : (ct)) #define pi acos(-1) #define prt(ct) \ for (auto &(it) : (ct)) \ cerr << it << " "; \ cerr << nl #define prtp(ct) \ for (auto &(it) : (ct)) \ cerr << it.F << " " << it.S << nl; \ cerr << nl typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; typedef vector<pii> vp; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> baap; void swap(char &a, char &b) { auto tm = a; a = b; b = tm; } void mina(int &a, int b) { a = (a < b ? a : b); } void maxa(int &a, int b) { a = (a > b ? a : b); } const int mod = 1000000007; const int mod2 = 998244353; const int INF = 1e16 + 18; const int N = 1e5 + 15; const int M = 3e3 + 13; int dp[M][M]; char ch[M][M]; string ans; void get_ans(int x, int y) { if (x == 0 || y == 0) return; if (ch[x][y] != '#') { ans += ch[x][y]; get_ans(x - 1, y - 1); } else { if (dp[x - 1][y] > dp[x][y - 1]) get_ans(x - 1, y); else get_ans(x, y - 1); } } void fft() { string s, t; cin >> s >> t; int n = Z(s), m = Z(t); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; ch[i][j] = s[i - 1]; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); ch[i][j] = '#'; } } } get_ans(n, m); reverse(all(ans)); cout << ans; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; // cin>>tc; rep(i, 1, tc + 1) { // cout<<"Case #"<<i<<": "; fft(); if (i < tc) cout << nl; } return 0; }
replace
58
59
58
59
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; #define FOR(i, a, b) for (ll i = (a); i < (b); (i)++) #define DBG 1 #define TR(X) \ ({ \ if (DBG) \ cerr << "TR " << (#X) << " = " << (X) << endl; \ }) #define TRM(i, j, c) \ ({ \ if (DBG) \ cerr << "TR (" << (i) << ", " << (j) << ") = " << (c)[i][j] << endl; \ }) int main(void) { cin.sync_with_stdio(false); cin.tie(0); string s, t; cin >> s >> t; vvl dp(s.size() + 1, vl(t.size() + 1, 0)); FOR(i, 1, s.size() + 1) FOR(j, 1, t.size() + 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); } TRM(i, j, dp); } ll idx_i = s.size(), idx_j = t.size(); vector<char> res(dp[s.size()][t.size()]); while (idx_i >= 1 && idx_j >= 1) { ll left = dp[idx_i - 1][idx_j]; ll up = dp[idx_i][idx_j - 1]; ll lup = dp[idx_i - 1][idx_j - 1]; TRM(idx_i, idx_j, dp); TR(dp[idx_i - 1][idx_j]); TR(dp[idx_i][idx_j - 1]); if (s[idx_i - 1] == t[idx_j - 1] && lup <= up && lup <= left) { res[dp[idx_i][idx_j] - 1] = s[idx_i - 1]; TR(res[dp[idx_i][idx_j] - 1]); idx_i--; idx_j--; } else if (up == left) { if (idx_i < idx_j) { idx_j--; } else { idx_i--; } } else if (up > left) { if (idx_j == 1) { idx_i--; } else idx_j--; } else { if (idx_i == 1) { idx_j--; } else idx_i--; } } for (const auto &it : res) { cout << it; } cout << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; #define FOR(i, a, b) for (ll i = (a); i < (b); (i)++) #define DBG 0 #define TR(X) \ ({ \ if (DBG) \ cerr << "TR " << (#X) << " = " << (X) << endl; \ }) #define TRM(i, j, c) \ ({ \ if (DBG) \ cerr << "TR (" << (i) << ", " << (j) << ") = " << (c)[i][j] << endl; \ }) int main(void) { cin.sync_with_stdio(false); cin.tie(0); string s, t; cin >> s >> t; vvl dp(s.size() + 1, vl(t.size() + 1, 0)); FOR(i, 1, s.size() + 1) FOR(j, 1, t.size() + 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); } TRM(i, j, dp); } ll idx_i = s.size(), idx_j = t.size(); vector<char> res(dp[s.size()][t.size()]); while (idx_i >= 1 && idx_j >= 1) { ll left = dp[idx_i - 1][idx_j]; ll up = dp[idx_i][idx_j - 1]; ll lup = dp[idx_i - 1][idx_j - 1]; TRM(idx_i, idx_j, dp); TR(dp[idx_i - 1][idx_j]); TR(dp[idx_i][idx_j - 1]); if (s[idx_i - 1] == t[idx_j - 1] && lup <= up && lup <= left) { res[dp[idx_i][idx_j] - 1] = s[idx_i - 1]; TR(res[dp[idx_i][idx_j] - 1]); idx_i--; idx_j--; } else if (up == left) { if (idx_i < idx_j) { idx_j--; } else { idx_i--; } } else if (up > left) { if (idx_j == 1) { idx_i--; } else idx_j--; } else { if (idx_i == 1) { idx_j--; } else idx_i--; } } for (const auto &it : res) { cout << it; } cout << endl; }
replace
10
11
10
11
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 3 * 1e3 + 10; int dp[N][N], al, bl, r, c; string a, b; vector<char> ans; int main() { cin >> a >> b; al = a.length(); bl = b.length(); for (int i = 1; i <= al; i++) { for (int j = 1; j <= bl; j++) { if (a[i] == b[j]) dp[i][j] = max(dp[i - 1][j - 1] + 1, max(dp[i - 1][j], dp[i][j - 1])); else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int f = dp[al][bl]; r = al; c = bl; while (f != 0) { if (a[r - 1] == b[c - 1]) { ans.push_back(a[r - 1]); r--; c--; f--; } else { if (dp[r - 1][c] == dp[r][c - 1]) { if (r - 1 != 0) r--; else c--; } else if (dp[r - 1][c] > dp[r][c - 1]) r--; else c--; } } // cout<<dp[al][bl]<<"\n"; for (auto x = ans.crbegin(); x != ans.crend(); x++) cout << *x; }
#include <bits/stdc++.h> using namespace std; const int N = 3 * 1e3 + 10; int dp[N][N], al, bl, r, c; string a, b; vector<char> ans; int main() { cin >> a >> b; al = a.length(); bl = b.length(); for (int i = 1; i <= al; i++) { for (int j = 1; j <= bl; j++) { if (a[i - 1] == b[j - 1]) dp[i][j] = max(dp[i - 1][j - 1] + 1, max(dp[i - 1][j], dp[i][j - 1])); else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int f = dp[al][bl]; r = al; c = bl; while (f != 0) { if (a[r - 1] == b[c - 1]) { ans.push_back(a[r - 1]); r--; c--; f--; } else { if (dp[r - 1][c] == dp[r][c - 1]) { if (r - 1 != 0) r--; else c--; } else if (dp[r - 1][c] > dp[r][c - 1]) r--; else c--; } } // cout<<dp[al][bl]<<"\n"; for (auto x = ans.crbegin(); x != ans.crend(); x++) cout << *x; }
replace
16
17
16
17
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define ln "\n" #define pb push_back #define pll pair<ll, ll> #define ppll pair<ll, pll> #define vll vector<ll> #define vpll vector<pll> #define vvll vector<vector<ll>> #define sll stack<ll> #define qll queue<ll> #define mp make_pair #define f first #define s second #define bs binary_search #define lb lower_bound #define ub upper_bound #define Test \ ll t; \ cin >> t; \ while (t--) #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define init(x, n, v) \ for (ll i = 0; i <= n; i++) \ x[i] = v; #define all(x) x.begin(), x.end() ll MOD = 1e9 + 7, MAX = 1e18; int main() { fast_io; string a, b, c = ""; ll n, m, i, j; cin >> a >> b; n = a.size(); m = b.size(); ll dp[n + 1][m + 1]; for (i = 0; i <= n; i++) { for (j = 0; j <= m; j++) { if (!i || !j) 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][j - 1], dp[i - 1][j]); } } for (i = n; i > 0;) { for (j = m; j > 0;) { if (a[i - 1] == b[j - 1]) c.pb(a[i - 1]), i--, j--; else { if (dp[i][j] == dp[i - 1][j]) i--; else j--; } } } reverse(all(c)); cout << c; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ln "\n" #define pb push_back #define pll pair<ll, ll> #define ppll pair<ll, pll> #define vll vector<ll> #define vpll vector<pll> #define vvll vector<vector<ll>> #define sll stack<ll> #define qll queue<ll> #define mp make_pair #define f first #define s second #define bs binary_search #define lb lower_bound #define ub upper_bound #define Test \ ll t; \ cin >> t; \ while (t--) #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); #define init(x, n, v) \ for (ll i = 0; i <= n; i++) \ x[i] = v; #define all(x) x.begin(), x.end() ll MOD = 1e9 + 7, MAX = 1e18; int main() { fast_io; string a, b, c = ""; ll n, m, i, j; cin >> a >> b; n = a.size(); m = b.size(); ll dp[n + 1][m + 1]; for (i = 0; i <= n; i++) { for (j = 0; j <= m; j++) { if (!i || !j) 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][j - 1], dp[i - 1][j]); } } i = n, j = m; while (i && j) { if (a[i - 1] == b[j - 1]) c.pb(a[i - 1]), i--, j--; else { if (dp[i][j] == dp[i - 1][j]) i--; else j--; } } reverse(all(c)); cout << c; return 0; }
replace
48
58
48
57
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; string ch, ch1; int n, m, dp[4000][4000]; int lcs(int i, int j) { if (i == n || j == m) return 0; int x, y; if (ch[i] == ch1[j]) return dp[i][j] = 1 + lcs(i + 1, j + 1); x = lcs(i + 1, j); y = lcs(i, j + 1); return dp[i][j] = max(x, y); } int main() { cin >> ch >> ch1; memset(dp, -1, sizeof(dp)); n = ch.size(); m = ch1.size(); int l = lcs(0, 0); int i = 0, j = 0; while (i < n && j < m) { if (ch[i] == ch1[j]) { printf("%c", ch[i]); i++; j++; } else if (dp[i + 1][j] >= dp[i][j + 1]) i++; else j++; } // cout<<l<<endl ; }
#include <bits/stdc++.h> using namespace std; string ch, ch1; int n, m, dp[4000][4000]; int lcs(int i, int j) { if (i == n || j == m) return 0; if (dp[i][j] != -1) return dp[i][j]; int x, y; if (ch[i] == ch1[j]) return dp[i][j] = 1 + lcs(i + 1, j + 1); x = lcs(i + 1, j); y = lcs(i, j + 1); return dp[i][j] = max(x, y); } int main() { cin >> ch >> ch1; memset(dp, -1, sizeof(dp)); n = ch.size(); m = ch1.size(); int l = lcs(0, 0); int i = 0, j = 0; while (i < n && j < m) { if (ch[i] == ch1[j]) { printf("%c", ch[i]); i++; j++; } else if (dp[i + 1][j] >= dp[i][j + 1]) i++; else j++; } // cout<<l<<endl ; }
insert
7
7
7
9
TLE
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fi first #define se second const int N = 3030; const long long mod = 1e9 + 9; using namespace std; string s, t; int d[N][N]; int p[N][N]; int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); 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]) { d[i][j] = d[i - 1][j - 1] + 1; p[i][j] = 3; } else if (d[i - 1][j] > d[i][j - 1]) { d[i][j] = d[i - 1][j]; p[i][j] = 1; } else { d[i][j] = d[i][j - 1]; p[i][j] = 2; } } } string res = ""; int n = s.size(), m = t.size(); while (n || m) { if (p[n][m] == 3) { res += s[n - 1]; } int dif = p[n][m]; if (dif & 1) { n -= 1; } if (dif & 2) { m -= 1; } } reverse(res.begin(), res.end()); cout << res << "\n"; }
#include <bits/stdc++.h> #define fi first #define se second const int N = 3030; const long long mod = 1e9 + 9; using namespace std; string s, t; int d[N][N]; int p[N][N]; int main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(0); 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]) { d[i][j] = d[i - 1][j - 1] + 1; p[i][j] = 3; } else if (d[i - 1][j] > d[i][j - 1]) { d[i][j] = d[i - 1][j]; p[i][j] = 1; } else { d[i][j] = d[i][j - 1]; p[i][j] = 2; } } } string res = ""; int n = s.size(), m = t.size(); while (n && m) { if (p[n][m] == 3) { res += s[n - 1]; } int dif = p[n][m]; if (dif & 1) { n -= 1; } if (dif & 2) { m -= 1; } } reverse(res.begin(), res.end()); cout << res << "\n"; }
replace
36
37
36
37
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define F(n) \ for (int i = 0; i < n; i++) \ ; #define FI(n) \ for (int i = 0; i <= n; i++) \ ; #define FS(a, n) \ for (int i = a; i < n; i++) \ ; #define vi vector<int>; #define vl vector<long int>; #define vll vector<long long>; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string a; string b; cin >> a >> b; int n = a.size(); int m = b.size(); int dp[n + 1][m + 1]; memset(dp, 0, sizeof(dp)); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (a[i - 1] == b[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } // for(int i = 0; i < n + 1; i++) { // for(int j = 0; j < m + 1; j++) { // cout << dp[i][j] << " "; // } // cout << endl; // } int i = n; int j = m; string ans = ""; while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { ans = ans + a[i - 1]; i--; j--; } else if (dp[i][j - 1] > dp[i - 1][j]) { j--; } else { i--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define F(n) \ for (int i = 0; i < n; i++) \ ; #define FI(n) \ for (int i = 0; i <= n; i++) \ ; #define FS(a, n) \ for (int i = a; i < n; i++) \ ; #define vi vector<int>; #define vl vector<long int>; #define vll vector<long long>; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string a; string b; cin >> a >> b; int n = a.size(); int m = b.size(); int dp[n + 1][m + 1]; memset(dp, 0, sizeof(dp)); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else if (a[i - 1] == b[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } // for(int i = 0; i < n + 1; i++) { // for(int j = 0; j < m + 1; j++) { // cout << dp[i][j] << " "; // } // cout << endl; // } int i = n; int j = m; string ans = ""; while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { ans = ans + a[i - 1]; i--; j--; } else if (dp[i][j - 1] > dp[i - 1][j]) { j--; } else { i--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
delete
18
23
18
18
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define vi vector<int> #define all(x) (x).begin(), (x).end() #define F first #define S second #define pb push_back #define pp pair<int, int> #define rep(i, l, r) for (int i = l; i < r; i++) int main() { #ifndef ONLINE_JUDGE freopen("input.txt.txt", "r", stdin); freopen("output.txt.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); string s, t; cin >> s >> t; int m = s.size(); int n = t.size(); int dp[m][n]; pp par[m][n]; rep(i, 0, m) { rep(j, 0, n) { dp[i][j] = 0; if (s[i] == t[j]) { if (i > 0 and j > 0) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = 1; } else { if (i > 0) dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (j > 0) dp[i][j] = max(dp[i][j], dp[i][j - 1]); } } } rep(i, 0, m) { rep(j, 0, n) { par[i][j] = {-1, -1}; if (s[i] == t[j]) { if (i > 0 and j > 0 and dp[i][j] == 1 + dp[i - 1][j - 1]) par[i][j] = {i - 1, j - 1}; else par[i][j] = {-1, -1}; } else { if (i > 0 and dp[i][j] == dp[i - 1][j]) par[i][j] = {i - 1, j}; if (j > 0 and dp[i][j] == dp[i][j - 1]) par[i][j] = {i, j - 1}; } } } int i = m - 1; int j = n - 1; string ans; while (pp(i, j) != pp(-1, -1)) { if (s[i] == t[j]) ans.pb(s[i]); int t = par[i][j].first; j = par[i][j].second; i = t; } reverse(all(ans)); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define vi vector<int> #define all(x) (x).begin(), (x).end() #define F first #define S second #define pb push_back #define pp pair<int, int> #define rep(i, l, r) for (int i = l; i < r; i++) int main() { // #ifndef ONLINE_JUDGE // freopen("input.txt.txt","r",stdin); // freopen("output.txt.txt","w",stdout); // #endif ios::sync_with_stdio(false); cin.tie(0); string s, t; cin >> s >> t; int m = s.size(); int n = t.size(); int dp[m][n]; pp par[m][n]; rep(i, 0, m) { rep(j, 0, n) { dp[i][j] = 0; if (s[i] == t[j]) { if (i > 0 and j > 0) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = 1; } else { if (i > 0) dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (j > 0) dp[i][j] = max(dp[i][j], dp[i][j - 1]); } } } rep(i, 0, m) { rep(j, 0, n) { par[i][j] = {-1, -1}; if (s[i] == t[j]) { if (i > 0 and j > 0 and dp[i][j] == 1 + dp[i - 1][j - 1]) par[i][j] = {i - 1, j - 1}; else par[i][j] = {-1, -1}; } else { if (i > 0 and dp[i][j] == dp[i - 1][j]) par[i][j] = {i - 1, j}; if (j > 0 and dp[i][j] == dp[i][j - 1]) par[i][j] = {i, j - 1}; } } } int i = m - 1; int j = n - 1; string ans; while (pp(i, j) != pp(-1, -1)) { if (s[i] == t[j]) ans.pb(s[i]); int t = par[i][j].first; j = par[i][j].second; i = t; } reverse(all(ans)); cout << ans << endl; }
replace
10
14
10
14
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int dp[3010][3010]; int main() { int dp[1010][1010]; string S, T; cin >> S; cin >> T; memset(dp, 0, sizeof(dp)); 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; dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]); } } string str = ""; int i = S.size(); int j = T.size(); while (0 < i && 0 < j) { int x = dp[i][j]; if (dp[i - 1][j] == x) { i--; } else if (dp[i][j - 1] == x) { j--; } else { str = S[i - 1] + str; i--; j--; } } cout << str << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[3010][3010]; int main() { string S, T; cin >> S; cin >> T; memset(dp, 0, sizeof(dp)); 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; dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]); } } string str = ""; int i = S.size(); int j = T.size(); while (0 < i && 0 < j) { int x = dp[i][j]; if (dp[i - 1][j] == x) { i--; } else if (dp[i][j - 1] == x) { j--; } else { str = S[i - 1] + str; i--; j--; } } cout << str << endl; return 0; }
delete
5
6
5
5
0
p03165
C++
Runtime Error
#include "bits/stdc++.h" using ll = long long; using namespace std; // 0 -> a-1 #define rep(i, a) for (int i = 0; (i) < (int)(a); (i)++) // a -> b-1 #define reps(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++) // a-1 -> 0 #define rrep(i, a) for (int i = (int)a - 1; (i) >= 0; (i)--) // a-1 -> b #define rreps(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); (i)--) #define MP(a, b) make_pair((a), (b)) #define PB(a) push_back((a)) #define all(v) (v).begin(), (v).end() // next_permutation(all(v)) #define PERM(v) next_permutation(all(v)) /*sort(all(v)); (v).erase(unique(all(v)), v.end())*/ #define UNIQUE(v) \ sort(all(v)); \ (v).erase(unique(all(v)), v.end()) #define CIN(type, x) \ type x; \ cin >> x #define YES(f) \ if ((f)) { \ cout << "YES" << endl; \ } else { \ cout << "NO" << endl; \ } #define Yes(f) \ if ((f)) { \ cout << "Yes" << endl; \ } else { \ cout << "No" << endl; \ } #define MINV(v) min_element(all(v)) #define MAXV(v) max_element(all(v)) #define MIN3(a, b, c) min(min(a, b), c) #define MIN4(a, b, c, d) min(MIN3(a, b, c), d) #define MIN5(a, b, c, d, e) min(MIN4(a, b, c, d), e) #define MIN6(a, b, c, d, e, f) min(MIN5(a, b, c, d, e), f) #define MAX3(a, b, c) max(max(a, b), c) #define MAX4(a, b, c, d) max(MAX3(a, b, c), d) #define MAX5(a, b, c, d, e) max(MAX4(a, b, c, d), e) #define MAX6(a, b, c, d, e, f) max(MAX5(a, b, c, d, e), f) // b is [a, c) #define RANGE(a, b, c) ((a) <= (b) && (b) < (c)) // c is [a, e) && d is [b, f) #define RANGE2D(a, b, c, d, e, f) (RANGE((a), (c), (e)) && RANGE((b), (d), (f))) #define chmin(a, b) a = min(a, (b)) #define chmin3(a, b, c) a = MIN3(a, (b), (c)) #define chmin4(a, b, c, d) a = MIN4(a, (b), (c), (d)) #define chmin5(a, b, c, d, e) a = MIN5(a, (b), (c), (d), (e)) #define chmin6(a, b, c, d, e, f) a = MIN6(a, (b), (c), (d), (e), (f)) #define chmax(a, b) a = max(a, (b)) #define chmax3(a, b, c) a = MAX3(a, (b), (c)) #define chmax4(a, b, c, d) a = MAX4(a, (b), (c), (d)) #define chmax5(a, b, c, d, e) a = MAX5(a, (b), (c), (d), (e)) #define chmax6(a, b, c, d, e, f) a = MAX6(a, (b), (c), (d), (e), (f)) #define fcout cout << fixed << setprecision(12) #define RS resize #define CINV(v, N) \ do { \ v.RS(N); \ rep(i, N) cin >> v[i]; \ } while (0); #define RCINV(v, N) \ do { \ v.RS(N); \ rrep(i, N) cin >> v[i]; \ } while (0); #define MOD 1000000007 template <class T> inline T GET() { T x; cin >> x; return x; } void init(); void solve(); signed main() { init(); solve(); } int dp[3123][3123]; void init() { string s[2]; cin >> s[0] >> s[1]; int l[2]; l[0] = s[0].length(); l[1] = s[1].length(); reps(i, 1, l[0] + 1) { reps(j, 1, l[1] + 1) { if (s[0][i - 1] == s[1][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 res = ""; int jma = l[1] + 1; rreps(i, l[0] + 1, 1) { rreps(j, jma, 1) { if (dp[i - 1][j - 1] + 1 == dp[i][j] && dp[i - 1][j] + 1 == dp[i][j] && dp[i][j - 1] + 1 == dp[i][j]) { res += s[0][i - 1]; break; } } } assert(res.length() == dp[l[0]][l[1]]); reverse(all(res)); cout << res << endl; } void solve() {}
#include "bits/stdc++.h" using ll = long long; using namespace std; // 0 -> a-1 #define rep(i, a) for (int i = 0; (i) < (int)(a); (i)++) // a -> b-1 #define reps(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++) // a-1 -> 0 #define rrep(i, a) for (int i = (int)a - 1; (i) >= 0; (i)--) // a-1 -> b #define rreps(i, a, b) for (int i = (int)(a)-1; (i) >= (int)(b); (i)--) #define MP(a, b) make_pair((a), (b)) #define PB(a) push_back((a)) #define all(v) (v).begin(), (v).end() // next_permutation(all(v)) #define PERM(v) next_permutation(all(v)) /*sort(all(v)); (v).erase(unique(all(v)), v.end())*/ #define UNIQUE(v) \ sort(all(v)); \ (v).erase(unique(all(v)), v.end()) #define CIN(type, x) \ type x; \ cin >> x #define YES(f) \ if ((f)) { \ cout << "YES" << endl; \ } else { \ cout << "NO" << endl; \ } #define Yes(f) \ if ((f)) { \ cout << "Yes" << endl; \ } else { \ cout << "No" << endl; \ } #define MINV(v) min_element(all(v)) #define MAXV(v) max_element(all(v)) #define MIN3(a, b, c) min(min(a, b), c) #define MIN4(a, b, c, d) min(MIN3(a, b, c), d) #define MIN5(a, b, c, d, e) min(MIN4(a, b, c, d), e) #define MIN6(a, b, c, d, e, f) min(MIN5(a, b, c, d, e), f) #define MAX3(a, b, c) max(max(a, b), c) #define MAX4(a, b, c, d) max(MAX3(a, b, c), d) #define MAX5(a, b, c, d, e) max(MAX4(a, b, c, d), e) #define MAX6(a, b, c, d, e, f) max(MAX5(a, b, c, d, e), f) // b is [a, c) #define RANGE(a, b, c) ((a) <= (b) && (b) < (c)) // c is [a, e) && d is [b, f) #define RANGE2D(a, b, c, d, e, f) (RANGE((a), (c), (e)) && RANGE((b), (d), (f))) #define chmin(a, b) a = min(a, (b)) #define chmin3(a, b, c) a = MIN3(a, (b), (c)) #define chmin4(a, b, c, d) a = MIN4(a, (b), (c), (d)) #define chmin5(a, b, c, d, e) a = MIN5(a, (b), (c), (d), (e)) #define chmin6(a, b, c, d, e, f) a = MIN6(a, (b), (c), (d), (e), (f)) #define chmax(a, b) a = max(a, (b)) #define chmax3(a, b, c) a = MAX3(a, (b), (c)) #define chmax4(a, b, c, d) a = MAX4(a, (b), (c), (d)) #define chmax5(a, b, c, d, e) a = MAX5(a, (b), (c), (d), (e)) #define chmax6(a, b, c, d, e, f) a = MAX6(a, (b), (c), (d), (e), (f)) #define fcout cout << fixed << setprecision(12) #define RS resize #define CINV(v, N) \ do { \ v.RS(N); \ rep(i, N) cin >> v[i]; \ } while (0); #define RCINV(v, N) \ do { \ v.RS(N); \ rrep(i, N) cin >> v[i]; \ } while (0); #define MOD 1000000007 template <class T> inline T GET() { T x; cin >> x; return x; } void init(); void solve(); signed main() { init(); solve(); } int dp[3123][3123]; void init() { string s[2]; cin >> s[0] >> s[1]; int l[2]; l[0] = s[0].length(); l[1] = s[1].length(); reps(i, 1, l[0] + 1) { reps(j, 1, l[1] + 1) { if (s[0][i - 1] == s[1][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 res = ""; int jma = l[1] + 1; rreps(i, l[0] + 1, 1) { rreps(j, jma, 1) { if (dp[i - 1][j - 1] + 1 == dp[i][j] && dp[i - 1][j] + 1 == dp[i][j] && dp[i][j - 1] + 1 == dp[i][j]) { res += s[0][i - 1]; jma = j; break; } else if (dp[i][j - 1] + 1 == dp[i][j]) { break; } } } assert(res.length() == dp[l[0]][l[1]]); reverse(all(res)); cout << res << endl; } void solve() {}
insert
116
116
116
119
-6
fd82defb-768c-4af5-b06e-5601a0b7c94b.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p03165/C++/s909525431.cpp:118: void init(): Assertion `res.length() == dp[l[0]][l[1]]' failed.
p03165
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; using ll = long long; using ull = unsigned long long; using vb = vector<bool>; using vvb = vector<vb>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using pii = pair<int, int>; using pll = pair<ll, ll>; #define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) for (ll i = (ll)(n)-1; i >= 0; --i) #define ALL(obj) (obj).begin(), (obj).end() #define rALL(obj) (obj).rbegin(), (obj).rend() #define eb(val) emplace_back(val) const double PI = acos(-1); const double EPS = 1e-10; const ll MOD = 1e9 + 7; // #define int ll void cioacc() { // accelerate cin/cout cin.tie(0); ios::sync_with_stdio(false); } signed main() { cioacc(); string s, t; cin >> s >> t; ll ssz = (ll)s.size(), tsz = (ll)t.size(); vector<vector<string>> dp(ssz + 1, vector<string>(tsz + 1)); vector<string> last(tsz + 1); REP(i, ssz) { vector<string> now(tsz + 1); REP(j, tsz) { if (s[i] == t[j]) { now[j + 1] = last[j] + s[i]; continue; } now[j + 1] = (now[j].size() >= last[j + 1].size() ? now[j] : last[j + 1]); } last = now; } cout << last[tsz] << endl; }
#include "bits/stdc++.h" using namespace std; using ll = long long; using ull = unsigned long long; using vb = vector<bool>; using vvb = vector<vb>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using pii = pair<int, int>; using pll = pair<ll, ll>; #define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) for (ll i = (ll)(n)-1; i >= 0; --i) #define ALL(obj) (obj).begin(), (obj).end() #define rALL(obj) (obj).rbegin(), (obj).rend() #define eb(val) emplace_back(val) const double PI = acos(-1); const double EPS = 1e-10; const ll MOD = 1e9 + 7; // #define int ll void cioacc() { // accelerate cin/cout cin.tie(0); ios::sync_with_stdio(false); } signed main() { cioacc(); string s, t; cin >> s >> t; ll ssz = (ll)s.size(), tsz = (ll)t.size(); vector<vector<string>> dp(ssz + 1, vector<string>(tsz + 1)); vector<string> last(tsz + 1); REP(i, ssz) { vector<string> now(tsz + 1); REP(j, tsz) { if (s[i] == t[j]) { now[j + 1] = last[j] + s[i]; continue; } now[j + 1] = (now[j].size() >= last[j + 1].size() ? now[j] : last[j + 1]); } last = move(now); } cout << last[tsz] << endl; }
replace
42
43
42
43
TLE
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; void print_err() { cerr << "\n"; } template <class T, class... Arg> void print_err(T x, Arg &&...args) { cerr << x << " "; print_err(args...); } template <class T> void print_container(T &cont) { for (auto iter : cont) { cerr << iter << " "; } cerr << "\n"; } #ifdef local #define debug(...) print_err(_VA_ARGS_) #define debug_c(container) print_container(container) #else #define debug(...) #define debug_c(container) #endif // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define mod 1000000009 #define ll long long int #define ff first #define ss second #define pb push_back #define mp make_pair #define pll pair<ll, ll> #define vl vector<ll> #define vpll vector<pair<ll, ll>> #define mll map<ll, ll> #define mllrev map<ll, ll, greater<int>> #define ps(x, y) fixed << setprecision(y) << x #define w(t) \ ll t; \ cin >> t; \ while (t--) #define fo(i, n) for (ll i = 0; i < n; ++i) #define foab(i, a, b) for (ll i = a; i < b; ++i) #define inp cin >> #define pr cout << #define all(A) (A).begin(), (A).end() #define fastio \ ios_base::sync_with_stdio(NULL); \ cin.tie(NULL); \ cout.tie(NULL); class Graph { public: list<ll> *adj_list; ll v; Graph(ll v) { this->v = v; adj_list = new list<ll>[v]; } void add_edge(ll u, ll v, bool bidirectional = true) { adj_list[u].push_back(v); if (bidirectional) { adj_list[v].push_back(u); } } void print(); void connected_compo(); void dfs(vector<vector<ll>> vect, ll k1, ll k2, ll n); void bfs(ll v, bool visited[]); }; int dp[3001][3001]; int recursive(string str1, string str2, int n, int m) { if (n == 0 || m == 0) { return 0; } if (dp[n][m] != -1) { return dp[n][m]; } if (str1[n - 1] == str2[m - 1]) { return dp[n][m] = 1 + recursive(str1, str2, n - 1, m - 1); } else { return dp[n][m] = max(recursive(str1, str2, n - 1, m), recursive(str1, str2, n, m - 1)); } return dp[n][m]; } void solve() { memset(dp, -1, sizeof(dp)); ll n, m; string str1, str2; cin >> str1 >> str2; n = str1.size(); m = str2.size(); // cout << str1 << " " << str2 << endl; ll temp = recursive(str1, str2, n, m); string ans = ""; ll i = n, j = m; // cout << dp[n][m]; while (i != 0 and j != 0) { if (str1[i - 1] == str2[j - 1]) { ans.pb(str1[i - 1]); i--; j--; } else { if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } } reverse(ans.begin(), ans.end()); cout << ans << endl; // return; } // Rod cutting problem test case // 5 // 100 // 23 15 50 // 4000 // 3 4 5 // 7 // 5 5 2 // 4 // 2 1 1 // 5 // 5 3 2 int main() { fastio #ifdef local freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif // w(t) { solve(); // } return 0; }
#include <bits/stdc++.h> using namespace std; void print_err() { cerr << "\n"; } template <class T, class... Arg> void print_err(T x, Arg &&...args) { cerr << x << " "; print_err(args...); } template <class T> void print_container(T &cont) { for (auto iter : cont) { cerr << iter << " "; } cerr << "\n"; } #ifdef local #define debug(...) print_err(_VA_ARGS_) #define debug_c(container) print_container(container) #else #define debug(...) #define debug_c(container) #endif // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; #define ordered_set \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> #define mod 1000000009 #define ll long long int #define ff first #define ss second #define pb push_back #define mp make_pair #define pll pair<ll, ll> #define vl vector<ll> #define vpll vector<pair<ll, ll>> #define mll map<ll, ll> #define mllrev map<ll, ll, greater<int>> #define ps(x, y) fixed << setprecision(y) << x #define w(t) \ ll t; \ cin >> t; \ while (t--) #define fo(i, n) for (ll i = 0; i < n; ++i) #define foab(i, a, b) for (ll i = a; i < b; ++i) #define inp cin >> #define pr cout << #define all(A) (A).begin(), (A).end() #define fastio \ ios_base::sync_with_stdio(NULL); \ cin.tie(NULL); \ cout.tie(NULL); class Graph { public: list<ll> *adj_list; ll v; Graph(ll v) { this->v = v; adj_list = new list<ll>[v]; } void add_edge(ll u, ll v, bool bidirectional = true) { adj_list[u].push_back(v); if (bidirectional) { adj_list[v].push_back(u); } } void print(); void connected_compo(); void dfs(vector<vector<ll>> vect, ll k1, ll k2, ll n); void bfs(ll v, bool visited[]); }; int dp[3001][3001]; int recursive(string &str1, string &str2, int n, int m) { if (n == 0 || m == 0) { return 0; } if (dp[n][m] != -1) { return dp[n][m]; } if (str1[n - 1] == str2[m - 1]) { return dp[n][m] = 1 + recursive(str1, str2, n - 1, m - 1); } else { return dp[n][m] = max(recursive(str1, str2, n - 1, m), recursive(str1, str2, n, m - 1)); } return dp[n][m]; } void solve() { memset(dp, -1, sizeof(dp)); ll n, m; string str1, str2; cin >> str1 >> str2; n = str1.size(); m = str2.size(); // cout << str1 << " " << str2 << endl; ll temp = recursive(str1, str2, n, m); string ans = ""; ll i = n, j = m; // cout << dp[n][m]; while (i != 0 and j != 0) { if (str1[i - 1] == str2[j - 1]) { ans.pb(str1[i - 1]); i--; j--; } else { if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } } reverse(ans.begin(), ans.end()); cout << ans << endl; // return; } // Rod cutting problem test case // 5 // 100 // 23 15 50 // 4000 // 3 4 5 // 7 // 5 5 2 // 4 // 2 1 1 // 5 // 5 3 2 int main() { fastio #ifdef local freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif // w(t) { solve(); // } return 0; }
replace
71
72
71
72
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define int long long #define MOD 998244353 using namespace std; int32_t main() { string a, b; cin >> a >> b; int m = a.length(); int n = b.length(); int dp[300][300] = {0}; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (a[i - 1] == b[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 = m, j = n; while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { ans += a[i - 1]; i--; j--; } else if (dp[i][j] == dp[i - 1][j]) i--; else j--; } reverse(ans.begin(), ans.end()); cout << ans; }
#include <bits/stdc++.h> #define pb push_back #define int long long #define MOD 998244353 using namespace std; int32_t main() { string a, b; cin >> a >> b; int m = a.length(); int n = b.length(); int dp[3001][3001] = {0}; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (a[i - 1] == b[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 = m, j = n; while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { ans += a[i - 1]; i--; j--; } else if (dp[i][j] == dp[i - 1][j]) i--; else j--; } reverse(ans.begin(), ans.end()); cout << ans; }
replace
10
11
10
11
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long long dp[3005][3005]; int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); string s, t; cin >> s >> t; int n = s.size(); int m = t.size(); 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; dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1])); } string ans; int x = n, y = m; while (x > 0 || y > 0) if (s[x - 1] == t[y - 1] && dp[x - 1][y - 1] + 1 == dp[x][y]) { ans += s[x - 1]; x--; y--; } else if (dp[x - 1][y] == dp[x][y]) x--; else y--; reverse(ans.begin(), ans.end()); cout << ans; }
#include <bits/stdc++.h> using namespace std; long long dp[3005][3005]; int main() { // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); ios_base::sync_with_stdio(false); string s, t; cin >> s >> t; int n = s.size(); int m = t.size(); 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; dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1])); } string ans; int x = n, y = m; while (x > 0 && y > 0) if (s[x - 1] == t[y - 1] && dp[x - 1][y - 1] + 1 == dp[x][y]) { ans += s[x - 1]; x--; y--; } else if (dp[x - 1][y] == dp[x][y]) x--; else y--; reverse(ans.begin(), ans.end()); cout << ans; }
replace
27
28
27
28
0
p03165
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <math.h> #include <string> #include <vector> #define ll long long int using namespace std; int main() { string a, b; cin >> a >> b; ll aa, bb; // cout<<1; aa = a.size(); bb = b.size(); // cout<<2; vector<vector<ll>> arr(aa + 1, vector<ll>(bb + 1, 0)); for (ll i = 0; i <= aa; i++) { for (ll j = 0; j <= bb; j++) { if (i == 0 || j == 0) { arr[i][j] = 0; // cout<<arr[i][j]<<" "; continue; } if (a[i - 1] == b[j - 1]) arr[i][j] = max(arr[i - 1][j - 1] + 1, max(arr[i - 1][j], arr[i][j - 1])); else arr[i][j] = max(arr[i - 1][j - 1], max(arr[i - 1][j], arr[i][j - 1])); // cout<<arr[i][j]<<" "; } // cout<<endl; } vector<char> ans; ll j = bb; ll i = aa; while (1) { if (i == 0) break; if (j == 0) break; while (arr[i][j] == arr[i - 1][j] && i != 0) i--; // cout<<i<<endl; ans.push_back(a[i - 1]); i--; // cout<<a[i]<<endl; if (i == 0) break; if (j == 0) break; while (arr[i][j] == arr[i][j - 1] && j >= 1) j--; // cout<<" hello :"<<j<<endl; // cout<<"check i "<<i<<endl; if (i == 0) break; if (j == 0) break; } for (ll l = ans.size() - 1; l >= 0; l--) cout << ans[l]; // cin>>a; return 0; }
#include <algorithm> #include <cmath> #include <cstdlib> #include <iostream> #include <math.h> #include <string> #include <vector> #define ll long long int using namespace std; int main() { string a, b; cin >> a >> b; ll aa, bb; // cout<<1; aa = a.size(); bb = b.size(); // cout<<2; vector<vector<ll>> arr(aa + 1, vector<ll>(bb + 1, 0)); for (ll i = 0; i <= aa; i++) { for (ll j = 0; j <= bb; j++) { if (i == 0 || j == 0) { arr[i][j] = 0; // cout<<arr[i][j]<<" "; continue; } if (a[i - 1] == b[j - 1]) arr[i][j] = max(arr[i - 1][j - 1] + 1, max(arr[i - 1][j], arr[i][j - 1])); else arr[i][j] = max(arr[i - 1][j - 1], max(arr[i - 1][j], arr[i][j - 1])); // cout<<arr[i][j]<<" "; } // cout<<endl; } if (arr[aa][bb] == 0) return 0; vector<char> ans; ll j = bb; ll i = aa; while (1) { if (i == 0) break; if (j == 0) break; while (arr[i][j] == arr[i - 1][j] && i != 0) i--; // cout<<i<<endl; ans.push_back(a[i - 1]); i--; // cout<<a[i]<<endl; if (i == 0) break; if (j == 0) break; while (arr[i][j] == arr[i][j - 1] && j >= 1) j--; // cout<<" hello :"<<j<<endl; // cout<<"check i "<<i<<endl; if (i == 0) break; if (j == 0) break; } for (ll l = ans.size() - 1; l >= 0; l--) cout << ans[l]; // cin>>a; return 0; }
insert
36
36
36
38
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define LL long long using namespace std; int dp[1005][1005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(dp, 0, sizeof(dp)); string s, t; cin >> s; cin >> t; 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; 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]); } int a = s.size(), b = t.size(); string ans; while (a && b) { if (s[a - 1] == t[b - 1]) { ans.push_back(s[a - 1]); a--; b--; } else if (dp[a - 1][b] > dp[a][b - 1]) a--; else b--; } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define LL long long using namespace std; int dp[3005][3005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); memset(dp, 0, sizeof(dp)); string s, t; cin >> s; cin >> t; 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; 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]); } int a = s.size(), b = t.size(); string ans; while (a && b) { if (s[a - 1] == t[b - 1]) { ans.push_back(s[a - 1]); a--; b--; } else if (dp[a - 1][b] > dp[a][b - 1]) a--; else b--; } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
replace
3
4
3
4
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; const int N = 1e5; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s, t; cin >> s >> t; vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1)); for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1])); } } stack<char> ans; int ans_size = dp[s.size()][t.size()]; pair<int, int> curr = {s.size(), t.size()}; while (ans_size > 0) { int curr_val = dp[curr.first][curr.second]; int prev_val = dp[curr.first - 1][curr.second - 1]; if (prev_val + 1 == curr_val && !((curr.first == 1) ^ (curr.second == 1))) { ans.push(s[curr.first - 1]); ans_size--; curr = {curr.first - 1, curr.second - 1}; continue; } prev_val = dp[curr.first - 1][curr.second]; if (prev_val == curr_val) curr = {curr.first - 1, curr.second}; else curr = {curr.first, curr.second - 1}; } while (!ans.empty()) { cout << ans.top(); ans.pop(); } cout << "\n"; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; const int N = 1e5; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s, t; cin >> s >> t; vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1)); for (int i = 1; i <= s.size(); i++) { for (int j = 1; j <= t.size(); j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1])); } } stack<char> ans; int ans_size = dp[s.size()][t.size()]; pair<int, int> curr = {s.size(), t.size()}; while (ans_size > 0) { int curr_val = dp[curr.first][curr.second]; int prev_val = dp[curr.first - 1][curr.second - 1]; if (prev_val + 1 == curr_val && s[curr.first - 1] == t[curr.second - 1]) { ans.push(s[curr.first - 1]); ans_size--; curr = {curr.first - 1, curr.second - 1}; continue; } prev_val = dp[curr.first - 1][curr.second]; if (prev_val == curr_val) curr = {curr.first - 1, curr.second}; else curr = {curr.first, curr.second - 1}; } while (!ans.empty()) { cout << ans.top(); ans.pop(); } cout << "\n"; }
replace
28
29
28
29
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]); } } } // main function 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 3005 // 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]); } } } // main function 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
// Main maut ko takiya, aur kafan ko chaadar banakar audhta hoon! #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; using ll = long long; using ld = long double; #define pb emplace_back #define push emplace #define mp make_pair #define ff first #define ss second #define all(v) v.begin(), v.end() #define len(a) (int)a.size() #define reset(a, val) memset(a, val, sizeof(a)) // setbase - cout << setbase (16); cout << 100 << endl; Prints 64 // setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77 #define int long long #define vll vector<int> #define pll pair<int, int> #define vvll vector<vector<int>> #define vpll vector<pair<int, int>> mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); // shuffle(a.begin(), a.end(), rng) or shuffle(a, a+n, rng) instead of // random_shuffle uniform_int_distribution<int/ld>(l, r)(rng) for generating // random integer/double numbers in [l, r] #ifdef ONLINE_JUDGE #define endl '\n' #pragma comment(linker, "/stack:268435456") #pragma GCC optimize("O3") #pragma GCC optimize("O2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #endif #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " "; __f(comma + 1, args...); } #else #define trace(...) #endif // ifndef ONLINE_JUDGE struct custom_hash { static int splitmix64(int x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(int x) const { static const int FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; template <class T, class cmp = less<T>> using ordered_set = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>; #define unordered_map fast_unordered_map template <class Key, class Value, class Hash = std::hash<Key>> // Hash = custom_hash for ints using fast_unordered_map = gp_hash_table<Key, Value, Hash>; #define unordered_set fast_unordered_set template <class Key, class Hash = std::hash<Key>> // Hash = custom_hash for ints using fast_unordered_set = gp_hash_table<Key, null_type, Hash>; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < v.size(); ++i) os << v[i] << " "; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto &it : v) os << it << " "; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << v.first << " " << v.second; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &v) { for (auto &it : v) os << "(" << it.first << "=>" << it.second << ")" << endl; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const unordered_map<T, S> &v) { for (auto &it : v) os << "(" << it.first << "=>" << it.second << ")" << endl; return os; } const int mod = 1e9 + 7; const int inf = 2e18; const int ninf = -2e18; int power(int x, int y, int mod = inf) { int ans = 1; x %= mod; while (y > 0) { if (y % 2) ans = (x * ans) % mod; x = (x * x) % mod; y >>= 1; } return ans; } const int N = 3010, M = 3010; int dp[N][M]; string s, t; string backtrack(int i, int j) { if (i == 0 or j == 0) return ""; if (s[i - 1] == t[j - 1]) return backtrack(i - 1, j - 1) + s[i - 1]; if (dp[i - 1][j] > dp[i][j - 1]) return backtrack(i - 1, j); return backtrack(i, j - 1); } string lcs() { int n = len(s), m = len(t); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } return backtrack(n, m); } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); time_t t1, t2; t1 = clock(); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif cin >> s >> t; cout << lcs(); t2 = clock(); #ifndef ONLINE_JUDGE cerr << endl << "time taken: " << t2 - t1 << endl; #endif // ONLINE_JUDGE return 0; }
// Main maut ko takiya, aur kafan ko chaadar banakar audhta hoon! #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; using ll = long long; using ld = long double; #define pb emplace_back #define push emplace #define mp make_pair #define ff first #define ss second #define all(v) v.begin(), v.end() #define len(a) (int)a.size() #define reset(a, val) memset(a, val, sizeof(a)) // setbase - cout << setbase (16); cout << 100 << endl; Prints 64 // setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77 #define int long long #define vll vector<int> #define pll pair<int, int> #define vvll vector<vector<int>> #define vpll vector<pair<int, int>> mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); // shuffle(a.begin(), a.end(), rng) or shuffle(a, a+n, rng) instead of // random_shuffle uniform_int_distribution<int/ld>(l, r)(rng) for generating // random integer/double numbers in [l, r] #ifdef ONLINE_JUDGE #define endl '\n' #pragma comment(linker, "/stack:268435456") #pragma GCC optimize("O3") #pragma GCC optimize("O2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define _CRT_SECURE_NO_WARNINGS #endif #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " "; __f(comma + 1, args...); } #else #define trace(...) #endif // ifndef ONLINE_JUDGE struct custom_hash { static int splitmix64(int x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(int x) const { static const int FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; template <class T, class cmp = less<T>> using ordered_set = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>; #define unordered_map fast_unordered_map template <class Key, class Value, class Hash = std::hash<Key>> // Hash = custom_hash for ints using fast_unordered_map = gp_hash_table<Key, Value, Hash>; #define unordered_set fast_unordered_set template <class Key, class Hash = std::hash<Key>> // Hash = custom_hash for ints using fast_unordered_set = gp_hash_table<Key, null_type, Hash>; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < v.size(); ++i) os << v[i] << " "; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto &it : v) os << it << " "; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &v) { os << v.first << " " << v.second; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const map<T, S> &v) { for (auto &it : v) os << "(" << it.first << "=>" << it.second << ")" << endl; return os; } template <typename T, typename S> ostream &operator<<(ostream &os, const unordered_map<T, S> &v) { for (auto &it : v) os << "(" << it.first << "=>" << it.second << ")" << endl; return os; } const int mod = 1e9 + 7; const int inf = 2e18; const int ninf = -2e18; int power(int x, int y, int mod = inf) { int ans = 1; x %= mod; while (y > 0) { if (y % 2) ans = (x * ans) % mod; x = (x * x) % mod; y >>= 1; } return ans; } const int N = 3010, M = 3010; int dp[N][M]; string s, t; string backtrack(int i, int j) { if (i == 0 or j == 0) return ""; if (s[i - 1] == t[j - 1]) return backtrack(i - 1, j - 1) + s[i - 1]; if (dp[i - 1][j] > dp[i][j - 1]) return backtrack(i - 1, j); return backtrack(i, j - 1); } string lcs() { int n = len(s), m = len(t); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i - 1] == t[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } return backtrack(n, m); } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); time_t t1, t2; t1 = clock(); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // freopen("error.txt", "w", stderr); // #endif cin >> s >> t; cout << lcs(); t2 = clock(); #ifndef ONLINE_JUDGE cerr << endl << "time taken: " << t2 - t1 << endl; #endif // ONLINE_JUDGE return 0; }
replace
167
172
167
172
0
p03165
C++
Time Limit Exceeded
#include <iostream> using namespace std; #define forr(i, m, n) for (int i = (m); i <= (n); i++) #define DBG 0 #define MAXN 3001 string z[MAXN][MAXN]; int main() { size_t ns, nt; int cnt; string s, t, str; cin >> s >> t; ns = s.size(); nt = t.size(); if (DBG) { cout << "s:" << s << " t:" << t << " nst:" << ns << " " << nt << endl; } str = ""; forr(j, 0, nt - 1) { if (s[0] == t[j]) str = string(1, s[0]); z[0][j] = str; } forr(i, 1, ns - 1) { z[i][0] = (s[i] == t[0] ? string(1, s[i]) : ""); forr(j, 1, nt - 1) { string cat, cat2; str = z[i - 1][j - 1] + (s[i] == t[j] ? string(1, s[i]) : ""); cat = (z[i - 1][j].size() > z[i][j - 1].size() ? z[i - 1][j] : z[i][j - 1]); z[i][j] = (cat.size() > str.size() ? cat : str); } } cout << z[ns - 1][nt - 1]; }
#include <iostream> using namespace std; #define forr(i, m, n) for (int i = (m); i <= (n); i++) #define DBG 0 #define MAXN 3001 string z[MAXN][MAXN]; int main() { size_t ns, nt; int cnt; string s, t, str; cin >> s >> t; ns = s.size(); nt = t.size(); if (DBG) { cout << "s:" << s << " t:" << t << " nst:" << ns << " " << nt << endl; } str = ""; forr(j, 0, nt - 1) { if (s[0] == t[j]) str = string(1, s[0]); z[0][j] = str; } forr(i, 1, ns - 1) { z[i][0] = (s[i] == t[0] ? string(1, s[i]) : ""); forr(j, 1, nt - 1) { int s1, s2, s3; s1 = z[i - 1][j].size(); s2 = z[i][j - 1].size(); s3 = z[i - 1][j - 1].size() + (s[i] == t[j] ? 1 : 0); if (s1 >= s2 && s1 >= s3) z[i][j] = z[i - 1][j]; else if (s2 >= s1 && s2 >= s3) z[i][j] = z[i][j - 1]; else z[i][j] = z[i - 1][j - 1] + (s[i] == t[j] ? string(1, s[i]) : ""); } } cout << z[ns - 1][nt - 1]; }
replace
27
32
27
37
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int #define vi vector<int> #define ii pair<int, int> #define pb push_back #define mp make_pair #define ff first #define ss second #define pll pair<ll, ll> #define vv vector int main() { 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, m; string s1, s2; cin >> s1 >> s2; n = s1.size(), m = s2.size(); int dp[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else 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]); } } string ans = ""; /*for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++) cout<<dp[i][j]<<' '; cout<<'\n'; }*/ int i = n, j = m; while (i || j) { if (dp[i - 1][j] != dp[i][j] && dp[i][j - 1] != dp[i][j]) ans = s1[i - 1] + ans, i--, j--; else if (dp[i - 1][j] == dp[i][j]) i--; else j--; } cout << ans; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define vi vector<int> #define ii pair<int, int> #define pb push_back #define mp make_pair #define ff first #define ss second #define pll pair<ll, ll> #define vv vector int main() { 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, m; string s1, s2; cin >> s1 >> s2; n = s1.size(), m = s2.size(); int dp[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else 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]); } } string ans = ""; /*for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++) cout<<dp[i][j]<<' '; cout<<'\n'; }*/ int i = n, j = m; while (i > 0 && j > 0) { if (dp[i - 1][j] != dp[i][j] && dp[i][j - 1] != dp[i][j]) ans = s1[i - 1] + ans, i--, j--; else if (dp[i - 1][j] == dp[i][j]) i--; else j--; } cout << ans; }
replace
43
44
43
44
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long 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); cout.tie(NULL); string a, b; cin >> a >> b; ll i, j, m = a.length(), n = b.length(); int dp[m + 1][n + 1]; memset(dp, 0, sizeof dp); for (i = 1; i <= m; i++) { for (j = 1; j <= n; 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]); } } // cout<<dp[m][n]; i = m; j = n; vector<char> out; while (i > 0 && j > 0 && dp[i][j] > 0) { if (dp[i][j] == dp[i - 1][j]) i--; else if (dp[i][j] == dp[i][j - 1]) j--; else { i--; j--; out.push_back(a[i]); } } for (i = out.size() - 1; i >= 0; i--) cout << out[i]; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string a, b; cin >> a >> b; ll i, j, m = a.length(), n = b.length(); int dp[m + 1][n + 1]; memset(dp, 0, sizeof dp); for (i = 1; i <= m; i++) { for (j = 1; j <= n; 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]); } } // cout<<dp[m][n]; i = m; j = n; vector<char> out; while (i > 0 && j > 0 && dp[i][j] > 0) { if (dp[i][j] == dp[i - 1][j]) i--; else if (dp[i][j] == dp[i][j - 1]) j--; else { i--; j--; out.push_back(a[i]); } } for (i = out.size() - 1; i >= 0; i--) cout << out[i]; return 0; }
delete
6
11
6
6
0
p03165
C++
Runtime Error
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string s, t; cin >> s >> t; s = " " + s; t = " " + t; int dp[110][110] = {0}; for (int y = 0; y < (int)s.size(); y++) { for (int x = 0; x < (int)t.size(); x++) { if (y == 0 || x == 0) continue; if (s[y] == t[x]) { dp[y][x] = dp[y - 1][x - 1] + 1; } else { dp[y][x] = max(dp[y - 1][x], dp[y][x - 1]); } } } int length = dp[(int)s.size() - 1][(int)t.size() - 1]; string ans = ""; int itrx = (int)t.size() - 1, itry = (int)s.size() - 1; while (length) { if (s[itry] == t[itrx]) { ans += s[itry]; length--; itry--; itrx--; } else if (dp[itry][itrx] == dp[itry - 1][itrx]) { itry--; } else if (dp[itry][itrx] == dp[itry][itrx - 1]) { itrx--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <string> #include <vector> using namespace std; int main() { string s, t; cin >> s >> t; s = " " + s; t = " " + t; int dp[3010][3010] = {0}; for (int y = 0; y < (int)s.size(); y++) { for (int x = 0; x < (int)t.size(); x++) { if (y == 0 || x == 0) continue; if (s[y] == t[x]) { dp[y][x] = dp[y - 1][x - 1] + 1; } else { dp[y][x] = max(dp[y - 1][x], dp[y][x - 1]); } } } int length = dp[(int)s.size() - 1][(int)t.size() - 1]; string ans = ""; int itrx = (int)t.size() - 1, itry = (int)s.size() - 1; while (length) { if (s[itry] == t[itrx]) { ans += s[itry]; length--; itry--; itrx--; } else if (dp[itry][itrx] == dp[itry - 1][itrx]) { itry--; } else if (dp[itry][itrx] == dp[itry][itrx - 1]) { itrx--; } } reverse(ans.begin(), ans.end()); cout << ans << endl; }
replace
13
14
13
14
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define vi vector<int> #define vl vector<long long> #define vvi vector<vector<int>> #define fin(ar, k, n) \ for (int i = k; i < n; i++) \ cin >> ar[i] #define fout(ar, k, n) \ for (int i = k; i < n; i++) \ cout << ar[i] << ' ' #define all(z) z.begin(), z.end() #define mcc ((int)1e9 + 7) #define mcf 998244353 #define mi map<int, int> #define mem(a, n) memset(a, n, sizeof(a)) #define pii pair<int, int> #define mp(a, b) make_pair(a, b) using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> fst; const int N = 1e3 + 4; int dp[N][N]; string s, t; stack<char> ans; void solve() { cin >> s; cin >> t; int n = s.length(), 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] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int i = n, j = m; while (i != 0 && j != 0) { if (dp[i][j] == dp[i - 1][j]) i--; else if (dp[i][j] == dp[i][j - 1]) j--; else { ans.push(s[i - 1]); i--; j--; } } while (!ans.empty()) { cout << ans.top(); ans.pop(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; // cin>>t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define vi vector<int> #define vl vector<long long> #define vvi vector<vector<int>> #define fin(ar, k, n) \ for (int i = k; i < n; i++) \ cin >> ar[i] #define fout(ar, k, n) \ for (int i = k; i < n; i++) \ cout << ar[i] << ' ' #define all(z) z.begin(), z.end() #define mcc ((int)1e9 + 7) #define mcf 998244353 #define mi map<int, int> #define mem(a, n) memset(a, n, sizeof(a)) #define pii pair<int, int> #define mp(a, b) make_pair(a, b) using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> fst; const int N = 3e3 + 4; int dp[N][N]; string s, t; stack<char> ans; void solve() { cin >> s; cin >> t; int n = s.length(), 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] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int i = n, j = m; while (i != 0 && j != 0) { if (dp[i][j] == dp[i - 1][j]) i--; else if (dp[i][j] == dp[i][j - 1]) j--; else { ans.push(s[i - 1]); i--; j--; } } while (!ans.empty()) { cout << ans.top(); ans.pop(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; // cin>>t; while (t--) solve(); return 0; }
replace
28
29
28
29
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<long, long> pll; typedef vector<int> vi; typedef vector<long long> vll; #define forn(i, n) for (int i = 0; i < n; i++) #define mp makepair #define pb push_back string s, t; string ans; int main() { cin >> s >> t; int dp[200][200] = {0}; for (int i = 1; i <= s.length(); i++) { for (int j = 1; j <= t.length(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); } } } string ans = ""; int i = s.length(); int j = t.length(); while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { i--; j--; ans = t[j] + ans; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<long, long> pll; typedef vector<int> vi; typedef vector<long long> vll; #define forn(i, n) for (int i = 0; i < n; i++) #define mp makepair #define pb push_back string s, t; string ans; int main() { cin >> s >> t; int dp[3001][3001] = {0}; for (int i = 1; i <= s.length(); i++) { for (int j = 1; j <= t.length(); j++) { if (s[i - 1] == t[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } else { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); } } } string ans = ""; int i = s.length(); int j = t.length(); while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { i--; j--; ans = t[j] + ans; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } cout << ans << endl; }
replace
18
19
18
19
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fastread \ std::ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define endl '\n' #define pb push_back #define mp make_pair #define print(a) \ for (auto x : a) \ cout << x << ' '; \ cout << endl; #define printM(b) \ for (auto y : b) { \ print(y); \ } #define printP(p) cout << p.first << ' ' << p.second << endl; using namespace std; typedef long long int ll; typedef long double ld; typedef unsigned long long ull; int main() { fastread; ll n, m, i, j; string s, t; cin >> s >> t; n = s.length(); m = t.length(); string dp[2][m + 1]; for (i = 0; i <= n; ++i) for (j = 0; j <= m; ++j) { if (i == 0 || j == 0) dp[i % 2][j] = ""; else if (s[i - 1] == t[j - 1]) dp[i % 2][j] = dp[(i + 1) % 2][j - 1] + s[i - 1]; else { if (dp[(i + 1) % 2][j].length() >= dp[i % 2][j - 1].length()) dp[i % 2][j] = dp[(i + 1) % 2][j]; else dp[i % 2][j] = dp[i % 2][j - 1]; } } cout << dp[n % 2][m]; return 0; }
#include <bits/stdc++.h> #define fastread \ std::ios::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define endl '\n' #define pb push_back #define mp make_pair #define print(a) \ for (auto x : a) \ cout << x << ' '; \ cout << endl; #define printM(b) \ for (auto y : b) { \ print(y); \ } #define printP(p) cout << p.first << ' ' << p.second << endl; using namespace std; typedef long long int ll; typedef long double ld; typedef unsigned long long ull; int main() { fastread; ll n, m, i, j; string s, t; cin >> s >> t; n = s.length(); m = t.length(); string dp[2][m + 1]; for (j = 0; j <= m; ++j) dp[0][j] = ""; for (i = 1; i <= n; ++i) for (j = 1; j <= m; ++j) { if (s[i - 1] == t[j - 1]) dp[i % 2][j] = dp[(i + 1) % 2][j - 1] + s[i - 1]; else { if (dp[(i + 1) % 2][j].length() >= dp[i % 2][j - 1].length()) dp[i % 2][j] = dp[(i + 1) % 2][j]; else dp[i % 2][j] = dp[i % 2][j - 1]; } } cout << dp[n % 2][m]; return 0; }
replace
38
43
38
43
TLE
p03165
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define ll long long int #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n; i >= 0; i--) #define REP(i, s, t) for (int i = s; i <= t; i++) #define RREP(i, s, t) for (int i = s; i >= t; i--) #define dump(x) cerr << #x << " = " << (x) << endl; #define INF 2000000000 #define mod 1000000007 #define INF2 1000000000000000000 int dp[3010][3010]; int main(void) { cin.tie(0); ios::sync_with_stdio(false); string S, T; cin >> S >> T; rep(i, S.length()) { rep(j, T.length()) { 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]); } } // cout << dp[S.length()][T.length()] << endl; int i = S.length(), j = T.length(); string A; while (i > 0 || j > 0) { // cout << i << " " << j << " " << dp[i][j] << A << endl; if (S[i - 1] == T[j - 1]) { A = S[i - 1] + A; i--; j--; } else if (dp[i][j] == dp[i - 1][j]) i--; else j--; } cout << A << endl; return 0; }
#include "bits/stdc++.h" using namespace std; #define ll long long int #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n; i >= 0; i--) #define REP(i, s, t) for (int i = s; i <= t; i++) #define RREP(i, s, t) for (int i = s; i >= t; i--) #define dump(x) cerr << #x << " = " << (x) << endl; #define INF 2000000000 #define mod 1000000007 #define INF2 1000000000000000000 int dp[3010][3010]; int main(void) { cin.tie(0); ios::sync_with_stdio(false); string S, T; cin >> S >> T; rep(i, S.length()) { rep(j, T.length()) { 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]); } } // cout << dp[S.length()][T.length()] << endl; int i = S.length(), j = T.length(); string A; while (i > 0 && j > 0) { // cout << i << " " << j << " " << dp[i][j] << A << endl; if (S[i - 1] == T[j - 1]) { A = S[i - 1] + A; i--; j--; } else if (dp[i][j] == dp[i - 1][j]) i--; else j--; } cout << A << endl; return 0; }
replace
29
30
29
30
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int main(void) { string s1; cin >> s1; string s2; cin >> s2; int n = s1.size(); int m = s2.size(); int dp[n + 1][n + 1]; memset(dp, 0, sizeof(dp)); 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 (s1[i - 1] == s2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int i = n; int j = m; string ans; while (i > 0 && j > 0) { if (s1[i - 1] == s2[j - 1]) { ans.push_back(s1[i - 1]); i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else j--; } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { string s1; cin >> s1; string s2; cin >> s2; int n = s1.size(); int m = s2.size(); vector<vector<int>> dp; for (int i = 0; i < s1.size() + 1; i++) { vector<int> temp; for (int j = 0; j < s2.size() + 1; j++) temp.push_back(0); dp.push_back(temp); } 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 (s1[i - 1] == s2[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int i = n; int j = m; string ans; while (i > 0 && j > 0) { if (s1[i - 1] == s2[j - 1]) { ans.push_back(s1[i - 1]); i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else j--; } reverse(ans.begin(), ans.end()); cout << ans; return 0; }
replace
13
15
13
20
0
p03165
C++
Time Limit Exceeded
/* Author- Abhijeet Trigunait */ #include <bits/stdc++.h> #define lld long long int #define F first #define S second #define P pair<int, int> #define pb push_back #define mod 1e9 + 7 #define setbits(x) __builtin_popcountll(x) #define zerobits(x) __builtin_ctzll(x) #define gcd(x, y) __gcd(x, y) #define endl '\n' using namespace std; lld dp[3005][3005]; string getStr(string &s1, string &s2, lld len) { string lcs; lld i = 0, j = 0; while (len > 0) { if (s1[i] == s2[j]) { lcs.pb(s1[i]); ++i; ++j; --len; } else { if (dp[i][j + 1] > dp[i + 1][j]) { ++j; } else ++i; } } return lcs; } lld getLength(string s1, string s2, lld i, lld j) { if (i >= s1.length() or j >= s2.length()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (s1[i] == s2[j]) { return dp[i][j] = 1 + getLength(s1, s2, i + 1, j + 1); } else { return dp[i][j] = max(getLength(s1, s2, i, j + 1), getLength(s1, s2, i + 1, j)); } } string solve(string s1, string s2) { memset(dp, -1, sizeof(dp)); lld len = getLength(s1, s2, 0, 0); return getStr(s1, s2, len); } int main() { string s1, s2; cin >> s1 >> s2; cout << solve(s1, s2) << endl; }
/* Author- Abhijeet Trigunait */ #include <bits/stdc++.h> #define lld long long int #define F first #define S second #define P pair<int, int> #define pb push_back #define mod 1e9 + 7 #define setbits(x) __builtin_popcountll(x) #define zerobits(x) __builtin_ctzll(x) #define gcd(x, y) __gcd(x, y) #define endl '\n' using namespace std; lld dp[3005][3005]; string getStr(string &s1, string &s2, lld len) { string lcs; lld i = 0, j = 0; while (len > 0) { if (s1[i] == s2[j]) { lcs.pb(s1[i]); ++i; ++j; --len; } else { if (dp[i][j + 1] > dp[i + 1][j]) { ++j; } else ++i; } } return lcs; } lld getLength(string &s1, string &s2, lld i, lld j) { if (i >= s1.length() or j >= s2.length()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (s1[i] == s2[j]) { return dp[i][j] = 1 + getLength(s1, s2, i + 1, j + 1); } else { return dp[i][j] = max(getLength(s1, s2, i, j + 1), getLength(s1, s2, i + 1, j)); } } string solve(string s1, string s2) { memset(dp, -1, sizeof(dp)); lld len = getLength(s1, s2, 0, 0); return getStr(s1, s2, len); } int main() { string s1, s2; cin >> s1 >> s2; cout << solve(s1, s2) << endl; }
replace
36
37
36
37
TLE
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; long n, m, w[100000], v[1000000], kq = -5; long long f[5000][5000]; string s, t; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s >> t; s = ' ' + s; t = ' ' + t; for (long i = 1; i < s.size(); i++) { for (long j = 1; j < t.size(); j++) { if (s[i] == t[j]) { f[i][j] = f[i - 1][j - 1] + 1; } else f[i][j] = max(f[i - 1][j], f[i][j - 1]); } } string res = ""; long m = s.size() - 1; long n = t.size() - 1; while (m > 0 && n > 0) { if (s[m] == t[n]) { res = s[m] + res; m--; n--; } if (f[m][n] == f[m - 1][n]) { m--; } else n--; } cout << res; }
#include <bits/stdc++.h> using namespace std; long n, m, w[100000], v[1000000], kq = -5; long long f[5000][5000]; string s, t; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s >> t; s = ' ' + s; t = ' ' + t; for (long i = 1; i < s.size(); i++) { for (long j = 1; j < t.size(); j++) { if (s[i] == t[j]) { f[i][j] = f[i - 1][j - 1] + 1; } else f[i][j] = max(f[i - 1][j], f[i][j - 1]); } } string res = ""; long m = s.size() - 1; long n = t.size() - 1; while (m > 0 && n > 0) { if (s[m] == t[n]) { res = s[m] + res; m--; n--; continue; } if (f[m][n] == f[m - 1][n]) { m--; } else n--; } cout << res; }
insert
28
28
28
29
-11
p03165
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <valarray> #include <vector> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; // macros #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> #define ld long double #define pb push_back #define f first #define s second #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long #define ull unsigned long long #define watch(x) cout << "debug : " << #x << " -> " << x << endl; // globals ll const mod = 1e9 + 7; ll const size_1d = 1e6 + 7; ll const size_2d = 3 * (1e3 + 7); // functions ll power(ll x, ll y, ll mod = 2e18) { ll ans = 1; x %= mod; while (y) { if (y & 1) ans = (x * ans) % mod; x = (x * x) % mod; y >>= 1; } return ans; } ll modInverse(ll a, ll m) { ll m0 = m; ll 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 gcdext(ll a, ll b, ll *x = 0, ll *y = 0) { if (a == 0) { *x = 0; *y = 1; return b; } ll x1, y1; ll gcd1 = gcdext(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd1; } int randomize() { return (rand() % 10000); } // CODE STARTS HERE int dp[size_2d][size_2d]; int main() { IOS; string s1, s2; cin >> s1 >> s2; for (int i = 0; i < s1.length(); ++i) { if (s1[i] == s2[0]) dp[i][0] = 1; else if (i != 0) dp[i][0] = dp[i - 1][0]; } for (int i = 0; i < s2.length(); ++i) { if (s2[i] == s1[0]) dp[0][i] = 1; else if (i != 0) dp[0][i] = dp[0][i - 1]; } for (int i = 1; i < s1.length(); ++i) { for (int j = 1; j < s2.length(); ++j) { if (s1[i] == s2[j]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } // cout << dp[s1.length()-1][s2.length()-1] << endl; int index = dp[s1.length() - 1][s2.length() - 1]; if (index == 0) return cout << "" << endl, 0; char lcs[index + 1]; lcs[index] = ' '; int i = s1.length() - 1, j = s2.length() - 1; while (i >= 0 && j >= 0) { if (s1[i] == s2[j]) { lcs[index - 1] = s1[i]; i--; j--; index--; } else if (i == 0 && j != 0) { j--; } else if (i != 0 && j == 0) { i--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } int count = 1; for (auto &m : lcs) { if (count > dp[s1.length() - 1][s2.length() - 1]) break; else cout << m; count++; } cout << endl; // cout << lcs << endl; // for (int i = 0; i < s1.length(); ++i) // { // for (int j = 0; j < s2.length(); ++j) // { // cout << dp[i][j] << " "; // } // cout << endl; // } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <valarray> #include <vector> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; // macros #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> #define ld long double #define pb push_back #define f first #define s second #define IOS \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ll long long #define ull unsigned long long #define watch(x) cout << "debug : " << #x << " -> " << x << endl; // globals ll const mod = 1e9 + 7; ll const size_1d = 1e6 + 7; ll const size_2d = 3 * (1e3 + 7); // functions ll power(ll x, ll y, ll mod = 2e18) { ll ans = 1; x %= mod; while (y) { if (y & 1) ans = (x * ans) % mod; x = (x * x) % mod; y >>= 1; } return ans; } ll modInverse(ll a, ll m) { ll m0 = m; ll 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 gcdext(ll a, ll b, ll *x = 0, ll *y = 0) { if (a == 0) { *x = 0; *y = 1; return b; } ll x1, y1; ll gcd1 = gcdext(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd1; } int randomize() { return (rand() % 10000); } // CODE STARTS HERE int dp[size_2d][size_2d]; int main() { IOS; string s1, s2; cin >> s1 >> s2; for (int i = 0; i < s1.length(); ++i) { if (s1[i] == s2[0]) dp[i][0] = 1; else if (i != 0) dp[i][0] = dp[i - 1][0]; } for (int i = 0; i < s2.length(); ++i) { if (s2[i] == s1[0]) dp[0][i] = 1; else if (i != 0) dp[0][i] = dp[0][i - 1]; } for (int i = 1; i < s1.length(); ++i) { for (int j = 1; j < s2.length(); ++j) { if (s1[i] == s2[j]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } // cout << dp[s1.length()-1][s2.length()-1] << endl; int index = dp[s1.length() - 1][s2.length() - 1]; if (index == 0) return cout << "" << endl, 0; char lcs[index + 1]; lcs[index] = ' '; int i = s1.length() - 1, j = s2.length() - 1; while (i >= 0 && j >= 0) { if (s1[i] == s2[j]) { lcs[index - 1] = s1[i]; i--; j--; index--; } else if (i == 0 && j != 0) { j--; } else if (i != 0 && j == 0) { i--; } else if (i == 0 && j == 0) { break; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } int count = 1; for (auto &m : lcs) { if (count > dp[s1.length() - 1][s2.length() - 1]) break; else cout << m; count++; } cout << endl; // cout << lcs << endl; // for (int i = 0; i < s1.length(); ++i) // { // for (int j = 0; j < s2.length(); ++j) // { // cout << dp[i][j] << " "; // } // cout << endl; // } return 0; }
insert
134
134
134
136
0
p03165
C++
Runtime Error
// #include "bits/stdc++.h" #define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(c) begin(c), end(c) #define int ll #define SZ(x) ((int)(x).size()) #define pb push_back #define mp make_pair // typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<double, double> pdd; typedef vector<vector<int>> mat; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)1e9 + 7; const double EPS = 1e-9; signed main() { cin.tie(0); ios::sync_with_stdio(false); string S, T; cin >> S >> T; vector<vector<int>> DP(3010, vector<int>(3010, 0)); vector<vector<pii>> PAR(3010, vector<pii>(3010, mp(0, 0))); rep(i, 0, SZ(S)) rep(j, 0, SZ(T)) { if (S[i] == T[j]) { DP[i + 1][j + 1] = DP[i][j] + 1; PAR[i][j] = mp(i - 1, j - 1); } else { if (chmax(DP[i + 1][j + 1], DP[i][j + 1])) { PAR[i][j] = mp(i - 1, j); } if (chmax(DP[i + 1][j + 1], DP[i + 1][j])) { PAR[i][j] = mp(i, j - 1); } } } string ANS; int l = SZ(S) - 1, r = SZ(T) - 1; while (true) { if (S[l] == T[r]) ANS += S[l]; if (l <= 0 && r <= 0) break; auto par = PAR[l][r]; l = par.first; r = par.second; } reverse(all(ANS)); cout << ANS << endl; return 0; }
// #include "bits/stdc++.h" #define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) #define all(c) begin(c), end(c) #define int ll #define SZ(x) ((int)(x).size()) #define pb push_back #define mp make_pair // typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<double, double> pdd; typedef vector<vector<int>> mat; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)1e9 + 7; const double EPS = 1e-9; signed main() { cin.tie(0); ios::sync_with_stdio(false); string S, T; cin >> S >> T; vector<vector<int>> DP(3010, vector<int>(3010, 0)); vector<vector<pii>> PAR(3010, vector<pii>(3010, mp(0, 0))); rep(i, 0, SZ(S)) rep(j, 0, SZ(T)) { if (S[i] == T[j]) { DP[i + 1][j + 1] = DP[i][j] + 1; PAR[i][j] = mp(i - 1, j - 1); } else { if (chmax(DP[i + 1][j + 1], DP[i][j + 1])) { PAR[i][j] = mp(i - 1, j); } if (chmax(DP[i + 1][j + 1], DP[i + 1][j])) { PAR[i][j] = mp(i, j - 1); } } } string ANS; int l = SZ(S) - 1, r = SZ(T) - 1; while (true) { if (S[l] == T[r]) ANS += S[l]; if (l < 0 || r < 0 || l == 0 && r == 0) break; auto par = PAR[l][r]; l = par.first; r = par.second; } reverse(all(ANS)); cout << ANS << endl; return 0; }
replace
94
95
94
95
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <iostream> #include <stdio.h> using namespace std; using namespace __gnu_pbds; #define fr(i, j, n) for (long long i = j; i < (n); ++i) #define bk(i, j, n) for (long long i = j; i >= n; --i) #define pb push_back #define mp make_pair #define F first #define S second #define endl "\n" #define MOD 1000000007 #define debug1(x) cout << #x << " " << x << endl; #define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl; #define debug3(x, y, z) \ cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z \ << endl; #define srt(x) sort(x.begin(), x.end()); #define run \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> typedef long long ll; ll modpower(ll a, ll b, ll c) { ll res = 1; while (b) { if (b & 1LL) res = (res * a) % c; a = (a * a) % c; b >>= 1; } return res; } ll dp[3005][3005]; int main() { run; string s, t; cin >> s >> t; ll n1 = s.length(); ll n2 = t.length(); fr(i, 0, n1) { fr(j, 0, n2) { if (s[i] == t[j]) { if (i && j) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = 1; // max(dp[i-1][j],dp[i][j-1]); } if (i) dp[i][j] = max(dp[i - 1][j], dp[i][j]); if (j) dp[i][j] = max(dp[i][j - 1], dp[i][j]); } } ll len = dp[n1 - 1][n2 - 1]; ll i = n1 - 1, j = n2 - 1; string u; while (len) { if (s[i] == t[j]) { u.pb(s[i]); i--, j--, len--; } else if (dp[i - 1][j] > dp[i][j - 1]) { i--; } else { j--; } } if (!u.length()) return 0; bk(i, u.length() - 1, 0) { cout << u[i]; } // cout<<dp[n1-1][n2-1]; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <iostream> #include <stdio.h> using namespace std; using namespace __gnu_pbds; #define fr(i, j, n) for (long long i = j; i < (n); ++i) #define bk(i, j, n) for (long long i = j; i >= n; --i) #define pb push_back #define mp make_pair #define F first #define S second #define endl "\n" #define MOD 1000000007 #define debug1(x) cout << #x << " " << x << endl; #define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl; #define debug3(x, y, z) \ cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z \ << endl; #define srt(x) sort(x.begin(), x.end()); #define run \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> typedef long long ll; ll modpower(ll a, ll b, ll c) { ll res = 1; while (b) { if (b & 1LL) res = (res * a) % c; a = (a * a) % c; b >>= 1; } return res; } ll dp[3005][3005]; int main() { run; string s, t; cin >> s >> t; ll n1 = s.length(); ll n2 = t.length(); fr(i, 0, n1) { fr(j, 0, n2) { if (s[i] == t[j]) { if (i && j) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = 1; // max(dp[i-1][j],dp[i][j-1]); } if (i) dp[i][j] = max(dp[i - 1][j], dp[i][j]); if (j) dp[i][j] = max(dp[i][j - 1], dp[i][j]); } } ll len = dp[n1 - 1][n2 - 1]; ll i = n1 - 1, j = n2 - 1; string u; while (len) { if (s[i] == t[j]) { u.pb(s[i]); i--, j--, len--; } else if (i && j && dp[i - 1][j] > dp[i][j - 1] || !j) { i--; } else { j--; } } if (!u.length()) return 0; bk(i, u.length() - 1, 0) { cout << u[i]; } // cout<<dp[n1-1][n2-1]; }
replace
73
74
73
74
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> // #pragma GCC optimize ("O3") #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pii pair<int, int> #define point pair<double, double> #define segment pair<point, point> #define circle pair<point, double> // titik pusat, radius #define line pair<double, double> // m,c #define ins insert #define er erase #define FORU(a, b, c) for (int a = b; a <= c; a++) #define FORD(a, b, c) for (int a = b; a >= c; a--) #define FOR(a, b) for (int a = 1; a <= b; a++) #define FORA(a, b) for (auto(a) : (b)) const double EPS = 1e-9; const double PI = acos(-1); const int MOD = 1e9 + 7; #define yu using #define don namespace #define sei std yu don sei; // const int nPrime=100000; // bool isprime[nPrime]; // vector<int>prime; // // void pre() //{ // memset(isprime,1,sizeof(isprime)); // isprime[1]=0; // for(int i=2;i*i<=nPrime;i++) // { // for(int j=i*i;j<nPrime;j+=i) // { // isprime[j]=0; // } // } // for(int i=2;i<nPrime;i++) // { // if(isprime[i])prime.pb(i); // } // } // ll pwr(ll b,ll e) // { // if(e==0)return 1; // if(e==1)return b; // ll temp=pwr(b,e/2); // if(e%2==0) // { // return (temp*temp); // } // else // { // return (temp*temp*b); // } // } // ll modpow(ll b,ll e) // { // b%=MOD; // if(e==0)return 1; // if(e==1)return b%MOD; // ll temp=modpow(b,e/2); // if(e%2==0) // { // return (temp*temp) %MOD; // } // else // { // return ((temp*temp %MOD )*b)%MOD; // } // } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // int n,m; // bool valid(int x,int y) //{ // if(x<0)return 0; // if(y<0)return 0; // if(x>=n)return 0; // if(y>=m)return 0; // return 1; // } // int p[]={-1,0,1,0}; // int q[]={0,1,0,-1}; // int p[]={-1,-1,-1,0,0,1,1,1}; // int q[]={-1,0,1,-1,1,-1,0,1}; point add(point a, point b) { return {a.fi + b.fi, a.se + b.se}; } point sub(point a, point b) { return {a.fi - b.fi, a.se - b.se}; } // double dist(point a,point b) //{ // return ((a.fi-b.fi)*(a.fi-b.fi) + (a.se-b.se)*(a.se-b.se)); // } double dist(point a, point b) { return sqrt((a.fi - b.fi) * (a.fi - b.fi) + (a.se - b.se) * (a.se - b.se)); } // double distPS(point a,segment b)//heron formula, extreemely prone to precison // error //{ // double AB=rdist(a,b.fi); // double AC=rdist(a,b.se); // double BC=rdist(b.fi,b.se); // double s=AB+AC+BC; // s/=2.0; // return 2*sqrt(s*(s-AB)*(s-BC)*(s-AC))/BC; // } double dabs(double a) { if (a + EPS > 0) return a; return -a; } double dotProduct(point a, point b) { return a.fi * b.fi + a.se * b.se; } double crossProduct(point a, point b) { return a.fi * b.se - a.se * b.fi; } int ccw(point a, point b, point c) // -1 CCW 0 COL 1 CW { double tmp = crossProduct(sub(b, a), sub(c, a)); if (tmp > EPS) { return -1; } else if (tmp < -EPS) { return 1; } else { return 0; } } point intLiLi(point a, point b, point c, point d) { return mp( ((a.fi * b.se - b.fi * a.se) * (c.fi - d.fi) - (a.fi - b.fi) * (c.fi * d.se - d.fi * c.se)) / ((a.fi - b.fi) * (c.se - d.se) - (a.se - b.se) * (c.fi - d.fi)), ((a.fi * b.se - b.fi * a.se) * (c.se - d.se) - (a.se - b.se) * (c.fi * d.se - d.fi * c.se)) / ((a.fi - b.fi) * (c.se - d.se) - (a.se - b.se) * (c.fi - d.fi))); } string s, t; int memo[1010][1010]; int dp(int i, int j) { if (i == s.size() || j == t.size()) return memo[i][j] = 0; if (memo[i][j] != -1) return memo[i][j]; if (s[i] == t[j]) return memo[i][j] = max(dp(i + 1, j + 1) + 1, max(dp(i + 1, j), dp(i, j + 1))); return memo[i][j] = max(dp(i + 1, j), dp(i, j + 1)); } void bt(int i, int j) { if (i == s.size() || j == t.size()) return; if (s[i] == t[j]) { // cerr<<memo[i+1][j+1] << " " <<memo[i][j+1] <<" "<< memo[i+1][j]<<endl; if (memo[i][j] == memo[i + 1][j + 1] + 1) { // cout<<"A"<<endl; cout << s[i]; bt(i + 1, j + 1); } else if (memo[i][j] == memo[i + 1][j]) { // cout<<"B"<<endl; bt(i + 1, j); } else { // cout<<"C"<<endl; bt(i, j + 1); } } else if (memo[i][j] == memo[i + 1][j]) { // cout<<"D"<<endl; // cerr<<memo[i][j+1] <<" "<< memo[i+1][j]<<endl; bt(i + 1, j); } else { // cout<<"E"<<endl; // cerr<<memo[i][j+1] <<" "<< memo[i+1][j]<<endl; bt(i, j + 1); } } int main() { memset(memo, -1, sizeof(memo)); cin >> s >> t; dp(0, 0); bt(0, 0); return 0; }
#include <bits/stdc++.h> // #pragma GCC optimize ("O3") #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pii pair<int, int> #define point pair<double, double> #define segment pair<point, point> #define circle pair<point, double> // titik pusat, radius #define line pair<double, double> // m,c #define ins insert #define er erase #define FORU(a, b, c) for (int a = b; a <= c; a++) #define FORD(a, b, c) for (int a = b; a >= c; a--) #define FOR(a, b) for (int a = 1; a <= b; a++) #define FORA(a, b) for (auto(a) : (b)) const double EPS = 1e-9; const double PI = acos(-1); const int MOD = 1e9 + 7; #define yu using #define don namespace #define sei std yu don sei; // const int nPrime=100000; // bool isprime[nPrime]; // vector<int>prime; // // void pre() //{ // memset(isprime,1,sizeof(isprime)); // isprime[1]=0; // for(int i=2;i*i<=nPrime;i++) // { // for(int j=i*i;j<nPrime;j+=i) // { // isprime[j]=0; // } // } // for(int i=2;i<nPrime;i++) // { // if(isprime[i])prime.pb(i); // } // } // ll pwr(ll b,ll e) // { // if(e==0)return 1; // if(e==1)return b; // ll temp=pwr(b,e/2); // if(e%2==0) // { // return (temp*temp); // } // else // { // return (temp*temp*b); // } // } // ll modpow(ll b,ll e) // { // b%=MOD; // if(e==0)return 1; // if(e==1)return b%MOD; // ll temp=modpow(b,e/2); // if(e%2==0) // { // return (temp*temp) %MOD; // } // else // { // return ((temp*temp %MOD )*b)%MOD; // } // } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } // int n,m; // bool valid(int x,int y) //{ // if(x<0)return 0; // if(y<0)return 0; // if(x>=n)return 0; // if(y>=m)return 0; // return 1; // } // int p[]={-1,0,1,0}; // int q[]={0,1,0,-1}; // int p[]={-1,-1,-1,0,0,1,1,1}; // int q[]={-1,0,1,-1,1,-1,0,1}; point add(point a, point b) { return {a.fi + b.fi, a.se + b.se}; } point sub(point a, point b) { return {a.fi - b.fi, a.se - b.se}; } // double dist(point a,point b) //{ // return ((a.fi-b.fi)*(a.fi-b.fi) + (a.se-b.se)*(a.se-b.se)); // } double dist(point a, point b) { return sqrt((a.fi - b.fi) * (a.fi - b.fi) + (a.se - b.se) * (a.se - b.se)); } // double distPS(point a,segment b)//heron formula, extreemely prone to precison // error //{ // double AB=rdist(a,b.fi); // double AC=rdist(a,b.se); // double BC=rdist(b.fi,b.se); // double s=AB+AC+BC; // s/=2.0; // return 2*sqrt(s*(s-AB)*(s-BC)*(s-AC))/BC; // } double dabs(double a) { if (a + EPS > 0) return a; return -a; } double dotProduct(point a, point b) { return a.fi * b.fi + a.se * b.se; } double crossProduct(point a, point b) { return a.fi * b.se - a.se * b.fi; } int ccw(point a, point b, point c) // -1 CCW 0 COL 1 CW { double tmp = crossProduct(sub(b, a), sub(c, a)); if (tmp > EPS) { return -1; } else if (tmp < -EPS) { return 1; } else { return 0; } } point intLiLi(point a, point b, point c, point d) { return mp( ((a.fi * b.se - b.fi * a.se) * (c.fi - d.fi) - (a.fi - b.fi) * (c.fi * d.se - d.fi * c.se)) / ((a.fi - b.fi) * (c.se - d.se) - (a.se - b.se) * (c.fi - d.fi)), ((a.fi * b.se - b.fi * a.se) * (c.se - d.se) - (a.se - b.se) * (c.fi * d.se - d.fi * c.se)) / ((a.fi - b.fi) * (c.se - d.se) - (a.se - b.se) * (c.fi - d.fi))); } string s, t; int memo[3010][3010]; int dp(int i, int j) { if (i == s.size() || j == t.size()) return memo[i][j] = 0; if (memo[i][j] != -1) return memo[i][j]; if (s[i] == t[j]) return memo[i][j] = max(dp(i + 1, j + 1) + 1, max(dp(i + 1, j), dp(i, j + 1))); return memo[i][j] = max(dp(i + 1, j), dp(i, j + 1)); } void bt(int i, int j) { if (i == s.size() || j == t.size()) return; if (s[i] == t[j]) { // cerr<<memo[i+1][j+1] << " " <<memo[i][j+1] <<" "<< memo[i+1][j]<<endl; if (memo[i][j] == memo[i + 1][j + 1] + 1) { // cout<<"A"<<endl; cout << s[i]; bt(i + 1, j + 1); } else if (memo[i][j] == memo[i + 1][j]) { // cout<<"B"<<endl; bt(i + 1, j); } else { // cout<<"C"<<endl; bt(i, j + 1); } } else if (memo[i][j] == memo[i + 1][j]) { // cout<<"D"<<endl; // cerr<<memo[i][j+1] <<" "<< memo[i+1][j]<<endl; bt(i + 1, j); } else { // cout<<"E"<<endl; // cerr<<memo[i][j+1] <<" "<< memo[i+1][j]<<endl; bt(i, j + 1); } } int main() { memset(memo, -1, sizeof(memo)); cin >> s >> t; dp(0, 0); bt(0, 0); return 0; }
replace
164
165
164
165
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using lli = long long int; int main() { string s, t; int m, n; cin >> s >> t; m = s.size(); n = t.size(); int dp[m + 1][n + 1][2]; // last [2] here 0 stores value and 1 will store from // the valve came // 1 if it came from diagonal // 2 if it came from row // 3 if it came from column // when row and column are same and add 2 // we may store the the cordinates from where it came by using pair in vector int i, j; for (i = 0; i <= m; i++) dp[i][0][0] = 0; for (i = 0; i <= n; i++) dp[0][i][0] = 0; for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j][0] = dp[i - 1][j - 1][0] + 1; dp[i][j][1] = 1; } else { dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j - 1][0]); if (dp[i - 1][j][0] > dp[i][j - 1][0]) dp[i][j][1] = 3; else dp[i][j][1] = 2; } } } int len = dp[m][n][0]; char ans[len + 1]; j = n; for (i = m; i > 0;) { for (; j > 0;) { if (dp[i][j][1] == 1) { ans[len--] = t[j - 1]; i--; j--; } else if (dp[i][j][1] == 2) { j--; } else { i--; } } if (j <= 0) break; } len = dp[m][n][0]; for (i = 1; i <= len; i++) cout << ans[i]; return 0; }
#include <bits/stdc++.h> using namespace std; using lli = long long int; int main() { string s, t; int m, n; cin >> s >> t; m = s.size(); n = t.size(); int dp[m + 1][n + 1][2]; // last [2] here 0 stores value and 1 will store from // the valve came // 1 if it came from diagonal // 2 if it came from row // 3 if it came from column // when row and column are same and add 2 // we may store the the cordinates from where it came by using pair in vector int i, j; for (i = 0; i <= m; i++) dp[i][0][0] = 0; for (i = 0; i <= n; i++) dp[0][i][0] = 0; for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { if (s[i - 1] == t[j - 1]) { dp[i][j][0] = dp[i - 1][j - 1][0] + 1; dp[i][j][1] = 1; } else { dp[i][j][0] = max(dp[i - 1][j][0], dp[i][j - 1][0]); if (dp[i - 1][j][0] > dp[i][j - 1][0]) dp[i][j][1] = 3; else dp[i][j][1] = 2; } } } int len = dp[m][n][0]; char ans[len + 1]; j = n; for (i = m; i > 0;) { for (; j > 0;) { if (dp[i][j][1] == 1) { ans[len--] = t[j - 1]; i--; j--; } else if (dp[i][j][1] == 2) { j--; } else { i--; } if (i <= 0) break; } if (j <= 0) break; } len = dp[m][n][0]; for (i = 1; i <= len; i++) cout << ans[i]; return 0; }
insert
55
55
55
57
0
p03165
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long #define pll pair<ll, ll> #define F first #define S second #define pb push_back #define ld long double ll mod = 1000000007; string ans; ll func1(vector<vector<pll>> &dp, string s, string t, int a, int b) { if (a == 0 || b == 0) return 0; if (dp[a][b].F != -1) return dp[a][b].F; if (s[a - 1] == t[b - 1]) { // ans.pb(s[a-1]); dp[a][b].S = 1; return dp[a][b].F = 1 + func1(dp, s, t, a - 1, b - 1); } ll x = func1(dp, s, t, a - 1, b); ll y = func1(dp, s, t, a, b - 1); if (x >= y) { dp[a][b].S = 2; return dp[a][b].F = x; } dp[a][b].S = 0; return dp[a][b].F = y; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t = 1; // cin>>t; while (t--) { string s, t; cin >> s >> t; vector<vector<pll>> dp(s.size() + 1, vector<pll>(t.size() + 1, {-1, -1})); ll tp = func1(dp, s, t, s.size(), t.size()); ll x = dp[s.size()][t.size()].S; ll i = s.size(), j = t.size(); while (i * j != 0) { if (x == 1) { ans.pb(s[i - 1]); i--; j--; } if (x == 0) j--; else if (x == 2) i--; x = dp[i][j].S; } reverse(ans.begin(), ans.end()); cout << ans; } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define pll pair<ll, ll> #define F first #define S second #define pb push_back #define ld long double ll mod = 1000000007; string ans; ll func1(vector<vector<pll>> &dp, string &s, string &t, int a, int b) { if (a == 0 || b == 0) return 0; if (dp[a][b].F != -1) return dp[a][b].F; if (s[a - 1] == t[b - 1]) { // ans.pb(s[a-1]); dp[a][b].S = 1; return dp[a][b].F = 1 + func1(dp, s, t, a - 1, b - 1); } ll x = func1(dp, s, t, a - 1, b); ll y = func1(dp, s, t, a, b - 1); if (x >= y) { dp[a][b].S = 2; return dp[a][b].F = x; } dp[a][b].S = 0; return dp[a][b].F = y; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t = 1; // cin>>t; while (t--) { string s, t; cin >> s >> t; vector<vector<pll>> dp(s.size() + 1, vector<pll>(t.size() + 1, {-1, -1})); ll tp = func1(dp, s, t, s.size(), t.size()); ll x = dp[s.size()][t.size()].S; ll i = s.size(), j = t.size(); while (i * j != 0) { if (x == 1) { ans.pb(s[i - 1]); i--; j--; } if (x == 0) j--; else if (x == 2) i--; x = dp[i][j].S; } reverse(ans.begin(), ans.end()); cout << ans; } }
replace
12
13
12
13
TLE
p03165
C++
Time Limit Exceeded
/****************************************** * AUTHOR : RAJAGOPALAN * * NICK : ARNO * * INSTITUTION : VIT * ******************************************/ #include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define alli(a, n, k) (a + k), (a + n + k) #define REP(i, a, b, k) for (__typeof(a) i = a; i < b; i += k) #define REPI(i, a, b, k) for (__typeof(a) i = a; i > b; i -= k) #define REPITER(it, a) \ for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) #define eps 1e-6 #define pi 3.141592653589793 using namespace std; template <class T> inline T gcd(T x, T y) { if (!y) return x; return gcd(y, x % y); } typedef vector<int> VII; typedef vector<ll> VLL; typedef pair<int, int> PII; typedef vector<pair<int, int>> VPII; typedef vector<pair<int, PII>> VPPI; const int MOD = 1e9 + 7; const int INF = 1e9; string LCS(string s, string t, int i, int j, string res) { if (i >= s.size() || j >= t.size()) return res; string a1, a2, a3; if (s[i] == t[j]) { a1 = LCS(s, t, i + 1, j + 1, res + s[i]); } else { a2 = LCS(s, t, i + 1, j, res); a3 = LCS(s, t, i, j + 1, res); } if (a1.size() >= a2.size() && a1.size() >= a3.size()) return a1; else if (a2.size() >= a1.size() && a2.size() >= a3.size()) return a2; else return a3; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; cout << LCS(s, t, 0, 0, ""); return 0; }
/****************************************** * AUTHOR : RAJAGOPALAN * * NICK : ARNO * * INSTITUTION : VIT * ******************************************/ #include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define alli(a, n, k) (a + k), (a + n + k) #define REP(i, a, b, k) for (__typeof(a) i = a; i < b; i += k) #define REPI(i, a, b, k) for (__typeof(a) i = a; i > b; i -= k) #define REPITER(it, a) \ for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) #define eps 1e-6 #define pi 3.141592653589793 using namespace std; template <class T> inline T gcd(T x, T y) { if (!y) return x; return gcd(y, x % y); } typedef vector<int> VII; typedef vector<ll> VLL; typedef pair<int, int> PII; typedef vector<pair<int, int>> VPII; typedef vector<pair<int, PII>> VPPI; const int MOD = 1e9 + 7; const int INF = 1e9; string LCS(string s, string t, int i, int j, string res) { if (i >= s.size() || j >= t.size()) return res; string a1, a2, a3; if (s[i] == t[j]) { a1 = LCS(s, t, i + 1, j + 1, res + s[i]); } else { a2 = LCS(s, t, i + 1, j, res); a3 = LCS(s, t, i, j + 1, res); } if (a1.size() >= a2.size() && a1.size() >= a3.size()) return a1; else if (a2.size() >= a1.size() && a2.size() >= a3.size()) return a2; else return a3; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; int u = s.size(), v = t.size(); int dp[u + 1][v + 1]; for (int i = 0; i <= u; ++i) { for (int j = 0; j <= v; ++j) { if (i == 0 || j == 0) dp[i][j] = 0; else if (s[i - 1] == t[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } string ans = ""; int i = u, j = v; while (i > 0 && j > 0) { if (s[i - 1] == t[j - 1]) { ans += 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; return 0; }
replace
57
58
57
85
TLE
p03165
C++
Runtime Error
/* _oo0oo_ o8888888o 88" . "88 (| -_- |) 0\ = /0 ___/`---'\___ .' \\| |// '. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' |_/ | \ .-\__ '-' ___/-. / ___'. .' /--.--\ `. .'___ ."" '< `.___\_<|>_/___.' >' "". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `_. \_ __\ /__ _/ .-` / / =====`-.____`.___ \_____/___.-`___.-'===== `=---=' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Vikash Kumar @ Codechef/codeforces ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ #include <bits/stdc++.h> #define ll long long int #define ll_MAX LLONG_MAX #define ll_MIN LLONG_MIN #define loop(i, a, b) for (int i = (int)a; i < (int)b; ++i) #define rloop(i, a, b) for (int i = (int)a; i <= (int)b; ++i) #define loopl(i, a, b) for (ll i = (ll)a; i < (ll)b; ++i) #define loopr(i, a, b) for (int i = (int)a; i >= (int)b; --i) #define count_1(n) __builtin_popcountll(n) #define pb push_back #define eb emplace_back #define ab(a) (a < 0) ? (-1 * a) : a #define mset(a, b, c) loop(i, 0, b) a[i] = c #define F first #define S second #define clr(x) x.clear() #define MOD 1000000007 #define MAX 1e9 #define MIN -1e9 #define itoc(c) ((char)(((int)'0') + c)) #define vi vector<int> #define vvi vector<vi> #define pll pair<ll, ll> #define pii pair<int, int> #define all(p) p.begin(), p.end() #define mid(s, e) (s + (e - s) / 2) #define mini INT_MIN const int MX = 10010896; const int lmt = 3164; const int N = 10000001; int flag[MX >> 6]; #define ifc(i) (flag[i >> 6] & (1 << ((i >> 1) & 31))) #define isc(i) (flag[i >> 6] |= (1 << ((i >> 1) & 31))) #define chk(n) (n == 2 || (n > 2 && (n & 1) && !ifc(n))) #define Baby_Run \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) using namespace std; 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; } void sieve() { int i, j, k; for (i = 3; i < lmt; i += 2) if (!ifc(i)) for (j = i * i, k = i << 1; j < MX; j += k) isc(j); } // const ll INF = 1e18L + 5; int lcs[3009][3009]; inline void solve() { /*_Start_*/ string s, t; cin >> s >> t; int m = s.size(), n = t.size(); for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) lcs[i][j] = 0; else { if (s[i - 1] == t[j - 1]) lcs[i][j] = lcs[i - 1][j - 1] + 1; else lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]); } } } string ans = ""; int i = m, j = n; while (i > 0 and j > 0) { if (s[i - 1] == t[j - 1]) { ans += s[i - 1]; i--; j--; } else if (lcs[i][j - 1] > lcs[i - 1][j]) j--; else i--; } reverse(ans.begin(), ans.end()); cout << ans << endl; /*_End_*/ } int main() { cout << fixed << setprecision(12); // freopen("input.txt", "r", stdin); Baby_Run; ll t = 1; //,tc=1; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif // cin>>t; while (t--) { solve(); } return 0; }
/* _oo0oo_ o8888888o 88" . "88 (| -_- |) 0\ = /0 ___/`---'\___ .' \\| |// '. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' |_/ | \ .-\__ '-' ___/-. / ___'. .' /--.--\ `. .'___ ."" '< `.___\_<|>_/___.' >' "". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `_. \_ __\ /__ _/ .-` / / =====`-.____`.___ \_____/___.-`___.-'===== `=---=' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Vikash Kumar @ Codechef/codeforces ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ #include <bits/stdc++.h> #define ll long long int #define ll_MAX LLONG_MAX #define ll_MIN LLONG_MIN #define loop(i, a, b) for (int i = (int)a; i < (int)b; ++i) #define rloop(i, a, b) for (int i = (int)a; i <= (int)b; ++i) #define loopl(i, a, b) for (ll i = (ll)a; i < (ll)b; ++i) #define loopr(i, a, b) for (int i = (int)a; i >= (int)b; --i) #define count_1(n) __builtin_popcountll(n) #define pb push_back #define eb emplace_back #define ab(a) (a < 0) ? (-1 * a) : a #define mset(a, b, c) loop(i, 0, b) a[i] = c #define F first #define S second #define clr(x) x.clear() #define MOD 1000000007 #define MAX 1e9 #define MIN -1e9 #define itoc(c) ((char)(((int)'0') + c)) #define vi vector<int> #define vvi vector<vi> #define pll pair<ll, ll> #define pii pair<int, int> #define all(p) p.begin(), p.end() #define mid(s, e) (s + (e - s) / 2) #define mini INT_MIN const int MX = 10010896; const int lmt = 3164; const int N = 10000001; int flag[MX >> 6]; #define ifc(i) (flag[i >> 6] & (1 << ((i >> 1) & 31))) #define isc(i) (flag[i >> 6] |= (1 << ((i >> 1) & 31))) #define chk(n) (n == 2 || (n > 2 && (n & 1) && !ifc(n))) #define Baby_Run \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) using namespace std; 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; } void sieve() { int i, j, k; for (i = 3; i < lmt; i += 2) if (!ifc(i)) for (j = i * i, k = i << 1; j < MX; j += k) isc(j); } // const ll INF = 1e18L + 5; int lcs[3009][3009]; inline void solve() { /*_Start_*/ string s, t; cin >> s >> t; int m = s.size(), n = t.size(); for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) lcs[i][j] = 0; else { if (s[i - 1] == t[j - 1]) lcs[i][j] = lcs[i - 1][j - 1] + 1; else lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]); } } } string ans = ""; int i = m, j = n; while (i > 0 and j > 0) { if (s[i - 1] == t[j - 1]) { ans += s[i - 1]; i--; j--; } else if (lcs[i][j - 1] > lcs[i - 1][j]) j--; else i--; } reverse(ans.begin(), ans.end()); cout << ans << endl; /*_End_*/ } int main() { cout << fixed << setprecision(12); // freopen("input.txt", "r", stdin); Baby_Run; ll t = 1; //,tc=1; // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // #endif // cin>>t; while (t--) { solve(); } return 0; }
replace
135
138
135
138
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; #define int long long int #define pb push_back #define fi(n) for (int i = 0; i < n; i++) #define fi1(n) for (int i = 1; i < n; i++) #define fj(n) for (int j = 0; j < n; j++) #define fj1(n) for (int j = 1; j < n; j++) #define rep(z, a, n) for (z = a; z < n; z++) #define rep2(z, a, n) for (z = a; z < n; z += 2) #define clr(x) memset(x, 0, sizeof(x)) #define print(x, n) \ for (int i = 0; i < n; i++) \ cout << x[i] << " "; \ cout << "\n" #define nl "\n" #define vi vector<int> #define mp make_pair #define pairi pair<int, int> #define vpairi vector<pair<int, int>> #define ff first #define ss second #define inf 2e18 #define input \ "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//input.txt" #define output \ "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//output.txt" #define error \ "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//error.txt" int ctoi(char a) { int x = a - 48; return x; } string itos(int a) { string out_string; stringstream ss; ss << a; out_string = ss.str(); return out_string; } char itoc(int a) { return itos(a)[0]; } int pow(int e, int x) { int ans = 1; while (x > 0) { if (x & 1) ans *= e; e *= e; x >>= 1; } return ans; } string bin(int x) { bitset<sizeof(1) * CHAR_BIT> bits(x); string b = bits.to_string(); return b; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen(input, "r", stdin); freopen(output, "w", stdout); freopen(error, "w", stderr); #endif int T = 1; // cin>>T; while (T--) { int n, k, m, x, y; // cin>>n; // int a[n];clr(a); string a, b; cin >> a >> b; n = a.length(); m = b.length(); int ans[n + 1][m + 1]; clr(ans); fi1(n + 1) { fj1(m + 1) { if (a[i - 1] == b[j - 1]) { ans[i][j] = ans[i - 1][j - 1] + 1; } else { ans[i][j] = max(ans[i - 1][j], ans[i][j - 1]); } } } // cout<<ans[n][m]<<nl; list<char> lcs; int i = n, j = m; while (i >= 1 && j >= 1) { if (a[i - 1] == b[j - 1]) { lcs.push_front(a[i - 1]); i--; j--; } else { if (ans[i - 1][j] > ans[i][j - 1]) i--; else j--; } } for (auto x : lcs) cout << x; cout << nl; } return 0; }
#include <bits/stdc++.h> #include <iostream> using namespace std; #define int long long int #define pb push_back #define fi(n) for (int i = 0; i < n; i++) #define fi1(n) for (int i = 1; i < n; i++) #define fj(n) for (int j = 0; j < n; j++) #define fj1(n) for (int j = 1; j < n; j++) #define rep(z, a, n) for (z = a; z < n; z++) #define rep2(z, a, n) for (z = a; z < n; z += 2) #define clr(x) memset(x, 0, sizeof(x)) #define print(x, n) \ for (int i = 0; i < n; i++) \ cout << x[i] << " "; \ cout << "\n" #define nl "\n" #define vi vector<int> #define mp make_pair #define pairi pair<int, int> #define vpairi vector<pair<int, int>> #define ff first #define ss second #define inf 2e18 #define input \ "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//input.txt" #define output \ "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//output.txt" #define error \ "C://Users//Piyush Aggarwal//Documents//Cpp files//input-output//error.txt" int ctoi(char a) { int x = a - 48; return x; } string itos(int a) { string out_string; stringstream ss; ss << a; out_string = ss.str(); return out_string; } char itoc(int a) { return itos(a)[0]; } int pow(int e, int x) { int ans = 1; while (x > 0) { if (x & 1) ans *= e; e *= e; x >>= 1; } return ans; } string bin(int x) { bitset<sizeof(1) * CHAR_BIT> bits(x); string b = bits.to_string(); return b; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE #endif int T = 1; // cin>>T; while (T--) { int n, k, m, x, y; // cin>>n; // int a[n];clr(a); string a, b; cin >> a >> b; n = a.length(); m = b.length(); int ans[n + 1][m + 1]; clr(ans); fi1(n + 1) { fj1(m + 1) { if (a[i - 1] == b[j - 1]) { ans[i][j] = ans[i - 1][j - 1] + 1; } else { ans[i][j] = max(ans[i - 1][j], ans[i][j - 1]); } } } // cout<<ans[n][m]<<nl; list<char> lcs; int i = n, j = m; while (i >= 1 && j >= 1) { if (a[i - 1] == b[j - 1]) { lcs.push_front(a[i - 1]); i--; j--; } else { if (ans[i - 1][j] > ans[i][j - 1]) i--; else j--; } } for (auto x : lcs) cout << x; cout << nl; } return 0; }
delete
64
67
64
64
0
p03165
C++
Runtime Error
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #define N 3000 int flag[N][N], dp[N][N]; char s1[N], s2[N]; void LCS() { memset(dp, 0, sizeof(dp)); memset(flag, 0, sizeof(flag)); int n = strlen(s1), m = strlen(s2); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1, flag[i][j] = 0; else if (dp[i - 1][j] >= dp[i][j - 1]) dp[i][j] = dp[i - 1][j], flag[i][j] = 1; else dp[i][j] = dp[i][j - 1], flag[i][j] = -1; } } void PrintLCS(int i, int j) { if (i == 0 || j == 0) return; if (flag[i][j] == 0) { PrintLCS(i - 1, j - 1); std::cout << s1[i - 1]; } else if (flag[i][j] == 1) PrintLCS(i - 1, j); else PrintLCS(i, j - 1); } int main() { std::cin >> s1 >> s2; LCS(); PrintLCS(strlen(s1), strlen(s2)); return 0; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #define N 3001 int flag[N][N], dp[N][N]; char s1[N], s2[N]; void LCS() { memset(dp, 0, sizeof(dp)); memset(flag, 0, sizeof(flag)); int n = strlen(s1), m = strlen(s2); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1, flag[i][j] = 0; else if (dp[i - 1][j] >= dp[i][j - 1]) dp[i][j] = dp[i - 1][j], flag[i][j] = 1; else dp[i][j] = dp[i][j - 1], flag[i][j] = -1; } } void PrintLCS(int i, int j) { if (i == 0 || j == 0) return; if (flag[i][j] == 0) { PrintLCS(i - 1, j - 1); std::cout << s1[i - 1]; } else if (flag[i][j] == 1) PrintLCS(i - 1, j); else PrintLCS(i, j - 1); } int main() { std::cin >> s1 >> s2; LCS(); PrintLCS(strlen(s1), strlen(s2)); return 0; }
replace
4
5
4
5
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int dp[3400][3400]; int main() { string s, t; getline(cin, s); getline(cin, t); if (s.size() < t.size()) s.swap(t); for (int i = 0; i < s.size() + 1; ++i) { for (int j = 0; j < t.size() + 1; ++j) { if (i == 0 || j == 0) { dp[i][j] = 0; } else { if (s[i - 1] == t[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int a = s.size(); int b = t.size(); string ans; while (1) { if (a == 0 && b == 0) break; if (s[a - 1] == t[b - 1]) { ans = ans + s[a - 1]; a = a - 1; b = b - 1; } else { if (dp[a - 1][b] == dp[a][b]) a = a - 1; else b = b - 1; } } reverse(ans.begin(), ans.end()); cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[3400][3400]; int main() { string s, t; getline(cin, s); getline(cin, t); if (s.size() < t.size()) s.swap(t); for (int i = 0; i < s.size() + 1; ++i) { for (int j = 0; j < t.size() + 1; ++j) { if (i == 0 || j == 0) { dp[i][j] = 0; } else { if (s[i - 1] == t[j - 1]) dp[i][j] = 1 + dp[i - 1][j - 1]; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } int a = s.size(); int b = t.size(); string ans; while (1) { if (a == 0 || b == 0) break; if (s[a - 1] == t[b - 1]) { ans = ans + s[a - 1]; a = a - 1; b = b - 1; } else { if (dp[a - 1][b] == dp[a][b]) a = a - 1; else b = b - 1; } } reverse(ans.begin(), ans.end()); cout << ans << "\n"; return 0; }
replace
28
29
28
29
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> #define endl '\n' using namespace std; int dp[1001][1001]; int lcs(string a, string b, int m, int n) { if (m == 0 || n == 0) return 0; if (dp[m][n] != -1) return dp[m][n]; if (a[m - 1] == b[n - 1]) return dp[m][n] = 1 + lcs(a, b, m - 1, n - 1); return dp[m][n] = max(lcs(a, b, m, n - 1), lcs(a, b, m - 1, n)); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int m, n; string a, b; getline(cin, a); getline(cin, b); m = a.length(); n = b.length(); for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = -1; } } for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else if (a[i - 1] == b[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int i = m, j = n; string ans = ""; while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { ans += a[i - 1]; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define endl '\n' using namespace std; int dp[3001][3001]; int lcs(string a, string b, int m, int n) { if (m == 0 || n == 0) return 0; if (dp[m][n] != -1) return dp[m][n]; if (a[m - 1] == b[n - 1]) return dp[m][n] = 1 + lcs(a, b, m - 1, n - 1); return dp[m][n] = max(lcs(a, b, m, n - 1), lcs(a, b, m - 1, n)); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int m, n; string a, b; getline(cin, a); getline(cin, b); m = a.length(); n = b.length(); for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = -1; } } for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) dp[i][j] = 0; else if (a[i - 1] == b[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } int i = m, j = n; string ans = ""; while (i > 0 && j > 0) { if (a[i - 1] == b[j - 1]) { ans += a[i - 1]; i--; j--; } else if (dp[i - 1][j] > dp[i][j - 1]) i--; else j--; } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
replace
3
4
3
4
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define mod 1000000007 #define INF (1 << 29) #define MAX_N 1000010 /* ちゃんと考えてわかって実装 */ int main(void) { string s, t; cin >> s >> t; int dp[3000][3000]; for (int i = 0; i <= s.size(); i++) dp[0][i] = 0; for (int i = 0; i <= t.size(); i++) dp[i][0] = 0; 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]); } } } int i = s.size(); int j = t.size(); string ans = ""; if (dp[i][j] == 0) string ans = " "; while (i > 0 && j > 0) { if (dp[i - 1][j] == dp[i][j]) { i--; } else if (dp[i][j - 1] == dp[i][j]) { j--; } else { ans = s[i - 1] + ans; i--; j--; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define mod 1000000007 #define INF (1 << 29) #define MAX_N 1000010 /* ちゃんと考えてわかって実装 */ int main(void) { string s, t; cin >> s >> t; int dp[3001][3001]; for (int i = 0; i <= s.size(); i++) dp[0][i] = 0; for (int i = 0; i <= t.size(); i++) dp[i][0] = 0; 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]); } } } int i = s.size(); int j = t.size(); string ans = ""; if (dp[i][j] == 0) string ans = " "; while (i > 0 && j > 0) { if (dp[i - 1][j] == dp[i][j]) { i--; } else if (dp[i][j - 1] == dp[i][j]) { j--; } else { ans = s[i - 1] + ans; i--; j--; } } cout << ans << endl; return 0; }
replace
12
13
12
13
-11
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int lookup[20][20]; string str = ""; void LCS(string p, string q, int m, int n) { if (m == 0 || n == 0) return; if (p[m - 1] == q[n - 1]) { str += p[m - 1]; return LCS(p, q, m - 1, n - 1); } if (lookup[m - 1][n] > lookup[m][n - 1]) return LCS(p, q, m - 1, n); else return LCS(p, q, m, n - 1); } void LCS_length(string p, string q, int m, int n) { for (int i = 0; i <= m; i++) lookup[i][0] = 0; for (int j = 0; j <= n; j++) lookup[0][j] = 0; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (p[i - 1] == q[j - 1]) lookup[i][j] = lookup[i - 1][j - 1] + 1; else lookup[i][j] = max(lookup[i - 1][j], lookup[i][j - 1]); } } } int main() { string X; string Y; cin >> X >> Y; // printf("Longest Common Subsequence is // %d\n",LCS_length(X,Y,X.length(),Y.length())); LCS_length(X, Y, X.length(), Y.length()); LCS(X, Y, X.length(), Y.length()); for (int i = str.length() - 1; i >= 0; i--) cout << str[i]; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int lookup[3005][3005]; string str = ""; void LCS(string p, string q, int m, int n) { if (m == 0 || n == 0) return; if (p[m - 1] == q[n - 1]) { str += p[m - 1]; return LCS(p, q, m - 1, n - 1); } if (lookup[m - 1][n] > lookup[m][n - 1]) return LCS(p, q, m - 1, n); else return LCS(p, q, m, n - 1); } void LCS_length(string p, string q, int m, int n) { for (int i = 0; i <= m; i++) lookup[i][0] = 0; for (int j = 0; j <= n; j++) lookup[0][j] = 0; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (p[i - 1] == q[j - 1]) lookup[i][j] = lookup[i - 1][j - 1] + 1; else lookup[i][j] = max(lookup[i - 1][j], lookup[i][j - 1]); } } } int main() { string X; string Y; cin >> X >> Y; // printf("Longest Common Subsequence is // %d\n",LCS_length(X,Y,X.length(),Y.length())); LCS_length(X, Y, X.length(), Y.length()); LCS(X, Y, X.length(), Y.length()); for (int i = str.length() - 1; i >= 0; i--) cout << str[i]; cout << endl; return 0; }
replace
4
5
4
5
0
p03165
C++
Runtime Error
#define ll long long int #include <bits/stdc++.h> // #include <fstream> using namespace std; #define ff first #define ss second #define mp make_pair #define pb push_back #define pf push_front #define vi vector<ll> #define pii pair<ll, ll> #define qi queue<ll> #define mem(a, b) memset(a, b, sizeof(a)) #define print(a) cout << a << '\n' #define sorted(arr) sort(arr.begin(), arr.end()) #define gcd(a, b) gcd((a), (b)) #define lcm(a, b) ((a) * (b)) / gcd((a), (b)) #define all(v) v.begin(), v.end() #define read(a) scanf("%d", &a) #define f(a, n) for (ll i = a; i < n; i++) #define in2(n, m, arr) \ for (ll i = 0; i < n; i++) { \ for (ll j = 0; j < m; j++) \ cin >> arr[i][j]; \ } #define debug(a) cerr << a << " "; #define debug1(a) cerr << a << '\n' /*typedef tree<int, null_type, less<int>, rb_tree_tag , * tree_order_statistics_node_update> Set;*/ const unsigned ll mod = 1e9 + 7; const ll inf = (ll)1e18 + 2; const ll N = (ll)1e6 + 2; // // template<typename... T> // void write(T&... args) //{ // ((cout << args << ' '), ...); //} // ll power ( ll x , ll y) //{ // ll res = 1; // while (y > 0) // { // if (y & 1) // res = (res * x) ; // y = y >> 1; // x = (x * x) ; // } // return res ; //} // // struct node{ // ll x , y , z; //}; // map<pair<ll, ll>,ll> c; // vector<set<ll>> tree; // vector<ll> ar ; // set<ll> s; // ll get(ll x) //{ // if(x == ar[x]) // return x; // return ar[x] = get(ar[x]); //} // void merge(ll a ,ll b) //{ // a = get(a); // b = get(b); // if(a == b) // { // return ; // } // ar[b] = ar[a]; // } // // ll sev[100006]; // vector<ll> pri; // void seive() //{ // for(ll i = 2 ; i < 100006 ; i ++) // { // if(!sev[i]) // { // for(ll j = i ; j < 100006 ; j += i) // { // if(!sev[j]) // sev[j] = i; // } // } // if(sev[i] == i) // pri.pb(i); // } //} void solve() { string s1, s2; cin >> s1 >> s2; ll n = s1.size(), m = s2.size(); ll arr[n + 1][m + 1]; mem(arr, 0); ll mx = 0; for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= m; j++) { if (s1[i - 1] == s2[j - 1]) { arr[i][j] = arr[i - 1][j - 1] + 1; } arr[i][j] = max({arr[i][j], arr[i - 1][j], arr[i][j - 1]}); mx = max(mx, arr[i][j]); // cout << arr[i][j] << ' '; } // cout << '\n'; } ll l = n, r = m; // cout << mx << '\n'; string ans = ""; while (l > 0 && r > 0) { while (arr[l][r] == mx) r--; r++; ans += s2[r - 1]; l--, r--, mx--; } reverse(all(ans)); print(ans); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t; t = 1; // cin >> t; // seive(); while (t--) { solve(); } cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
#define ll long long int #include <bits/stdc++.h> // #include <fstream> using namespace std; #define ff first #define ss second #define mp make_pair #define pb push_back #define pf push_front #define vi vector<ll> #define pii pair<ll, ll> #define qi queue<ll> #define mem(a, b) memset(a, b, sizeof(a)) #define print(a) cout << a << '\n' #define sorted(arr) sort(arr.begin(), arr.end()) #define gcd(a, b) gcd((a), (b)) #define lcm(a, b) ((a) * (b)) / gcd((a), (b)) #define all(v) v.begin(), v.end() #define read(a) scanf("%d", &a) #define f(a, n) for (ll i = a; i < n; i++) #define in2(n, m, arr) \ for (ll i = 0; i < n; i++) { \ for (ll j = 0; j < m; j++) \ cin >> arr[i][j]; \ } #define debug(a) cerr << a << " "; #define debug1(a) cerr << a << '\n' /*typedef tree<int, null_type, less<int>, rb_tree_tag , * tree_order_statistics_node_update> Set;*/ const unsigned ll mod = 1e9 + 7; const ll inf = (ll)1e18 + 2; const ll N = (ll)1e6 + 2; // // template<typename... T> // void write(T&... args) //{ // ((cout << args << ' '), ...); //} // ll power ( ll x , ll y) //{ // ll res = 1; // while (y > 0) // { // if (y & 1) // res = (res * x) ; // y = y >> 1; // x = (x * x) ; // } // return res ; //} // // struct node{ // ll x , y , z; //}; // map<pair<ll, ll>,ll> c; // vector<set<ll>> tree; // vector<ll> ar ; // set<ll> s; // ll get(ll x) //{ // if(x == ar[x]) // return x; // return ar[x] = get(ar[x]); //} // void merge(ll a ,ll b) //{ // a = get(a); // b = get(b); // if(a == b) // { // return ; // } // ar[b] = ar[a]; // } // // ll sev[100006]; // vector<ll> pri; // void seive() //{ // for(ll i = 2 ; i < 100006 ; i ++) // { // if(!sev[i]) // { // for(ll j = i ; j < 100006 ; j += i) // { // if(!sev[j]) // sev[j] = i; // } // } // if(sev[i] == i) // pri.pb(i); // } //} void solve() { string s1, s2; cin >> s1 >> s2; ll n = s1.size(), m = s2.size(); ll arr[n + 1][m + 1]; mem(arr, 0); ll mx = 0; for (ll i = 1; i <= n; i++) { for (ll j = 1; j <= m; j++) { if (s1[i - 1] == s2[j - 1]) { arr[i][j] = arr[i - 1][j - 1] + 1; } arr[i][j] = max({arr[i][j], arr[i - 1][j], arr[i][j - 1]}); mx = max(mx, arr[i][j]); // cout << arr[i][j] << ' '; } // cout << '\n'; } ll l = n, r = m; // cout << mx << '\n'; string ans = ""; while (l > 0 && r > 0 && mx > 0) { while (arr[l][r] == mx) l--; l++; while (arr[l][r] == mx) r--; r++; ans += s2[r - 1]; l--, r--, mx--; } reverse(all(ans)); print(ans); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t; t = 1; // cin >> t; // seive(); while (t--) { solve(); } cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
replace
118
119
118
122
0
Time elapsed: 34ms
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; void solve() { string s, t; cin >> s >> t; int n = s.size(), m = t.size(); int arr[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if ((i == 0) || (j == 0)) arr[i][j] = 0; else { if (s[i - 1] == t[j - 1]) { arr[i][j] = arr[i - 1][j - 1] + 1; } else { arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]); } } } } // cout<<arr[n][m]; char backtrack[arr[n][m]]; int z = arr[n][m] - 1, count1 = 0; int a = n, b = m; while (count1 < arr[n][m]) { if (arr[a][b] != max(arr[a - 1][b], arr[a][b - 1])) { backtrack[z] = s[a - 1]; z--; a--; b--; count1++; } else { if (arr[a][b] == arr[a - 1][b]) { a--; } else { b--; } } } for (int i = 0; i < arr[n][m]; i++) { cout << backtrack[i]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("/home/ubuntu/.config/sublime-text-3/Packages/User/input.txt", "r", stdin); freopen("/home/ubuntu/.config/sublime-text-3/Packages/User/output.txt", "w", stdout); #endif solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; void solve() { string s, t; cin >> s >> t; int n = s.size(), m = t.size(); int arr[n + 1][m + 1]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if ((i == 0) || (j == 0)) arr[i][j] = 0; else { if (s[i - 1] == t[j - 1]) { arr[i][j] = arr[i - 1][j - 1] + 1; } else { arr[i][j] = max(arr[i - 1][j], arr[i][j - 1]); } } } } // cout<<arr[n][m]; char backtrack[arr[n][m]]; int z = arr[n][m] - 1, count1 = 0; int a = n, b = m; while (count1 < arr[n][m]) { if (arr[a][b] != max(arr[a - 1][b], arr[a][b - 1])) { backtrack[z] = s[a - 1]; z--; a--; b--; count1++; } else { if (arr[a][b] == arr[a - 1][b]) { a--; } else { b--; } } } for (int i = 0; i < arr[n][m]; i++) { cout << backtrack[i]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); }
delete
49
55
49
49
0
p03165
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define FASTIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define fr(a, b) for (long long i = a; i < b; i++) #define vi vector<ll> const ll MAX = 1e3; const ll mod = 1e9 + 7; const ll INF = 1e18 + 12; set<pair<ll, ll>> ss; ll dp[MAX][MAX]; // SAMEER KUMAR SINGH// // ================================================================================================================================// // ================================================================================================================================// // ================================================================================================================================// int fun(string s1, string s2, ll n1, ll n2) { if (n1 < 0 || n2 < 0) return 0; if (dp[n1][n2] != 0) return dp[n1][n2]; if (s1[n1] == s2[n2]) { dp[n1][n2] = 1 + fun(s1, s2, n1 - 1, n2 - 1); return dp[n1][n2]; } else { ll k1 = fun(s1, s2, n1 - 1, n2); ll k2 = fun(s1, s2, n1, n2 - 1); dp[n1][n2] = max(k1, k2); return dp[n1][n2]; } } int main() { FASTIO; ll tt = 1; // cin>>tt; while (tt--) { // ll n1,n2;cin>>n1>>n2; string s1, s2; cin >> s1 >> s2; for (ll i = 1; i <= s1.size(); i++) { for (ll 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]); } } } vector<char> vv; ll i = s1.size(); ll j = s2.size(); while (i > 0 && j > 0) { if (s1[i - 1] == s2[j - 1]) { vv.push_back(s1[i - 1]); i--; j--; } else if (dp[i - 1][j] >= dp[i][j - 1]) { i--; } else { j--; } } reverse(vv.begin(), vv.end()); for (auto x : vv) cout << x; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define FASTIO \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define fr(a, b) for (long long i = a; i < b; i++) #define vi vector<ll> const ll MAX = 1e4; const ll mod = 1e9 + 7; const ll INF = 1e18 + 12; set<pair<ll, ll>> ss; ll dp[MAX][MAX]; // SAMEER KUMAR SINGH// // ================================================================================================================================// // ================================================================================================================================// // ================================================================================================================================// int fun(string s1, string s2, ll n1, ll n2) { if (n1 < 0 || n2 < 0) return 0; if (dp[n1][n2] != 0) return dp[n1][n2]; if (s1[n1] == s2[n2]) { dp[n1][n2] = 1 + fun(s1, s2, n1 - 1, n2 - 1); return dp[n1][n2]; } else { ll k1 = fun(s1, s2, n1 - 1, n2); ll k2 = fun(s1, s2, n1, n2 - 1); dp[n1][n2] = max(k1, k2); return dp[n1][n2]; } } int main() { FASTIO; ll tt = 1; // cin>>tt; while (tt--) { // ll n1,n2;cin>>n1>>n2; string s1, s2; cin >> s1 >> s2; for (ll i = 1; i <= s1.size(); i++) { for (ll 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]); } } } vector<char> vv; ll i = s1.size(); ll j = s2.size(); while (i > 0 && j > 0) { if (s1[i - 1] == s2[j - 1]) { vv.push_back(s1[i - 1]); i--; j--; } else if (dp[i - 1][j] >= dp[i][j - 1]) { i--; } else { j--; } } reverse(vv.begin(), vv.end()); for (auto x : vv) cout << x; } return 0; }
replace
9
10
9
10
0
p03165
C++
Time Limit Exceeded
/*********************************** *******AUTHOR SHASHI KANT ********** ****GMAIL [email protected]* ************************************ ***********************************/ #include <bits/stdc++.h> #define ll long long int using namespace std; /*class graph { map<int,list<int>>adj; public : void addedge(int a, int b) { adj[a].push_back(b); adj[b].push_back(a); } }*/ // vector<vector<int>>dp(1000000); int dp[3005][3005]; string lcsString(string &str1, string &str2, int len) { string LCS; int i = 0; int j = 0; while (len > 0) { if (str1[i] == str2[j]) { LCS.push_back(str1[i]); i++; j++; len--; } else { if (dp[i + 1][j] > dp[i][j + 1]) i++; else j++; } } return LCS; } int lcs(string str1, string str2, int i, int j) { if (i >= str1.size() || j >= str2.size()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (str1[i] == str2[j]) { dp[i][j] = 1 + lcs(str1, str2, i + 1, j + 1); return dp[i][j]; } else { int l1 = lcs(str1, str2, i + 1, j); int l2 = lcs(str1, str2, i, j + 1); dp[i][j] = max(l1, l2); return dp[i][j]; } } int main() { string str1, str2; cin >> str1 >> str2; int n = str1.size(); int m = str2.size(); memset(dp, -1, sizeof(dp)); int len = lcs(str1, str2, 0, 0); cout << lcsString(str1, str2, len); return 0; }
/*********************************** *******AUTHOR SHASHI KANT ********** ****GMAIL [email protected]* ************************************ ***********************************/ #include <bits/stdc++.h> #define ll long long int using namespace std; /*class graph { map<int,list<int>>adj; public : void addedge(int a, int b) { adj[a].push_back(b); adj[b].push_back(a); } }*/ // vector<vector<int>>dp(1000000); int dp[3005][3005]; string lcsString(string &str1, string &str2, int len) { string LCS; int i = 0; int j = 0; while (len > 0) { if (str1[i] == str2[j]) { LCS.push_back(str1[i]); i++; j++; len--; } else { if (dp[i + 1][j] > dp[i][j + 1]) i++; else j++; } } return LCS; } int lcs(string &str1, string &str2, int i, int j) { if (i >= str1.size() || j >= str2.size()) return 0; if (dp[i][j] != -1) return dp[i][j]; if (str1[i] == str2[j]) { dp[i][j] = 1 + lcs(str1, str2, i + 1, j + 1); return dp[i][j]; } else { int l1 = lcs(str1, str2, i + 1, j); int l2 = lcs(str1, str2, i, j + 1); dp[i][j] = max(l1, l2); return dp[i][j]; } } int main() { string str1, str2; cin >> str1 >> str2; int n = str1.size(); int m = str2.size(); memset(dp, -1, sizeof(dp)); int len = lcs(str1, str2, 0, 0); cout << lcsString(str1, str2, len); return 0; }
replace
42
43
42
43
TLE