feat(DP): new dp problems formated

This commit is contained in:
2025-11-04 21:14:50 -03:00
parent 669c13147b
commit 3b03c32703
925 changed files with 30660 additions and 0 deletions

56
decode-ways/src/ac.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll numDecodings(string s)
{
if (s[0] == '0')
return 0;
for (int i = 1; i < s.size(); i++)
{
if (s[i] == '0' && s[i - 1] == '0')
return 0;
}
// each letter can be a part of the message on their on
// they can extend the last letter to also form another part of the mesage
// let dp[i][{0, 1}] be the number of different messages ending at the i(th) char when the i(th) char
// is not concatenated with i-1(th) char or is.
// if s[i] is between 1 and 26 then it can be added alone, so dp[i][0] = dp[i - 1][0] + dp[i - 1][1]
// if s[i] concatenated with s[i - 1] and is between 1 and 26 then they can be added togheter, dp[i][1] = dp[i - 2][0] + dp[i - 2][1]
// dp[1][0] = 1, dp[1][1] = 0
int N = s.size();
vector<vector<ll>> dp(N + 1, vector<ll>(2, 0));
dp[1][0] = 1, dp[1][1] = 0;
dp[0][0] = 1;
for (int i = 2; i <= N; i++)
{
char current = s[i - 1], last = s[i - 2];
string aux = "";
aux += last;
aux += current;
if (current != '0')
{
dp[i][0] = dp[i - 1][0] + dp[i - 1][1];
}
int code = stoi(aux);
if (10 <= code && code <= 26)
{
dp[i][1] = dp[i - 2][0] + dp[i - 2][1];
}
}
return dp[N][0] + dp[N][1];
}
int main()
{
int n; cin >> n;
string code; cin >> code;
cout << numDecodings(code) << endl;
return 0;
}

View File

@@ -0,0 +1,18 @@
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
setName("compare two numbers");
registerTestlibCmd(argc, argv);
long long ja = ans.readLong();
long long pa = ouf.readLong();
if (ja != pa)
quitf(_wa, "expected %lld, found %lld", ja, pa);
quitf(_ok, "answer is %lld", ja);
return 0;
}

View File

@@ -0,0 +1,89 @@
#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;
const int MIN_N = 1;
const int MAX_N = 100;
const int rnd_test_n = 100;
template <typename T> void append(vector<T> &dest, const vector<T> &orig) {
dest.insert(dest.end(), orig.begin(), orig.end());
}
string output_tc(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("12"));
tests.push_back(output_tc("226"));
tests.push_back(output_tc("06"));
return tests;
}
vector<string> generate_manual_tests() {
vector<string> tests;
tests.push_back(output_tc("101010101010"));
tests.push_back(output_tc("123456789"));
return tests;
}
string rnd_test(int i){
int min_n = MIN_N;
int max_n = MAX_N;
if(i<rnd_test_n / 3){
max_n = 5;
}
else if(i<rnd_test_n / 2){
max_n = 20;
}
int x = rnd.next(min_n, max_n);
return output_tc(rnd.next("[1-9]{" + to_string(x) + "}"));
}
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;
}
string extreme_test_1(){
return output_tc(string(100, '1'));
}
string extreme_test_2(){
return output_tc(string(100, '0'));
}
vector<string> generate_extreme_tests(){
vector<string> tests;
tests.push_back(extreme_test_1());
tests.push_back(extreme_test_2());
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;
}

View File

@@ -0,0 +1 @@
generator

5963
decode-ways/src/testlib.h Normal file

File diff suppressed because it is too large Load Diff

View 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, 100, "n");
inf.readEoln();
string s = inf.readToken("[0-9]+");
inf.readEoln();
inf.readEof();
ensuref(n == (int)s.size(), "String 's' should have length %i", n);
return 0;
}