admin管理员组

文章数量:815323

字节跳动校招题

不会搞循环队列,边界情况分开讨论了一下

#include <bits/stdc++.h>
using namespace std;struct zhuzi{vector<int> color;
};int main(){int n,m,c;scanf("%d%d%d",&n,&m,&c);zhuzi *O = new zhuzi[10005];for(int i = 0;i < n;i ++){int num_i;scanf("%d",&num_i);for(int j = 0;j < num_i;j ++){int x;scanf("%d",&x);O[i].color.push_back(x);    ///bug2,之前写成O[j]了,应该以珠子号i为准,这里0-4代表珠子编号1-5}}int pos = 0;    //距离差值int hash[c+1];    //每m一组,如m = 3,则0,1,2;1,2,3;2,3,4...,每组初始化一遍hash数组bool hash2[c+1];    ///bug1,注意三个颜色1-3,单元数应该有4个0-3,0号作废memset(hash2,false,sizeof(hash2));int count = 0;    //不满足的珠子个数while(pos < n){memset(hash,0,sizeof(hash));if(pos + m <= n){for(int i = pos;i < pos + m;i ++)for(int j = 0;j < O[i].color.size();j ++)hash[O[i].color[j]] ++;for(int i = 1;i <= c;i ++)///bug1,之前从0号颜色遍历到2号就截止了,这样3号颜色会丢失if(hash[i] > 1 && !hash2[i]){//cout << i << " ";count ++;hash2[i] = true;}pos ++;}else if(pos + m > n){for(int i = pos;i < n;i ++)for(int j = 0;j < O[i].color.size();j ++)hash[O[i].color[j]] ++;    //3,4int sup = pos + m - n;//超过的数目,需要从0号开始重新比对,如n=5,m=3,则需要3,4,0,sup = 1for(int i = 0;i < sup;i ++)for(int j = 0;j < O[i].color.size();j ++)hash[O[i].color[j]] ++;    //0for(int i = 1;i <= c;i ++)///bug1,之前从0号颜色遍历到2号就截止了,这样3号颜色会丢失if(hash[i] > 1 && !hash2[i]){//cout << i << " ";count ++;hash2[i] = true;}pos ++;}}//cout << endl;cout << count << endl;return 0;
}

本文标签: 字节跳动校招题