for 循环中的向量变量

vector variable inside for loop

我在测试用例的循环中定义了一个空向量变量,在执行每个测试用例后我预计该向量会变空,但它也存储了早期测试用例的先前结果请帮助非常感谢。

我的问题陈述是this.,输入为:

  • 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.

目标: Codechef 挑战分为三个部分。在一个挑战中,每个部门都有N个问题,但有些问题可能会在多个部门之间共享。每个问题都由一个代码唯一标识——一个仅包含大写英文字母的字符串。每位参赛者只能提交其中一个组别。

Chef 想找出每个问题在所有 3 个分区中的正确解决方案总数。给定一个包含 N 个问题代码的列表,以及每个分区中每个问题的正确解决方案的数量,找出每个问题的正确解决方案的总数,并将它们按非递减顺序排序。

我的代码将 input 作为:

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

预期的 output 是:

1 2 3
1 1 2 2
300

但是我的输出有错误:

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

我对上述问题的代码是:

#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;
}

你的 vector (v) 在外部 for 循环的每个 运行 上被重置(为空)但是你的 map (m) 不是。那就是每次都保留它的内容,并且你的新输入被附加到它上面。因此,您的 for (auto x : m) { 将每个先前循环的数据附加到您的(最初为空)向量。

map<string, int>m; 的声明移动到 内部 外循环以解决您的问题:

#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;
}

或者,您可以每次都使用相同的映射,并在外循环的每个 运行 上重置(清除)它,方法是添加一个 m.clear(); 作为该循环内的第一行。