saturday night (almost) live# PhotoGear - 摄影器材
w*x
1 楼
//Print strings with certain prefix in a sorted string array
void GetIndex(const char* strs[], int nBeg, int nEnd, char chr, int nPos,
OUT int& index1, int& index2);
void PrintComPrefix(const char* strs[], int n, const char* szPre)
{
assert(strs && szPre && n > 0);
const char* p = szPre;
int nIndex1 = 0;
int nIndex2 = n-1;
while (*p != 0 && nIndex1 >= 0)
{
GetIndex(strs, nIndex1, nIndex2, *p, p-szPre, nIndex1, nIndex2);
p++;
}
if (nIndex1 >= 0 && nIndex2 >= nIndex1)
{
for (int i = nIndex1; i <= nIndex2; i++)
cout< }
else cout<}
void GetIndex(const char* strs[], int nBeg, int nEnd, char chr, int nPos,
OUT int& index1, int& index2)
{
if (nBeg < 0 || nEnd < nBeg || nPos < 0)
{
index1 = index2 = -1;
return;
}
//left boundary
int s = nBeg;
int e = nEnd;
index1 = -1;
while (s <= e)
{
int nMid = (s + e)/2;
if (strs[nMid][nPos] == chr && (nMid == nBeg ||
strs[nMid][nPos] != strs[nMid-1][nPos]))
{
index1 = nMid;
break;
}
else if (strs[nMid][nPos] > chr || strs[nMid][nPos] == chr)
e = nMid - 1;
else s = nMid + 1;
}
//right boundary
s = nBeg;
e = nEnd;
index2 = -1;
while (s <= e)
{
int nMid = (s + e)/2;
if (strs[nMid][nPos] == chr && (nMid == nEnd ||
strs[nMid][nPos] != strs[nMid+1][nPos]))
{
index2 = nMid;
break;
}
else if (strs[nMid][nPos] < chr || strs[nMid][nPos] == chr)
s = nMid + 1;
else e = nMid - 1;
}
}
void GetIndex(const char* strs[], int nBeg, int nEnd, char chr, int nPos,
OUT int& index1, int& index2);
void PrintComPrefix(const char* strs[], int n, const char* szPre)
{
assert(strs && szPre && n > 0);
const char* p = szPre;
int nIndex1 = 0;
int nIndex2 = n-1;
while (*p != 0 && nIndex1 >= 0)
{
GetIndex(strs, nIndex1, nIndex2, *p, p-szPre, nIndex1, nIndex2);
p++;
}
if (nIndex1 >= 0 && nIndex2 >= nIndex1)
{
for (int i = nIndex1; i <= nIndex2; i++)
cout<
else cout<}
void GetIndex(const char* strs[], int nBeg, int nEnd, char chr, int nPos,
OUT int& index1, int& index2)
{
if (nBeg < 0 || nEnd < nBeg || nPos < 0)
{
index1 = index2 = -1;
return;
}
//left boundary
int s = nBeg;
int e = nEnd;
index1 = -1;
while (s <= e)
{
int nMid = (s + e)/2;
if (strs[nMid][nPos] == chr && (nMid == nBeg ||
strs[nMid][nPos] != strs[nMid-1][nPos]))
{
index1 = nMid;
break;
}
else if (strs[nMid][nPos] > chr || strs[nMid][nPos] == chr)
e = nMid - 1;
else s = nMid + 1;
}
//right boundary
s = nBeg;
e = nEnd;
index2 = -1;
while (s <= e)
{
int nMid = (s + e)/2;
if (strs[nMid][nPos] == chr && (nMid == nEnd ||
strs[nMid][nPos] != strs[nMid+1][nPos]))
{
index2 = nMid;
break;
}
else if (strs[nMid][nPos] < chr || strs[nMid][nPos] == chr)
s = nMid + 1;
else e = nMid - 1;
}
}