1129 Recommendation System (25 point(s))

1129 Recommendation System (25 point(s))

Recommendation system predicts the preference that a user would give to an item. Now you are asked to program a very simple recommendation system that rates the user's preference by the number of times that an item has been accessed by this user.

Input Specification:

Each input file contains one test case. For each test case, the first line contains two positive integers: N (≤ 50,000), the total number of queries, and K (≤ 10), the maximum number of recommendations the system must show to the user. Then given in the second line are the indices of items that the user is accessing -- for the sake of simplicity, all the items are indexed from 1 to N. All the numbers in a line are separated by a space.

Output Specification:

For each case, process the queries one by one. Output the recommendations for each query in a line in the format:

query: rec[1] rec[2] ... rec[K]

where query is the item that the user is accessing, and rec[i] (i=1, ... K) is the i-th item that the system recommends to the user. The first K items that have been accessed most frequently are supposed to be recommended in non-increasing order of their frequencies. If there is a tie, the items will be ordered by their indices in increasing order.

Note: there is no output for the first item since it is impossible to give any recommendation at the time. It is guaranteed to have the output for at least one query.

Sample Input:

12 3
3 5 7 5 5 3 2 1 8 3 8 12

Sample Output:

5: 3
7: 3 5
5: 3 5 7
5: 5 3 7
3: 5 3 7
2: 5 3 7
1: 5 3 2
8: 5 3 1
3: 5 3 1
8: 3 5 1
12: 3 5 8

考察点:STL set的理解和灵活使用

题意:

用户每想购买一件商品,就向他推荐K个商品,这K个商品从他从前的购买过的商品中按照购买次数从高到低的顺序选择K个,如果有购买次数相同的,就按照商品的编号从小到大排序。 

心路历程:

好好写一下做这道题的心路历程。首先,这道题的输出是随着查询的次数而增长的,对于每一次查询,要基于购买次数和下标进行排序,可想而知,如果每一次查询都重新排序,时间复杂度必然太高。需要注意,每一次查询我们还需要修改购买次数,而排序后我们不能够通过下标再访问了,那还需要基于线性结构的查找,这也是很复杂的。

显然,我们需要寻找一种数据结构来维护这个排序,利用红黑树的set自动排序,我们可以对结构体(包括下标和访问次数)重载运算符<来实现。这个方法我在做题的时候也是想到了,但是问题仍然是如何去修改set中的元素呢?因为我们不能够只通过商品下标来查找,于是我们需要额外维护一个数组记录每个商品的访问次数,仅仅是为了我们对set的查询。

似乎一切迎刃而解了,但是set的元素并不能修改,怎么办呢?先删除后插入。

