[Python] 알고리즘/Silver

[1411] 비슷한 단어 (Python)

-Becca- 2022. 3. 1. 19:48

[문제]

https://www.acmicpc.net/problem/1411

 

1411번: 비슷한 단어

첫째 줄에 단어의 개수 N이 주어진다. 둘째 줄부터 N개의 줄에 한 줄에 하나씩 단어가 주어진다. 단어의 길이는 최대 50이고, N은 100보다 작거나 같은 자연수이다. 모든 단어의 길이는 같고, 중복

www.acmicpc.net


알고리즘 분류는 구현, 문자열, 브루트포스 알고리즘 입니다.

처음 나오는 문자를 1, 그 다음 나오는 문자를 2, ... 로 바꾸어서 풀었습니다.


[코드]

import sys

N = int(sys.stdin.readline())

li = list()
for i in range(N):
    word = sys.stdin.readline().rstrip()
    
    k = 1
    s = ""
    dt = dict()
    
    for j in range(26):
        dt[chr(j + 97)] = -1;

    dt[word[0]] = 1
    for j in range(len(word)):
        if j != 0 and dt[word[j]] == -1:
            k += 1
            dt[word[j]] = k
            s += str(k)
        else:
            s += str(dt[word[j]])
        
    li.append(s)
    
res = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        if li[i] == li[j]:
            res += 1

print(res)