SWEA(SWExpertacAdemy)

[SWEA] 1213. [S/W 문제해결 기본] 3일차 - String D3 (자바)

심층코드 2025. 5. 15. 20:23

1213. [S/W 문제해결 기본] 3일차 - String D3

[문제]

주어지는 영어 문장에서 특정한 문자열의 개수를 반환하는 프로그램을 작성하여라.

Starteatingwellwiththeseeighttipsforhealthyeating,whichcoverthebasicsofahealthydietandgoodnutrition.

위 문장에서 ti 를 검색하면, 답은 4이다.

[제약 사항]

총 10개의 테스트 케이스가 주어진다.

문장의 길이는 1000자를 넘어가지 않는다.

한 문장에서 검색하는 문자열의 길이는 최대 10을 넘지 않는다.

한 문장에서는 하나의 문자열만 검색한다. 

[입력]

각 테스트 케이스의 첫 줄에는 테스트 케이스의 번호가 주어지고 그 다음 줄에는 찾을 문자열, 그 다음 줄에는 검색할 문장이 주어진다.

[출력]

#부호와 함께 테스트 케이스의 번호를 출력하고, 공백 문자 후 테스트 케이스의 답을 출력한다.

입력1
ti
Starteatingwellwiththeseeighttipsforhealthyeating,whichcoverthebasics ...
2
ing
Thedoublehelixformsthestructuralbasisofsemi-conservativeDNAreplication.1,2Less ...
...
 
출력#1 4
#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();
	}

}