1597:Searching the Web

45 篇文章 0 订阅
27 篇文章 0 订阅

Searching the Web


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

version 1(Time limit exceeded):

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
typedef pair<int,int> P;
vector<string>docus[maxn];
map<string,vector<P> >idx;
string w[] = {"the","a","to","and","or","not"};
set<string> 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);
            set<string>sl; //one line may includes two or more same word
            while(ss>>s){
                if(stw.count(s)) continue;
                if(!idx.count(s)) idx[s] = vector<P>();
                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<P>res;
        stringstream ss(s);
        if(s[0] == 'N'){
            ss>>a;ss>>a;
            set<int>ds;
            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;
            set<int>ad,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<int>::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<<docus[res[i].first][res[i].second]<<endl;
            }
        }
        printf("==========\n");
    }
    return 0;
}

还是WA。。。。。。

version 2:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
typedef pair<int,int> P;
vector<string>docus[maxn];
map<string,vector<P> >idx;
string w[] = {"the","a","to","and","or","not"};
set<string> 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);
            set<string>sl; //one line may includes two or more same word
            while(ss>>s){
                if(stw.count(s)) continue;
                if(!idx.count(s)) idx[s] = vector<P>();
                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<P>res;
        stringstream ss(s);
        if(s[0] == 'N'){
            ss>>a;ss>>a;
            set<int>ds;
            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<<docus[res[i].first][res[i].second]<<endl;
            }
        }
        printf("==========\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值