跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[程序员] 分辨多个用户之间是否是分身的算法?

发表于

我有一个需求是为了分辨多个用户之间是否是分身(需求背景是为了防止新注册用户薅羊毛,优惠力度挺大的,是分身的用户只要一人享受了优惠,其他人不能再次享受), 所以我要将多个用户之间人为的去关联,比如用户 A 关联了 B ,A 和 B 互为分身;用户 B 再关联了 C ,B 和 C 互为 分身,C 和 A 也互为分身,因为有中间人 B ,以此类推,中间人的层级不限,这种用推荐使用什么算法实现呢?

我自己想到的是多存数据将这种层级平铺:

当用户 A 直接关联用户 B 时,我们在 associations 表中插入两条记录:

associations 表结构:user_id 关联用户 ID ,associated_user_id:被关联用户 ID ,is_direct:是否是直接关联

INSERT INTO associations (user_id, associated_user_id, is_direct) VALUES (A 的用户 ID, B 的用户 ID, TRUE);
INSERT INTO associations (user_id, associated_user_id, is_direct) VALUES (B 的用户 ID, A 的用户 ID, TRUE);

当用户 B 再直接关联用户 C 时,不仅插入 B 和 C 之间的直接关联记录,还插入 A 和 C 之间的间接关联记录:

INSERT INTO associations (user_id, associated_user_id, is_direct) VALUES (B 的用户 ID, C 的用户 ID, TRUE);
INSERT INTO associations (user_id, associated_user_id, is_direct) VALUES (C 的用户 ID, B 的用户 ID, TRUE);
INSERT INTO associations (user_id, associated_user_id, is_direct) VALUES (A 的用户 ID, C 的用户 ID, FALSE);
INSERT INTO associations (user_id, associated_user_id, is_direct) VALUES (C 的用户 ID, A 的用户 ID, FALSE);

需要用到的场景有: 取消关联某两个用户之间的关联 查询给定用户的所有分身

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.