题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
/*
思路:对于R中的每个数,根据其位数c,将I表中的每个数按照c拆分,eg: I中的某一个数位123456,R中的某一个数位26,则c = 2, 故I中的数123456就拆分为12 23 34 45 56,将这些数存到一张表中,然后用这张表对比R中的数26,如果存在26的话,就说明这个数是符合要求的,用map<int, map<int,int>>来存储r和I中符合要求的数的值和下标即可
eg: map<int, map<int,int>>存完之后应该是这样的:
3 0 3 7 9 13 14 I中含有数字3(R中的一个数)的数的下标)
123 453 3 456456 453 123 (I中含有数字3的数字)
6----......(形式同上)
然后将该哈希表按照要求输出就行
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <map>
using namespace std;
int wc(int input){
int c = 0;
if(input == 0){
return 1;
}
while(input){
int tmp = input % 10;
input /= 10;
c++;
}
return c;
}
vector<int> check(vector<long long> arr, int x) { // 获取重复数字下标
vector<int> pos;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] == x) {
pos.push_back(i);
}
}
return pos;
}
vector<int> splitI(int input, int dc){
vector<int> res;
vector<int> temp;
int c = 0;
while(input){
int tmp = input % 10;
temp.push_back(tmp);
input /= 10;
c++;
}
reverse(temp.begin(), temp.end());
for(int i = 0; i <= c-dc; i++){
int x = dc;
int j = i;
int r = 0;
while(j < i+dc && x){
r += pow(10,--x) * temp[j++];
}
res.push_back(r);
}
return res;
}
void removeDuplicates(vector<int>& nums) { // 删去数组中的重复元素
if (nums.empty()) return ;
int i = 0;
for (int j = 1; j < nums.size(); ++j) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
}
int main() {
int Rn, In;
vector<int> R;
vector<long long> I;
cin >> In;
int n = In;
while (n--) {
int x;
cin >> x;
I.push_back(x);
}
cin >> Rn;
n = Rn;
while (n--) {
int x;
cin >> x;
R.push_back(x);
}
sort(R.begin(), R.end());
removeDuplicates(R);
map<int, map<int, int>> m;
for(auto r : R){
int w = wc(r); // 计算R中当前数字的位数
for(int i = 0; i < In; i++){
vector<int> t = splitI(I[i], w);
int ts = t.size();
for(int j = 0; j < ts; j++){
if(t[j] == r){
m[r][i] = I[i]; // 构造哈希表,存储下标和数字
}
}
}
}
int count = 0;
for(auto val : m){
count += val.second.size()*2;
}
count += m.size() * 2;
cout << count << ' '; // 要输出的总数
for(auto val : m){
cout << val.first << ' ' << val.second.size() << ' ';
for(auto v : val.second){
cout << v.first << ' ' << v.second << ' ';
}
}
cout << endl;
return 0;
}
