UVA 1597 Search the Web

来源:互联网 发布:天人网络电视官网 编辑:程序博客网 时间:2024/06/02 19:36

这个题就是考察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;}


0 0