64,643
社区成员
发帖
与我相关
我的任务
分享
void testCaseMinSequence()
{
int a[] = { 2,1,0,1,1,9,1,0,1,1 };
for (auto i: a)
{
printf("%d ", i);
}
int match = 0;
int m = sizeof(a)/sizeof(int);
cout << "\ntotal num: " << m << endl;
int n = 0;
/*计算出颜色个数*/
for (int i = 0; i < m; i++)
{
int colorBit = 1<<a[i];
//cout << (colorBit|match) << " " << (colorBit&match) << endl;
if (0 == (colorBit&match))
{
match |= colorBit;
n++;
}
}
printf("color num: %d match: %x\n", n, match);
int matchNum = m;
int matchColor = 0;
vector<int> vecPos;
/*第i位置开始, 查找比已知个数更少的包含所有颜色的位置*/
for (int i = 0; i < m; i++)
{
matchColor = 0;
for (int j = 0; j < matchNum; j++)
{
int pos = i + j;
if (pos >= m) pos -= m;
matchColor |= 1<<a[pos];
if (matchColor == match)
{
if (j + 1 < matchNum)
{
vecPos.clear();
}
matchNum = j + 1;
vecPos.push_back(i);
//cout << "push_back " << i << endl;
break;
}
}
}
printf("matchNum: %d \n", matchNum);
for (auto i : vecPos)
{
printf("matchPos: %d\n", i);
for (int j = 0; j < matchNum; j++)
{
int pos = i + j;
if (pos >= m) pos -= m;
printf("pos[%d]=%d ", pos, a[pos]);
}
printf("\n");
}
}
void testCaseMinSequence()
{
int a[] = { 2,1,0,1,1,9,1,0,1,1 };
for (auto i: a)
{
printf("%d ", i);
}
int match = 0;
int m = sizeof(a)/sizeof(int);
cout << "\ntotal num: " << m << endl;
int n = 0;
/*计算出颜色个数*/
for (int i = 0; i < m; i++)
{
int colorBit = 1<<a[i];
//cout << (colorBit|match) << " " << (colorBit&match) << endl;
if (0 == (colorBit&match))
{
match |= colorBit;
n++;
}
}
printf("color num: %d match: %x\n", n, match);
int matchNum = m;
int matchColor = 0;
vector<int> vecPos;
/*第i位置开始, 查找比已知个数更少的包含所有颜色的位置*/
for (int i = 0; i < m; i++)
{
matchColor = 0;
for (int j = 0; j < matchNum; j++)
{
int pos = i + j;
if (pos >= m) pos -= m;
matchColor |= 1<<a[pos];
if (matchColor == match)
{
if (j + 1 < matchNum)
{
vecPos.clear();
}
matchNum = j + 1;
vecPos.push_back(i);
break;
}
}
}
printf("matchNum: %d \n", matchNum);
printf("matchPos: ");
for (auto i : vecPos)
{
cout << i << endl;
for (int j = 0; j < matchNum; i++)
{
int pos = i + j;
if (pos >= m) pos -= m;
printf("%d ", a[pos]);
}
cout << endl;
}
}