사용자 이름, 이메일 및 전화 번호를 순서에 관계 없이 보유하는 연락처 목록이 있다고 가정하면 동일한 연락처(같은 사람이 여러 연락처를 갖고 있는 경우)를 찾아 동일한 연락처를 반환해야 합니다. 함께. 우리는 다음을 명심해야 합니다 -
-
연락처는 모든 순서에 따라 사용자 이름, 이메일 및 전화 필드를 저장할 수 있습니다.
-
동일한 사용자 이름 또는 동일한 이메일 또는 전화번호를 사용하는 경우 두 개의 연락처가 동일합니다.
따라서 입력이 연락처 =[{"Amal", "amal@gmail.com", "+915264"},{ "Bimal", "bimal321@yahoo.com", " +1234567"},{ "Amal123", "+1234567", "amal_new@gmail.com"},{ "AmalAnother", "+962547", "amal_new@gmail.com"}], 출력은 [ 0,2,3], [1] 인덱스 [0,2,3]의 연락처는 동일하고 인덱스 1의 다른 연락처입니다.
이 문제를 해결하기 위해 다음 단계를 따릅니다. −
-
generate_graph() 함수를 정의하십시오. 이것은 cnt, n, 행렬이 필요합니다.
-
0에서 n 사이의 i에 대해 수행
-
0에서 n 사이의 j에 대해 수행
-
행렬[i, j] :=0
-
-
-
0에서 n 사이의 i에 대해 수행
-
i + 1에서 n 사이의 j에 대해 수행
-
cnt[i].slot1이 cnt[j].slot1과 같거나 cnt[i].slot1이 cnt[j].slot2와 같은 경우 orcnt[i].slot1이 cnt[j].slot3 또는 cnt[ i].slot2는 cnt[j].slot1과 동일 orcnt[i].slot2는 cnt[j].slot2와 동일하거나 cnt[i].slot2는 cnt[j].slot3과 동일 orcnt[i].slot3 cnt[j].slot1 또는 cnt[i].slot3이 cnt[j].slot2와 동일 orcnt[i].slot3이 cnt[j].slot3과 동일하면
-
행렬[i, j] :=1
-
행렬[j, i] :=1
-
루프에서 나오다
-
-
-
-
Visit_using_dfs() 함수를 정의합니다. 이것은 i, 행렬, 방문, 솔, n
을 취합니다. -
방문[i] :=사실
-
sol 끝에 i 삽입
-
0에서 n 사이의 j에 대해 수행
-
행렬[i][j]가 0이 아니고 방문하지 않은[j]가 0이 아니면
-
방문_사용_dfs(j, 행렬, 방문, 솔, n)
-
-
-
기본 방법에서 다음을 수행하십시오 -
-
n :=cnt의 크기
-
sol :=새 목록
-
matrix :=n x n 크기의 정사각형 행렬을 만듭니다.
-
방문:=크기가 n인 배열을 만들고 0으로 채움
-
생성_그래프(cnt, n, 행렬)
-
0에서 n 사이의 i에 대해 수행
-
방문하지 않은 경우[i]가 0이 아니면
-
방문_사용_dfs(i, 행렬, 방문, 솔, n)
-
sol 끝에 -1 삽입
-
-
-
범위 0에서 sol 크기까지의 i에 대해
-
sol[i]가 -1과 같으면
-
다음 줄로 이동
-
-
그렇지 않으면
-
디스플레이 솔[i]
-
-
예시
이해를 돕기 위해 다음 구현을 살펴보겠습니다. −
class contact:
def __init__(self, slot1, slot2, slot3):
self.slot1 = slot1
self.slot2 = slot2
self.slot3 = slot3
def generate_graph(cnt, n, matrix):
for i in range(n):
for j in range(n):
matrix[i][j] = 0
for i in range(n):
for j in range(i + 1, n):
if (cnt[i].slot1 == cnt[j].slot1 or cnt[i].slot1 == cnt[j].slot2 or cnt[i].slot1 == cnt[j].slot3 or cnt[i].slot2 == cnt[j].slot1 or cnt[i].slot2 == cnt[j].slot2 or cnt[i].slot2 == cnt[j].slot3 or cnt[i].slot3 == cnt[j].slot1 or cnt[i].slot3 == cnt[j].slot2 or cnt[i].slot3 == cnt[j].slot3):
matrix[i][j] = 1
matrix[j][i] = 1
break
def visit_using_dfs(i, matrix, visited, sol, n):
visited[i] = True
sol.append(i)
for j in range(n):
if (matrix[i][j] and not visited[j]):
visit_using_dfs(j, matrix, visited, sol, n)
def get_similar_contacts(cnt):
n = len(cnt)
sol = []
matrix = [[None] * n for i in range(n)]
visited = [0] * n
generate_graph(cnt, n, matrix)
for i in range(n):
if (not visited[i]):
visit_using_dfs(i, matrix, visited, sol, n)
sol.append(-1)
for i in range(len(sol)):
if (sol[i] == -1):
print()
else:
print(sol[i], end = " ")
cnt = [contact("Amal", "amal@gmail.com", "+915264"),
contact("Bimal", "bimal321@yahoo.com", "+1234567"),
contact("Amal123", "+915264", "amal_new@gmail.com"),
contact("AmalAnother", "+962547", "amal_new@gmail.com")]
get_similar_contacts(cnt) 입력
cnt = [contact("Amal", "amal@gmail.com", "+915264"),
contact("Bimal", "bimal321@yahoo.com", "+1234567"),
contact("Amal123", "+915264", "amal_new@gmail.com"),
contact("AmalAnother", "+962547", "amal_new@gmail.com")] 출력
0 2 3 1