UVA 1597 Search the Web

本文介绍了一种使用STL容器优化文章搜索的方法,通过构建映射关系,实现高效的文章内容查找。该方法避免了暴力扫描可能导致的超时问题,通过map容器将文章中的单词与其行数进行对应,进一步利用web数组存储所有文本信息,同时通过lines数组记录每篇文章的起始和终止行。此外,提供了一个字符串转换函数,用于处理输入的关键词,确保大小写统一并移除非字母字符。通过这种方式,能够快速定位并输出符合关键词的文章内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个题就是考察STL容器的综合运用,由于文章比较长并且请求很多,直接暴力扫肯定会超时,所以需要用map作个映射。

我的思路:1.用一个map<int,map<string,set<int>>>,第一个int代表第几篇文章,第二个map把单词及其行数对应起来,因为一个单词可能在同一行中出现两次,所以用了 一 个                           set来存单词的行位置.                        

                        因为要输出相应的文本,所以用一个web数组存下所有的输入,行数用line计算。                       

                         lines存储每篇文章的起始行和终止行。                       

                         2.由于要查询的关键词都是由字母组成的,并且忽略大小写,所以可以把输入的字符串处理一下,把字母都变成小写,非字母变成空格,再用stringstream读取。

#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<map>
#include<sstream>
#include<cstdio>
#include<set>
using namespace std;
map<int,map<string,set<int> > >Search; //int表示第几篇文章,第二个map中的string代表文章中的单词,最里面的int代表行数
vector<string>web; //存放所有文本
vector<pair<int,int> >lines;
string trans(string &s)
{
    int len = s.length();
    for(int i = 0; i < len; i++)
        if(!isalpha(s[i])) s[i] = ' ';
        else s[i] = tolower(s[i]);
    return s;
}
int main()
{
    int i,j,k,line = 1;
    int n;
    cin >> n;
    pair<int,int>ha;
    getchar();
    string s1,s2;
    for(i = 1; i <= n; i++){
       ha.first = line-1;
       while(1){
         getline(cin,s1);
         web.push_back(s1);
         ++line;
         if(s1[0] == '*') {ha.second = line - 3; lines.push_back(ha); break;}
         s2 = trans(s1);
         stringstream ss(s2);
         while(ss >> s1) {
            if(!Search[i].count(s1)) Search[i][s1] = set<int>();
            Search[i][s1].insert(line-2);
         }
       }
    }
    int m;
    cin >> m;
    getchar();
    for(i = 1; i <= m; i++){
        string s;
        getline(cin,s);
        int no = 1,first = 1;
        int k1 = s.find(" AND "),k2 = s.find(" OR "),k3 = s.find("NOT ");
        if(k1 != -1 || k2 != -1){
            s1 = s.substr(0,(k1 > 0 ? k1 : k2));
            s2 = s.substr(k1 > 0 ? k1+5 : k2+4);
            set<int>cnt;
            for(j = 1; j <= n; j++){
                cnt.clear();
                int x = 0, y = 0;
                if(Search[j].count(s1)){
                    for(set<int>::iterator it = Search[j][s1].begin(); it != Search[j][s1].end(); ++it)
                        cnt.insert(*it);
                    x = 1;
                }
                if(Search[j].count(s2)){
                    for(set<int>::iterator it = Search[j][s2].begin(); it != Search[j][s2].end(); ++it)
                        cnt.insert(*it);
                        y = 1;
                }
                if((k1 != -1 && x && y) || (k2 != -1 && (x || y))){
                    if(!first) cout << "----------\n";first = no = 0;
                    for(set<int>::iterator it = cnt.begin(); it != cnt.end(); ++it)
                        cout << web[*it] << endl;
                }
            }
        }
        else if(k3 != -1){
            s1 = s.substr(4);
            for(j = 1; j <= n; j++)
                if(!Search[j].count(s1)){
                    if(!first) cout << "----------\n";
                    for(k = lines[j-1].first; k <= lines[j-1].second; k++)
                        cout << web[k] << endl;
                    first = no = 0;
                }
        }
        else{
            for(j = 1; j <= n; j++){
                if(Search[j].count(s)){
                    if(!first) cout << "----------\n";
                    no = first = 0;
                    for(set<int>::iterator it = Search[j][s].begin(); it != Search[j][s].end(); ++it)
                        cout << web[*it] << endl;
                }
            }
        }
        if(no)cout << "Sorry, I found nothing.\n";
        cout << "==========\n";
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值