vector variable inside for loop

Viewed 119

I have defined an empty vector variable inside the loop for the test case, after execution of each test case I expect the vector will go empty, but it is storing the previous result of the earlier test cases as well please help thanks a lot.

My problem statement is this. which takes input as:

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.

  • The first line of each test case contains a single integer N.

  • N lines follow. For each valid i, the i-th of these lines contains a string S3,i followed by a space and an integer C3,i — the problem code and the number of correct solutions on the i-th problem in the third division.

  • N more lines follow. For each valid i, the i-th of these lines contains a string S2,I followed by a space and an integer C2,i — the problem code and the number of correct solutions on the i-th problem in the second division.

  • Finally, N more lines follow. For each valid i, the i-th of these lines contains a string S1,i followed by a space and an integer C1,i — the problem code and the number of correct solutions on the i-th problem in the first division.

GOAL: Codechef challenges have three divisions. In one challenge, there are N problems in each division, but some problems may be shared among multiple divisions. Each problem is uniquely identified by a code — a string containing only uppercase English letters. Each participant can only submit in one of the divisions.

Chef wants to find the number of correct solutions, in total among all 3 divisions, for each problem. Given a list of N problem codes with the numbers of correct solutions for each problem in each division, find the total number of correct solutions for each problem and sort them in non-decreasing order.

My code takes input as:

3
1
A 1
B 2
C 3
2
AA 1
AB 1
AB 1
AC 1
AC 1
AD 1
1
Z 100
Z 100
Z 100

The expected output is:

1 2 3
1 1 2 2
300

But my output has errors:

1 2 3 
1 1 1 2 2 2 3 
1 1 1 2 2 2 3 300 

My code for above problem is:

#include <iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;


int main() {
    map<string,int>m;
    int t,n,c_1;
    string s_1;
    cin>>t;
    for(int i=0;i<t;i++){
        cin>>n;
        for(int k=0;k<3;k++){
            for(int j=0;j<n;j++){
                cin>>s_1;
                cin>>c_1;
                m[s_1]+=c_1;
            }
        }
        vector<int>v;
        for(auto x :m){
            v.push_back(x.second);
        }
        sort(v.begin(),v.end());
        for(auto y: v){
            cout<<y<<" ";
        }
        cout<<"\n" ;
    }
return 0;
}
1 Answers

Your vector (v) is being reset (to empty) on each run of the outer for loop but your map (m) isn't. That is keeping its content each time, and your new inputs are being appended to it. Thus, your for (auto x : m) { appends the data from each previous loop to your (initially empty) vector.

Move the declaration of map<string, int>m; to inside that outer loop to fix your problem:

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <string>

using std::cin, std::cout;
using std::string;
using std::map;
using std::vector;
using std::sort;

int main()
{
    int t, n, c_1;
    string s_1;
    cin >> t;
    for (int i = 0; i < t; i++) {
        map<string, int>m; // Move the map to here, so it's created afresh for each loop.
        cin >> n;
        for (int k = 0; k < 3; k++) {
            for (int j = 0; j < n; j++) {
                cin >> s_1;
                cin >> c_1;
                m[s_1] += c_1;
            }
        }
        vector<int>v;
        for (auto x : m) {
            v.push_back(x.second);
        }
        sort(v.begin(), v.end());
        for (auto y : v) {
            cout << y << " ";
        }
        cout << "\n";
    }
    return 0;
}

Alternatively, you can use the same map each time and just reset (clear) it on each run of the outer loop, by adding a m.clear(); as the first line inside that loop.

Related