티스토리 뷰
문제는 위와 같으며 최대 문서의 길이가 2500 이고, 검색하고 싶은 단어의 최대 길이가 50 이므로 처음부터 순서대로 모든 경우를 검색하여 문제를 해결할 수 있습니다.
파이썬 코드는 다음과 같습니다.
from sys import stdin
string = stdin.readline().strip()
search = stdin.readline().strip()
count = 0
find_idx = 0
while find_idx <= len(string) - len(search):
if string[find_idx:find_idx + len(search)] == search:
count += 1
find_idx += len(search) + 1
else:
find_idx += 1
print(count)
자바 코드는 다음과 같습니다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String string = br.readLine();
String word = br.readLine();
int count = 0;
int findIdx = 0;
while (string.length() - word.length() >= findIdx) {
if (string.substring(findIdx, findIdx + word.length()).equals(word)) {
count += 1;
findIdx += word.length();
} else {
findIdx += 1;
}
}
System.out.println(count);
}
}
'알고리즘' 카테고리의 다른 글
[알고리즘 / 백준] 1302 - 베스트셀러 (0) | 2020.11.13 |
---|---|
[알고리즘 / 백준] 1568 - 새 (0) | 2020.11.13 |
[알고리즘 / 프로그래머스] 124 나라의 숫자 (0) | 2020.11.12 |
[알고리즘 / 백준] 11004 - K번째 수 (0) | 2020.11.12 |
[알고리즘 / 백준] 2751 - 수 정렬하기 2 (0) | 2020.11.12 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- programmers
- Dynamic Programming
- map
- ionic
- 순열
- permutation
- DFS
- array
- CodePipeline
- cloudfront
- java
- string
- 수학
- Combination
- CodeDeploy
- Baekjoon
- 프로그래머스
- 소수
- AWS
- 조합
- BFS
- ECR
- CodeCommit
- SWIFT
- Algorithm
- 에라토스테네스의 체
- search
- spring
- EC2
- sort
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함