URAL 2002. Test Task(登陆模拟 map )
2015-02-11 20:47
309 查看
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2002
Time limit: 0.5 second
Memory limit: 64 MB
It was an ordinary grim October morning. The sky was covered by heavy gray clouds. It was a little rainy. The rain drops fell on the windows with quiet bangs. Ilya was sitting at the computer and gloomy
looking out of the window at the bleak scenery. Suddenly, a note caught his attention from the lower right corner of the screen. It said: “You have 1 unread email message(s)”. The boy braced himself for a bit of useless spam and opened the letter. But it turned
out much more interesting...
Dear Sir, You have received this message from the “Rutnok BKS” HR department!
We have received your application for a software developer and we found your CV quite interesting. We would like to suggest a simple test task to evaluate your professional skills. Your task is to implement
the register system for a forum. It must support three types of operations:
“register username password”: to register a new user with name “username” and password “password”. If such user already exists in the database, the system should output the error message “fail: user already exists”. Otherwise, it should output message “success:
new user added”.
“login username password”: to log into the system as user “username” with password “password”. If such user does not exist, the system should output “fail: no such user”. Otherwise, if the user enters an incorrect password, the system should output “fail:
incorrect password”. Otherwise, if the user is already logged in the system at that moment, it should output “fail: already logged in”. Otherwise, it should output “success: user logged in”.
“logout username”: to log out of the system as user “username”. If such user does not exist, the system should output “fail: no such user”. Otherwise, if the user isn’t in the system at that moment, it should output “fail: already logged out”. Otherwise,
it should output “success: user logged out”.
Use this letter as a formal description of the algorithm and follow the described format of system messages. Good luck!
Ilya stopped doing anything else and started solving the test task. You can try to solve it as well!
that “username” and “password” are non-empty strings with length of up to 30 characters. All characters in these strings have codes from 33 to 126.
Problem Author: Kirill Borozdin
Problem Source: Ural Regional School Programming Contest 2013
代码如下:
2002. Test Task
Time limit: 0.5 secondMemory limit: 64 MB
It was an ordinary grim October morning. The sky was covered by heavy gray clouds. It was a little rainy. The rain drops fell on the windows with quiet bangs. Ilya was sitting at the computer and gloomy
looking out of the window at the bleak scenery. Suddenly, a note caught his attention from the lower right corner of the screen. It said: “You have 1 unread email message(s)”. The boy braced himself for a bit of useless spam and opened the letter. But it turned
out much more interesting...
Dear Sir, You have received this message from the “Rutnok BKS” HR department!
We have received your application for a software developer and we found your CV quite interesting. We would like to suggest a simple test task to evaluate your professional skills. Your task is to implement
the register system for a forum. It must support three types of operations:
“register username password”: to register a new user with name “username” and password “password”. If such user already exists in the database, the system should output the error message “fail: user already exists”. Otherwise, it should output message “success:
new user added”.
“login username password”: to log into the system as user “username” with password “password”. If such user does not exist, the system should output “fail: no such user”. Otherwise, if the user enters an incorrect password, the system should output “fail:
incorrect password”. Otherwise, if the user is already logged in the system at that moment, it should output “fail: already logged in”. Otherwise, it should output “success: user logged in”.
“logout username”: to log out of the system as user “username”. If such user does not exist, the system should output “fail: no such user”. Otherwise, if the user isn’t in the system at that moment, it should output “fail: already logged out”. Otherwise,
it should output “success: user logged out”.
Use this letter as a formal description of the algorithm and follow the described format of system messages. Good luck!
Ilya stopped doing anything else and started solving the test task. You can try to solve it as well!
Input
The first line contains an integer n that is the number of operations (1 ≤ n ≤ 100). Each of the next n lines contains a single query in the format given above. We can assumethat “username” and “password” are non-empty strings with length of up to 30 characters. All characters in these strings have codes from 33 to 126.
Output
For each operation, print the message on a single line in the format given above. Be sure to put spaces and punctuation marks in the right places in the messages.Sample
input | output |
---|---|
6 register vasya 12345 login vasya 1234 login vasya 12345 login anakin C-3PO logout vasya logout vasya | success: new user added fail: incorrect password success: user logged in fail: no such user success: user logged out fail: already logged out |
Problem Source: Ural Regional School Programming Contest 2013
代码如下:
#include <cstdio> #include <cstring> #include <map> #include <algorithm> #include <iostream> #include <string> using namespace std; map<string,string>m1, m2; int main() { int n; string s1, s2, s3; while(~scanf("%d",&n)) { m1.clear(); m2.clear(); for(int i = 0; i < n; i++) { cin >>s1; if(s1 == "register") { cin >>s2>>s3; if(m1.find(s2) == m1.end()) { m1.insert(make_pair(s2, s3)); cout<<"success: new user added"<<endl; } else { cout<<"fail: user already exists"<<endl; } } else if(s1 == "login") { cin >>s2>>s3; // m2.insert(make_pair(s2, s3)); map<string, string>::iterator it; if(m1.find(s2) != m1.end())//已注册 { it = m1.find(s2); if(it->second != s3) { cout<<"fail: incorrect password"<<endl; continue; } else if(m2.find(s2) != m2.end())//已登陆 { cout<<"fail: already logged in"<<endl; } else if(it->second == s3) { m2.insert(make_pair(s2, s3)); cout<<"success: user logged in"<<endl; } } else//未注册 { cout<<"fail: no such user"<<endl; } } else if(s1 == "logout") { cin >>s2; if(m1.find(s2) == m1.end()) { cout<<"fail: no such user"<<endl; } else if(m2.find(s2) == m2.end()) { cout<<"fail: already logged out"<<endl; } else { map<string, string>::iterator it; it = m2.find(s2); m2.erase(it); cout<<"success: user logged out"<<endl; } } } } return 0; }
相关文章推荐
- Python爬虫之模拟登陆知乎
- 12.综合运用:模拟王者荣耀的登陆界面,并实现两个页面的跳转
- UVA 156 【map的使用】【字符串模拟】
- 一步一步学WF系列(四)——工作流模拟登陆
- Python_模拟登陆新浪微博
- Python模拟登陆v2ex
- [PHP]用PHP模拟126邮箱的登陆过程来收取邮件
- Node.js:实现知乎(www.zhihu.com)模拟登陆,获取用户关注主题
- python实战系列之模拟用户密码登陆系统(一)
- PL/SQL REPORT 开发--模拟登陆
- 模拟登陆局部生成静态页面代码案例
- python requests添加cookies模拟登陆爬取网页
- URL模拟登陆出现的小问题(新手)
- php实现简单的模拟登陆
- 腾讯微博采集器 模拟登陆 2012-11-26版
- [URAL][1520][Empire Strikes Back][模拟退火]
- 使用OKHttp模拟登陆知乎,兼谈OKHttp中Cookie的使用!
- cURL模拟登陆的各种问题
- 使用OkHttp模拟登陆LeetCode
- C# 模拟网站登陆