Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
33arsenic75 committed Jun 10, 2024
1 parent 46296a9 commit 613e087
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 1 deletion.
37 changes: 37 additions & 0 deletions problem-of-the-day/day13/AB Flipping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)


void solve() {
int n;string s;cin>>n>>s;
int ans = 0;
vector<bool>a(n+1,false),b(n+1,false);
b[n-1] = (s[n-1]=='B');
a[0] = (s[0]=='A');
for(int i = n-2;i>=0;i--){
b[i] = (s[i]=='B') || (b[i+1]);
}
rep(i,1,n){
a[i] = (s[i] == 'A') || (a[i-1]);
}
rep(i,0,n-1){
ans += a[i] && b[i+1];
}
cout<<ans<<'\n';
}


int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
42 changes: 42 additions & 0 deletions problem-of-the-day/day13/Digit Queries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
using namespace std;
#define int long long
// ------------------------------------------***--------------------------------------------------

int pow(int i){
if(i==0)return 1;
return 10*pow(i-1);
}

vector<int>a;
int32_t main() {
int curr = 1;
int digit = 1;
while(digit<=18){
a.push_back(curr);
curr+=9*digit*pow(digit-1);
digit++;
}
int q,qq;cin>>q;
while(q--){
cin>>qq;
int i = 0;
for(i = 0;i<18;i++){
if(a[i]>qq)break;
}
digit = i;
int base = pow(digit-1);
int offset = qq - a[digit-1];
base+=(offset)/digit;
offset%=digit;
// cout<<base<<' '<<digit<<' '<<offset<<'\n';
string num = to_string(base);
cout<<num[offset]<<'\n';
// if(offset==0)cout<<num[len(num)-1]<<'\n';
// else cout<<num[offset-1]<<'\n';
}
return 0;
}
47 changes: 47 additions & 0 deletions problem-of-the-day/day13/Even Number Addicts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <bits/stdc++.h>
#define endl '\n'

typedef long long int ll;
using namespace std;

bool solve(int odd, int even, bool alice_winning, bool alice, vector<vector<vector<int>>>& memo) {
if(odd == 0 && even == 0) {
return alice_winning;
}
if(alice && memo[odd][even][alice_winning] != -1)
return memo[odd][even][alice_winning];
if(alice) {
bool option1 = false, option2 = false;
if(odd > 0) option1 = solve(odd - 1, even, !alice_winning, false, memo);
if(even > 0) option2 = solve(odd, even - 1, alice_winning, false, memo);
return memo[odd][even][alice_winning] = max(option1, option2);
} else {
bool option1 = true, option2 = true;
if(odd > 0) option1 = solve(odd - 1, even, alice_winning, true, memo);
if(even > 0) option2 = solve(odd, even - 1, alice_winning, true, memo);
return min(option1, option2);
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin >> t;
while(t--) {
int n, el;
cin >> n;
int odd = 0, even = 0;
for(int i = 0; i < n; i++) {
cin >> el;
odd += (abs(el) % 2 == 1);
even += (abs(el) % 2 == 0);
}
vector<vector<vector<int>>> memo(n+1, vector<vector<int>>(n+1, vector<int>(2, -1)));
bool res = solve(odd, even, true, true, memo);
cout << (res ? "Alice" : "Bob") << endl;
}

return 0;
}
30 changes: 30 additions & 0 deletions problem-of-the-day/day13/Mortal Kombat Tower.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)

void solve() {
int n;cin>>n;
vector<int>a(n+1);
rep(i,1,n+1)cin>>a[i];
vector<int>f(n+2,0);
vector<int>m(n+2,0);
if(a[n]==1)f[n]=1;
for(int i = n-1 ; i>=1 ; i--){
f[i] = min( m[i+2] + a[i] + a[i+1],m[i+1] + a[i]);
m[i] = min(f[i+1],f[i+2]);
}
cout<<f[1]<<'\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
48 changes: 48 additions & 0 deletions problem-of-the-day/day13/Rudolf and the Ugly String.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)

///DP solution
void solve() {
int n;string s;cin>>n>>s;
int i = n-2;
vector<int>ans(n+1,0);
for(int i = n-2; i>=0;i--){
if(s.substr(i,3)=="map" || s.substr(i,3)=="pie"){
ans[i] = 1ll + min(ans[i+1],min(ans[i+2],ans[i+3]));
}
else ans[i] = ans[i+1];
}
cout<<ans[0]<<'\n';
}

/// greedy
void solve(){
int n;
cin >> n;
string s;
cin >> s;
vector<int> va;
for (string sul : {"mapie", "pie", "map"}) {
for (int pos = 0; (pos = s.find(sul, pos)) != string::npos;) {
s[pos + sul.length() / 2] = '?';
va.push_back(pos + sul.length() / 2);
}
}
cout << va.size() << endl;
}


int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
51 changes: 51 additions & 0 deletions problem-of-the-day/day13/YetnotherrokenKeoard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include<iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)


void solve() {
string s;cin>>s;
int n = s.size();
vector<bool>a(n,true);
int B=0,b=0;
char c;
for(int i = n-1; i>=0 ;i--){
c = s[i];
if(c=='b'){
a[i]=false;
b++;
}
else if(c=='B'){
a[i]=false;
B++;
}
else{
if('a'<=c && c<='z' && b>0){
a[i]=false;
b--;
}
else if('A'<=c && c<='Z' && B>0){
a[i]=false;
B--;
}
}
}
rep(i,0,n){
if(a[i])cout<<s[i];
}
cout<<'\n';
}

int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
3 changes: 2 additions & 1 deletion problem-of-the-day/day13/dsa.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ After covering all the major data structures, we will now be focusing on few spe
5. [Digit Queries](https://cses.fi/problemset/task/2431/):- Make sure to use long long in this problem. What happens when you use int. Also implement it in python. Why no such issue occurs there. What is the internal implementation of integers in python.

## Resources
1. [Introduction to Dynamic Programming](https://cp-algorithms.com/dynamic_programming/intro-to-dp.html)

1. [Introduction to Dynamic Programming](https://cp-algorithms.com/dynamic_programming/intro-to-dp.html)
2. [Greedy Algorithm](https://www.geeksforgeeks.org/greedy-algorithms/):- Prim's and Kruskal's Algorithm. Which is greedy ? What about Dijkstra ?

**Note:** Solutions to [day12](../day12) are uploaded.

0 comments on commit 613e087

Please sign in to comment.