Skip to content

Commit 93b026d

Browse files
committed
tree algo added
1 parent 0c795ad commit 93b026d

14 files changed

+1181
-0
lines changed

Cp_algorithm/a.out

-19.9 KB
Binary file not shown.

Dp_Algo/0-1_knap.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll ;
8+
typedef set<pair<int,int>> spi;
9+
typedef set<pair<ll,ll>> spl;
10+
typedef vector<pair<int,int>> vpi;
11+
typedef vector<int> vi;
12+
typedef vector<ll> vl;
13+
typedef vector<bool> vb;
14+
typedef vector<char> vc;
15+
typedef vector<pair<ll,ll>> vpl;
16+
typedef vector<string> vs;
17+
typedef map<int,int>mi;
18+
typedef map<ll,ll> ml;
19+
typedef set<string> ss;
20+
typedef set<char>sc;
21+
typedef set<int> si;
22+
typedef set<ll> sl;
23+
#define pan cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
24+
// define values.
25+
#define mod 1e9+7LL
26+
#define phi 1.618
27+
/* Bit-Stuff */
28+
#define get_set_bits(a) (__builtin_popcount(a))
29+
#define get_set_bitsll(a) ( __builtin_popcountll(a))
30+
#define get_trail_zero(a) (__builtin_ctz(a))
31+
#define get_lead_zero(a) (__builtin_clz(a))
32+
#define get_parity(a) (__builtin_parity(a))
33+
/* Abbrevations */
34+
#define ff first
35+
#define ss second
36+
#define mp make_pair
37+
#define line cout<<endl;
38+
#define pb push_back
39+
#define Endl "\n"
40+
// loops
41+
#define loop(i,start,end) for(ll i=ll(start);i<ll(end);i++)
42+
#define loop0(num) for(ll i=0;i<ll(num);i++)
43+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
44+
// Some print
45+
#define no cout<<"NO"<<endl;
46+
#define yes cout<<"YES"<<endl;
47+
#define cc ll test;cin>>test;while(test--)
48+
// sort
49+
#define all(V) (V).begin(),(V).end()
50+
#define srt(V) sort(all(V))
51+
#define srtGreat(V) sort(all(V),greater<ll>())
52+
// function
53+
54+
ll power(ll x, ll y, ll p)
55+
{
56+
ll res = 1;
57+
x = x % p;
58+
while (y > 0)
59+
{
60+
if (y & 1)
61+
res = (res*x) % p;
62+
y = y>>1;
63+
x = (x*x) % p;
64+
}
65+
return res;
66+
}
67+
/* ascii value
68+
A=65,Z=90,a=97,z=122 1=49
69+
*/
70+
/* -----------------------------------------------------------------------------------*/
71+
// freopen("input.txt", "r", stdin);
72+
// freopen("output.txt", "w", stdout);
73+
74+
75+
int main()
76+
{
77+
cout<<"No of object : ";
78+
ll n;
79+
cin>>n;
80+
cout<<"Enter profit : ";
81+
vl profit(n+1,0),weight(n+1,0);
82+
ll temp=0;
83+
for(ll i=1;i<=n;i++)
84+
{
85+
cin>>temp;
86+
profit[i]=temp;
87+
}
88+
cout<<"Enter weight now : ";
89+
loop(i,1,n+1)
90+
cin>>weight[i];
91+
cout<<"Enter size of bucket : ";
92+
ll size=0;
93+
cin>>size;
94+
ll arr[n+1][size+1];
95+
loop(i,0,n+1)
96+
{
97+
loop(j,0,size+1)
98+
{
99+
if(i==0||j==0)
100+
{
101+
arr[i][j]=0;
102+
}
103+
else
104+
{
105+
if(j>=weight[i])
106+
arr[i][j]=max(arr[i-1][j],profit[i]+arr[i-1][j-weight[i]]);
107+
else
108+
arr[i][j]=arr[i-1][j];
109+
}
110+
}
111+
}
112+
cout<<"matrix look like "<<endl;
113+
loop(i,0,n+1)
114+
{
115+
loop(j,0,size+1)
116+
{
117+
cout<<arr[i][j]<<" ";
118+
}
119+
line;
120+
}
121+
}

