Timus 2002. Test Task 一个登陆系统

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:

  1. “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”.
  2. “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”.
  3. “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 assume
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.

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

实现一个登陆系统。

思路:

1 使用map,在map中的就是已经注册的了

2 使用数据结构保存用户名,是否登陆和密码

3 使用if else判断处理第一个字符串-命令

类似很多人都写的什么图书馆管理系统,什么信息系统之类的登陆控制管理,都是很简单的东西,一步一步写就不会错了,完成速度相当于打字速度。

#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <unordered_set>
using namespace std;

namespace
{
	struct States
	{
		bool logined;
		string name;
		string pw;
		States(string n="", string p="", bool l = false):
		name(n), pw(p), logined(l){}
	};
}

void TestTask2002()
{
	int opTimes = 0;
	cin>>opTimes;

	unordered_map<string, States> umSS;
	string command, name, pw;

	while (opTimes--)
	{
		cin>>command>>name;
		if ("register" == command)
		{
			cin>>pw;//logout不用pw的
			if (umSS.count(name)) cout<<"fail: user already exists\n";
			else
			{
				States st(name, pw);
				umSS[name] = st;
				cout<<"success: new user added\n";
			}
		}
		else if ("login" == command)
		{
			cin>>pw;
			if (!umSS.count(name)) cout<<"fail: no such user\n";
			else if (pw != umSS[name].pw) cout<<"fail: incorrect password\n";
			else if (umSS[name].logined) cout<<"fail: already logged in\n";
			else
			{
				cout<<"success: user logged in\n";
				umSS[name].logined = true;
			}
		}
		else if ("logout" == command)
		{
			if (!umSS.count(name)) cout<<"fail: no such user\n";
			else if (!(umSS[name].logined)) cout<<"fail: already logged out\n";
			else
			{
				cout<<"success: user logged out\n";
				umSS[name].logined = false;
			}
		}
	}
}
时间: 2024-08-26 12:44:46

Timus 2002. Test Task 一个登陆系统的相关文章

URAL 2002. Test Task(登陆模拟 map )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2002 2002. Test Task 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 win

查看登陆系统用户的信息的三种方法详解

查看登陆系统用户的信息的三种方法详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.who这个命令显示可以谁在登陆,但是这个有很多的花式玩法,这个命令超简单 语法:who [OPTION]... [ FILE | ARG1 ARG2 ] 1.参数:-u,显示闲置时间,若该用户在前一分钟之内有进行任何动作,将标示成"."号,如果该用户已超过24小时没有任何动作,则标示出"old"字符串. 例如: 2.参数:-m,此参数的效果和指定"a

XMPP登陆与项目已有登陆系统冲突#解决方案#的流程图

做XMPP项目,一般如果两个登陆系统,比如我们的项目有一个OA系统,有一个XMPP登陆系统,正常的做法是让服务器端的人操作数据库,将OA系统的数据库导入到XMPP系统中也就是openfire数据库中,但是如果服务器端不愿意或者不想做,其实咱们做app的也是可以做.下面将基本的思路介绍下: XMPP登陆与项目已有登陆系统冲突#解决方案#的流程图,布布扣,bubuko.com

JSP/Servlet Web 学习笔记 DayThree —— 实现一个登陆小界面

项目说明 利用JSP.HTML.JS实现了一个简易的登陆系统.根据前两天的所学,实现了如下功能: a)用户名.密码验证(不基于数据库,只做一个简单的表单数据获取并验证) b)页面访问次数统计 c)验证用户名.密码为空的提醒弹窗 d)一个简易的根据输入错误次数来实现的防恶意登陆的小弹窗 相关知识点: a)主要利用form的POST方法传递数据,在此之外再基本利用<jsp:param>标签配合<jsp:forward>传递其他的相关数据. b)此外JavaScript可以完美嵌套于JS

Ubuntu中使用Docker/LXC迅速启动一个桌面系统

2013年07月18日 | 标签: cloud, container, docker, lxc | 作者:vpsee Docker 是 dotCloud 最近几个月刚宣布的开源引擎,旨在提供一种应用程序的自动化部署解决方案,简单的说就是,在 Linux 系统上迅速创建一个容器(类似虚拟机)并在容器上部署和运行应用程序,并通过配置文件可以轻松实现应用程序的自动化安装.部署和升级,非常方便.因为使用 了容器,所以可以很方便的把生产环境和开发环境分开,互不影响,这是 docker 最普遍的一个玩法.更

从无到有,用Nodejs+express+mongodb搭建简易登陆系统

前端处理server表示很蛋疼,初学Node,虽然感觉异常强大,但是学起来还是有些吃力的,Node是工具,它不是万能的,搭建一个系统还是需要借助其他一些工具,对于我这个没怎么接触server的前端来说,挑战是有的.昨天参考一些资料尝试用Node+express+mongodb搭建一个简易的登陆系统,在此记之. express是一个灵活的nodejs web应用框架, 提供一系列强大特性帮助你创建各种Web应用. Mongodb是数据库. 1.安装express,express安装比较简单,直接用

使用 Docker/LXC 迅速启动一个桌面系统

使用 Docker/LXC 迅速启动一个桌面系统 Docker 是 dotCloud 最近几个月刚宣布的开源引擎,旨在提供一种应用程序的自动化部署解决方案,简单的说就是,在 Linux 系统上迅速创建一个容器(类似虚拟机)并在容器上部署和运行应用程序,并通过配置文件可以轻松实现应用程序的自动化安装.部署和升级,非常方便.因为使用 了容器,所以可以很方便的把生产环境和开发环境分开,互不影响,这是 docker 最普遍的一个玩法.更多的玩法还有大规模 web 应用.数据库部署.持续部署.集群.测试环

【Mac系统 + Python + Django】之开发一个发布会系统【Django视图(一)】

这里我们要进行开发一个发布会系统来了解Django框架,来看第一部分. 目录: 一.登录功能 二.Cookie和Session 三.Django认证系统 一.登录功能 返回目录 在上一章,我们已经简单用了html模板,在它基础上继续开发: 1.修改index.html文件为发布会html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> &l

一个Windows 系统究竟有多复杂?

一个Windows 系统究竟有多复杂? 来源:开发者WEB Microsoft Windows问世于1985年,起初仅仅是Microsoft-DOS模拟环境,后续的系统版本由于微软不断的更新升级,不但易用,也慢慢的成为家家户户人们最喜爱的操作系统. 下面我们从代码行数.开发难度,参与人员的数量,开发的时间长度等角度来说说,一个windows系统有多复杂. Windows XP 大约40,000,000行代码. Windows Vista 大约50,000,000行代码. Windows 7 大约