ural 2002. Test Task 简单模拟 stl map的使用

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 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



简单模拟下,map的使用。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define INF 999999999
#define eps 0.00001
#define LL __int64d
#define pi acos(-1.0)
 

#define IN freopen("input.txt", "r", stdin)    
#define OUT freopen("output.txt", "w", stdout)
//IN;
//OUT;

map<string,pair<string,int> >my;

int main()
{
	string op,name,paw;
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		my.clear();
		for(int i=0;i<n;i++)
		{
			cin>>op;
			cin>>name;
			
			pair <string,int> tem;
			if(op=="register")
			{
				cin>>paw;
				if(my.count(name)!=0)
					puts("fail: user already exists");
				else
				{ 
					tem.first=paw;
					tem.second=0; 
					my[name]=tem;
					puts("success: new user added");
				}
			}
			else if(op=="login")
			{
				cin>>paw;
				if(my.count(name)==0)
				{
					puts("fail: no such user");
				}
				else
				{
					tem=my[name];
					if(tem.first!=paw)
					puts("fail: incorrect password"); 
					else if(tem.second==1)
					{
						puts("fail: already logged in");
					}
					else
					{
						puts("success: user logged in");
						my[name].second=1;
					} 
				} 
			}
			else 
			{ 
				if(my.count(name)==0)
				{
					puts("fail: no such user");
				}
				else
				{
					tem=my[name];
					if(tem.second==1)
					{
						puts("success: user logged out");
						my[name].second=0;
					}
					else
						puts("fail: already logged out");
				}
			}
		}
	} 
	return 0;
}
/*
6
register vasya 12345
login vasya 1234
login vasya 12345
login anakin C-3PO
logout vasya
logout vasya
*/








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值