QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#639917#2428. Comma SprinklerTrentiumAC ✓1017ms121084kbC++143.3kb2024-10-13 23:52:582024-10-13 23:52:58

Judging History

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

  • [2024-10-13 23:52:58]
  • 评测
  • 测评结果:AC
  • 用时:1017ms
  • 内存:121084kb
  • [2024-10-13 23:52:58]
  • 提交

answer

#include "bits/extc++.h"

using namespace std;
using namespace __gnu_pbds;
#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 gp_hash_table<ll, vector<ll>> string_map;
typedef gp_hash_table<ll, bool> string_set;
typedef pair<ll, bool> node;

const ll B = 857, M = 783628135776413;

ll hasher(const string& x) {
    ll cur = 15;
    for(char i : x) cur = (cur * B + i) % M;
    return cur;
}

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);

    string_map ads;
    string_map adp;
    string_set viss;
    string_set visp;
    vector<node> bfs;

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

    vector<string> toPrint;
    forR(ci, nocom.size()) {
        string i = nocom[ci];
        if(i == ".") toPrint.push_back(". ");
        else {
            if(visp[hasher(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: 1ms
memory: 3868kb

Test #2:

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

Test #3:

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

Test #4:

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

Test #5:

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

Test #6:

score: 0
Accepted
time: 60ms
memory: 94740kb

Test #7:

score: 0
Accepted
time: 102ms
memory: 82992kb

Test #8:

score: 0
Accepted
time: 99ms
memory: 84092kb

Test #9:

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

Test #10:

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

Test #11:

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

Test #12:

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

Test #13:

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

Test #14:

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

Test #15:

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

Test #16:

score: 0
Accepted
time: 204ms
memory: 76784kb

Test #17:

score: 0
Accepted
time: 189ms
memory: 94896kb

Test #18:

score: 0
Accepted
time: 203ms
memory: 121084kb

Test #19:

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

Test #20:

score: 0
Accepted
time: 209ms
memory: 88348kb

Test #21:

score: 0
Accepted
time: 167ms
memory: 85700kb

Test #22:

score: 0
Accepted
time: 159ms
memory: 83072kb

Test #23:

score: 0
Accepted
time: 45ms
memory: 20812kb

Test #24:

score: 0
Accepted
time: 631ms
memory: 102896kb

Test #25:

score: 0
Accepted
time: 1017ms
memory: 108992kb

Test #26:

score: 0
Accepted
time: 103ms
memory: 84464kb

Test #27:

score: 0
Accepted
time: 44ms
memory: 67652kb

Test #28:

score: 0
Accepted
time: 464ms
memory: 102432kb

Test #29:

score: 0
Accepted
time: 28ms
memory: 7760kb

Test #30:

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

Test #31:

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

Test #32:

score: 0
Accepted
time: 39ms
memory: 7600kb

Test #33:

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

Test #34:

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

Test #35:

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

Test #36:

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

Test #37:

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

Test #38:

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

Test #39:

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

Test #40:

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

Test #41:

score: 0
Accepted
time: 11ms
memory: 10332kb

Test #42:

score: 0
Accepted
time: 10ms
memory: 9904kb

Test #43:

score: 0
Accepted
time: 5ms
memory: 10428kb

Test #44:

score: 0
Accepted
time: 7ms
memory: 10060kb

Test #45:

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

Test #46:

score: 0
Accepted
time: 10ms
memory: 10496kb

Test #47:

score: 0
Accepted
time: 52ms
memory: 36828kb

Test #48:

score: 0
Accepted
time: 59ms
memory: 36156kb

Test #49:

score: 0
Accepted
time: 48ms
memory: 36752kb

Test #50:

score: 0
Accepted
time: 33ms
memory: 31276kb

Test #51:

score: 0
Accepted
time: 45ms
memory: 29948kb

Test #52:

score: 0
Accepted
time: 41ms
memory: 30652kb

Test #53:

score: 0
Accepted
time: 89ms
memory: 60600kb

Test #54:

score: 0
Accepted
time: 84ms
memory: 61144kb

Test #55:

score: 0
Accepted
time: 80ms
memory: 61244kb

Test #56:

score: 0
Accepted
time: 88ms
memory: 58744kb

Test #57:

score: 0
Accepted
time: 75ms
memory: 61184kb

Test #58:

score: 0
Accepted
time: 79ms
memory: 59208kb

Test #59:

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

Test #60:

score: 0
Accepted
time: 3ms
memory: 8988kb

Test #61:

score: 0
Accepted
time: 24ms
memory: 8136kb

Test #62:

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