UVa 1597 TLE

大神的: https://github.com/morris821028/UVa/blob/master/temp/1597%20-%20Searching%20the%20Web.cpp

GITHUB大神的代码片段,思路一样基本一致,注释处是大神的代码,起初以为是处理document判断输出 10* '-' 不同导致效率差距很大,但是把AC代码片段放进去就快了一点点,远不如大神的代码快,并非cout所致。

后来发现是stringstream导致的,真的超级超级慢,后来改成char[] 速度提升了非常多,能够在0.3S内运行完毕,但是还是wa,在Udebug上找了测试数据也没发现问题,不明。

另外重定向cout出现不明错误。

#include <iostream>
#include <map>
#include <set>
#include <climits>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <ctime>
using namespace std;
const int MAXN = 1505;
map<string, int> dict;
stringstream ss;
string line, word;
vector<int> whichPsg(MAXN);
vector<string> orignalLine(MAXN);
vector<int> articleLine(105);
map<int, set<int> > whereIsWord;
int main(){
    ios::sync_with_stdio(false);
//    ifstream myin("data.in");
//    cin.rdbuf(myin.rdbuf());
//    ofstream myout("mine.out");
//    cout.rdbuf(myout.rdbuf());
    int pasg;
    cin >> pasg;
    cin.get();
    int lineNum = 0;
//    clock_t inputStart,inputEnd, calcStart, calcEnd;
//    inputStart = clock();
    for(int i = 0 ; i < pasg; ++i){
        while(getline(cin,line)){
            articleLine[i] = lineNum;
            if(line == "**********") break;
            ss.clear();
            orignalLine[lineNum] = line;
            int stringlenth = line.length();
            for(int j = 0; j < stringlenth; ++j){
                if(isalpha(line[j])) line[j] = tolower(line[j]);
                else line[j] = ' ';
            }
            ss.str(line);
            while(ss >> word){
                if(!dict.count(word)){
                    dict[word] = dict.size();
                }
                int temp = dict[word];
                whereIsWord[temp].insert(lineNum);
            }
            whichPsg[lineNum] = i;
            ++lineNum;
        }
    }
    articleLine[pasg] = lineNum;
    int opNum;
    cin >>opNum;
    cin.get();
    for(int i = 0; i < opNum; ++i){
        vector<string> option;
        getline(cin, line);
        static char optionTemp[32768];
        int j = 0,k = 0;
        for(int j = 0; j < line.length(); ++j){
            if(isalpha(line[j])){
              k = 0;
              while(isalpha(line[j])){
                    optionTemp[k++] = tolower(line[j]);++j;
              }
//              j--;
              optionTemp[k] = '\0';
              option.push_back(optionTemp);
            }
        }
        j = option.size();
//上面为char[] 版本 下面为stringstream 超慢版本
//        for(int ii = 0; ii < j; ++ii){
//            cout <<"options: "<< option[ii] << '\n';
//        }
//        ss.str(line);
//        int j = 0;
//        while(ss >> line ){
//            option[j++] = line;
//        }
        bool found = false;
        vector<int> howManyWordsIn(pasg),howManyWordsIn_2(pasg);
        if(j == 1){
            if(dict.count(option[0])){
            int obj = dict[option[0]];
//            cout << option[0]<<'\n';
            set<int> &where = whereIsWord[obj] ;
                int lastPsg = whichPsg[*where.begin()];
                for(set<int> ::iterator it =  where.begin(); it != where.end(); ++it){
                    if(lastPsg!= whichPsg[*it]){
                        cout << "----------\n";
                        lastPsg = whichPsg[*it];
                    }
                    found = true;
                    cout << orignalLine[*it] <<'\n';
                }

//                set<int> :: iterator it = where.begin();
//                for(int I = 0 ; I < pasg; ++I){
//                    while (it != where.end() && *it < articleLine[I])
//                        ++it;
//                    if (it != where.end() && *it < articleLine[I+1]) {
//                        while (it != where.end() && *it < articleLine[j+1]){
//                            if (found){
//                                    cout << "----------\n";
//                            }
//                            found = true;
//                            cout << orignalLine[*it] << endl, ++it;
//                        }
//                    }
//                }

            }
        }else if(j == 2){
                int obj = dict[option[1]];
                set<int> &where = whereIsWord[obj];
                for(set<int> :: iterator it =  where.begin(); it != where.end(); ++it){
                    ++howManyWordsIn[whichPsg[*it]];
                }
                int lastPsg ;
                for(int I = 0; I < pasg; ++I){
                    if(howManyWordsIn[I] == 0){
                        found = true;
                        lastPsg = I;
                        break;
                    }
                }
                for(int I = 0; I < lineNum; ++I ){
                    if(howManyWordsIn[whichPsg[I]] == 0){
                        if(lastPsg != whichPsg[I]){
                            lastPsg = whichPsg[I];
                            cout << "----------\n";
                        }
                        cout << orignalLine[I] << endl;
                    }
            }
        }else{
            int obj1 = dict[option[0]], obj2 = dict[option[2]];
            set<int> &where1 = whereIsWord[obj1], &where2 = whereIsWord[obj2] ;
                for(set<int> :: iterator it =  where1.begin(); it != where1.end(); ++it){
                    ++howManyWordsIn[whichPsg[*it]];
                }
                for(set<int> ::iterator it =  where2.begin(); it != where2.end(); ++it){
                        ++howManyWordsIn_2[whichPsg[*it]];
                }
                int lastPsg ;
                if(option[1] == "and"){
                    for(int I = pasg - 1; I > -1; --I){
                        if(howManyWordsIn[I] && howManyWordsIn_2[I]){
                            found = true;
                            lastPsg = I;
                        }else
                           howManyWordsIn[I] = 0;
                    }
                }else{
                    for(int I = pasg - 1; I > -1; --I){
                        if(howManyWordsIn[I] || howManyWordsIn_2[I]){
                            found = true;
                            lastPsg = I;
                            howManyWordsIn[I] = 1 ;
                        }else
                            howManyWordsIn[I] = 0;
                    }
                }
                set<int> outputLine;
                set_union(where1.begin(),where1.end(),where2.begin(),where2.end(),inserter(outputLine,outputLine.begin()));
                for(set<int> ::iterator it = outputLine.begin(); it!= outputLine.end(); ++it){
                    if(howManyWordsIn[whichPsg[*it]]){
                        if(lastPsg != whichPsg[*it]){
                                lastPsg = whichPsg[*it];
                                cout << "----------\n";
                        }
                        cout << orignalLine[*it] << endl;
                    }
                }
        }
        if(!found) cout << "Sorry, I found nothing.\n";
        cout <<"==========\n";
    }
//    calcEnd = clock();
//    cout << "It costs\n";
//    cout << inputEnd - inputStart << ' ' << calcEnd - calcStart << ' ';
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值