题解 | #数据分类处理#

数据分类处理

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

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

int main() {
    int N;//I序列个数
    vector<string> vec_I;
    vector<string> vec_R;
    set<int> set_R;
    vector<int> result;//存放输出结果
    while (cin >> N) { // 注意 while 处理多个 case
        int num_I;
        for(int i=0;i<N;i++){
            cin>> num_I;
            vec_I.push_back(to_string(num_I));
        }
        int n;//R序列个数
        cin>>n;
        int num_R;
        for(int j=0;j<n;j++){
            cin>>num_R;
            //vec_R.push_back(to_string(num_R));
            set_R.insert(num_R);//升序且去重  
            //set_R.insert(to_string(num_R));把字符串直接放到容器中排序会有问题   16 4 26会按照字符串顺序排成16 26 4
        }

        for(auto it1=set_R.begin();it1!=set_R.end();it1++){
            vec_R.push_back(to_string(*it1));
        }
        
        for(auto it_R=vec_R.begin();it_R!=vec_R.end();it_R++){
            result.push_back(stoi(*it_R));//下面需要判断有没有满足条件的I
            int flag_have=0;//I满足条件标志位
            int cout=0;//记录序列I中满足每个R<i>条件的个数

            //第一遍:判断R<i>是否有效 ,记录I序列中满足条件的个数cout
            for(int k=0;k<vec_I.size();k++){//第一次遍历只记录个数
                int pos=vec_I[k].find(*it_R);
                if(pos!=-1){//如果I序列包含R中的数字
                    flag_have=1;
                    cout++;
                }
            }
            if(flag_have==0) result.pop_back();//如果没有满足条件的,需要把加入的R<i>弹出

            //第二遍:往result里放cout   对应的值和下标
            if(flag_have==1){
                result.push_back(cout);
                for(int k2=0;k2<vec_I.size();k2++){//第一次遍历只记录个数
                    int pos2=vec_I[k2].find(*it_R);
                    if(pos2!=-1){//如果I序列包含R中的数字
                        result.push_back(k2);
                        result.push_back(stoi(vec_I[k2]));
                    }
                }
            }
        }
    }
    cout<<result.size()<<" ";
    for(auto it=result.begin();it!=result.end();it++){
        cout<<*it<<" ";
    }
    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

昨天 11:08
门头沟学院 Java
投递京东等公司9个岗位
点赞 评论 收藏
分享
码农索隆:想看offer细节
点赞 评论 收藏
分享
人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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