题意
能拿出勇气挑战这种题,首先给你致敬。首先请确保你已经读懂了题面,我不会在这里翻译。我会说一些你可能没注意到的地方或者是这个题没有说明白的地方。
注意点
- 数据是以空行分割的(也就是说保证没有多余空行),并且保证一行一命令,每个case的开头和结尾分别是启动和关机命令。但是,数据并没有保证没有多余空格,所以建议处理命令的时候过滤空格。
- 关于消息机制,要知道当用户登录的时候显示的消息是未读消息,这一点题面说的很隐晦,而且还有一点,每次登陆和刷新之后,所有消息都会被读掉,也就是说上次显示过的消息,下次就不会显示。更为坑人的一点是,当两个人加为好友之后,一个人说话,系统会向另外一个人发通知,在加好友之前说过的话并不会推送到另外一个人那里。所以这里我们应该才用stack来实现这个消息系统,给每一个用户带一个stack,相当于收件箱,显示完就弹出,完全符合这个题的意图。
- 关于个人界面的输出,这几乎是本题最大的坑点,首先你要注意排版,强烈建议跑完sample之后使用diff完全比较以防悲剧,同时,你很可能处理错了收件箱为空时的情况。
你可能以为当收件箱为空时,输出应该是这样的:
123+------------------+| User Name : Mary |+------------------+
然而答案他却长这样:
1234+------------------+| User Name : Mary |+------------------++------------------+
我想你应该知道咋改了。 - 然后还有一个比较那啥的错误,就是你忘了实现refresh功能,你也没发现,因为sample没有。
- 常规错误也要查一下,就是执行命令的判断不到位(你应该判断系统开机与否,用户注册情况,用户登录情况,用户重名情况)。
额外测试数据
不妨试一下这个数据。时间戳是我随便写的(因为没影响),如果你用了时间戳排序(虽然并没有需要),可能会有差异。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[01/01,00:00] NEW SYSTEM START [01/01,01:00] "Zuck" REGISTER [01/02,12:00] "Mary" REGISTER [01/03,11:34] "Lily" REGISTER [02/03,13:44] "Zuck" AND "Lily" MAKE FRIENDSHIP [03/25,17:27] "Zuck" AND "Mary" MAKE FRIENDSHIP [04/01,12:00] "Lily" LOGIN [04/01,12:32] "Lily" SAYS "What a boring day is!" [04/01,13:00] "Mary" LOGIN [04/01,13:50] "Mary" SAYS "Today is funny!" [04/01,14:00] "Lily" LOGOUT [04/01,15:00] "Mary" LOGOUT [04/01,16:12] "Zuck" LOGIN [04/01,13:00] "Mary" LOGIN [04/01,13:50] "Mary" SAYS "Today is funny!" [03/25,17:27] "Zuck" AND "Mary" MAKE FRIENDSHIP [04/01,16:30] "Zuck" REFRESH [04/01,16:30] "Zuck" LOGOUT [04/01,16:30] "Zuck" REFRESH [04/01,23:59] SYSTEM SHUTDOWN |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
Case 1: [01/01,00:00] New System Started. [01/01,01:00] Zuck Registered. [01/02,12:00] Mary Registered. [01/03,11:34] Lily Registered. [02/03,13:44] Zuck and Lily are now friends. [03/25,17:27] Zuck and Mary are now friends. [04/01,12:00] Lily Logined. +-----------------------------------------+ | User Name : Lily | +-----------------------------------------+ | [02/03,13:44] Zuck add you as a friend. | +-----------------------------------------+ [04/01,12:32] Lily said : What a boring day is! [04/01,13:00] Mary Logined. +-----------------------------------------+ | User Name : Mary | +-----------------------------------------+ | [03/25,17:27] Zuck add you as a friend. | +-----------------------------------------+ [04/01,13:50] Mary said : Today is funny! [04/01,14:00] Lily Logout. [04/01,15:00] Mary Logout. [04/01,16:12] Zuck Logined. +-------------------------------------------------+ | User Name : Zuck | +-------------------------------------------------+ | [04/01,13:50] Mary said : Today is funny! | | [04/01,12:32] Lily said : What a boring day is! | | [03/25,17:27] Mary add you as a friend. | | [02/03,13:44] Lily add you as a friend. | +-------------------------------------------------+ [04/01,13:00] Mary Logined. +------------------+ | User Name : Mary | +------------------+ +------------------+ [04/01,13:50] Mary said : Today is funny! [04/01,16:30] Zuck Refreshed. +-------------------------------------------+ | User Name : Zuck | +-------------------------------------------+ | [04/01,13:50] Mary said : Today is funny! | +-------------------------------------------+ [04/01,16:30] Zuck Logout. [04/01,23:59] System Shutdown. |
接下来上代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
// // SCUT13J.cpp // playground // // Created by Adam Chang on 2015/05/17. // Copyright (c) 2015年 Adam Chang. All rights reserved. // #include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <ctime> #include <iomanip> #include <cmath> #include <set> #include <stack> #include <cmath> #include <map> using namespace std; struct msgType{ int type;//1 for say;2 for friendship string arg1; string arg2; string timeStamp; msgType(string _timeStamp,int _type,string _arg1,string _arg2){ type = _type; timeStamp = _timeStamp; arg1 = _arg1;//hadle / usr1 arg2 = _arg2;//message body/usr2 } }; struct userType{ string handle; bool isLogin; set<string> friendList; stack<int> msgstk; userType(string _handle){ handle = _handle; isLogin = false; friendList.clear(); while(!msgstk.empty()){ msgstk.pop(); } } userType(){ handle.clear(); isLogin = false; friendList.clear(); while(!msgstk.empty()){ msgstk.pop(); } } }; void profile(string &handle); map<string, userType> userbase; vector<msgType> msgbase; int caseCnt = 0; bool isSystemOnline = false; void poweron(string &timeStamp){ if(isSystemOnline){ return; }else{ isSystemOnline = true; userbase.clear(); msgbase.clear(); cout << "Case " << ++caseCnt << ':' << endl; cout << timeStamp << " New System Started." << endl; } } void poweroff(string &timeStamp){ if (!isSystemOnline) { return; }else{ isSystemOnline = false; cout << timeStamp << " System Shutdown." << endl; } } void useradd(string &timeStamp,string &_handle){ if (!isSystemOnline) { return; } if (userbase.count(_handle)) { return; }else{ userbase[_handle] = userType(_handle); cout << timeStamp << ' ' << _handle << " Registered." << endl; } } void login(string &timeStamp,string &handle){ if (!isSystemOnline) { return; } if (userbase.count(handle) == 0) { return; } userType& current = userbase[handle]; if (current.isLogin) { return; }else{ current.isLogin = true; cout << timeStamp << ' ' << handle << " Logined." << endl; profile(handle); } } void logoff(string &timeStamp,string &handle){ if (!isSystemOnline) { return; } if (userbase.count(handle) == 0) { return; } userType& current = userbase[handle]; if (!current.isLogin) { return; }else{ current.isLogin = false; cout << timeStamp << ' ' << handle << " Logout." << endl; } } void mkfriend(string &timeStamp,string &_handle1,string &_handle2){ if (!isSystemOnline) { return; } if (_handle1 == _handle2) { return; } bool exist1 = userbase.count(_handle1); bool exist2 = userbase.count(_handle2); if (exist1 && exist2) { userType &usr1 = userbase[_handle1]; userType &usr2 = userbase[_handle2]; if (usr1.friendList.count(_handle2) || usr2.friendList.count(_handle1)) { return; }else{ usr1.friendList.insert(_handle2); usr2.friendList.insert(_handle1); msgbase.push_back(msgType(timeStamp,2,_handle1,_handle2)); usr1.msgstk.push((int)msgbase.size()-1); usr2.msgstk.push((int)msgbase.size()-1); cout << timeStamp << ' ' << _handle1 << " and " << _handle2 << " are now friends." << endl; } }else{ return; } } void potwit(string &timeStamp,string &handle,string &msgBody){ if (!isSystemOnline) { return; } if (userbase.count(handle) == 0) { return; } userType &usr = userbase[handle]; if (usr.isLogin == false) { return; } msgbase.push_back(msgType(timeStamp,1,handle,msgBody)); for (set<string>::iterator iter = usr.friendList.begin();iter != usr.friendList.end();iter++) { userbase[*iter].msgstk.push((int)msgbase.size()-1); } cout << timeStamp << ' ' << handle << " said : " << msgBody << endl; } void refresh(string &timeStamp,string &handle){ if (!isSystemOnline) { return; } if (userbase.count(handle) == 0) { return; } userType &usr = userbase[handle]; if (usr.isLogin == false) { return; } cout << timeStamp << ' ' << handle << " Refreshed." << endl; profile(handle); } void profile(string &handle){ userType &usr = userbase[handle]; vector<int> currmsg; int maxlen = 12+(int)handle.size(); //for (int i = usr.lastcur; i < msgbase.size(); i++) { while (!usr.msgstk.empty()) { int i = usr.msgstk.top(); usr.msgstk.pop(); msgType &now = msgbase[i]; if (now.type == 1) { if (usr.friendList.count(now.arg1)) { currmsg.push_back(i); maxlen = max((int)(22+now.arg1.size()+now.arg2.size()),maxlen); } }else{ if (now.arg1 == handle) { currmsg.push_back(i); maxlen = max((int)(35+now.arg2.size()),maxlen); }else if(now.arg2 == handle){ currmsg.push_back(i); maxlen = max((int)(35+now.arg1.size()),maxlen); } } } printf("+-"); for (int i = 1; i <= maxlen; i++) { putchar('-'); } printf("-+\n"); printf("| User Name : "); cout << handle; for (int i = 1; i <= maxlen-(12+(int)handle.size()); i++) { putchar(' '); } printf(" |\n"); printf("+-"); for (int i = 1; i <= maxlen; i++) { putchar('-'); } printf("-+\n"); if (currmsg.size()) { for (int mi = 0; mi < currmsg.size(); mi++) { msgType &now = msgbase[currmsg[mi]]; if (now.type == 1) { printf("| "); cout << now.timeStamp; putchar(' '); cout << now.arg1 << ' ' << "said : " << now.arg2; for (int i = 1; i <= maxlen-(int)(22+now.arg1.size()+now.arg2.size()); i++) { putchar(' '); } printf(" |"); cout << endl; }else{ printf("| "); cout << now.timeStamp; putchar(' '); if (now.arg1 == handle) { cout << now.arg2 << " add you as a friend."; for (int i = 1; i <= maxlen-(int)(35+now.arg2.size()); i++) { putchar(' '); } }else{ cout << now.arg1 <<" add you as a friend."; for (int i = 1; i <= maxlen-(int)(35+now.arg1.size()); i++) { putchar(' '); } } printf(" |"); cout << endl; } } } printf("+-"); for (int i = 1; i <= maxlen; i++) { putchar('-'); } printf("-+"); cout << endl; } int main(){ //freopen("sample.txt","r",stdin); string cmd; while (getline(cin, cmd)) { if (cmd.size() == 0) { continue; } int cur = 0; for (; cur < cmd.size(); cur++) { if (cmd[cur] != ' ') { break; } } string timeStamp; for (int i = 0; i < 13; i++,cur++) { timeStamp += cmd[cur]; } cur++; for (; cur < cmd.size(); cur++) { if (cmd[cur] != ' ') { break; } } if (cmd[cur] == 'N') { poweron(timeStamp); continue; } if (cmd[cur] == 'S') { poweroff(timeStamp); continue; } string arg1; cur++; for (; cur < cmd.size(); cur++) { if (cmd[cur] == '"') { break; } arg1 += cmd[cur]; } cur++; for (; cur < cmd.size(); cur++) { if (cmd[cur] != ' ') { break; } } if (cmd[cur] == 'R') { cur+=2; if (cmd[cur] == 'G') { useradd(timeStamp, arg1); continue; }else if(cmd[cur] == 'F'){ refresh(timeStamp, arg1); continue; } } if (cmd[cur] == 'L') { cur+=3; if (cmd[cur] == 'O') { logoff(timeStamp, arg1); }else if(cmd[cur] == 'I'){ login(timeStamp, arg1); } continue; } if (cmd[cur] == 'A') { cur+=3; for (; cur < cmd.size(); cur++) { if (cmd[cur] != ' ') { break; } } cur++; string arg2; for (; cur < cmd.size(); cur++) { if (cmd[cur] == '"') { break; } arg2 += cmd[cur]; } mkfriend(timeStamp, arg1, arg2); continue; } if (cmd[cur] == 'S') { cur+=4; for (; cur < cmd.size(); cur++) { if (cmd[cur] != ' ') { break; } } cur++; string arg2; for (; cur < cmd.size(); cur++) { if (cmd[cur] == '"') { break; } arg2 += cmd[cur]; } potwit(timeStamp, arg1, arg2); continue; } } return 0; } |
模拟题的话注意点到位就不会错,其实代码本体的意义不大。