Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e562f97

Browse files
Added more cool stuff
1 parent f20bfc7 commit e562f97

File tree

4 files changed

+159
-7
lines changed

4 files changed

+159
-7
lines changed

‎DPandbitmasking/Mehta.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <cstdio>
2+
#include <cmath>
3+
#include <iostream>
4+
#include <set>
5+
#include <algorithm>
6+
#include <vector>
7+
#include <map>
8+
#include <cassert>
9+
#include <string>
10+
#include <cstring>
11+
using namespace std;
12+
#define rep(i,a,b) for(int i = a; i < b; i++)
13+
#define S(x) scanf("%d",&x)
14+
#define P(x) printf("%d\n",x)
15+
typedef long long int LL;
16+
typedef pair<int, int > pii;
17+
const int MAXN = 2001;
18+
LL P[11] = {1,2,3,5,7,11,13,17,19,23,29};
19+
LL dp[2][MAXN][MAXN];
20+
int profit[MAXN];
21+
int weight[MAXN];
22+
vector<pii > v;
23+
int main() {
24+
int N,W;
25+
scanf("%d%d",&N,&W);
26+
assert(N >= 1 && N <= 2000);
27+
assert(W >= 1 && W <= 2000);
28+
rep(i,1,N+1) {
29+
scanf("%d%d",&profit[i], &weight[i]);
30+
assert(profit[i] >= 1 && profit[i] <= 1000000000);
31+
assert(weight[i] >= 1 && weight[i] <= 2000);
32+
v.push_back(make_pair(profit[i], weight[i]));
33+
}
34+
sort(v.begin(), v.end());
35+
rep(i,1,N+1) {
36+
profit[i] = v[i-1].first;
37+
weight[i] = v[i-1].second;
38+
}
39+
rep(p,0,11) rep(i,1,N+1) rep(j,1,W+1) {
40+
if(!p) {
41+
dp[0][i][j] = max(dp[0][i-1][j], dp[0][i][j-1]);
42+
if(j >= weight[i])
43+
dp[0][i][j] = max(dp[0][i][j], dp[0][i-1][j-weight[i]]+profit[i]);
44+
} else {
45+
int cur = p&1;
46+
int prev = 1-cur;
47+
dp[cur][i][j] = dp[prev][i][j];
48+
dp[cur][i][j] = max(dp[cur][i][j], dp[cur][i-1][j]);
49+
dp[cur][i][j] = max(dp[cur][i][j], dp[cur][i][j-1]);
50+
if(j >= weight[i]) {
51+
dp[cur][i][j] = max(dp[cur][i][j], dp[cur][i-1][j-weight[i]]+profit[i]);
52+
dp[cur][i][j] = max(dp[cur][i][j], dp[prev][i-1][j-weight[i]]+P[p]*profit[i]);
53+
}
54+
}
55+
}
56+
cout << dp[0][N][W] << endl;
57+
return 0;
58+
}

‎DPandbitmasking/Stringmaker.cpp‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
According to Ancient Ninjas , string making is an art form . There are various methods of string making , one of them uses previously generated strings to make the new one . Suppose you have two strings A and B , to generate a new string C , you can pick a subsequence of characters from A and a subsequence of characters from B and then merge these two subsequences to form the new string.
11+
Though while merging the two subsequences you can not change the relative order of individual subsequences. What this means is - suppose there two characters Ai and Aj in the subsequence chosen from A , where i < j , then after merging if i acquires position k and j acquires p then k<p should be true and the same apply for subsequence from C.
12+
Given string A , B , C can you count the number of ways to form string C from the two strings A and B by the method described above. Two ways are different if any of the chosen subsequence is different .
13+
As the answer could be large so return it after modulo 10^9+7 .
14+
Input Format :
15+
Line 1 : String A
16+
Line 2 : String B
17+
Line 3 : String C
18+
Output Format :
19+
The number of ways to form string C
20+
Constralls :
21+
1 <= |A|, |B|, |C| <= 50
22+
Sample Input :
23+
abc
24+
abc
25+
abc
26+
Sample Output :
27+
8
28+
*/
29+
30+
#include <bits/stdc++.h>
31+
using namespace std;
32+
33+
typedef long long ll;
34+
#define MOD 1000000007
35+
ll dp[100][100][100];
36+
37+
int memsolver(string a,string b,string c){
38+
ll x = a.size();
39+
ll y = b.size();
40+
ll z = c.size();
41+
42+
ll ans = 0;
43+
44+
if (z == 0)
45+
{
46+
return 1;
47+
}
48+
49+
if (x<=0 && y<=0)
50+
{
51+
return 0;
52+
}
53+
54+
if (dp[x][y][z]!=-1)
55+
{
56+
return dp[x][y][z];
57+
}
58+
59+
for (ll i = 0; i < a.size(); ++i)
60+
{
61+
if (a[i]==c[0])
62+
{
63+
ans+=memsolver(a.substr(i+1), b, c.substr(1));
64+
}
65+
}
66+
67+
68+
69+
for (ll i = 0; i < b.size(); ++i)
70+
{
71+
if (b[i] == c[0])
72+
{
73+
ans+=memsolver(a, b.substr(i+1), c.substr(1));
74+
}
75+
}
76+
77+
dp[x][y][z] = ans%MOD;
78+
return ans%MOD;
79+
}
80+
81+
int solve(string a,string b,string c)
82+
{
83+
memset(dp, -1, sizeof(dp));
84+
return memsolver(a, b, c);
85+
}
86+
87+
int main( int argc , char ** argv )
88+
{
89+
ios_base::sync_with_stdio(false) ;
90+
cin.tie(NULL) ;
91+
92+
string a,b,c;
93+
cin>>a>>b>>c;
94+
95+
cout<<solve(a,b,c)<<endl;
96+
97+
return 0 ;
98+
}

‎DPandbitmasking/Test.txt‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
6
2-
1 1 1 0 1 1
3-
1 1 1 0 0 1
4-
0 0 1 0 1 1
5-
0 1 1 1 0 0
6-
0 1 0 1 1 0
7-
0 1 1 0 0 1
1+
abc
2+
abc
3+
abc

‎DPandbitmasking/a.out‎

1.01 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /