UVA - 210:Concurrency Simulator

題目鏈接:https://vjudge.net/problem/UVA-210

題目分析

就是一道模擬題,但是細節有點多。
寫代碼兩個小時,調試代碼用了兩天。。。很長時間不刷題了,這道雖然算法簡單但是細節滿滿的題目對我來說是一個很好的熱身。

  • 盡量不要去使用匿名名字空間,發現對調試過程不怎么友好(陳碩大大說的對)。
  • 使用枚舉類型對程序的可讀性、可維護性的提升非常大
  • 重載輸入輸出運算符的時候一定要記得返回stream對象
  • 這種帶有switch語句的,可以使用Stragety模式,這里沒有使用,因為每條語句只有執行,沒有復雜的行為
  • 多組數據一定要有init函數清空數據
  • 使用智能指針不要用引用,這一點點內存的消耗不算什么,但是使用引用往往會帶來錯誤:當我們在對象內部不小心將其釋放掉的時候就會產生段錯誤,而且很難排查
  • 良好的抽象是巧妙設計的基礎,每個類應該只負責其分內的事,不要嘗試讓其去做超過他權限的事,因為這樣往往會讓事情一團糟。封裝、抽象能夠幫助我們處理復雜的情況。
  • 通過ulimit -c unlimited命令開啟生成core文件可以幫助進行調試
  • gdb調試開始的時候可以使用run < input.txt重定向輸入和輸出
  • cgdb真好用

AC代碼

