C. Registration system(map)

C. Registration system
time limit per test5 seconds
memory limit per test64 megabytes
inputstandard input
outputstandard output
A new e-mail service “Berlandesk” is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that’s why they ask you to help. You’re suggested to implement the prototype of site registration system. The system should work on the following principle.

Each time a new user wants to register, he sends to the system a request with his name. If such a name does not exist in the system database, it is inserted into the database, and the user gets the response OK, confirming the successful registration. If the name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1, name2, …), among these numbers the least i is found so that namei does not yet exist in the database.

Input
The first line contains number n (1 ≤ n ≤ 105). The following n lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters.

Output
Print n lines, which are system responses to the requests: OK in case of successful registration, or a prompt with a new name, if the requested name is already taken.

Examples
input
4
abacaba
acaba
abacaba
acab
outputCopy
OK
OK
abacaba1
OK
inputCopy
6
first
first
second
second
third
third
output
OK
first1
OK
second1
OK
third1

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<queue>
#include<map>
using namespace std;
const int N=1e5+5;
int main()
{
   map<string,int>s;//map的便利之处,自己做题的时候都没想到
   int n;
   cin>>n;
   while(n--)
   {
   	string str;
   	cin>>str;
   	if(s[str])//该字符串出现的次数
   	{
   		cout<<str<<s[str]<<endl;
	}
	else
	cout<<"OK"<<endl;
	s[str]++;
	} 
	return 0;
}

//优秀的stl啊,我还是掌握的不行

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例,展示了在`register`方法中根据不同的情况抛出异常: ```java import java.util.HashMap; import java.util.Map; // 自定义异常类 class UserRegistrationException extends Exception { public UserRegistrationException(String message) { super(message); } } class User { private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } } class UserRegistration { private Map<String, User> userMap; public UserRegistration() { userMap = new HashMap<>(); } public void register(String username, String password, String confirmPassword) throws UserRegistrationException { if (userMap.containsKey(username)) { throw new UserRegistrationException("该用户名已存在,请选择其他用户名"); } if (!password.equals(confirmPassword)) { throw new UserRegistrationException("两次输入的密码不一致"); } // 注册用户 User newUser = new User(username, password); userMap.put(username, newUser); System.out.println("用户注册成功"); } } class Example { public static void main(String[] args) { UserRegistration registration = new UserRegistration(); try { // 模拟注册过程 registration.register("user1", "password123", "password123"); // 正常情况 registration.register("user2", "password456", "password789"); // 密码不一致情况 registration.register("user1", "password123", "password123"); // 用户名已存在情况 } catch (UserRegistrationException e) { System.out.println(e.getMessage()); } } } ``` 在上面的示例中,我们定义了一个`UserRegistrationException`自定义异常类,用于表示用户注册过程中可能出现的异常情况。 `UserRegistration`类中的`register`方法接收用户名、密码和确认密码作为参数,并在不同的情况下抛出异常。如果用户名已存在,将抛出一个自定义异常,如果两次输入的密码不一致,也会抛出一个自定义异常。 在`Example`类的`main`方法中,我们创建了一个`UserRegistration`对象,并模拟了三种不同的注册情况:正常情况、密码不一致情况和用户名已存在情况。通过捕获并处理可能抛出的自定义异常,我们可以根据不同的情况进行相应的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值