1213. [S/W 문제해결 기본] 3일차 - String D3
[문제]
주어지는 영어 문장에서 특정한 문자열의 개수를 반환하는 프로그램을 작성하여라.
Starteatingwellwiththeseeighttipsforhealthyeating,whichcoverthebasicsofahealthydietandgoodnutrition.
위 문장에서 ti 를 검색하면, 답은 4이다.
[제약 사항]
총 10개의 테스트 케이스가 주어진다.
문장의 길이는 1000자를 넘어가지 않는다.
한 문장에서 검색하는 문자열의 길이는 최대 10을 넘지 않는다.
한 문장에서는 하나의 문자열만 검색한다.
[입력]
각 테스트 케이스의 첫 줄에는 테스트 케이스의 번호가 주어지고 그 다음 줄에는 찾을 문자열, 그 다음 줄에는 검색할 문장이 주어진다.
[출력]
#부호와 함께 테스트 케이스의 번호를 출력하고, 공백 문자 후 테스트 케이스의 답을 출력한다.
입력1
ti
Starteatingwellwiththeseeighttipsforhealthyeating,whichcoverthebasics ...
2
ing
Thedoublehelixformsthestructuralbasisofsemi-conservativeDNAreplication.1,2Less ...
...
ti
Starteatingwellwiththeseeighttipsforhealthyeating,whichcoverthebasics ...
2
ing
Thedoublehelixformsthestructuralbasisofsemi-conservativeDNAreplication.1,2Less ...
...
출력#1 4
#2 2
...
#2 2
...
[코드]
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
for (int i = 1; i <= 10; i++) {
int N=Integer.parseInt(br.readLine());
String A=br.readLine();
String B=br.readLine();
char[] arr_A=new char[A.length()];
char[] arr_B=new char[B.length()];
for (int j1 = 0; j1 < arr_A.length; j1++) {
arr_A[j1]=A.charAt(j1);
}
for (int j2 = 0; j2 < arr_B.length; j2++) {
arr_B[j2]=B.charAt(j2);
}
int search=0;
for (int i2 = 0; i2 <= B.length()-A.length(); i2++) {
int a_b=i2;
int total=0;
for (int i3 = 0; i3 < A.length(); i3++) {
if(arr_B[a_b]==arr_A[i3]) {
a_b++;
total++;
if(total==arr_A.length) {
search++;
}
}
}
}
System.out.println("#"+N+" "+search);
}
br.close();
}
}'SWEA(SWExpertacAdemy)' 카테고리의 다른 글
| [SWEA] 4466. 최대 성적표 만들기 D3 (자바) (0) | 2025.05.16 |
|---|---|
| [SWEA] 14692. 통나무 자르기 D3 (자바) (0) | 2025.05.15 |
| [SWEA] 10804. 문자열의 거울상 D3 (자바) (0) | 2025.05.15 |
| [SWEA] 5549. 홀수일까 짝수일까 D3 (자바) (0) | 2025.05.15 |
| [SWEA] 1234. [S/W 문제해결 기본] 10일차 - 비밀번호 D3 (자바) (1) | 2025.05.15 |