文档介绍:百度面试笔试题 解答答案
专家回答:
第一题
   简评
   百度的主要业务是搜索,搜索的基本原理如下
1.编写爬虫程序到互联网上抓取网页海量的网页。
2.将抓取来的网页通过抽取,以一定的格式保存在能快速检索的文件系统中。
3.把用户输入的字符串进行拆分成关键字去文件系统中查询并返回结果。
由以上3点可见,字符串的分析,抽取在搜索引擎中的地位是何等重要。
因此,百度的笔试面试题中,出现这样的题就变得理所当然了。
以下是该题的java实现,代码如下:
程序代码 程序代码
import javanet*;
import javaio*;
import javautil*;
/** * ***@author tzy * 在j2sdk142下测试通过 */
public class FileNameStat{
    private String srcPath;//要统计的文件路径
    private Map statMap;//用于统计的map
   
    public FileNameStat(String srcPath)
    {
       thissrcPath=srcPath; 软件开发网 wwwmsctocom
       statMap=new TreeMap();
    }
   
    /*获得要统计的URL的文件名*/
    public String getFileName(String urlString)
    {
       URL url=null;
       String filePath=null;
       String fileName=null;
       try
       {
           url=new URL(urlString);
           filePath=urlgetPath();
           int index=0;
           if ((index=filePathlastIndexOf("/"))!=-1)
           {
           fileName=filePathsubstring(index+1);
           }
           else
           {
               fileName="";
           }
       }
       catch(MalformedURLException e)
       {
       }
       return fileName;
    }
   
    /*统计指定文件名的个数*/
    public void stat(String filename)
    {
       Integer count=null;
       if(statMapget(filename)!=null)
       {
           count=(Integer)statMapget(filename);