题解 | #数据分类处理#

数据分类处理

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

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


int main() {
    int numI ;
    cin>>numI;
    string inputI;
    getline(cin,inputI);
    int numR;
    cin>>numR;
    int count = numR;
    set<int> setR;//存放R
    while(count--)
    {
        int temp;
        cin>> temp;
        setR.insert(temp);
    }
    numR = setR.size();
    //由于不提供随机访问,将R写入vector
    vector<int> R;
    for(auto i:setR)
        R.push_back(i);
    vector<string> I;//存放I
    stringstream ss(inputI);
    string tempstr;
    while(getline(ss,tempstr,' '))
    {
        I.push_back(tempstr);
    }
    vector<vector<int>> result(numR);//存储子串含有R的I序号
    int resultNum = numR*2;
    for(int i = 0; i < I.size();i++)//i为I序号
    {
        for(int t = 0; t < R.size(); t++)//t为R序号
        {
            //将int转成string方便比较
            stringstream rss;
            string Rstr;
            rss << R[t];
            getline(rss,Rstr);
            int strlen = Rstr.size();
            if(I[i].size()<strlen)
                continue;
            //比较R[t]和I[i]中所有大小为strlen的子串
            for(int j = 0; j <= I[i].size()-strlen;j++)
            {
                string str("");
                str = I[i].substr(j,strlen);
                if(str==Rstr)
                {
                    result[t].push_back(i);
                    resultNum+=2;
                    break;
                }
            }
        }
    }
    for(auto i:result)
        if(i.size()==0)
            resultNum-=2;
    cout<<resultNum<<" ";
    for(int i = 0; i < result.size();i++)
    {
        if(result[i].size()!=0)
        {
            cout<<R[i]<<" "<<result[i].size()<<" ";
            for(auto t:result[i])
                cout<<t-1<<" "<<I[t]<<" ";
        }

       
    }





    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

你背过凌晨4点的八股文么:简历挂了的话会是流程终止,像我一样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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