题解 | #数据分类处理#

数据分类处理

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

/*
	HW其他6  数据分类处理

*/
#include<bits/stdc++.h>
using namespace std;

vector<int>R;
vector<string>temp,I;
map<int,vector<int>>myhash;

string tostr(int x)				//数字转字符串
{
    string ans="";
    if(x==0)return "0";
    while(x){
        ans+=(x%10)+'0';
        x/=10;
    }
    return string(ans.rbegin(),ans.rend());			//得到正确的序列
}

void getin()					//获取输入,并保存在I,R中,给R去重保存在temp中备用
{
    int n;
    int flag=0;
    while(cin>>n){
        for(int i=0;i<n;i++){
            int num;
            cin>>num;
            if(flag)R.push_back(num);
            else I.push_back(tostr(num));
        }
        flag=1;
    }
    stable_sort(R.begin(),R.end());
    int last=R[0];
    temp.push_back(tostr(last));
    int cur;
    for(int i=1;i<R.size();i++){
        cur=R[i];
        if(cur==last)continue;
        temp.push_back(tostr(cur));
        last=cur;
    }
    return;
}

int main() {
    getin();
    if(R.size()==0)return 0;
    int cnt=0;								//用来计数,有多少个数字需要输出
    for(int i=0;i<temp.size();i++){					//针对去重后的数字进行搜索
        string t=temp[i];
        int flag=0;										//因为有多次重复命中,但是开头只需要说明一次
        for(int j=0;j<I.size();j++){
            if(I[j].find(t)==string::npos)continue;
            if(flag==0){
                cnt+=2;										//直接+2
                flag=1;										//只有第一次找到才会声明有这个数
                //cout<<endl<<t<<": ";
            }
            int mykey=stoi(t);
            myhash[mykey].push_back(j);					//将命中的内容存在该数字为KEY的hash表中
            //cout<<j<<" "<<I[j]<<" ";					//直接记录下标,之后直接用下标访问            
            cnt+=2;										//直接+2
        }
    }
    cout<<cnt<<" ";
    for(auto it=myhash.begin();it!=myhash.end();it++){
        cout<<it->first<<" "<<it->second.size()<<" ";
        for(int i=0;i<it->second.size();i++){
            int k=it->second[i];
            cout<<k<<" "<<I[k]<<" ";
        }
    }

    return 0;
}
// 64 位输出请用 printf("%lld")
全部评论

相关推荐

06-02 15:17
门头沟学院 Java
心爱的idea:怎么会呢 应该是打招呼有问题 问就说实习6个月全国可飞随时到岗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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