22nd November 2022- DSA Lab

HackerRank:-

Reciprocal Cycles: 

Just use this rotation algo. to get the changed string everytime and check for the digits in it, and check for the powers and then sum it, and lastly check for the overall number.

void foo(string s,int n){

    for(int i=0;i<n;i++){

        cout<<"First Rotation: "<<s.substr(1)+s[0];

        cout<<endl;

        s=s.substr(1)+s[0];

        // cout<<"The String currently is "<<s[i]<<" ";

        // cout<<"The rotated string is "<<s[i]<<" ";

    }

    cout<<"Last Rotation of the string: "<<s<<endl;

}


 Djikstra's Shortest Path Algo.:-

#include <iostream>

#include<vector>

#define inf 99999

#define min(x,y) (x<y?x:y)

using namespace std;

void foo(string s,int n){

    for(int i=0;i<n;i++){

        cout<<"First Rotation: "<<s.substr(1)+s[0];

        s=s.substr(1)+s[0];

    }

    cout<<"Last Rotation of the string: "<<s<<endl;

}

void p(vector<vector<int>> vec){

    for(int i=0;i<vec.size();i++){

        for(int j=0;j<vec.size();j++){

            cout<<vec[i][j]<<" ";

        }

        cout<<endl;

    }

    cout<<endl;

}

void djikstra(vector<vector<int>> vec){

    int nodes=vec.size();

    vector<vector<int>> dist(nodes);

    for(int i=0;i<nodes;i++){

        for(int j=0;j<nodes;j++){

            dist[i].push_back(vec[i][j]);

        }

    }

    for(int k=0;k<nodes;k++){

        for(int i=0;i<nodes;i++){

            for(int j=0;j<nodes;j++){

                dist[i][j]=min(dist[i][j],(dist[i][k]+dist[k][j]));

            }

        }

    }

    p(dist);

}

int main() {

    vector<vector<int>> graph={

      {0,1,inf,2,2},

        {inf,0,1,3,2},

        {inf,2,0,3,4},

        {3,1,2,0,2},

        {4,1,3,2,0}

    };

    // cout<<min(2,3)<<endl;



    foo("foobarbuzz",5);

    return 0;

}

Comments

Popular Posts