https://www.acmicpc.net/problem/1543
1543번: 문서 검색
세준이는 영어로만 이루어진 어떤 문서를 검색하는 함수를 만들려고 한다. 이 함수는 어떤 단어가 총 몇 번 등장하는지 세려고 한다. 그러나, 세준이의 함수는 중복되어 세는 것은 빼고 세야 한
www.acmicpc.net
https://github.com/stellaluminary/Baekjoon
GitHub - stellaluminary/Baekjoon
Contribute to stellaluminary/Baekjoon development by creating an account on GitHub.
github.com
만약 s 안에 t라는 문자가 존재한다면 그 위치에 해당하는 것을 삭제한 s를 갱신한다.
이후 counting을 통해 그 개수를 측정한다.
s = input()
t = input()
cnt = 0
while t in s:
for i in range(len(s)):
if s[i:i+len(t)] == t:
s = s[i+len(t):]
break
cnt += 1
print(cnt)
방법 1은 s에 대한 소거법이라면 본 방법은 s에 대한 위치별 값을 세는 방법에 대한 코드이다.
s = input()
t = input()
cnt = 0
n = 0
while n <= len(s) - len(t):
if s[n:n+len(t)] == t:
cnt += 1
n += len(t)
else:
n += 1
print(cnt)
내장함수 count를 통해 계산
print(input().count(input()))