题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2002
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 |
代码如下:
#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; }
URAL 2002. Test Task(登陆模拟 map )
原文地址:http://blog.csdn.net/u012860063/article/details/43739801