Skip to content

Commit e16f8a4

Browse files
committed
Add DP, Graph and Pattern Searching
1 parent a1169f6 commit e16f8a4

File tree

47 files changed

+4836
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+4836
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Bit Masking - Find All Subsets of a Given Set.cpp
2+
3+
#include <bits/stdc++.h>
4+
#include <vector>
5+
using namespace std;
6+
#define NL '\n'
7+
8+
vector<int> vi;
9+
10+
11+
int main()
12+
{
13+
int tcases, I, J, K, N, n, m, cnt = 0, len;
14+
15+
for(I = 1; I <= 10; I++)
16+
vi.push_back(I);
17+
18+
int numOfSubsets = (1 << vi.size());
19+
20+
for(I = 0; I < numOfSubsets; I++)
21+
{
22+
int pos = vi.size() - 1;
23+
int bitmask = I;
24+
25+
cout << "{ ";
26+
while(bitmask > 0)
27+
{
28+
if((bitmask & 1) == 1)
29+
cout << vi[pos] << " ";
30+
31+
bitmask = bitmask >> 1;
32+
pos--;
33+
}
34+
cout << "}\n";
35+
36+
}
37+
38+
return 0;
39+
}
40+
41+
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Classical 0 -1 Knapsack.cpp
2+
#include <iostream>
3+
#include <cstdio>
4+
5+
#include <string>
6+
#include <cstring>
7+
#include <sstream>
8+
9+
#include <vector>
10+
#include <stack>
11+
#include <queue>
12+
#include <deque>
13+
#include <list>
14+
#include <map>
15+
#include <set>
16+
17+
#include <algorithm>
18+
#include <bitset>
19+
#include <functional>
20+
#include <numeric>
21+
#include <utility>
22+
#include <iomanip>
23+
24+
#include <cmath>
25+
#include <cstdlib>
26+
#include <cctype>
27+
#include <cstdlib>
28+
#include <ctime>
29+
using namespace std;
30+
31+
int caseno = 1;
32+
#define NL '\n'
33+
#define FOR(I,J,K) for(I = J; I < K; I++)
34+
#define REV(I,J,K) for(I = J; I > K; I--)
35+
#define SF scanf
36+
#define PF printf
37+
#define CLR(ar) memset(ar, 0, sizeof(ar))
38+
#define SET(ar) memset(ar, -1, sizeof(ar))
39+
#define PC() printf("Case %d: ", caseno++)
40+
#define READ() freopen("in.txt", "r", stdin)
41+
#define WRITE() freopen("out.txt", "w", stdout)
42+
#define BOOST std::ios_base::sync_with_stdio(0);
43+
44+
template <class T> inline T MAX(T &a, T &b) { return a > b ? a : b; }
45+
46+
typedef long long LL;//NOTES:"%lld"
47+
typedef unsigned long long ULL;//NOTES:"%llu"
48+
typedef long long int64;//NOTES:int64
49+
typedef unsigned long long uint64;//NOTES:uint64
50+
51+
#define INF 2147483647
52+
#define MOD 1000000007
53+
const double PI = 2 * acos(0.0);
54+
const double EPS = 1e-11;
55+
const int SIZE = 1e4;
56+
57+
int weight[SIZE], cost[SIZE], dp[SIZE][SIZE], n, CAP;
58+
59+
int func(int pos, int taken)
60+
{
61+
if (pos >= n) /// All the objects are taken.
62+
return 0;
63+
64+
if (dp[pos][taken] != -1) /// This state have already calculated.
65+
return dp[pos][taken];
66+
67+
int profit1 = 0, profit2 = 0;
68+
69+
if (taken + weight[pos] <= CAP) /// If the i'th object is taken then the profit will be "profit1".
70+
profit1 = cost[pos] + func(pos + 1, taken + weight[pos]);
71+
72+
profit2 = func(pos + 1, taken); /// If the i'th object is not taken then the profit will be "profit2".
73+
74+
dp[pos][taken] = MAX(profit1, profit2); /// We will take the maximum profit.
75+
return dp[pos][taken];
76+
}
77+
78+
int main()
79+
{
80+
///BOOST
81+
int tcases, I, J, K, N, m, cnt = 0, len;
82+
///READ();
83+
///WRITE();
84+
SET(dp);
85+
86+
SF("%d %d", &n, &CAP);
87+
88+
for(I = 0; I < n; I++)
89+
SF("%d %d", &weight[I], &cost[I]);
90+
91+
PF("%d\n", func(0, 0));
92+
93+
return 0;
94+
}
95+
96+
97+
/// n CAPACITY
98+
/// WEIGHT COST
99+
/*
100+
4 10
101+
1 120
102+
4 280
103+
3 150
104+
4 200
105+
*/
106+
107+
/*
108+
4 500
109+
100 2
110+
100 3
111+
200 3
112+
400 4
113+
*/
114+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Coin Change (Variant 1).cpp
2+
#include <iostream>
3+
#include <cstdio>
4+
5+
#include <string>
6+
#include <cstring>
7+
#include <sstream>
8+
9+
#include <vector>
10+
#include <stack>
11+
#include <queue>
12+
#include <deque>
13+
#include <list>
14+
#include <map>
15+
#include <set>
16+
17+
#include <algorithm>
18+
#include <bitset>
19+
#include <functional>
20+
#include <numeric>
21+
#include <utility>
22+
#include <iomanip>
23+
24+
#include <cmath>
25+
#include <cstdlib>
26+
#include <cctype>
27+
#include <cstdlib>
28+
#include <ctime>
29+
using namespace std;
30+
31+
int caseno = 1;
32+
#define NL '\n'
33+
#define FOR(I,J,K) for(I = J; I < K; I++)
34+
#define REV(I,J,K) for(I = J; I > K; I--)
35+
#define SF scanf
36+
#define PF printf
37+
#define CLR(ar) memset(ar, 0, sizeof(ar))
38+
#define SET(ar) memset(ar, -1, sizeof(ar))
39+
#define PC() printf("Case %d: ", caseno++)
40+
#define READ() freopen("in.txt", "r", stdin)
41+
#define WRITE() freopen("out.txt", "w", stdout)
42+
#define BOOST std::ios_base::sync_with_stdio(0);
43+
44+
typedef long long LL;//NOTES:"%lld"
45+
typedef unsigned long long ULL;//NOTES:"%llu"
46+
typedef long long int64;//NOTES:int64
47+
typedef unsigned long long uint64;//NOTES:uint64
48+
49+
#define INF 2147483647
50+
#define MOD 1000000007
51+
const double PI = 2 * acos(0.0);
52+
const double EPS = 1e-11;
53+
const int SIZE = 100;
54+
55+
int coin[] = { 0, 2, 3 };
56+
int dp[SIZE][SIZE], n = 2, make;
57+
58+
int coinChange(int pos, int amount)
59+
{
60+
if (pos > n){
61+
if (amount == make)
62+
return 1;
63+
else
64+
return 0;
65+
}
66+
67+
if (dp[pos][amount] != -1)
68+
return dp[pos][amount];
69+
70+
int ret1 = 0, ret2 = 0;
71+
if (amount + coin[pos] <= make)
72+
ret1 = coinChange(pos, amount + coin[pos]);
73+
74+
ret2 = coinChange(pos + 1, amount);
75+
76+
return dp[pos][amount] = ret1 | ret2;
77+
}
78+
79+
80+
int main()
81+
{
82+
///BOOST
83+
int tcases, I, J, K, N, m, cnt = 0, len;
84+
///READ();
85+
///WRITE();
86+
while (true)
87+
{
88+
SET(dp);
89+
90+
SF("%d", &make);
91+
cout << coinChange(1, 0) << NL;
92+
}
93+
94+
return 0;
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// DCP 186 - Make It Palindrome.cpp
2+
3+
/// Template by Zayed ///
4+
5+
///************************************************************///
6+
/// #include <bits/stdc++.h>
7+
#include <iostream>
8+
#include <cstdio>
9+
10+
#include <string>
11+
#include <cstring>
12+
#include <sstream>
13+
14+
//#include <tuple>
15+
#include <vector>
16+
#include <stack>
17+
#include <queue>
18+
#include <deque>
19+
#include <list>
20+
#include <map>
21+
//#include<unordered_map>
22+
#include <set>
23+
//#include<unordered_set>
24+
25+
#include <algorithm>
26+
#include <bitset>
27+
#include <cmath>
28+
#include <cstdlib>
29+
///************************************************************///
30+
using namespace std;
31+
int caseno = 1;
32+
///************************************************************///
33+
#define NL '\n'
34+
#define SF scanf
35+
#define PF printf
36+
//#define PC() printf("Case %d: ", caseno++)//NOTES:printf
37+
#define PC cout << "Case "//NOTES:cout
38+
#define CN cout << caseno++ << ": "//NOTES:cout
39+
#define CLR(ar) memset(ar, 0, sizeof(ar))
40+
#define SET(ar) memset(ar, -1, sizeof(ar))
41+
#define READ() freopen("input.txt", "r", stdin)
42+
#define WRITE() freopen("output.txt", "w", stdout)
43+
#define BOOST std::ios_base::sync_with_stdio(0);
44+
///************************************************************///
45+
typedef long long LL;//NOTES:"%lld"
46+
typedef unsigned long long ULL;//NOTES:"%llu"
47+
///************************************************************///
48+
#define INF (1 << 31) - 1
49+
#define MOD 1000000007
50+
#define PRIME 999998727899999 // (largest prime below 10^16)
51+
#define PB push_back
52+
#define pii pair<int, int>
53+
#define pic pair<int, char>
54+
#define pci pair<char, int>
55+
#define pLL pair<LL, LL>
56+
#define pis pair<int, string>
57+
#define psi pair<string, int>
58+
#define pss pair<string, string>
59+
//#define tiii tuple<int, int, int>
60+
#define PI 2 * acos(0.0)
61+
#define EPS 1e-11
62+
///************************************************************///
63+
// Numeric Functions
64+
template < class T > inline void SWAP(T &a, T &b) { T t = a; a = b; b = t; }
65+
inline LL POW(LL base, LL power){
66+
LL res = base; if (power == 0) return 1;
67+
for (int I = 0; I < power - 1; I++) res *= base; return res;
68+
}
69+
// Translator Functions
70+
int ToInt(string s) { int r = 0; istringstream sin(s); sin >> r; return r; }//NOTES:ToInt(
71+
double ToDouble(string s) { double r = 0; istringstream sin(s); sin >> r; return r; }//NOTES:ToDouble(
72+
string ToString(int n) { string s; stringstream convert; convert << n; s = convert.str(); return s; }//NOTES:ToString(
73+
///************************************************************///
74+
75+
/*
76+
*******4 Direction Array*******
77+
int dx[] = {0, 0, - 1, 1}, dy[] = {-1, 1, 0, 0};
78+
*******8 Direction Array*******
79+
int dx[] = {0, 0, -1, +1, -1, -1, +1, +1}, dy[] = {-1, +1, 0, 0, -1, +1, -1, +1};
80+
********Knight Moves********
81+
int dx[] = {-2, -2, -1, -1, +1, +1, +2, +2}, dy[] = {-1, +1, -2, +2, -2, +2, -1, +1};
82+
*/
83+
84+
const int SIZE = 1e3 + 10;
85+
string S;
86+
int dp[SIZE][SIZE];
87+
88+
89+
int FindMinInsertions(int I, int J)
90+
{
91+
if(I > J)
92+
return INF;
93+
if(I == J)
94+
return 0;
95+
if (I + 1 == J)
96+
return (S[I] == S[J]) ? 0 : 1;
97+
98+
if(dp[I][J] != -1)
99+
return dp[I][J];
100+
101+
int ret1 = 0, ret2 = 0, mn = 0;
102+
103+
if(S[I] == S[J])
104+
return FindMinInsertions(I + 1, J - 1);
105+
else
106+
{
107+
ret1 = FindMinInsertions(I, J - 1);
108+
ret2 = FindMinInsertions(I + 1, J);
109+
dp[I][J] = 1 + min(ret1 , ret2);
110+
}
111+
112+
return dp[I][J];
113+
114+
}
115+
116+
117+
int main()
118+
{
119+
BOOST
120+
///READ();
121+
///WRITE();
122+
int T, I, J, K, N, n, m, cnt = 0, len;
123+
124+
cin >> T;
125+
while(T--)
126+
{
127+
SET(dp);
128+
129+
cin >> S;
130+
len = S.length();
131+
132+
PC; CN;
133+
134+
cout << FindMinInsertions(0, len - 1) << NL;
135+
136+
}
137+
138+
return 0;
139+
}
140+
141+
142+
143+

0 commit comments

Comments
 (0)