Codeforce:4C. Registration system (映射)
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 name i 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
output
OK
OK
abacaba1
OK
input
6
first
first
second
second
third
third
output
OK
first1
OK
second1
OK
third1
思路:这道题是一道映射题,name对应的有几个即可
#include<bits/stdc++.h>
using namespace std;
int main() {
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t; cin >> t;
unordered_map<string, int>s; string str;//unordered_map 不进行排序优化时间,不过仅从900+ms减少到600+ms
while (t--) {
cin >> str;
s[str]++;
if (s[str] > 1)cout << str << s[str] - 1 << endl;
else cout << "OK" << endl;
}
}
//dalao的优化时间算法:154ms
#include<bits/stdc++.h>
using namespace std;
#define getchar_unlocked() _getchar_nolock()
template<typename T> inline bool sc(T& num) {
bool neg = 0; int c; num = 0;
while (c = getchar_unlocked(), c < 33) {
if (c == EOF) return false;
}
if (c == '-') {
neg = 1; c = getchar_unlocked();
}
for (; c > 47; c = getchar_unlocked()) num = num * 10 + c - 48; if (neg) num *= -1; return true; }
template<typename T, typename ...Args> inline void sc(T& num, Args&...args) { bool neg = 0; int c; num = 0; while (c = getchar_unlocked(), c < 33) { ; } if (c == '-') { neg = 1; c = getchar_unlocked(); } for (; c > 47; c = getchar_unlocked()) num = num * 10 + c - 48; if (neg) num *= -1; sc(args...); }
inline void getstr(string& str) {
str.clear(); char cur;
while (cur = getchar_unlocked(), cur < 33) { ; }
while (cur > 32) { str += cur; cur = getchar_unlocked(); }
}
int32_t main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
sc(n);
unordered_map<string, int> arr;
string str;
for (int i = 0; i < n; i++)
{
getstr(str);
int r = arr[str]++;
if (!r)
{
cout << "OK\n";
}
else
{
cout << str << r << '\n';
}
}
return 0;
}
Codeforce:4C. Registration system (映射)的更多相关文章
- (水题)Codeforces - 4C - Registration system
https://codeforces.com/problemset/problem/4/C 用来哈希的一道题目,用map也可以强行过,但是性能慢了6倍,说明是在字符串比较的时候花费了接近6倍的时间. ...
- (用了map) Registration system
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93241#problem/C (654123) http://codeforces.com ...
- ACM Registration system
Registration system 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 A new e-mail service "Berlandesk&q ...
- Codeforces Beta Round #4 (Div. 2 Only) C. Registration system hash
C. Registration system Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
- c题 Registration system
Description A new e-mail service "Berlandesk" is going to be opened in Berland in the near ...
- CodeForces-4C Registration system
// Registration system.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include <iostream> # ...
- nyoj Registration system
Registration system 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 A new e-mail service "Berlandesk&q ...
- codeforces Registration system
Registration system A new e-mail service "Berlandesk" is going to be opened in Berland in ...
- Codeforces Beta Round #4 (Div. 2 Only) C. Registration system【裸hash/map】
C. Registration system time limit per test 5 seconds memory limit per test 64 megabytes input standa ...
- Registration system
Registration system 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 A new e-mail service "Berlandesk&q ...
随机推荐
- 面试题——为什么 Vue 中不要用 index 作为 key?(diff 算法详解)
前言 在vue中使用v-for时需要,都会提示或要求使用 :key,有的的开发者会直接使用数组的 index 作为 key 的值,但不建议直接使用 index作为 key 的值,有时我们面试时也会遇 ...
- Mongoose查增改删
在src目录下新建一个文件夹models,用来存放数据模型和操作数据库的方法. 在models目录下新建一个文件user.js,用来管理用户信息相关的数据库操作. 相关的数据模型和数据库操作方法,最后 ...
- 基于DotNetty实现一个接口自动发布工具 - 通信实现
基于 DotNetty 实现通信 DotNetty : 是微软的 Azure 团队,使用 C#实现的 Netty 的版本发布.是.NET 平台的优秀网络库. 项目介绍 OpenDeploy.Commu ...
- [THUPC 2023 初赛] 快速 LCM 变换
题目描述 小 I 今天学习了快速最小公倍数变换(Fast Least-Common-Multiple Transform, FLT),于是他想考考你. 给定一个长度为 \(n\) 的正整数序列 \(r ...
- [ABC263C] Monotonically Increasing
Notes For two integer sequences of the same length $A_1,A_2,\dots,A_N$ and $B_1,B_2,\dots,B_N$, $A$ ...
- LR(0)分析法
LR(0)是一种自底向上的语法分析方法.两个基本动作是移进和规约. 具体例子如下 已知文法G[E] (1) E→aА (2) E→bB (3) A→cА (4) A→d (5) B→cB (6) B→ ...
- 华企盾DSC导致金蝶导入Excel导入不了的问题
需要把Excel的OLE控制关掉,并且金蝶的进程和Excel的进程高级设置要么都启用重定向,要么都不启用重定向
- 从零玩转Websocket实时通讯服务之前后端分离版本-websocket
title: 从零玩转Websocket实时通讯服务之前后端分离版本 date: 2021-10-25 00:47:12.945 updated: 2021-12-26 17:43:10.496 ur ...
- Python——第五章:Traceback模块
traceback 模块提供了在程序中处理和分析异常时的工具,帮助开发人员更好地理解程序出现问题的原因. 使用 traceback.format_exc() 函数可以获取当前异常的堆栈信息.可以把错误 ...
- Python——第二章:替换和切割
strip() 用法: .strip() 是字符串方法之一,在 Python 中用于移除字符串开头和结尾的空白字符(包括空格.制表符 \t.换行符\n等).这个方法返回一个新的字符串,原始字符串本身不 ...