| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2380 人关注过本帖, 1 人收藏
标题:非常适合初学者的题目三:百度的高级搜索方法(要求熟悉字符串的处理和文件 ...
只看楼主 加入收藏
hwdwow
Rank: 2
等 级:论坛游民
帖 子:119
专家分:98
注 册:2009-3-21
结帖率:72%
收藏(1)
 问题点数:0 回复次数:34 
非常适合初学者的题目三:百度的高级搜索方法(要求熟悉字符串的处理和文件IO)
百度的高级搜索方法(2007年初赛)
题面描述:
你尝试过在百度上使用site inurl语法查询吗? 如果还没有的话可以试一下:)
如输入 site:www.baidu.com inurl:news
则会搜出所有在www.baidu.com站点上的包含"news"子串的url。
现在我们有两份数据,一份是site_inurl.txt 一份是url.txt
site_inurl.txt中每行是一个site inurl语法组成的查询串,url.txt中保存的是url列表。
你能否在url列表中找出所有能被site_inurl.txt中的查询串检索到的url?
如site_inurl.txt内容如下:
site:www.baidu.com inurl:/more
site:zhidao.baidu.com inurl:/browse/
site:www. inurl:www20041223am
url.txt内容如下:
http://www.baidu.com/more/
http://www.baidu.com/guding/more.html
http://www.baidu.com/events/20060105/photomore.html
http://hi.baidu.com/browse/
http://hi.baidu.com/baidu/
http://www.
http://www.
则你的程序运行完输出的结果应该为:
http://www.baidu.com/more/
http://www.baidu.com/guding/more.html
http://www.
程序以命令行形式传入这两个文件名,第一个参数为site_inurl文件对应的文件名,第二个参数为url列表对应的文件名,程序的输出请输出到标准输出。
搜索更多相关主题的帖子: 文件 字符 百度 
2009-09-16 20:32
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
程序代码:
#include<iostream>                                                      
#include<string>                                                        
#include<fstream>                                                       
#include<cstdlib>                                                       
using namespace std;                                                    
int main()                                                              
{                                                                       
    string name1,name2;                                                 
    cin>>name1>>name2;                                                  
    ifstream in(name1.c_str(),ios_base::in);                            
    if( !in)                                                            
        cerr<<"file can not open"<<endl;                                
    string str;                                                         
    while(getline(in,str))                                              
    {                                                                   
        ifstream is;                                                    
        is.open(name2.c_str(),ios_base::in);                            
        if( ! is)                                                       
        {                                                               
            cerr<<"Error opening input stream"<<endl;                   
            exit(1);                                                    
        }                                                               
        size_t head=str.find("site:",0);                                
        size_t tail=str.find("inurl:",0);                               
        if( head==string::npos || tail==string::npos)                   
        {
            cerr<<" input format error"<<endl;
            exit(1);
        }
        string s_str,s_substr;
        s_str.assign(str,head+5,tail-6);
        s_substr.assign(str,tail+6,str.size()-tail);
        string s;
        while(getline(is,s))
        {

            if( s.find(s_str,0) != string::npos &&      \
                s.find(s_substr,0) != string::npos)
                cout<<s<<endl;

        }
        is.close();
    }
    in.close();
    return 0;
}
2009-09-16 23:45
限量版猪头
Rank: 2
等 级:论坛游民
威 望:1
帖 子:165
专家分:30
注 册:2006-2-5
收藏
得分:0 
这种问题,现在一般都是perl去解决了。。。

过会给出perl源码。。

[ 本帖最后由 限量版猪头 于 2009-9-17 11:14 编辑 ]

2009-09-17 11:12
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
其实我一个grep指令就可以解决了

perl你还要写各个读文件操作。
2009-09-17 11:13
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
其实归根揭底

都是正则表达式
2009-09-17 11:14
限量版猪头
Rank: 2
等 级:论坛游民
威 望:1
帖 子:165
专家分:30
注 册:2006-2-5
收藏
得分:0 
以下是引用Devil_W在2009-9-17 11:13的发言:

其实我一个grep指令就可以解决了
 
perl你还要写各个读文件操作。

grep的实现就是基于perl的,好不?

2009-09-17 11:14
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
框别人可以,

grep是shell的东西,怎么可能是基于perl
2009-09-17 11:16
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:0 
回复 6楼 限量版猪头
连我这个小菜鸟你都唬不了

我就是真命天子,顺我者生,逆我者死!
2009-09-17 11:18
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
程序代码:
/* grep.c - main driver file for grep.
   Copyright (C) 1992 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option) 
   any later version.                                                  

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
   GNU General Public License for more details.                   

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software      
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.        

   Written July 1992 by Mike Haertel.  */

#include <errno.h>
#include <stdio.h>

#ifndef errno
extern int errno;
#endif           

#ifdef STDC_HEADERS
#include <stdlib.h>
#else              
#include <sys/types.h>
extern char *malloc(), *realloc();
extern void free();               
#endif                            

#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>                                
#ifdef NEED_MEMORY_H                               
#include <memory.h>                                
#endif                                             
#else                                              
#include <strings.h>                               
#ifdef __STDC__                                    
extern void *memchr();                             
#else                                              
extern char *memchr();                             
#endif                                             
#define strrchr rindex                             
#endif                                             

#ifdef HAVE_UNISTD_H
#include <sys/types.h>
#include <fcntl.h>    
#include <unistd.h>   
#else                 
#define O_RDONLY 0    
extern int open(), read(), close();
#endif                             

#include "getpagesize.h"
#include "grep.h"       

#undef MAX
#define MAX(A,B) ((A) > (B) ? (A) : (B))

/* Provide missing ANSI features if necessary. */

#ifndef HAVE_STRERROR
extern int sys_nerr; 
extern char *sys_errlist[];
#define strerror(E) ((E) < sys_nerr ? sys_errlist[(E)] : "bogus error number")
#endif                                                                        

#ifndef HAVE_MEMCHR
#ifdef __STDC__    
#define VOID void  
#else              
#define VOID char  
#endif             
VOID *             
memchr(vp, c, n)   
     VOID *vp;     
     int c;        
     size_t n;     
{                  
  unsigned char *p;

  for (p = (unsigned char *) vp; n--; ++p)
    if (*p == c)                          
      return (VOID *) p;                  
  return 0;                               
}                                         
#endif                                    
                                          
/* Define flags declared in grep.h. */    
char *matcher;                            
int match_icase;                          
int match_words;                          
int match_lines;                          

/* Functions we'll use to search. */


很显然,grep是c的东西。
2009-09-17 11:22
BlueGuy
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:29
帖 子:4476
专家分:4055
注 册:2009-4-18
收藏
得分:0 
grep比 Perl早出来 10年

我就是真命天子,顺我者生,逆我者死!
2009-09-17 11:40
快速回复:非常适合初学者的题目三:百度的高级搜索方法(要求熟悉字符串的处理和 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013306 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved