feat: new dp problem formatted
This commit is contained in:
23
longest-palindromic-subsequence/src/TLE.cpp
Normal file
23
longest-palindromic-subsequence/src/TLE.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int solve(const string& s) {
|
||||
if (s.length() == 0) return 0;
|
||||
if (s.length() == 1) return 1;
|
||||
|
||||
if (s.front() == s.back()) {
|
||||
return 2 + solve(s.substr(1, s.length() - 2));
|
||||
} else {
|
||||
return max(solve(s.substr(1)), solve(s.substr(0, s.length() - 1)));
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n; cin >> n;
|
||||
string s; cin >> s;
|
||||
|
||||
cout << solve(s) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
33
longest-palindromic-subsequence/src/ac.cpp
Normal file
33
longest-palindromic-subsequence/src/ac.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
int n; cin >> n;
|
||||
string s; cin >> s;
|
||||
|
||||
/*
|
||||
dp[i][j] says if the substring from i to j is a palindrome
|
||||
*/
|
||||
vector<vector<int>> dp(n, vector<int>(n, 0));
|
||||
for (int i = n - 1; i >= 0; i--)
|
||||
{
|
||||
dp[i][i] = 1;
|
||||
for (int j = i + 1; j < n; j++)
|
||||
{
|
||||
if (s[i] == s[j])
|
||||
{
|
||||
dp[i][j] = dp[i + 1][j - 1] + 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << dp[0][n - 1] << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
17
longest-palindromic-subsequence/src/checker.cpp
Normal file
17
longest-palindromic-subsequence/src/checker.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
setName("compare two signed int%d's", 8 * int(sizeof(int)));
|
||||
registerTestlibCmd(argc, argv);
|
||||
|
||||
int ja = ans.readInt();
|
||||
int pa = ouf.readInt();
|
||||
|
||||
if (ja != pa)
|
||||
quitf(_wa, "expected %d, found %d", ja, pa);
|
||||
|
||||
quitf(_ok, "all answers are correct");
|
||||
}
|
||||
96
longest-palindromic-subsequence/src/generator.cpp
Normal file
96
longest-palindromic-subsequence/src/generator.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const int MIN_N = 1;
|
||||
const int MAX_N = 1000;
|
||||
const int rnd_test_n = 30;
|
||||
|
||||
template <typename T> void append(vector<T> &dest, const vector<T> &orig) {
|
||||
dest.insert(dest.end(), orig.begin(), orig.end());
|
||||
}
|
||||
|
||||
string output_tc(const string &s) {
|
||||
ostringstream oss;
|
||||
oss << s.size() << endl;
|
||||
oss << s << endl;
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
vector<string> generate_sample_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc("bbbab"));
|
||||
tests.push_back(output_tc("cbbd"));
|
||||
tests.push_back(output_tc("a"));
|
||||
return tests;
|
||||
}
|
||||
|
||||
vector<string> generate_manual_tests() {
|
||||
vector<string> tests;
|
||||
return tests;
|
||||
}
|
||||
|
||||
string generate_structured_string(int n) {
|
||||
string s = "";
|
||||
|
||||
if (rnd.next(0, 1) == 1) {
|
||||
string half = rnd.next("[a-z]{" + to_string((n + 1) / 2) + "}");
|
||||
s = half;
|
||||
reverse(half.begin(), half.end());
|
||||
s += half;
|
||||
for (int i = 0; i < n / 10; i++) {
|
||||
s[rnd.next(0, n - 1)] = rnd.next('a', 'z');
|
||||
}
|
||||
}
|
||||
else {
|
||||
s = rnd.next("[a-z]{" + to_string(n) + "}");
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
string rnd_test(int i) {
|
||||
int min_n = MIN_N;
|
||||
int max_n = MAX_N;
|
||||
|
||||
if (i < rnd_test_n / 3) {
|
||||
max_n = 10;
|
||||
} else if (i < rnd_test_n / 2) {
|
||||
max_n = 100;
|
||||
}
|
||||
|
||||
int n = rnd.next(min_n, max_n);
|
||||
return output_tc(generate_structured_string(n));
|
||||
}
|
||||
|
||||
vector<string> generate_random_tests() {
|
||||
vector<string> tests;
|
||||
for (int i = 0; i < rnd_test_n; i++) {
|
||||
tests.push_back(rnd_test(i));
|
||||
}
|
||||
return tests;
|
||||
}
|
||||
|
||||
vector<string> generate_extreme_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc(string(1000, 'a')));
|
||||
tests.push_back(output_tc(rnd.next("[a-z]{1000}")));
|
||||
return tests;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
registerGen(argc, argv, 1);
|
||||
vector<string> tests;
|
||||
size_t test = 0;
|
||||
|
||||
append(tests, generate_sample_tests());
|
||||
append(tests, generate_manual_tests());
|
||||
append(tests, generate_random_tests());
|
||||
append(tests, generate_extreme_tests());
|
||||
|
||||
for (const auto &t : tests) {
|
||||
startTest(++test);
|
||||
cout << t;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
1
longest-palindromic-subsequence/src/script.sh
Normal file
1
longest-palindromic-subsequence/src/script.sh
Normal file
@@ -0,0 +1 @@
|
||||
generator
|
||||
5963
longest-palindromic-subsequence/src/testlib.h
Normal file
5963
longest-palindromic-subsequence/src/testlib.h
Normal file
File diff suppressed because it is too large
Load Diff
16
longest-palindromic-subsequence/src/validator.cpp
Normal file
16
longest-palindromic-subsequence/src/validator.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "testlib.h"
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
registerValidation(argc, argv);
|
||||
int n = inf.readInt(1, 1000);
|
||||
inf.readEoln();
|
||||
string s = inf.readToken("[a-z]+");
|
||||
ensuref(n == (int)s.size(), "string provided %s does not have %i characters", s, n);
|
||||
inf.readEoln();
|
||||
inf.readEof();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user