数据分类处理/华为机试(C/C++)

版权声明:本文为博主原创文章,转载请注明出处 https://blog.csdn.net/thecentry。 https://blog.csdn.net/thecentry/article/details/82431957

题目描述

信息社会,有海量的数据需要分析处理,比如公安局分析身份证号码、 QQ 用户、手机号码、银行帐号等信息及活动记录。  

采集输入大数据和分类规则,通过大数据分类处理程序,将大数据分类输出。

输入描述:

一组输入整数序列I和一组规则整数序列R,I和R序列的第一个整数为序列的个数(个数不包含第一个整数);整数范围为0~0xFFFFFFFF,序列个数不限

输出描述:

从R依次中取出R<i>,对I进行处理,找到满足条件的I<j>: 

I<j>整数对应的数字需要连续包含R<i>对应的数字。比如R<i>为23,I<j>为231,那么I<j>包含了R<i>,条件满足 。 

按R<i>从小到大的顺序:

(1)先输出R<i>; 

(2)再输出满足条件的I<j>的个数; 

(3)然后输出满足条件的I<j>在I序列中的位置索引(从0开始); 

(4)最后再输出I<j>。 

附加条件: 

(1)R<i>需要从小到大排序。相同的R<i>只需要输出索引小的以及满足条件的I<j>,索引大的需要过滤掉 

(2)如果没有满足条件的I<j>,对应的R<i>不用输出 

(3)最后需要在输出序列的第一个整数位置记录后续整数序列的个数(不包含“个数”本身)

序列I:15,123,456,786,453,46,7,5,3,665,453456,745,456,786,453,123(第一个15表明后续有15个整数) 

序列R:5,6,3,6,3,0(第一个5表明后续有5个整数) 

输出:30, 3,6,0,123,3,453,7,3,9,453456,13,453,14,123,6,7,1,456,2,786,4,46,8,665,9,453456,11,456,12,786

说明:

30----后续有30个整数

3----从小到大排序,第一个R<i>为0,但没有满足条件的I<j>,不输出0,而下一个R<i>是3

6--- 存在6个包含3的I<j> 

0--- 123所在的原序号为0 

123--- 123包含3,满足条件 

示例1

输入

15 123 456 786 453 46 7 5 3 665 453456 745 456 786 453 123
5 6 3 6 3 0

输出

30 3 6 0 123 3 453 7 3 9 453456 13 453 14 123 6 7 1 456 2 786 4 46 8 665 9 453456 11 456 12 786

代码1:

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
bool match(int m,int n)
{
    string str1=to_string(m);
    string str2=to_string(n);
    int pos=str2.find(str1);
    if(pos!=-1)
        return true;
    else
        return false;
}
int main()
{
    int m,n;
    while(cin>>m)
    {
        vector<int> I;
        vector<int> R;
        for(int i=0;i<m;i++)
        {
            int  temp;
            cin>>temp;
            I.push_back(temp);         
        }
        cin>>n;
        for(int i=0;i<n;i++)
        {
            int  temp;
            cin>>temp;
            R.push_back(temp);         
        }
        sort(R.begin(),R.end());//排序
        R.erase(unique(R.begin(), R.end()), R.end());//去重
         
        vector< vector<int>> result;
        result.clear();
        int count=0;
        for(int i=0;i<R.size();i++)
        {
            int cnt1=0;//每个R[i]在I中存在的个数
            vector<int> temp;
            temp.push_back(R[i]);
            temp.push_back(0);
            for(int pos=0;pos<I.size();pos++)//遍历I
            {             
                if(match(R[i],I[pos]))//如果R[i]在I中
                {
                    cnt1++;//计数++
                    temp.push_back(pos);//把相应位置压入index
                    temp.push_back(I[pos]);//把在I中相应位置的值压入value
                }
            }
            if(cnt1!=0)//判断每个R[i]在I中的个数是否为0
            {
                temp[1]=cnt1;//把R[i]在I中对应找到的个数存入cnt
                count+=2+cnt1*2;
                result.push_back(temp);
            } 
        }       
 
        cout<<count;
        for(int i=0;i<result.size();i++)
       {
            int pcount=result[i][1];          
            for(int j=0;j<=pcount;j++)
           {
                cout<<" "<<result[i][j*2]<<" "<<result[i][j*2+1];
            }
             
        }
        cout<<endl;
 
    }
    return 0;
}




代码1,非本人所写;

代码2:

//第二十五题 数据分类处理
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

typedef struct Deg
{
	int i_num;
	string str;
	vector<int>vPosition;
};
bool Myfunction(Deg a, Deg b);
bool Myfunction(Deg a, Deg b)
{
	int ia = atoi(a.str.data());
	int ib = atoi(b.str.data());
	return (ia < ib);
}
int main()
{
	int num_I;
	int num_R;
	while (scanf("%d", &num_I) != EOF)
	{
		vector<string>vI;
		for (int i = 0; i < num_I; i++)
		{
			string temp;
			cin >> temp;
			vI.push_back(temp);
		}
		vector<Deg>vR;
		cin >> num_R;

		for (int i = 0; i < num_R; i++)
		{
			string temp;
			cin >> temp;
			Deg sTemp{ 0,temp };
			vR.push_back(sTemp);
		}
		sort(vR.begin(), vR.end(), Myfunction);
		int iMax = num_R - 1;
		for (int i = 0; i < iMax; i++)
		{
			if (vR[i].str == vR[i + 1].str)
			{
				vR.erase(vR.begin() + i, vR.begin() + i + 1);
				i--;
				iMax--;
			}
		}
		iMax++;
		//	int iMax = vR.size();
		int totalNum = 0;
		for (int i = 0; i < iMax; i++)
		{
			for (int j = 0; j < num_I; j++)
			{
				if (vI[j].find(vR[i].str) != string::npos)
				{
					vR[i].i_num++;
					vR[i].vPosition.push_back(j);
				}
			}
			if (vR[i].i_num)
			{
				totalNum += vR[i].i_num * 2 + 2;
			}
		}
		cout << totalNum << " ";
		for (int i = 0; i < iMax; i++)
		{
			if (vR[i].i_num)
			{
				cout << vR[i].str.c_str() << " " << vR[i].i_num << " ";
				for (int j = 0; j < vR[i].vPosition.size(); j++)
				{
					int iPosiotn = vR[i].vPosition[j];
					cout << iPosiotn << " " << vI[iPosiotn] << " ";
				}
			}
		}
		vR.clear();
		vI.clear();
        cout<<endl;
	}
	system("pause");
	return 0;
}


猜你喜欢

转载自blog.csdn.net/thecentry/article/details/82431957