Dp_Algo/LCS.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll ;
8+
typedef vector<ll> vl;
9+
typedef vector<bool> vb;
10+
typedef vector<char> vc;
11+
typedef map<ll,ll> ml;
12+
typedef set<char>sc;
13+
typedef set<ll> sl;
14+
#define pan cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
15+
// define values.
16+
#define mod 1000000007
17+
#define phi 1.618
18+
/* Bit-Stuff */
19+
#define get_set_bits(a) (__builtin_popcount(a))
20+
#define get_set_bitsll(a) ( __builtin_popcountll(a))
21+
#define get_trail_zero(a) (__builtin_ctz(a))
22+
#define get_lead_zero(a) (__builtin_clz(a))
23+
#define get_parity(a) (__builtin_parity(a))
24+
/* Abbrevations */
25+
#define ff first
26+
#define ss second
27+
#define mp make_pair
28+
#define line cout<<endl;
29+
#define pb push_back
30+
#define Endl "\n"
31+
// loops
32+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
33+
// Some print
34+
#define no cout<<"NO"<<endl;
35+
#define yes cout<<"YES"<<endl;
36+
#define cc ll test;cin>>test;while(test--)
37+
// sort
38+
#define all(V) (V).begin(),(V).end()
39+
#define srt(V) sort(all(V))
40+
#define srtGreat(V) sort(all(V),greater<ll>())
41+
// function
42+
43+
ll power(ll x,ll y)
44+
{
45+
ll res=1;
46+
// x=x%mod;
47+
while(y>0)
48+
{
49+
if(y%2==1)
50+
{
51+
res*=x;
52+
// res=res%mod;
53+
}
54+
y/=2; x*=x; // x=x%mod;
55+
}
56+
return res;
57+
}
58+
/* ascii value
59+
A=65,Z=90,a=97,z=122
60+
*/
61+
/* -----------------------------------------------------------------------------------*/
62+
63+
64+
ll solve()
65+
{
66+
string s1,s2;
67+
cin>>s1>>s2;
68+
ll len1=s1.length();
69+
ll len2=s2.length();
70+
vector<vector<ll>> dp((len1+1),vector<ll>(len2+1));
71+
for(ll i=0;i<=len1;i++)
72+
{
73+
for(ll j=0;j<=len2;j++)
74+
{
75+
if(i==0||j==0)
76+
dp[i][j]=0;
77+
else if(s1[i-1]==s2[j-1])
78+
dp[i][j]=1+dp[i-1][j-1];
79+
else
80+
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
81+
}
82+
}
83+
cout<<dp[len1][len2]<<endl;
84+
return 0;
85+
}
86+
87+
int main()
88+
{
89+
//freopen("input.txt"a, "r", stdin);
90+
pan;
91+
solve();
92+
}

Dp_Algo/fibonacci_number.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll ;
8+
typedef set<pair<int,int>> spi;
9+
typedef set<pair<ll,ll>> spl;
10+
typedef vector<pair<int,int>> vpi;
11+
typedef vector<int> vi;
12+
typedef vector<ll> vl;
13+
typedef vector<bool> vb;
14+
typedef vector<char> vc;
15+
typedef vector<pair<ll,ll>> vpl;
16+
typedef vector<string> vs;
17+
typedef map<int,int>mi;
18+
typedef map<ll,ll> ml;
19+
typedef set<string> ss;
20+
typedef set<char>sc;
21+
typedef set<int> si;
22+
typedef set<ll> sl;
23+
#define pan cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
24+
// define values.
25+
#define mod 10e9+9LL
26+
#define phi 1.618
27+
/* Bit-Stuff */
28+
#define get_set_bits(a) (__builtin_popcount(a))
29+
#define get_set_bitsll(a) ( __builtin_popcountll(a))
30+
#define get_trail_zero(a) (__builtin_ctz(a))
31+
#define get_lead_zero(a) (__builtin_clz(a))
32+
#define get_parity(a) (__builtin_parity(a))
33+
/* Abbrevations */
34+
#define ff first
35+
#define ss second
36+
#define mp make_pair
37+
#define line cout<<endl;
38+
#define pb push_back
39+
#define Endl "\n"
40+
// loops
41+
#define loop(i,start,end) for(ll i=ll(start);i<ll(end);i++)
42+
#define loop0(num) for(ll i=0;i<ll(num);i++)
43+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
44+
// Some print
45+
#define no cout<<"NO"<<endl;
46+
#define yes cout<<"YES"<<endl;
47+
#define cc ll test;cin>>test;while(test--)
48+
// sort
49+
#define all(V) (V).begin(),(V).end()
50+
#define srt(V) sort(all(V))
51+
#define srtGreat(V) sort(all(V),greater<ll>())
52+
// function
53+
54+
ll power(ll x, ll y, ll p)
55+
{
56+
ll res = 1;
57+
x = x % p;
58+
while (y > 0)
59+
{
60+
if (y & 1)
61+
res = (res*x) % p;
62+
y = y>>1;
63+
x = (x*x) % p;
64+
}
65+
return res;
66+
}
67+
/* ascii value
68+
A=65,Z=90,a=97,z=122 1=49
69+
*/
70+
/* -----------------------------------------------------------------------------------*/
71+
// freopen("input.txt", "r", stdin);
72+
// freopen("output.txt", "w", stdout);
73+
74+
void multiply(ll F[2][2], ll M[2][2]);
75+
void power(ll F[2][2], ll n);
76+
ll fib(ll n)
77+
{
78+
ll F[2][2] = {{1, 1}, {1, 0}};
79+
if (n == 0)
80+
return 0;
81+
power(F, n - 1);
82+
return F[0][0];
83+
}
84+
85+
void power(ll F[2][2],ll n)
86+
{
87+
if(n == 0 || n == 1)
88+
return;
89+
ll M[2][2] = {{1, 1}, {1, 0}};
90+
91+
power(F, n / 2);
92+
multiply(F, F);
93+
94+
if (n % 2 != 0)
95+
multiply(F, M);
96+
}
97+
98+
void multiply(ll F[2][2], ll M[2][2])
99+
{
100+
ll x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
101+
ll y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
102+
ll z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
103+
ll w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
104+
F[0][0] = x%1000000007;
105+
F[0][1] = y%1000000007;
106+
F[1][0] = z%1000000007;
107+
F[1][1] = w%1000000007;
108+
}
109+
110+
int main()
111+
{
112+
ll n;
113+
cin>>n;
114+
cout << fib(n)<<endl;
115+
return 0;
116+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy