QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#639882#2428. Comma SprinklerTrentiumTL 5986ms114944kbC++143.1kb2024-10-13 23:25:252024-10-13 23:25:25

Judging History

你现在查看的是最新测评结果

  • [2024-10-13 23:25:25]
  • 评测
  • 测评结果:TL
  • 用时:5986ms
  • 内存:114944kb
  • [2024-10-13 23:25:25]
  • 提交

answer

#include "bits/stdc++.h"

using namespace std;
#define forR(i, x) for(int i = 0; i < (x); ++i)
#define REP(i, a, b) for(int i =(a); i < (b); ++i)
#define boost() cin.sync_with_stdio(0); cin.tie(0)
typedef long long ll;
typedef vector<int> vi;
typedef pair<string, bool> node;

signed main(){
    boost();
    vector<string> tokens;
    string s; getline(cin, s);
    int ci = 0;
    bool addLast = false;
    while(ci < s.size()){
        if(s[ci] == ' ') {
            addLast = false;
            ++ci;
        } else if(s[ci] == ',') {
            tokens.push_back(",");
            addLast = false;
            ci += 2;
        } else if(s[ci] == '.') {
            tokens.push_back(".");
            if(ci + 1 == s.size()) ++ci;
            else ci += 2;
            addLast = false;
        } else {
            if(!addLast) {
                tokens.emplace_back();
                tokens.back().push_back(s[ci]);
                addLast = true;
                ++ci;
            } else {
                tokens.back().push_back(s[ci]);
                ++ci;
            }
        }
    }
    // for(string i : tokens) cout << i << '\n';
    vector<string> nocom;
    for(string i : tokens) if(i != ",") nocom.push_back(i);

    unordered_map<string, vector<string>> ads;
    unordered_map<string, vector<string>> adp;
    unordered_set<string> viss;
    unordered_set<string> visp;
    vector<node> bfs;

    forR(i, (int) nocom.size() - 1) if(nocom[i] != "." && nocom[i+1] != ".") {
        ads[nocom[i]].push_back(nocom[i+1]);
        adp[nocom[i+1]].push_back(nocom[i]);
    }
    forR(i, (int) tokens.size() - 1) if(tokens[i] != "." && tokens[i] != ",") {
        if(i > 0 && tokens[i-1] == ",") {
            visp.insert(tokens[i]);
            bfs.push_back({tokens[i], false});
        }
        if(i + 1 < tokens.size() && tokens[i+1] == ",") {
            bfs.push_back({tokens[i], true});
            viss.insert(tokens[i]);
        }
    }
    int tot = 0;
    while(!bfs.empty()){
        node cur = bfs.back();
        bfs.pop_back();
        ++tot;
        assert(tot < 2 * tokens.size() + 10);
        if(cur.second) {
            for(string nex : ads[cur.first]) if(!visp.count(nex)) {
                bfs.push_back({nex, false});
                visp.insert(nex);
            }
        } else {
            for(string nex : adp[cur.first]) if(!viss.count(nex)) {
                bfs.push_back({nex, true});
                viss.insert(nex);
            }
        }
    }

    vector<string> toPrint;
    forR(ci, nocom.size()) {
        string i = nocom[ci];
        if(i == ".") toPrint.push_back(". ");
        else {
            if(visp.count(i) && !toPrint.empty() && toPrint.back() != ", " && toPrint.back() != ". ") {
                toPrint.push_back(", ");
            } else if(!toPrint.empty() && toPrint.back() != ", " && toPrint.back() != ". ") {
                toPrint.push_back(" ");
            }
            toPrint.push_back(i);
        }
    }
    for(string i : toPrint) cout << i;
    cout << '\n';
}

Details

Test #1:

score: 100
Accepted
time: 0ms
memory: 3872kb

Test #2:

score: 0
Accepted
time: 0ms
memory: 3576kb

Test #3:

score: 0
Accepted
time: 0ms
memory: 3628kb

Test #4:

score: 0
Accepted
time: 0ms
memory: 3668kb

Test #5:

score: 0
Accepted
time: 0ms
memory: 3548kb

Test #6:

score: 0
Accepted
time: 83ms
memory: 99896kb

Test #7:

score: 0
Accepted
time: 136ms
memory: 76348kb

Test #8:

score: 0
Accepted
time: 160ms
memory: 77980kb

Test #9:

score: 0
Accepted
time: 1ms
memory: 3576kb

Test #10:

score: 0
Accepted
time: 0ms
memory: 3828kb

Test #11:

score: 0
Accepted
time: 1ms
memory: 3980kb

Test #12:

score: 0
Accepted
time: 0ms
memory: 3568kb

Test #13:

score: 0
Accepted
time: 1ms
memory: 3824kb

Test #14:

score: 0
Accepted
time: 0ms
memory: 3668kb

Test #15:

score: 0
Accepted
time: 0ms
memory: 3624kb

Test #16:

score: 0
Accepted
time: 882ms
memory: 114944kb

Test #17:

score: 0
Accepted
time: 651ms
memory: 98248kb

Test #18:

score: 0
Accepted
time: 543ms
memory: 107312kb

Test #19:

score: 0
Accepted
time: 0ms
memory: 3832kb

Test #20:

score: 0
Accepted
time: 353ms
memory: 90680kb

Test #21:

score: 0
Accepted
time: 206ms
memory: 80444kb

Test #22:

score: 0
Accepted
time: 160ms
memory: 72852kb

Test #23:

score: 0
Accepted
time: 17ms
memory: 21212kb

Test #24:

score: 0
Accepted
time: 5986ms
memory: 105808kb

Test #25:

score: -100
Time Limit Exceeded