农民领袖
- 积分
- 12135
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-7-23
- 最后登录
- 1970-1-1
|
把我当时自己的代码贴上来,注意,没过哈。- #include <algorithm>
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <unordered_map>
- #include <unordered_set>
- #include <set>
- #include <vector>
- using namespace std;
- namespace Solution {
- bool IsPositiveNumber(const std::string& s) {
- if (s.empty()) {
- return false;
- }
- for (const char& c : s) {
- if (c != ' ' && !isdigit(c)) {
- return false;
- }
- }
- return true;
- }
- class Chain {
- public:
- int head_;
- int tail_;
- int count_;
- unsigned int duration_;
-
- Chain(int head, int tail, int count, unsigned int duration):
- head_(head), tail_(tail), count_(count), duration_(duration) {}
-
- void Print() {
- cout << "start_job: " << head_ << endl;
- cout << "last_job: " << tail_ << endl;
- cout << "number_of_jobs: " << count_ << endl;
- vector<string> total = Convert(duration_);
- cout << "job_chain_runtime: " << total[0] << ":" << total[1] << ":" << total[2] << endl;
- vector<string> avg = Convert((int)(duration_ / count_));
- cout << "average_job_time: " << avg[0] << ":" << avg[1] << ":" << avg[2] << endl;
- cout << "-" << endl;
- }
- private:
- vector<string> Convert(int duration) {
- vector<string> result;
- int seconds = duration % (24 * 3600); // total displayable seconds
- result.push_back(Format(seconds / 3600)); // hours
- seconds %= 3600;
- result.push_back(Format(seconds / 60)); // minutes
- seconds %= 60; // seconds
- result.push_back(Format(seconds)); // seconds;
- return result;
- }
-
- string Format(int input) {
- if (input < 10) {
- return "0" + to_string(input);
- }
- return to_string(input);
- }
- };
- class JobRunner {
- public:
- JobRunner() {
- Reset();
- }
-
- // Reset the states to redo the job logging.
- void Reset() {
- is_garbage_ = false;
- graph_ = unordered_map<int, int>();
- durations_ = unordered_map<int, int>();
- in_degree_ = unordered_map<int, int>();
- heads_ = unordered_set<int>();
- unvisited_ = set<int>();
- results_ = vector<Chain>();
- }
-
- void Preprocess(const char* input) {
- // To take std::cin as input to this function, you just need to replace
- // the input argument type from "const char* " to "std::istream&" and
- // comment out the following line of stringstream.
- stringstream ssline(input);
- string line;
- while (getline(ssline, line, '\n')) {
- if (!line.empty() && line[0] == '#') {
- continue;
- }
- // cout << line << endl;
- vector<int> numbers;
- stringstream ssitem(line);
- while (ssitem.good()) {
- string number;
- getline(ssitem, number, ',');
- if (IsPositiveNumber(number)) {
- numbers.push_back(stoi(number));
- }
- }
- // cout << numbers.size() << endl;
- if (numbers.size() != 3) {
- is_garbage_ = true;
- break;
- }
-
- graph_[numbers[0]] = numbers[2];
- durations_[numbers[0]] = numbers[1];
- unvisited_.insert(numbers[0]);
- if (in_degree_.find(numbers[0]) == in_degree_.end()) {
- in_degree_[numbers[0]] = 0;
- heads_.insert(numbers[0]);
- }
- in_degree_[numbers[2]]++;
- if (heads_.find(numbers[2]) != heads_.end()) {
- heads_.erase(numbers[2]);
- }
- }
- }
-
- void GenerateOutput() {
- // PrintStatesForDebug();
- if (is_garbage_) {
- cout << "Malformed Input" << endl;
- return;
- }
- for (const auto& head : heads_) {
- Chain current = Chain(head, graph_[head], 1, durations_[head]);
- unvisited_.erase(head);
- int next = graph_[head];
- // Terminate if the next job does not end with 0.
- while (next != 0 && durations_.find(next) != durations_.end()) {
- UpdateChain(current, next);
- }
-
- results_.push_back(current);
- }
-
- // Processing the remaining cyclic graph.
- while (!unvisited_.empty()) {
- // cout << ", unvisted: " << unvisited.size()
- // << ", durations: " << durations.size() << endl;
-
- int head = *unvisited_.begin();
- Chain current = Chain(head, graph_[head], 1, durations_[head]);
- unvisited_.erase(head);
- int next = graph_[head];
- while (unvisited_.find(next) != unvisited_.end()) {
- UpdateChain(current, next);
- }
- results_.push_back(current);
- }
-
- sort(results_.begin(), results_.end(), [](const Chain& a, const Chain& b) {
- return a.duration_ > b.duration_ ? true : (a.duration_ == b.duration_ ? a.head_ < b.head_ : false);
- });
-
- cout << "-" << endl;
- for (auto& result : results_) {
- result.Print();
- }
- }
- private:
- void UpdateChain(Chain& current, int& next) {
- unvisited_.erase(next);
- current.tail_ = next;
- current.count_++;
- current.duration_ += durations_[next];
- next = graph_[next];
- }
- // Print states for debugging purpose only.
- void PrintStatesForDebug() {
- cout << "\ngraph and duration: " << endl;
- for (const auto& p : graph_) {
- cout << p.first << " -> " << p.second << " : " << durations_[p.first] << endl;
- }
-
- cout << "\nindegree: " << endl;
- for (const auto& p : in_degree_) {
- cout << p.first << " : " << p.second << endl;
- }
-
- cout << "\nheads: " << endl;
- for (const auto& h : heads_) {
- cout << h << " ";
- }
- cout << endl;
- }
- bool is_garbage_;
- unordered_map<int, int> graph_;
- unordered_map<int, int> durations_;
- unordered_map<int, int> in_degree_;
- unordered_set<int> heads_;
- // Ensure job id in order when encounted with circylic job chain.
- set<int> unvisited_;
-
- vector<Chain> results_;
- };
- } // namespace Solution
- int main() {
- /*
- std::string line;
- while (std::getline(std::cin, line)) {
- std::cout << line << std::endl;
- }
- */
-
- const char* kInput1 =
- "#first_job_id, runtime, next_job_id\n"
- "1, 10, 23\n"
- "3, 15, 7\n"
- "23, 36, 0\n"
- "7, 15, 0\n";
- const char* kInput2 =
- "#first_job_id, runtime, next_job_id\n"
- "1, 60, 23\n"
- "2, 23, 3\n"
- "3, 12, 0\n"
- "23, 30, 0\n";
- const char* kInput3 =
- "#first_job_id, runtime, next_job_id\n"
- "1, 86000, 23\n"
- "2, 23, 3\n"
- "3, 12, 0\n"
- "23, 400, 0\n";
-
- const char* kInput4 =
- "#first_job_id, runtime, next_job_id\n"
- "1, 1, 23\n"
- "2, 3, 3\n"
- "3, 4, 1\n"
- "23, 2, 2\n"
- "33, 15, 34\n"
- "24, 25, 33\n"
- "18, 35, 24\n";
-
- const char* kInput5 =
- "#first_job_id, runtime, next_job_id\n"
- "1, 1, 23\n"
- "2, 3, 3\n"
- "3, 400, 1\n"
- "23, 2, 2\n"
- "33, 15, 34\n"
- "24, 25, 33\n"
- "18, 35, 24\n"
- "10, 15, 11\n"
- "11, 16, 12\n"
- "12, 17, 13\n"
- "13, 18, 14\n"
- "14, 19, 10\n";
-
- const char* kInput6 =
- "#first_job_id, runtime, next_job_id\n"
- "1, 1, 0\n";
-
- const char* kInput7 =
- "#first_job_id, runtime, next_job_id\n"
- "1, a, 0\n";
-
- // A node has two input branches.
- const char* kInput8 =
- "#first_job_id, runtime, next_job_id\n"
- "1, 5, 2\n"
- "2, 5, 3\n"
- "3, 5, 4\n"
- "4, 5, 0\n"
- "6, 5, 7\n"
- "7, 5, 3\n";
-
- // A node has two output branches.
- const char* kInput9 =
- "#first_job_id, runtime, next_job_id\n"
- "1, 5, 2\n"
- "2, 5, 3\n"
- "3, 5, 4\n"
- "4, 5, 0\n"
- "6, 5, 7\n"
- "7, 5, 0\n";
- // cout << kInput << endl;
- // I'm assumeing there is no multiple input/output branches in the job chains.
- Solution::JobRunner runner;
- runner.Preprocess(kInput9);
- runner.GenerateOutput();
- return 0;
- }
复制代码 |
|