#include <iostream>
#include <array>
#include <vector>
#include <string>
#include <deque>
#include <memory>using namespace std;namespace {
enum TYPE {ASSIGN, PRINT, LOCK, UNLOCK, END
};
int n, quantum;constexpr int MAXN = 26;
array<int, MAXN> alpha = {};bool lock = false;
class Statement {
public:static constexpr int MAXN = 5;static array<int, MAXN> cost;static void init();string line;TYPE type;int var;int constant;int exec();friend istream& operator >> (istream& is, Statement &self);friend ostream& operator << (ostream& os, const Statement &self);
};class Program {
public:vector<Statement> statements;int idx = 0;int id;bool exec();Program(int _id) : id(_id) {}friend istream& operator >> (istream& is, Program& self);friend ostream& operator << (ostream& os, const Program &self);
};
deque<shared_ptr<Program>> readyQueue, blockedQueue;
shared_ptr<Program> p;ostream& operator << (ostream& os, const Statement &self) {
//    os << self.type;switch (self.type) {case ASSIGN:os << static_cast<char>('a' + self.var) << " = " << self.constant;break;case PRINT:os << "print " << static_cast<char>('a' + self.var);break;case LOCK:os << "lock";break;case UNLOCK:os << "unlock";break;case END:os << "end";break;}return os;
}ostream& operator << (ostream& os, const Program &self) {os << "ID:" << self.id << "\n";for (auto s : self.statements) {os << s << "\n";}os << "\n";return os;
}int Statement::exec() {switch (type) {case ASSIGN:
//        cout << "Test:" << line << endl;
//        cout << "Test:" << readyQueue.front()->id << " " << static_cast<char>('a' + var) << " = " << constant << endl;alpha[var] = constant;return cost[type];break;case PRINT:cout << p->id << ": " << alpha[var] << "\n";return cost[type];break;case END:
//        readyQueue.pop_front();
//        cout << "Test:" << type << " " << cost[type] << endl;
//        for (int i = 0; i < Statement::MAXN; ++i) {
//            cout << cost[i] << " ";
//        }
//        cout << endl;return cost[type];break;case LOCK:if (lock) {blockedQueue.push_back(p);return -1;} else {lock = true;return cost[type];}break;case UNLOCK:if (!blockedQueue.empty()) {readyQueue.push_front(blockedQueue.front());blockedQueue.pop_front();}lock = false;return cost[type];default:break;}
}bool Program::exec() {int time = quantum;while (time > 0) {int ret = statements[idx].exec();if (ret == -1) {//lockreturn false;}if (++idx == statements.size()) {//endreturn false;}time -= ret;}return true;
}constexpr int Statement::MAXN;
array<int, Statement::MAXN> Statement::cost;void Statement::init() {for (int i = 0; i < MAXN; ++i) cin >> cost[i];
}istream& operator >> (istream& is, Statement &self) {auto &line = self.line;getline(is, line);if (line[1] == ' ') {self.type = ASSIGN;self.var = line[0] - 'a';self.constant = stoi(line.substr(4));} else if (line[0] == 'p') {self.type = PRINT;self.var = line[6] - 'a';} else if (line[0] == 'l') {self.type = LOCK;} else if (line[0] == 'u') {self.type = UNLOCK;} else {self.type = END;}return is;
}
istream& operator >> (istream& is, Program& self) {auto &s = self.statements;do {s.push_back(Statement());is >> s.back();} while(s.back().type != END);return is;
}}void init() {readyQueue.clear();blockedQueue.clear();std::fill(alpha.begin(), alpha.end(), 0);lock = false;
}int main(int argc, char *argv[])
{ios::sync_with_stdio(false);int T, id = 0;cin >> T;for (int caseIdx = 0; caseIdx < T; ++caseIdx) {if (caseIdx) cout << "\n";init();cin >> n;Statement::init();cin >> quantum;string line;getline(cin, line);for (int i = 0; i < n; ++i) {readyQueue.push_back(make_shared<Program>(i + 1));cin >> *readyQueue.back();}
//        for (auto p : readyQueue) {
//            cout << *p;
//        }while (!readyQueue.empty()) {//TODO:加上了&導致出錯p = readyQueue.front();readyQueue.pop_front();if (p->exec()) {readyQueue.push_back(p);}
//            cout << "Test:[readyQueue]\n";
//            for (auto p : readyQueue) {
//                cout << *p;
//            }
//            cout << "Test:[blockedQueue]\n";
//            for (auto p : blockedQueue) {
//                cout << *p;
//            }
//            cout << flush;}
//        cout << "====================================\n";}return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/383525.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/383525.shtml
英文地址,請注明出處:http://en.pswp.cn/news/383525.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

UVA - 514:Rails

題目鏈接&#xff1a;https://vjudge.net/problem/UVA-514 題目分析 題目的意思是給一個棧輸入一系列數據&#xff0c;在這個過程中可以出棧&#xff0c;看能否達到某個結果。 剛開始我覺得這個情況好多&#xff0c;因此不是用模擬&#xff0c;而應該觀察結果本身。對于結果中…

UVA - 442:Matrix Chain Multiplication

題目鏈接&#xff1a;https://vjudge.net/problem/UVA-442 題目分析 題目的意思非常簡單&#xff0c;就是給定一個矩陣乘法的表達式然后計算就可以了。隨便寫寫 AC代碼 #include <iostream> #include <deque> #include <vector> #include <string>…

leetcode869. 重新排序得到 2 的冪

題目連接&#xff1a;https://leetcode-cn.com/problems/reordered-power-of-2/ 題目分析 如果直接順著題目的思路&#xff0c;得到數字n的全排列&#xff0c;然后再去判斷其是不是2的冪是比較復雜的。 我們應該注意到&#xff0c;因為數字是可以隨意排列的&#xff0c;因此所…

使用wireshark+ssh+tcpdump遠程抓包

因為需要抓取遠程服務器上的數據包&#xff0c;又不想使用tcpdump這種命令行工具進行&#xff08;用了wireshark后誰還愿意去看密密麻麻的命令行呢&#xff09;&#xff0c;所以在網上查找了一下使用wireshark遠程抓包的方法&#xff0c;在這里記錄一下。 原生支持 wireshark…

C++ Variadic Templates(可變參數模板)

本文參考侯捷老師的視頻&#xff1a;https://www.youtube.com/watch?vTJIb9TGfDIw&listPL-X74YXt4LVYo_bk-jHMV5T3LHRYRbZoH 以及C primer第五版 相關內容。 可變參數模板函數 //遞歸的終止條件 void print() {} //Variadic Templates //一般用于遞歸處理 template <…

Ubuntu修復Fix Busybox Initramfs錯誤

今天早上我打開電腦&#xff0c;進入Ubuntu系統&#xff0c;結果黑屏了&#xff0c;屏幕顯示&#xff1a; BusyBox v1.30.1 (Ubuntu 1:1.30.1-4ubuntu6.1) built-in shell (ash) Enter help for a list of built-in commands.(initramfs)然而我并不知道這個是什么意思&#x…

Leetcode第284場周賽

緒論 最近發現Leetcode每周的周賽難度挺適合我的&#xff0c;而且時間也比較友好&#xff08;不像Codeforces每次都是半夜&#xff09;。所以連續參加了三周的周賽。這次才想起來應該記錄一下自己的參賽歷程。一方面是總結經驗&#xff0c;另一方面有了記錄就更有動力去提升&a…

Leetcode第286場周賽

緒論 上周因為有事沒有參加周賽&#xff0c;這周沒有錯過。這次周賽拿到了人生第一個AK&#xff0c;參加大大小小的比賽這么多次&#xff0c;從來沒有AK過&#xff0c;淚目了。 感覺這次比賽的思維難度對我來講稍高一些&#xff0c;前三道題就花了一個小時&#xff0c;而以往…

第287場周賽

緒論 雖然是上周日參加的比賽&#xff0c;但是這周沒有怎么學習&#xff0c;每天就是玩耍。也導致對周賽的總結遲遲沒有進行。想著再拖下去下次周賽都要開始了&#xff0c;在這里補一下。 這場比賽總體比上場簡單一些&#xff0c;但是最后一道題因為忘記初始化類內變量導致調試…

第288場周賽

緒論 雖然沒有AK&#xff0c;但是不知道為什么排名比以前AK了都靠前。可能是因為最后一道題有些難度&#xff0c;縮小了我和大佬之間的差距。最后一個小時寫最后一道題&#xff0c;累死累活想了一個貪心遍歷的算法&#xff0c;當時是一直RE&#xff0c;后來下來調了調又WA了。 …

Clion遠程部署和運行

緒論 作為Clion的忠實粉絲&#xff0c;現在的我的幾乎所有的coding都是通過Clion完成。因為需要在服務器上進行開發&#xff0c;又離不開Clion&#xff0c;就了解了如何通過Clion遠程部署和開發。 主要是借鑒了博客&#xff1a;使用Clion優雅的完全遠程自動同步和遠程調試c。如…

C++ 單例模式 call_once : terminate called after throwing an instance of ‘std::system_error‘

在學習了C中可以使用call_once進行初始化資源后&#xff0c;我就想著寫一個單例模板供以后使用。 template<typename T> class SingleTon {using Ptr std::shared_ptr<T>;static Ptr p;static std::once_flag flag;template<typename ...Args>static void …

C++讀寫鎖造成死鎖

C14支持std::shared_timed_mutex C17支持std::shared_mutex 前者相比后者支持的操作更多&#xff0c;但是后者相對性能更好。 使用std::lock_guard<std::shared_mutex>和std::unique_lock<std::shared_mutex>互斥訪問使用std::shared_lock<std::shared_mutex…

每日一題:449. 序列化和反序列化二叉搜索樹

題目分析 題目鏈接&#xff1a;449. 序列化和反序列化二叉搜索樹 覺得序列化很簡單&#xff0c;前序遍歷、后序遍歷、中序遍歷、層序遍歷等等。其中得到前序遍歷和后序遍歷是可以通過遞歸解法反序列化的&#xff0c;覺得這樣子做有點復雜。就想著可不可以一次遍歷。一次遍歷的…

C++高效集合數據結構設計

緒論 在復雜算法實現過程中我們經常會需要一個高效的集合數據結構&#xff0c;支持常數級別的增、刪、查&#xff0c;以及隨機返回、遍歷&#xff0c;最好還能夠支持交集、并集、子集操作 哈希集合實現 大家可能很快想到unordered_set&#xff0c;unordered_set由于底層是哈…

C++ 工具函數庫

在寫一些大型項目的過程中經常需要一些工具函數&#xff0c;例如獲取隨機數、計時器、打印函數、重要常量&#xff08;如最大值&#xff09;、信號與槽等&#xff0c;由于每一個工程都自己手動實現一個實在是太傻&#xff0c;我將其總結放入一個文件中。 utils.h // Copyright…

muduo網絡庫使用入門

muduo網絡庫介紹 muduo網絡庫是陳碩大神開發的基于主從Reactor模式的&#xff0c;事件驅動的高性能網絡庫。 網絡編程中有很多是事務性的工作&#xff0c;使用muduo網絡庫&#xff0c;用戶只需要填上關鍵的業務邏輯代碼&#xff0c;并將回調注冊到框架中&#xff0c;就可以實…

C++ map/unordered_map元素類型std::pair<const key_type, mapped_type>陷阱

在開發的過程中需要遍歷一個unordered_map然后把他的迭代器傳給另一個對象&#xff1a; class A; class B { public:void deal(const std::pair<int, A>& item); }; std::unordered_map<int, A> mp; B b; for (auto &pr : mp) {b.deal(pr); }在我的項目中…

Ubuntu install ‘Bash to dock‘

緒論 在Ubuntu環境搭建這篇博客中記錄了使用Dash To Dock來配置Ubuntu的菜單項&#xff0c;使得實現macOS一樣的效果。為了配置新電腦的環境&#xff0c;我還是想安裝這個軟件。但是如今在Ubuntu Software中已經找不到這個軟件了&#xff0c;我在網上借鑒了一些博客的經驗才得…

Leetcode第309場周賽

Date: September 4, 2022 Difficulty: medium Rate by others: ???? Time consuming: 1h30min 題目鏈接 競賽 - 力扣 (LeetCode) 題目解析 2399. 檢查相同字母間的距離 class Solution {public:bool checkDistances(string s, vector<int>& distance) {vec…