1597:Searching the Web

Searching the Web


这道题就 "and" 查询麻烦点,别的还好,但是刚开始超时了。。。

version 1(Time limit exceeded):

#include
using namespace std;
const int maxn = 100 + 5;
typedef pair P;
vectordocus[maxn];
map >idx;
string w[] = {"the","a","to","and","or","not"};
set stw(w,w + sizeof(w)/sizeof(*w));
int cmp(P a,P b){
    if(a.first != b.first) return a.first < b.first;
    return a.second < b.second;
}
void print(){
    int n = 10;
    while(n--) putchar('-');
    putchar('\n');
}
int main(){
    int n,m;
    string s;
    scanf("%d",&n);getchar();
    for(int i = 0;i < n;i++){
        int cnt = 0;
        while(getline(cin,s) && s[0] != '*'){
            docus[i].push_back(s);
            for(int i = 0;i < s.size();i++){
                s[i] = tolower(s[i]);
                if(!isalpha(s[i])) s[i] = ' ';
            }
            stringstream ss(s);
            setsl; //one line may includes two or more same word
            while(ss>>s){
                if(stw.count(s)) continue;
                if(!idx.count(s)) idx[s] = vector

(); if(!sl.count(s)){ idx[s].push_back(make_pair(i,cnt)); sl.insert(s); } } cnt++; } } scanf("%d",&m);getchar(); string a,b; while(m--){ P t1,t2; getline(cin,s); vector

res; stringstream ss(s); if(s[0] == 'N'){ ss>>a;ss>>a; setds; for(int i = 0;i < idx[a].size();i++) ds.insert(idx[a][i].first); for(int i = 0;i < n;i++){ if(!ds.count(i)){ for(int j = 0;j < docus[i].size();j++) res.push_back(make_pair(i,j)); } } } else if(s.find('A') != string::npos){ ss>>a;ss>>s;ss>>b; setad,bd,d; for(int i = 0;i < idx[a].size();i++) ad.insert(idx[a][i].first); for(int i = 0;i < idx[b].size();i++) bd.insert(idx[b][i].first); for(set::iterator i = ad.begin(),j = bd.begin();i != ad.end() && j != bd.end();){ if(*i == *j){ d.insert(*i); i++,j++; } if(*i < *j) i++; if(*i > *j) j++; } int i = 0,j = 0; for(;i < idx[a].size() && j < idx[b].size();){ t1 = idx[a][i],t2 = idx[b][j]; if(t1 == t2 && d.count(t1.first)){ res.push_back(t1); i++,j++; continue; } if(t1 == t2) continue; if(cmp(t1,t2)){ if(d.count(t1.first)){ res.push_back(t1); i++; } }else{ if(d.count(t2.first)){ res.push_back(t2); j++; } } } if(j < idx[b].size()){ a = b; i = j; } if(i < idx[a].size()){ for(;i < idx[a].size();i++){ t1 = idx[a][i]; if(d.count(t1.first)) res.push_back(idx[a][i]); } } } else if(s.find('O') != string::npos){ ss>>a;ss>>s;ss>>b; int i = 0,j = 0; for(;i < idx[a].size() && j < idx[b].size();){ t1 = idx[a][i],t2 = idx[b][j]; if(t1 == t2){ res.push_back(t1); i++; j++; } else if(cmp(t1,t2)){ res.push_back(t1); i++; } else { res.push_back(t2); j++; } } if(j < idx[b].size()){ a = b; i = j; } if(i < idx[a].size()){ for(;i < idx[a].size();i++) res.push_back(idx[a][i]); } } else res.assign(idx[s].begin(),idx[s].end()); if(!res.size()) cout<<"Sorry, I found nothing.\n"; else{ int cnt = 0,pre = res[0].first; for(int i = 0;i < res.size();i++){ if(++cnt > 1 && res[i].first != pre) { pre = res[i].first; print(); } cout<

还是WA。。。。。。

version 2:

#include
using namespace std;
const int maxn = 100 + 5;
typedef pair P;
vectordocus[maxn];
map >idx;
string w[] = {"the","a","to","and","or","not"};
set stw(w,w + sizeof(w)/sizeof(*w));
int cmp(P a,P b){
    if(a.first != b.first) return a.first < b.first;
    return a.second < b.second;
}
void print(){
    int n = 10;
    while(n--) putchar('-');
    putchar('\n');
}
int main(){
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    int n,m;
    string s;
    scanf("%d",&n);getchar();
    for(int i = 0;i < n;i++){
        int cnt = 0;
        while(getline(cin,s) && s[0] != '*'){
            docus[i].push_back(s);
            for(int i = 0;i < s.size();i++){
                s[i] = tolower(s[i]);
                if(!isalpha(s[i])) s[i] = ' ';
            }
            stringstream ss(s);
            setsl; //one line may includes two or more same word
            while(ss>>s){
                if(stw.count(s)) continue;
                if(!idx.count(s)) idx[s] = vector

();                 if(!sl.count(s)){                     idx[s].push_back(make_pair(i,cnt));                     sl.insert(s);                 }             }             cnt++;         }     }     scanf("%d",&m);getchar();     string a,b;     while(m--){         P t1,t2;         getline(cin,s);         vector

res;         stringstream ss(s);         if(s[0] == 'N'){             ss>>a;ss>>a;             setds;             for(int i = 0;i < idx[a].size();i++) ds.insert(idx[a][i].first);             for(int i = 0;i < n;i++){                 if(!ds.count(i)){                     for(int j = 0;j < docus[i].size();j++) res.push_back(make_pair(i,j));                 }             }         }         else if(s.find('A') != string::npos){             ss>>a;ss>>s;ss>>b;             int i = 0,j = 0;             while(i < idx[a].size() && j < idx[b].size()){                 while(i < idx[a].size() && idx[a][i].first < idx[b][j].first){                     if(res.size() && idx[a][i].first == res.back().first) res.push_back(idx[a][i]);                     i++;                 }                 if(i == idx[a].size()) break;                 while(j < idx[b].size() && idx[b][j].first < idx[a][i].first){                     if(res.size() && idx[b][j].first == res.back().first) res.push_back(idx[b][j]);                     j++;                 }                 if(j == idx[b].size()) break;                 t1 = idx[a][i],t2 = idx[b][j];                 if(t1 == t2){ res.push_back(t1); i++; j++; }                 else if(cmp(t1,t2)){ res.push_back(t1); i++; }                 else { res.push_back(t2); j++; }             }             if(res.size()){                 int td = res.back().first;                 if(j < idx[b].size()){ a = b; i = j; }                 for(;i < idx[a].size() && idx[a][i].first == td;i++) res.push_back(idx[a][i]);             }         }         else if(s.find('O') != string::npos){             ss>>a;ss>>s;ss>>b;             int i = 0,j = 0;             while(i < idx[a].size() && j < idx[b].size()){                 t1 = idx[a][i],t2 = idx[b][j];                 if(t1 == t2){ res.push_back(t1); i++; j++; }                 else if(cmp(t1,t2)){ res.push_back(t1); i++; }                 else { res.push_back(t2); j++; }             }             if(j < idx[b].size()){ a = b; i = j; }             for(;i < idx[a].size();i++) res.push_back(idx[a][i]);         }         else res.assign(idx[s].begin(),idx[s].end());         if(!res.size()) cout<<"Sorry, I found nothing.\n";         else{             int pre = res[0].first;             for(int i = 0;i < res.size();i++){                 if(res[i].first != pre) { pre = res[i].first; print(); }                 cout<


转载于:https://www.cnblogs.com/JingwangLi/p/10202724.html

你可能感兴趣的:(1597:Searching the Web)