Search Here

Saturday, September 5, 2015

UVa - 482 ( Permutation Arrays Solution ) [ C ++ ]

=>Try Yourself First. For help Scroll Down .

This Problem is much tiring then it seems . The statement says to sort the Second Given Array according to the Index of the First one.

Clearance :

The Problem simply says to Sort the Second array according to the Element of the first array [ Which is the index of the resultant array ]  . How do you think first ? Just Scan both arrays and then Store the element of the Second array in a Resultant array whose index is the element of the First Array . Yes that's Right . That's what you have to do . But as this is a Data Structure problem then you need to be more Careful in Handling and Manipulating  Inputs. You should keep in Mind the following Things :

  • You have to take the first array as an String & You have to find how many elements will be there in the Second one . That means the Max value of this array is the Size of the Next array. ( Say Max is N )
  • You have to take the next array also as String. You can take total N string and put it into the another array . Or you can take the total Array as a String and Then Stream it ( That's what I did )
  • You should keep in mind that the elements of the Second is Not a Real Number . Every Elements of the Second Array is string . That's why don't Misunderstood it . 

Algorithm

  • To Sort the Second Array according to the First Array's elements ( as index ) You may put the Second array's elements into Another array at Position of Element of First Array. That is Result[ First[i]] = Second[i] .
  • Or You Can take First array's Elements along with Second Array's element into a VECTOR as a PAIR ( yes this rule is for C++ Users who Knows the STL Manipulation. ) And Finally Sort it into Ascending Order . And Print the Second Element of the Pair . 
Point to get AC : 
  • Scan a Blank line between Each Test Set.  Don't Forget to Print a Blank Line Between each Test Case.  For C++ Users Don't Forget to Clear All Vectors  .
********************________________________________________________________********************

Code :



#include<bits/stdc++.h>

using namespace std;
vector<int>first;
vector<string>second;
vector< pair<int, string> >result;

int main()
{
    int test, ind;
    scanf("%d",&test);
    string s, t, str;
    getchar();
    while(test--)
    {
        getchar();
        getline(cin,s);
        stringstream ss(s);
        while(ss>>ind) first.push_back(ind);
        getline(cin,str);
        stringstream sst(str);
        while(sst>>t) second.push_back(t);
        int sztot = min(first.size(), second.size());
        for(int i=0; i<sztot; i++)
        {
            result.push_back(make_pair(first[i],second[i]));
        }
        sort(result.begin(), result.end());
        for(int i=0; i<sztot; i++)
            cout<<result[i].second<<endl;
        first.clear();
        second.clear();
        result.clear();
        if(test)
            printf("\n");
    }
    return 0;
}

********************________________________________________________________********************
====================================
Try Following I/O To Get Accepted
==================================
5

3 1 2
32.0 54.7 -2

4 1 3 2
-9.1 +3..9 .7

1 2 3
.1 .2 .3

4 3 2 1
-7 +.3 .9     9.9

1 2
1.2 .3
=========
AC Output
=========
54.7
-2
32.0

+3..9
.7
-9.1

.1
.2
.3

9.9
.9
+.3
-7

1.2
.3
====================================================================

=>Question or Need Help ?? Leave a Comment .

3 comments:

  1. min(first.size(), second.size());
    Isn't first.size()==second.size() so why take out minimum

    ReplyDelete
  2. Watch the Second Sample in the given input at end of the post....

    The input size for both Case may not be equal. For example in the Second Sample ( At The End of the Post ) there are 4 index but only 3 elements are present .. Thats why we take minimum Size from both vector.

    ReplyDelete
  3. I have a question, how is it that you use getchar before and after the loop and it doesnot intake the first element in the first case ? Could you explain the getchar() case please

    ReplyDelete