SRM 579 Div II Level Two: UndoHistory
2013-07-28 01:42
507 查看
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12523
模拟每个步骤,用set保存history字符串。
代码如下:
模拟每个步骤,用set保存history字符串。
代码如下:
#include <algorithm> #include <iostream> #include <sstream> #include <string> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cctype> #include <climits> #include <cmath> using namespace std; /************** Program Begin *********************/ class UndoHistory { public: int minPresses(vector <string> lines) { int res = 0; string buf = ""; set <string> his; his.insert(""); int buf_size, his_size; for (int i = 0; i < lines.size(); i++) { buf_size = buf.size(); his_size = 0; for (int j = lines[i].size(); j >= 1; j--) { if ( !his.insert(lines[i].substr(0, j)).second ) { his_size = j; break; } } /* use buf */ if (his_size < buf_size + 2 && buf == lines[i].substr(0, buf_size)) { res += lines[i].size() - buf_size + 1; for (int j = buf_size + 1; j <= lines[i].size(); j++) { his.insert(lines[i].substr(0, j)); } } /* use history */ else if (his_size > 0) { res += lines[i].size() - his_size + 3; for (int j = his_size + 1; j <= lines[i].size(); j++) { his.insert(lines[i].substr(0, j)); } } /* from empty */ else { res += lines[i].size() + 3; for (int j = 1; j <= lines[i].size(); j++) { his.insert(lines[i].substr(0, j)); } } /* update buf */ buf = lines[i]; } return res; } }; /************** Program End ************************/
相关文章推荐
- SRM 584 Div II Level Two: Egalitarianism,DFS,BFS算法
- SRM 219 Div II Level Two: HealthFood
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
- SRM DIV2 579 UndoHistory
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
- SRM 233 Div II Level Two: PipeCuts
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
- SRM 299 Div II Level Two: Projections
- SRM 578 Div II Level Two: GooseInZooDivTwo, DFS
- SRM 585 Div II Level Two: TrafficCongestionDivTwo
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序( 比较函数 )
- SRM 222 Div II Level Two: GroceryBagger,STL map 用法
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 483 Div II Level Two: MovieSeating, TopCoder上正确率不到%10的题
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)
- SRM 578 Div II Level Three: WolfInZooDivTwo,Dynamic Programming,求教!
- SRM 219 Div II Level One: WaiterTipping,小心约分
- SRM 207 Div II Level One: TransportCounting