[BOJ] 1543 문서 검색
완전탐색, 문자열 예제
문제
https://www.acmicpc.net/problem/1543
문제 풀이
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Baekjoon.String; | |
import java.util.Scanner; | |
//문서 검색 - 완전탐색, 문자열 | |
public class BJ_1543 { | |
public static void main(String[] args) { | |
Scanner s = new Scanner(System.in); | |
String doc = s.nextLine(); //문서 | |
String word = s.nextLine(); //검색하고 싶은 단어 | |
//문서에서 검색 단어를 1로 변환 | |
// - "문서와 단어는 알파벳 소문자와 공백으로 이루어져 있다." | |
String check = doc.replace(word, "1"); | |
int count = 0; | |
for (int i = 0; i < check.length(); i++) { | |
char c = check.charAt(i); | |
if (c == '1') { //검색 단어를 1로 변환했기에 1의 개수는 검색 단어의 개수 | |
count++; | |
} | |
} | |
System.out.println(count); | |
} | |
} |
TMI
20/12/02: 알고리즘 스터디 문제 풀이🤓
댓글남기기