题解 | #数据分类处理#

数据分类处理

https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd

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

struct SJRecord {
    SJRecord(): mIs(), mIndexs() {}
    vector<int> mIs;
    vector<int> mIndexs;
    int mCount=0;
    int mR;
};
bool BInclude(int item, int num) {
    string item_str = to_string(item);
    string num_str = to_string(num);
    string::size_type tempresult = num_str.find(item_str);
    return tempresult != string::npos;
}
void TM24() {
    int Inum, Rnum, ii, rr;
    cin >> Inum;
    vector<int> inIs;////////////////////////////////////// I
    for (int i = 0; i < Inum; i++) {
        int tempI;
        cin >> tempI;
        inIs.push_back(tempI);
    }
    cin >> Rnum;
    vector<int> inRs;/////////////////////////////////////// R
    for (int i = 0; i < Rnum; i++) {
        int tempR;
        cin >> tempR;
        inRs.push_back(tempR);
    }
    //数据输入完毕

    //首先对R排序去重
    vector<int> afterRs;
    afterRs.push_back(inRs[0]);
    for (int i = 1; i < inRs.size(); i++) {
        for (int j = 0; j < afterRs.size(); j++) {
            if (inRs[i] == afterRs[j]) {
                break;
            } else if (inRs[i] < afterRs[j]) {
                afterRs.insert(afterRs.begin() + j, inRs[i]);
                break;
            }
            if (j == afterRs.size()-1) afterRs.push_back(inRs[i]);
        }
    }
    //排序去重完毕

    //查找元素
    vector<SJRecord> seachRecords;
    for (int i : afterRs) {
        SJRecord tempResult;
        tempResult.mR = i;
        for (int j = 0; j < inIs.size(); j++) {
            if (BInclude(i, inIs[j])) {
                tempResult.mCount++;
                tempResult.mIndexs.push_back(j);
                tempResult.mIs.push_back(inIs[j]);
            }
        }
        if (tempResult.mCount)  seachRecords.push_back(tempResult);
    }

    //输出
    int alloutcount = 0;
    for (auto item : seachRecords) {
        alloutcount += (2 * item.mCount + 2);
    }
    cout << alloutcount << " ";
    for (int i = 0; i < seachRecords.size(); i++) {
        cout << seachRecords[i].mR << " " << seachRecords[i].mCount << " ";
        for (int j = 0; j < seachRecords[i].mCount; j++) {
            cout << seachRecords[i].mIndexs[j] << " " << seachRecords[i].mIs[j] << " ";
        }
    }
}
int main() {
    TM24();
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

10-17 12:16
同济大学 Java
7182oat:快快放弃了然后发给我,然后让我也泡他七天最后再拒掉,狠狠羞辱他一把😋
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务