Блог пользователя ogfemboy

Автор ogfemboy, история, 2 года назад, По-английски

guys sorry for the bad formating yesterday

Question 1=>

so you are given an array of integeres, you need to count the maximum number of partiotions that you can do, here partition means dividing the array into 2 parts such that the sum of both parts are equal. In addition to this you can do this operation once, change a number to 0

test case 1=> 6

-1 5 0 0 5 0

answer is 3, here we change -1 to 0

Question 2 => you are given a string of lower case english alphabets, you need to find the numbebr of palindromic triplets, A palindromic triplets is as such, suppose you have 3 non overlapping substring S1,s2,s3 then this triplet is a plaindromic triplet if all the individual substrings are palindromes and non overlapping. We need to find the number of such triplets

test case 1=> 4

abaa

answer is 5

  • Проголосовать: нравится
  • -16
  • Проголосовать: не нравится

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

small correction: Instead of here we change -1 to 0, it should be here we change -1 to 5

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can a problem be done like this .. for every number store (rightsum-leftsum) in a map ..

for given case : 11 1 1 1 -9 -9 would be the corresponding (rightsum-leftsum)..

now check for each number .. if we change -1 to 0.. check how many times we have 1 in the map .. similarly check for -5 for element 5

»
2 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

for first question compute prefix sum and store the frequency of element in map now start iterating from left to right if you making ith element zero count frequency of element (sum+pref[i])/2 on left side and right side and take max of the sum.

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

for second question first compute matrix is_palindrome[i][j] (0 and 1) now find number of palindrome from i to j using dp. dp[i][j]=dp[i+1][j]+dp[i][j-1]+is_palindrome[i][j]-dp[i+1][j-1] (takes o(n^2)) now compute number of triplet from (0,i),(i+1,j-1),(j+1,n) using two nested loop and take the sum of the product . I think this will work here is code for dp.

#include<bits/stdc++.h>
using namespace std;
#define int long long

int palindrome(int i,int j,string &s,vector<vector<int>>&dp1){
    if(i==j){
        return dp1[i][j]=1;
    }
    if(i+1==j){
        return dp1[i][j]=(s[i]==s[j]?1:0);
    }
    if(dp1[i][j]!=-1)return dp1[i][j];

    if(s[i]!=s[j])return dp1[i][j]=0;

    return dp1[i][j]=palindrome(i+1,j-1,s,dp1);
}

int pcount(int i,int j,vector<vector<int>>&dp1){
    if(i==j){
        return dp1[j][i]=1;
    }
    if(i+1==j){
        if(dp1[i][j]==1){
            return dp1[j][i]=3;
        }
        else return dp1[j][i]=2;
    }
    if(dp1[j][i]!=-1)return dp1[j][i];

    return dp1[j][i]=pcount(i+1,j,dp1)+pcount(i,j-1,dp1)-pcount(i+1,j-1,dp1)+dp1[i][j];
}

int32_t main(){
    string s;
    cin>>s;
    int n=(int)s.size();
    vector<vector<int>>dp1(n,vector<int>(n,-1));
    for(int i=0; i<n; i++){
        for(int j=i; j<n; j++){
            if(dp1[i][j]==-1){
                palindrome(i,j,s,dp1);
            }
        }
    }
    pcount(0,n-1,dp1);


    }

}
»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

2nd question is DP we choose a substring if it is palindrome and 3 palindromes are required so we decrease the required count by one and will try to find the next remaining palindromes we can precompute the next indices for an index that will form a palindrome or we can check on go that will be overall worst worst-case O(n^2) here is the code:-

#include "bits/stdc++.h"
using namespace std;

int dp[1005][4];

bool ispalindrome(string s)
{
    string temp=s;
    reverse(temp.begin(),temp.end());
    if(s==temp)
        return true;
    return false;
}


int counttriplets(int ind,int rem,string s)
{
    if(rem==0)
        return 1;

    if(ind==s.size())
        return 0;

    if(dp[ind][rem]!=-1)
        return dp[ind][rem];

    string temp="";
    int ans=0;
    for(int i=ind;i<s.size();i++)
    {
        temp+=s[i];
        if(ispalindrome(temp)==true)
            ans+=(counttriplets(i+1,rem-1,s));
    }
    for(int i=ind+1;i<s.size();i++)
    {
        ans+=counttriplets(i,rem,s);
    }
    dp[ind][rem]=ans;
    return ans;
}



int main() {
    int n;
    cin>>n;
    string s;
    cin>>s;

    for(int i=0;i<=n;i++)
    {
        dp[i][0]=-1;
        dp[i][1]=-1;
        dp[i][2]=-1;
        dp[i][3]=-1;
    }
    int ans=0;
    ans=counttriplets(0,3,s);
    cout<<ans<<"\n";


    return 0;
}

I think this will work if any correction needed pls do let me know it would be grateful to know other approaches...

»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

whats the approach for second one?

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +6 Проголосовать: не нравится

I have pics for these questions. Drive Link

»
2 года назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

For question 2 ig the following code should work (comments to explain what i did) Let me know if there are any testcases on which my code fails.

Spoiler
»
2 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I think the following should work for the 1st one -

Spoiler