Responsive Ads Here
Showing posts with label Dynamic programming. Show all posts
Showing posts with label Dynamic programming. Show all posts

Monday, 26 June 2017

Maximize Dot Product

problem id:;--http://practice.geeksforgeeks.org/problems/maximize-dot-product/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second
#define gc() getchar()

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

template <typename T> void scan(T &angka){
angka=0;char input=gc();T kali=1;
while(!(48<=input&&input<=57)){ if(input=='-') kali=-1;input=gc();}
while(48<=input&&input<=57) angka=(angka<<3)+(angka<<1)+input-48,input=gc();angka*=kali;
}

ll a[0x405],b[0x405];
ll dp[0x405][0x405];
ll Magic(ll a[],ll b[],int n,int m){
rep(i,n+1)  rep(j,n+1)  dp[i][j]=0;
for(int i=1;i<=n;++i)
for(int j=i;j<=m;++j)
dp[i][j]=max(dp[i][j-1],dp[i-1][j-1]+b[j-1]*a[i-1]);
//Multiply every element of second array with first array
//Save corresponding index values of product into dp
return dp[n][m];
}

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t;
sc(t);
while(t--)
{
int n,m;
sc2(n,m);
rep(i,n) scan(a[i]);
rep(i,m) scan(b[i]);
ll Ans=(n>m)?Magic(b,a,m,n):Magic(a,b,n,m);// Put smaller array first
printf("%lld\n",Ans);
}
return 0;
}

Sunday, 18 June 2017

Transform to Palindrome

problem id:--https://www.hackerrank.com/contests/w33/challenges/transform-to-palindrome

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007
int graph[100005];
int dp[1005][1005];
int arr[1005];


int root(int id)
{
while(id!=graph[id])
id=graph[id];
return id;
}

bool isconnected(int n,int m)
{
return (root(n)==root(m));
}

void connect(int n,int m)
{
int loc=root(n);
graph[loc]=root(m);
//debug3(loc,n,a[loc]);
}

int palind_substr(int n)
{
//int n=str.length();
rep(i,n) rep(j,n) dp[i][j]=0;
rep(i,n) dp[i][i]=1;
int l=1;
while(l<n)
{
int i=0;
while(i<n-l)
{
if(graph[arr[i]]==graph[arr[i+l]]) {dp[i][i+l]=2; if(l>1) dp[i][i+l]+=dp[i+1][i+l-1];}
else dp[i][i+l]=max(dp[i][i+l-1],dp[i+1][i+l]);
++i;
}
++l;
}
//rep(i,n) {rep(j,n) pf(dp[i][j]); cout<<'\n'; }
return dp[0][n-1];
}

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int n,k,m;
sc3(n,k,m);
rep2(i,1,n+1) graph[i]=i;
int num1,num2;
while(k--)
{
cin>>num1>>num2;
if(!isconnected(num1,num2))
connect(num1,num2);
}
rep2(i,1,n+1) {int temp=i;graph[temp]=root(temp); }
rep(i,m) sc(arr[i]);
int ans=palind_substr(m);
//rep2(i,1,n+1) pf(graph[i]);
pf(ans);
return 0;
}

Longest Palindromic Subsequence

problem id:--http://practice.geeksforgeeks.org/problems/longest-palindromic-subsequence/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
//#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007
int dp[1005][1005];

int palind_substr(string str)
{
int n=str.length();
rep(i,n) rep(j,n) dp[i][j]=0;
rep(i,n) dp[i][i]=1;
int l=1;
while(l<n)
{
int i=0;
while(i<n-l)
{
if(str[i]==str[i+l]) {dp[i][i+l]=2; if(l>1) dp[i][i+l]+=dp[i+1][i+l-1];}
else dp[i][i+l]=max(dp[i][i+l-1],dp[i+1][i+l]);
++i;
}
++l;
}
//rep(i,n) {rep(j,n) pf(dp[i][j]); cout<<'\n'; }
return dp[0][n-1];
}

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t;
sc(t);
while(t--)
{
string str;
cin>>str;
int ans=palind_substr(str);
cout<<ans<<'\n';
}
return 0;
}

Friday, 9 June 2017

Count subsequences of type a^i b^j c^k

problem link :--http://practice.geeksforgeeks.org/problems/count-subsequences-of-type-ai-bj-ck/0

code:--

#include <bits/stdc++.h>

using namespace std;
#define ll   long long
#define      pii               std::pair<int,int>
#define      vi                std::vector<int>
#define      vll               std::vector<long long>
#define      mp(a,b)           make_pair(a,b)
#define      pb(a)             push_back(a)
#define sc(x)   scanf("%d",&x)
#define scll(x)   scanf("%lld",&x)
#define sc2(x,y)   scanf("%d%d",&x,&y)
#define sc3(x,y,z)   scanf("%d%d%d",&x,&y,&z)
#define     pf(x)   printf("%d ",x)
#define     pf2(x,y)   printf("%d %d ",x,y)
#define     pf3(x,y,z)   printf("%d %d %d ",x,y,z)
#define pfnl()   putchar('\n');
#define      each(it,s)        for(auto it = s.begin(); it != s.end(); ++it)
#define      rep(i, n)         for(int i = 0; i < (n); ++i)
#define rep2(i,j,n)   for(int i = j; i < (n); ++i)
#define      fill(a)           memset(a, 0, sizeof (a))
#define      sortA(v)          sort(v.begin(), v.end())
#define      sortD(v)          sort(v.begin(), v.end(), greater<auto>())
#define      X                 first
#define      Y                 second

#define debug(x) cerr<<"debug->"<<#x<<"::"<<x<<endl
#define debug2(x,y) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define debug3(x,y,z) cerr<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define MOD 1000000007

string str;

int magic()
{
int dp[4][105]={0};
rep(i,str.length()+1) dp[0][i]=1;
int i=0;
while(str[i]!='\0')
{
for(int j=1;j<=3;++j)
dp[j][i+1]=(str[i]=='`'+j)?(2*dp[j][i]+dp[j-1][i+1]):(dp[j][i]);
++i;
}
return dp[3][str.length()];
}

int main(int argc, char **argv) {
//std::ios::sync_with_stdio(false);
int t,ans;
sc(t);
while(t--)
{
cin>>str;
ans=magic();
pf(ans);
pfnl();
}
return 0;
}