일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- 그리디
- 그래프
- 애드 혹
- 2357
- 정렬
- 트리
- 정수론
- JavaScript
- 투 포인터
- 싸피
- 모던 JavaScript 튜토리얼
- BFS
- 에라토스테네스의 체
- 세그먼트 트리
- 13164
- 문자열
- 브루트포스
- 해시 테이블
- 맵
- 수학
- 슬라이딩 윈도우
- 플로이드-워셜
- 누적 합
- boj
- DP
- SSAFY
- Python
- DFS
- 이분 탐색
- 구현
- Today
- Total
목록문자열 (6)
흙금이네 블로그

아이디어 회문인 문자열을 구성하는 문자가 하나라면 -1, 두 문자 이상이라면 문자열의 길이보다 1 작은 값을 출력한다. 풀이 #1 (Python) def solution(): S = input() N = len(S) if S[:N//2] != S[N//2+N%2:][::-1]: print(N) elif S != S[0]*N: print(N-1) else: print(-1) solution() 풀이 #2 (JavaScript) const fs = require('fs'); const input = fs.readFileSync('/dev/stdin').toString().split('\n'); function solution() { function isPalindrome() { for (let i=0; i

아이디어 14725번 개미굴과 비슷한 문제로, 트라이를 이용해 디렉토리 구조를 출력한다. 풀이 #1 (Python) import sys input = sys.stdin.readline class Trie(): def __init__(self): self.root = dict() def insert(self, directories): node = self.root for directory in directories: if directory not in node: node[directory] = dict() node = node[directory] def search(self): def dfs(node, d): for directory in sorted(node.keys()): res.append(' '*d..

아이디어 맵을 이용해 모든 단어들이 순서에 맞게 적혀 있는지 확인한다. 풀이 #1 (Python) import sys input = sys.stdin.readline def solution(): N = int(input()) sentences = [input().split() for _ in range(N)] idx_dict = dict() for i in range(N): idx_dict[sentences[i].pop()] = i L = input().split() while L: word = L.pop() idx = idx_dict.get(word, -1) idx_dict[word] = -1 if idx >= 0: if sentences[idx]: idx_dict[sentences[idx].pop()..

아이디어 스택을 이용해 화학식의 원자들의 질량 합을 구한다. 풀이 #1 (Python) 재귀함수로 괄호 내 원자들의 질량 합을 구해나간다. def solution(): def calculate(idx): stack = [] i = idx while i < N: if formula[i] == '(': num, i = calculate(i+1) stack.append(num) elif formula[i] == ')': return sum(stack), i elif formula[i] in values: stack.append(values.get(formula[i], 0)) else: stack[-1] *= int(formula[i]) i += 1 return sum(stack), i formula = inp..

아이디어 맵을 이용하여 단어를 구성하는 알파벳의 순서 패턴을 저장하고 비슷한 문자열 쌍의 개수를 구한다. 풀이 #1 (Python) import sys input = sys.stdin.readline def solution(): N = int(input()) patterns = dict() res = 0 for _ in range(N): word = input().rstrip() alpha = dict() val = 1 p = '' for c in word: if alpha.get(c, 0) == 0: alpha[c] = val val += 1 p += f'{alpha.get(c, 0)}' cnt = patterns.get(p, 0) res += cnt patterns[p] = cnt+1 print(re..

아이디어 투 포인터로 사전순으로 가장 빠른 문자열을 만들어 나간다. 풀이 비교하는 두 문자가 같은 경우 이후에 사전순으로 앞서는 문자가 먼저 등장하는 포인터쪽의 문자를 더한다. import sys input = sys.stdin.readline def solution(): N = int(input()) S = [input().rstrip() for _ in range(N)] s = 0 e = N-1 res = '' for k in range(1, N+1): if S[s] < S[e]: res += S[s] s += 1 elif S[e] < S[s]: res += S[e] e -= 1 else: i, j = s, e while i