Day 20 C. Registration system

Problem
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 ≤ 10^5). 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

#include<iostream>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
int main()//一道非常经典的map记录string出现次数题目 题意看两组案例即可明白 
{
	map<string, int> m;
	string s;
	int n;
	cin >> n;
	while (n--)
	{
		cin >> s;
		m[s]++;
		if (m[s] > 1)
		{
			cout << s << m[s] - 1 << endl;
		}
		else
		{
			cout << "OK" << endl;
		}
	}
	return 0;
}
在Elasticsearch中,`AggregationBuilders.interval`用于构建**日期直方图聚合**(`Date Histogram Aggregation`),它允许根据时间间隔对数据进行分组。这种聚合通常用于按天、月、年等时间单位对时间序列数据进行统计分析。 以下是一个使用Java API实现的示例,展示了如何通过`DateHistogramAggregationBuilder`设置`interval`进行聚合操作: ### 示例:按天分组统计销售数据 ```java import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.index.search.SearchRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestClient; import org.elasticsearch.http.HttpHost; import java.io.IOException; import java.util.TimeZone; public class DateHistogramAggregationExample { public static void main(String[] args) throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); SearchRequest searchRequest = new SearchRequest("sales_data"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.size(0); // 构建日期直方图聚合,按天进行分组 sourceBuilder.aggregation(AggregationBuilders.dateHistogram("sales_per_day") .field("sale_date") .calendarInterval(DateHistogramInterval.DAY) .timeZone(TimeZone.getTimeZone("UTC"))); searchRequest.source(sourceBuilder); try { // 执行查询并获取响应 org.elasticsearch.search.SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); org.elasticsearch.search.aggregations.Aggregations aggregations = response.getAggregations(); // 获取聚合结果并遍历输出 org.elasticsearch.search.aggregations.bucket.histogram.DateHistogram dateHistogram = aggregations.get("sales_per_day"); for (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket entry : dateHistogram.getBuckets()) { System.out.println(entry.getKey() + " : " + entry.getDocCount()); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } finally { client.close(); } } } ``` 在上述代码中,`AggregationBuilders.dateHistogram("sales_per_day")`创建了一个日期直方图聚合,`field("sale_date")`指定聚合字段为`sale_date`,`calendarInterval(DateHistogramInterval.DAY)`表示按天进行分组,`timeZone`设置了时区为UTC[^3]。 --- ### 示例:按月分组统计用户注册数 ```java // 按月分组的日期直方图聚合 sourceBuilder.aggregation(AggregationBuilders.dateHistogram("users_registered_per_month") .field("registration_date") .calendarInterval(DateHistogramInterval.MONTH) .timeZone(TimeZone.getTimeZone("Asia/Shanghai"))); ``` 此示例中,`calendarInterval(DateHistogramInterval.MONTH)`表示按月分组统计用户注册数量,`timeZone`设置为`Asia/Shanghai`以匹配本地时间。 --- ### `interval`的可选单位 Elasticsearch支持多种时间间隔单位,包括但不限于: - `DateHistogramInterval.SECOND` - `DateHistogramInterval.MINUTE` - `DateHistogramInterval.HOUR` - `DateHistogramInterval.DAY` - `DateHistogramInterval.WEEK` - `DateHistogramInterval.MONTH` - `DateHistogramInterval.QUARTER` - `DateHistogramInterval.YEAR` 这些时间单位可以灵活地用于不同场景下的聚合需求[^2]。 --- ### 注意事项 - 如果字段不是日期类型(如`date`或`date_nanos`),聚合将无法正常执行。 - `calendarInterval`与`fixedInterval`的区别在于,`calendarInterval`遵循日历规则(如月份的天数不固定),而`fixedInterval`是固定的时间间隔(如每`30d`)。 - 使用`timeZone`时需确保时间字段在索引时已正确处理时区信息。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值