题解 | #数据分类处理#

数据分类处理

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

#include <future>
#include <iostream>
#include <iterator>
#include <utility>
#include <vector>
#include <algorithm>

using namespace std;

bool isContant(int x,int y){//判断x是否包含y
    string x_ctr,y_ctr;
    if(y==0){
        int temp=x;
        while(temp){
            if(temp%10==0) return true;
            temp/=10;
        }
        return false;
    }
    while(x){
        int temp=x%10;
        x/=10;
        x_ctr.push_back(temp);
    }
    while(y){
        int temp=y%10;
        y/=10;
        y_ctr.push_back(temp);
    }
    reverse(x_ctr.begin(),x_ctr.end());
    reverse(y_ctr.begin(),y_ctr.end());
    return x_ctr.find(y_ctr)!=string::npos;
}

int main() {
    int n1 ;
    cin>>n1;
    vector<int> I(n1);
    for(int i=0;i<n1;i++){
        cin>>I[i];
    }
    int n2,temp;
    cin>>n2;
    vector<int> R;

    for(int i=0;i<n2;i++){
        cin>>temp;
        if(R.empty()) R.push_back(temp);
        for(int j=0;j<R.size();j++){
            if(R[j]==temp) break;
            if(j==R.size()-1) R.push_back(temp);
        }
    }
    sort(R.begin(),R.end());
    int ans=0;
    vector<int> hash(R.size(),0);
    vector<vector<pair<int,int>>> ret(R.size());

    for(int i=0;i<R.size();i++){
        int count=0;
        for(int j=0;j<I.size();j++){
            if(isContant(I[j],R[i])){
                count++;//I中有多少种包含i的数
                pair<int, int> temp(j,I[j]);
                ret[i].push_back(temp);
            }       
        }
        if(count!=0){
            ans=ans+2+count*2;
            hash[i]=count;
        }
    }
    cout<<ans<<' ';
    for(int i=0;i<R.size();i++){
        if(hash[i]!=0){
            cout<<R[i]<<' '<<hash[i]<<' ';
            for(auto it:ret[i])
                cout<<it.first<<' '<<it.second<<' ';
        }
            
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务