注册 | 登录 忘记密码? 51cto首页 | 博客 | 论坛 | 招聘
热点文章 CCIE-Lab考试将新增10分钟..
 帮助

百度之星第一题竟然没过


2006-06-02 22:07:23
 标签:百度   [推送到技术圈]

找了一下bug,还是发现自己思维不够严密,左部为一个字符时竟然没法转换,改了一下,以后再看罗。

#include "stdio.h"
#include "string"
#include "string.h"
#include "iostream"
#include "map"
using namespace std;


#define MAX 1002
char strIn[MAX];
int nLen ,nLast;
class NodeType
{
public:
string strValue;
map<char,NodeType> mChildren;
};

NodeType Tree;
inline void InsertString(const string & str1,const string & str2)
{
int n=(int)str1.length();
NodeType *pCurNode = &Tree;
NodeType *pNextNode = NULL;
NodeType node;
// search the tree
for(int i=0;i<n;i++)
{
// not found cur char
if(pCurNode->mChildren.find(str1[i]) == pCurNode->mChildren.end())
{
node.strValue="";
node.mChildren.clear();
pCurNode->mChildren[str1[i]] = node;

}
// to next char
pCurNode = &(pCurNode->mChildren[str1[i]]);
}
// insert str2
pCurNode->strValue = str2;
return ;
}
inline void printCurString(int& bBegin, int nEnd)
{
for(int i=bBegin;i<=nEnd;i++) cout<<strIn[i];
bBegin = nEnd+1;
return ;
}
int Solve()
{
NodeType *pCurNode = NULL;
NodeType *pNextNode = NULL;
NodeType node;
string strMax;
int nLast =0;
int nEnd;
int i=0;
bool bGet;
while(i<nLen)
{
nLast = i;
nEnd = i;
pCurNode = &Tree;
strMax=pCurNode->strValue;
bGet = false;
while( i< nLen && pCurNode->mChildren.find(strIn[i]) != pCurNode->mChildren.end())
{
pCurNode = &(pCurNode->mChildren[strIn[i]]);
if(!pCurNode->strValue.empty())
{
strMax = pCurNode->strValue;
nEnd = i;
bGet = true;
}
i++;
}
i= nEnd +1;
if(bGet)
{
cout<<strMax;
}
else
{
printCurString(nLast,nEnd);
}
}

return 0;
}

int main(int argc, char* argv[])
{
int n;
freopen(argv[1],"r",stdin);
cin>>n;
string str1,str2;
//read data and struct the tree
while(n--)
{
cin>>str1>>str2;
InsertString(str1,str2);
}

cin>>strIn;

nLen= (int)strlen(strIn);
nLast =0;
Solve();
return 0;
}





    文章评论
 
2006-06-05 15:46:07
哪个字符?没看出来

2006-06-06 21:43:16
呵呵,是指形如 A 代表 xxx的时候辨识不了,贴出来的code已经改过来了..

2006-06-07 16:48:08
大约6年前,我也写这样的代码,呵呵,看着真亲切

2006-06-10 23:04:31
o?能说说那些亲切但现在又让你摒弃了的代码的形式吗?
或者给点儿建议过来?呵呵

 

发表评论

昵   称:
验证码:  点击图片可刷新验证码  博客过2级,无需填写验证码
内   容: