Computer Science, asked by uttararao99, 3 months ago

Question 1
Asteroid Size Estimator
A scientific research team is trying to study asteroids sizes in the space to assess the impact of collision on earth. It is not possible
size of the asteroid. Hence, they provided the relation of one asteroid size with another one. Given the relative size comparisons in
equation, your task is to come up with a program which generates an output with relationship with all the asteroids found. Arrange
size in descending order.
Develop a program to help the research team convert the input specification to standardized output specification. Your program is
the relations data as input and produce an output as per corresponding output specification as output.
Please note: Custom input option is not available for this question.
Sample Input# 1:
A,C,D,E
E=5 A
C=2 D
D=100 E
code in java or c++​

Answers

Answered by mdmuneerhasan
14

Answer:

#include<bits/stdc++.h>

using namespace std;

vector<string> vec;

int n=0;

string ast;

unordered_map<string,set<pair<int,string>>> adjList;

unordered_map<string,int> marker;

unordered_map<string,int> visited;

void bfs(string src){

queue<pair<string,int>> q;

q.push({src,1});

int put=0;

while(!q.empty()){

 auto next=q.front();

 if(put)cout<<"=";

 put=1;

 cout <<next.second<< next.first;

 q.pop();

 for(auto x:adjList[next.first]){

  q.push({x.second,next.second*x.first});

 }

}

}

void Muneer(){

getline(cin,ast);

int n=ast.size();

char ast2[n];

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

 ast2[i]=ast[i];

}

char *token = strtok(ast2, ",");  

   while (token != NULL)  

   {  

 vec.push_back(token);

       token = strtok(NULL, ",");  

   }  

 

n=vec.size();

string right,left,temp;

int j;

int dist;

while(cin >> left){

 

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

  if(left[j]=='=')break;

 }

 dist=stoi(left.substr(j+1));

 left=left.substr(0,j);

 cin >> right;  

 adjList[left].insert(make_pair(dist,right));

 marker[right]=1;

}

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

 if(marker[vec[i]]==0){

  bfs(vec[i]);

  return;

 }

}

 

 

}

int32_t main(){

ios_base::sync_with_stdio(false);

cin.tie(NULL);cout.tie(NULL);

int t=1;

// cin >>t;    

while (t--)Muneer();

return 0;

}

Explanation:

test it

Similar questions