QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#639902#2428. Comma SprinklerTrentiumWA 66ms94172kbC++143.5kb2024-10-13 23:45:482024-10-13 23:45:49

Judging History

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

  • [2024-10-13 23:45:49]
  • 评测
  • 测评结果:WA
  • 用时:66ms
  • 内存:94172kb
  • [2024-10-13 23:45:48]
  • 提交

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;

gp_hash_table<ll, string> back;
struct stringTo{
    ll B = 183, MOD = 234782937581;
    ll operator ()(const string& x){
        int cur = 0;
        for(char i : x) cur = (cur + i * B) % MOD;
        if(back.find(cur) == back.end()) back[cur] = x;
        return cur;
    }
} hasher;

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: 0ms
memory: 3820kb

Test #2:

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

Test #3:

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

Test #4:

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

Test #5:

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

Test #6:

score: 0
Accepted
time: 66ms
memory: 94172kb

Test #7:

score: -100
Wrong Answer
time: 38ms
memory: 41844kb