feat: added alternative solution and TLE solution to decode-ways problem, wrote the tutorial file and added some more manual test cases
This commit is contained in:
40
decode-ways/src/TLE.cpp
Normal file
40
decode-ways/src/TLE.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
typedef long long ll;
|
||||
using namespace std;
|
||||
|
||||
int ways(string &s, int n, int idx = 0)
|
||||
{
|
||||
if (idx > n)
|
||||
return 1;
|
||||
if (s[idx] == '0')
|
||||
return 0;
|
||||
|
||||
int count = ways(s, n, idx + 1);
|
||||
if (idx + 1 < n)
|
||||
{
|
||||
int num = stoi(s.substr(idx, 2));
|
||||
if (num >= 10 && num <= 26)
|
||||
count += ways(s, n, idx + 2);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
vector<bool> used;
|
||||
int numDecodings(string &s)
|
||||
{
|
||||
int n = (int)s.size();
|
||||
used.resize(n, false);
|
||||
return ways(s, n);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
string code;
|
||||
cin >> code;
|
||||
cout << numDecodings(code) << endl;
|
||||
return 0;
|
||||
}
|
||||
45
decode-ways/src/alternative_ac.cpp
Normal file
45
decode-ways/src/alternative_ac.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
typedef long long ll;
|
||||
using namespace std;
|
||||
|
||||
ll numDecodings(string s)
|
||||
{
|
||||
if (s[0] == '0')
|
||||
return 0;
|
||||
|
||||
int N = s.size();
|
||||
ll dp1 = 1, dp2 = 1;
|
||||
for (int i = 2; i <= N; i++)
|
||||
{
|
||||
ll next = 0;
|
||||
char current = s[i - 1], last = s[i - 2];
|
||||
string aux = "";
|
||||
aux += last;
|
||||
aux += current;
|
||||
if (current != '0')
|
||||
{
|
||||
next = dp2;
|
||||
}
|
||||
|
||||
int code = stoi(aux);
|
||||
if (10 <= code && code <= 26)
|
||||
{
|
||||
next += dp1;
|
||||
}
|
||||
swap(dp1, dp2);
|
||||
swap(dp2, next);
|
||||
}
|
||||
|
||||
return dp2;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
string code;
|
||||
cin >> code;
|
||||
cout << numDecodings(code) << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -30,6 +30,9 @@ vector<string> generate_sample_tests() {
|
||||
vector<string> generate_manual_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc("101010101010"));
|
||||
tests.push_back(output_tc("11111111111111111111"));
|
||||
tests.push_back(output_tc("111111111111111111111111111111111111111111111"));
|
||||
tests.push_back(output_tc("111111111111111111111000000000000000000000000"));
|
||||
tests.push_back(output_tc("123456789"));
|
||||
return tests;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user