这道题真的学了很多东西,花了一个多小时,参考了大神的做法(https://blog.youkuaiyun.com/richenyunqi/article/details/80173656),对set的理解更加深入了,如何合理高效地利用轮子,真的是机考中的重中之重!

#include<iostream>
#include<map>
#include<algorithm>
#include<set>
using namespace std;
const int MAX = 5e4+7;
struct item{
	int id;int cnt;
	item(int i,int c):id(i),cnt(c){}
	bool operator < (const item &other) const{
		if(cnt!=other.cnt) return cnt>other.cnt;
		else return id<other.id;
	}
};
set<item> s;
int itemCount[MAX]={0};
int main(void){
	int N,K,a;cin>>N>>K;
	for(int i=0;i<N;i++){
		cin>>a;
		if(i>0){
			cout<<a<<":";
			int output = 0;
			for(set<item>::iterator i=s.begin();i!=s.end()&&output<K;i++){
				cout<<" "<<(*i).id;
				output++;
			}
			cout<<endl;
		} 
		set<item>::iterator it = s.find(item(a,itemCount[a]));//寻找在set的红黑树中是否已经有这一项 
		itemCount[a]++;
		if(it!=s.end()){//如果有 
			s.erase(it);// 删除后再插入达到修改的目的 
			s.insert(item(a,itemCount[a]));
		}else{//没有找到 
			s.insert(item(a,itemCount[a])); 
		}
	}
	return 0;
}

 

SQL : insert into m_item(version,pattern_type,pattern_code,item_code,shipment,system_item_code,apply_start_date,apply_end_date,order_start_date,order_end_date,vendor_code,pma_code,information_category_code,subcategory_code,item_name,item_name_for_search,pos_item_name,maker_code,license_code,quantity,particular_type,tax_type,indefinite_type,marketprice_item_type,magazine_period_revised_type,magazine_store_information_output,special_order_item_type,reserve_item_type,sales_report_separation_type,consigned_item_type,item_size_width,item_size_height,item_size_depth,item_rank,specific_item_type,matrix_vertical_code,host_cycle_code,store_cycle_code,lead_time,common_delivery_code,label_code,order_unit,order_limit_quantity,minimum_order_quantity,cost_unit_price,recommendation_sales_price,gp,quantity_cost_price,quantity_sales_price,keyff_number,small_keyff_number,monday_no_delivery,tuesday_no_delivery,wednesday_no_delivery,thursday_no_delivery,friday_no_delivery,saturday_no_delivery,sunday_no_delivery,freshness_management_type,delivery_freshness,sales_freshness,date_quantity_unit,freshness_over_time_type,instore_aggregate_flag,date_type,addition_order_type,revised_order_type,price_change_unnecessary_flag,distribution_separation_type,return_permission_type,magazine_type,cut_reason_code,price_change_reason_code,image_discrimination_code,payment_status,pricing_status,misc_status,forced_content_apply_date,order_code,prepaidcard_payment_status,item_tax_rate,pos_image_discrimination_code,point_excluded_flag,price_sticker_output_type,leaflet_print_permission_type,recommendation_reason_discrimination_code,internet_item_type,recommendation_item_category_type,dutyfree_type,payment_status_2,stockout_permission_flag,delivery_start_date,first_delivery_end_date,disaster_common_delivery_type_code,serial_number_scan_type,update_datetime,update_function_id,update_person_id,update_count) values ('0725_002','03','517','010002','0','5ca60ba0-f900-46ef-a271-1b08a0cd98bd',DATE '2025-08-04',DATE '9999-12-31',DATE '2025-04-07',DATE '9999-12-31','90032','01','0102','1','アサヒ ザ・ビタリスト      350ml','アサヒ ザ・ビタリスト      350ml','アサヒ ザビタリスト350ml ','4901004 ','01',1,' ','1',' ',' ',' ',' ',' ',' ',' ',' ',66,123,66,' ',' ',' ','E','35','02',' ','4901004062451',24,999,0,175.10,216,18.9,175.10,216,' ',' ',' ',' ',' ',' ',' ',' ',' ','2',5,2,'2',' ','0','2','0','0',' ',' ',' ',' ',' ',' ','00',2,0,3,DATE '1900-01-01',' ',' ','100',' ',' ','1',' ','004',' ','10',' ',' ',' ',DATE '2025-04-15',DATE '2025-04-15','C',' ',TIMESTAMP '2025-07-25 04:42:53.093','SORD102319','SORD102319',0) SQL : duplicate key value violates unique constraint "m_item_scncs3_pkey" 哪块错了
07-30
# 分析:INSERT语句违反唯一约束错误 错误信息 `duplicate key value violates unique constraint "m_item_scncs3_pkey"` 表示你试图插入的数据违反了主键约束,也就是说,你试图插入的记录的主键值已经在表中存在。 ## 1. 问题分析 从你的INSERT语句和错误信息来看: 1. **错误类型**:违反主键约束 2. **约束名称**:`m_item_scncs3_pkey` 3. **约束字段**:根据约束名称推测是主键约束,具体字段需要查看表定义 从你的INSERT语句中可以看到插入的值: ```sql ('0725_002','03','517','010002','0', ...) ``` 这些值对应字段: ```sql (version, pattern_type, pattern_code, item_code, shipment, ...) ``` 根据你之前提供的表结构和主键定义: ```sql alter table m_item add constraint pk_m_item primary key (item_code, shipment, pattern_type, pattern_code, apply_start_date, version) ``` **主键字段**是: 1. item_code (item_code) 2. shipment (shipment) 3. pattern_type (pattern_type) 4. pattern_code (pattern_code) 5. apply_start_date (apply_start_date) 6. version (version) 你插入的值对应主键字段的值是: 1. item_code = '010002' 2. shipment = '0' 3. pattern_type = '03' 4. pattern_code = '517' 5. apply_start_date = DATE '2025-08-04' 6. version = '0725_002' 这意味着这个组合的主键值已经存在于表中,因此数据库拒绝插入这条记录。 ## 2. 解决方案 ### 方案1:检查数据,避免重复插入 在插入之前先检查该主键是否已经存在: ```sql -- 查询是否存在相同主键的记录 SELECT * FROM m_item WHERE item_code = '010002' AND shipment = '0' AND pattern_type = '03' AND pattern_code = '517' AND apply_start_date = DATE '2025-08-04' AND version = '0725_002'; ``` 如果查询结果不为空,则说明记录已经存在,可以选择更新而不是插入: ```sql -- 如果记录存在则更新 UPDATE m_item SET system_item_code = '5ca60ba0-f900-46ef-a271-1b08a0cd98bd', apply_end_date = DATE '9999-12-31', order_start_date = DATE '2025-04-07', -- 其他字段... WHERE item_code = '010002' AND shipment = '0' AND pattern_type = '03' AND pattern_code = '517' AND apply_start_date = DATE '2025-08-04' AND version = '0725_002'; ``` ### 方案2:使用INSERT ... ON CONFLICT(PostgreSQL) 如果你使用的是PostgreSQL,可以使用`ON CONFLICT`子句来处理冲突: ```sql INSERT INTO m_item ( version, pattern_type, pattern_code, item_code, shipment, system_item_code, apply_start_date, apply_end_date, order_start_date, order_end_date, vendor_code, pma_code, information_category_code, subcategory_code, item_name, item_name_for_search, pos_item_name, maker_code, license_code, quantity, particular_type, tax_type, indefinite_type, marketprice_item_type, magazine_period_revised_type, magazine_store_information_output, special_order_item_type, reserve_item_type, sales_report_separation_type, consigned_item_type, item_size_width, item_size_height, item_size_depth, item_rank, specific_item_type, matrix_vertical_code, host_cycle_code, store_cycle_code, lead_time, common_delivery_code, label_code, order_unit, order_limit_quantity, minimum_order_quantity, cost_unit_price, recommendation_sales_price, gp, quantity_cost_price, quantity_sales_price, keyff_number, small_keyff_number, monday_no_delivery, tuesday_no_delivery, wednesday_no_delivery, thursday_no_delivery, friday_no_delivery, saturday_no_delivery, sunday_no_delivery, freshness_management_type, delivery_freshness, sales_freshness, date_quantity_unit, freshness_over_time_type, instore_aggregate_flag, date_type, addition_order_type, revised_order_type, price_change_unnecessary_flag, distribution_separation_type, return_permission_type, magazine_type, cut_reason_code, price_change_reason_code, image_discrimination_code, payment_status, pricing_status, misc_status, forced_content_apply_date, order_code, prepaidcard_payment_status, item_tax_rate, pos_image_discrimination_code, point_excluded_flag, price_sticker_output_type, leaflet_print_permission_type, recommendation_reason_discrimination_code, internet_item_type, recommendation_item_category_type, dutyfree_type, payment_status_2, stockout_permission_flag, delivery_start_date, first_delivery_end_date, disaster_common_delivery_type_code, serial_number_scan_type, update_datetime, update_function_id, update_person_id, update_count ) VALUES ( '0725_002', '03', '517', '010002', '0', '5ca60ba0-f900-46ef-a271-1b08a0cd98bd', DATE '2025-08-04', DATE '9999-12-31', DATE '2025-04-07', DATE '9999-12-31', '90032', '01', '0102', '1', 'アサヒ ザ・ビタリスト      350ml', 'アサヒ ザ・ビタリスト      350ml', 'アサヒ ザビタリスト350ml ', '4901004 ', '01', 1, ' ', '1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 66, 123, 66, ' ', ' ', ' ', 'E', '35', '02', ' ', '4901004062451', 24, 999, 0, 175.10, 216, 18.9, 175.10, 216, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '2', 5, 2, '2', ' ', '0', '2', '0', '0', ' ', ' ', ' ', ' ', ' ', ' ', '00', 2, 0, 3, DATE '1900-01-01', ' ', ' ', '100', ' ', ' ', '1', ' ', '004', ' ', '10', ' ', ' ', ' ', DATE '2025-04-15', DATE '2025-04-15', 'C', ' ', TIMESTAMP '2025-07-25 04:42:53.093', 'SORD102319', 'SORD102319', 0 ) ON CONFLICT (item_code, shipment, pattern_type, pattern_code, apply_start_date, version) DO UPDATE SET system_item_code = EXCLUDED.system_item_code, apply_end_date = EXCLUDED.apply_end_date, order_start_date = EXCLUDED.order_start_date, -- 其他字段... update_datetime = NOW(), update_count = m_item.update_count + 1; ``` ### 方案3:修改主键字段 如果你确定不需要这个主键约束,或者需要修改主键字段,可以考虑删除约束并重新定义: ```sql -- 删除主键约束 ALTER TABLE m_item DROP CONSTRAINT m_item_scncs3_pkey; -- 添加新的主键约束(如果需要) ALTER TABLE m_item ADD CONSTRAINT pk_m_item PRIMARY KEY (new_column); ``` 但**不建议**随意修改主键,除非你完全理解业务需求和数据模型。 ## 3. 建议的改进 1. **使用唯一约束而不是主键(如果需要允许NULL)**: ```sql -- 如果某些字段可能为NULL,可以使用唯一约束 CREATE UNIQUE INDEX idx_unique_m_item ON m_item (item_code, shipment, pattern_type, pattern_code, apply_start_date, version); ``` 2. **使用序列或UUID生成唯一主键**: ```sql -- 如果item_code可以重复,可以考虑使用自增ID ALTER TABLE m_item ADD COLUMN id SERIAL PRIMARY KEY; ``` 3. **添加检查约束**: ```sql -- 确保shipment字段的取值范围 ALTER TABLE m_item ADD CONSTRAINT chk_shipment CHECK (shipment IN ('0', '1', '2', '3')); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值