Programming 版 (精华区)
发信人: hitool (HIT-007), 信区: Programming
标 题: [程序大战]20040720守擂程序的改进实现
发信站: 哈工大紫丁香 (Tue Aug 3 17:20:42 2004), 站内
本实现避免了原守擂程序重复比较的问题,充分利用了已有的比较结果,进而获得效率的提升。本实现是在原有守擂程序基础上作的修改,所以只把修改的函数贴出来,请大家指正。
void StrFilter(LPBYTE pBuffer, DWORD dwFileSize, int nLen, CString &sResult)
{
std::vector<Filter> vFilter;
DWORD dwID = 0;
int len = 0;
int len1=0;
for (DWORD i = nLen-1; i < dwFileSize;)
{
// 2004-7-23 9:57 最短长度处字符判断
BYTE ch = *(pBuffer + i);
if (!(ch >= 32 && ch <= 126))
{
i += nLen;
continue;
}
len=1;
ch = *(pBuffer + i-len );
for(; ch >= 32 && ch <= 126; )
ch = *(pBuffer + i -(++len));
ch = *(pBuffer + i+1 );
for(len1=1; ch >= 32 && ch <= 126; )
ch = *(pBuffer + i +(++len1));
len+=len1-1;
if (len >=nLen)
{
// 长度合格
Filter f;
f.dwID = dwID ++;
f.dwOffset = i-(len-len1) ;
f.lpString = new char[len+1];
if (!f.lpString ) return ;
strncpy(f.lpString , (LPCTSTR)pBuffer + i-(len-len1), len);
f.lpString[len] = 0;
f.nLength = len;
vFilter.push_back(f);
}
i+=len1+nLen;
}
std::vector<char> vBuffer;
for (PFilter p = vFilter.begin(); p != vFilter.end(); p ++)
{
CString sBuf;
sBuf.Format("%u: Offset:%u Length:%d\t%s\r\n",
p->dwID, p->dwOffset, p->nLength,p->lpString);
for ( int j = 0; j < sBuf.GetLength(); j ++)
vBuffer.push_back(sBuf.GetAt(j));
delete(p->lpString);
}
vBuffer.push_back(0);
sResult = &vBuffer[0];
}
void CTest1Dlg::OnButton3()
{
// TODO: Add your control notification handler code here
CRichEditCtrl *pRiched = &m_ctrlRichEd;
CLocalCalculagraph cc;
CFile f;
if (!f.Open("c:\\downloads\\i\\e-cpp2ga.rar",CFile::modeRead))
return;
DWORD dwSize = f.GetLength();
LPBYTE pBuffer = new BYTE[dwSize + 2];
f.Read(pBuffer+1 ,dwSize );
f.Close();
*pBuffer=0;
*(pBuffer+dwSize+1)=0;
cc.Start();
CString sResult;
StrFilter(pBuffer+1 , dwSize, 5, sResult);
cc.Stop();
pRiched->SetWindowText(sResult);
delete[] pBuffer ;
// 以下是用于评测的结果
CString sMsg;
sMsg.Format("Used time:%ums", cc.GetElapsedTime() );
AfxMessageBox(sMsg);
}
--
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 218.9.117.150]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:5.422毫秒