문제
숫자 문자열의 배열에서 숫자 패턴을 찾아야한다. 그리드 및 패턴 배열에서 각 문자열은 그리드의 행을 나타낸다. 예를 들어 다음 그리드를 고려해라 :
패턴배열 :
패턴은 두번째 행과 세번째 열에서 시작하여 다음 두 행까지 계속된다. 패턴은 그리드에 존재한다고 말할 수 있다. 패턴을 찾았는지 여부에 따라 반환값은 YES 또는 NO이다. 이 경우 반환값은 YES이다.
Function Description(기능 설명)
아래 에디터에서 gridSearch 함수를 완성하라. 이 함수는 그리드에 패턴이 존재할 경우 YES를 그렇지 않을 경우 NO를 반환한다.
gridSearch 에는 다음 매개변수가 있다 :
- string G[R]: 검색할 그리드
- string P[r]: 검색할 패턴
Input Format(입력 형식)
테스트 케이스의 수인 t가 입력의 첫번째 줄에 포함된다.
각각의 t 테스트 케이스는 다음과 같이 표시한다 :
첫번째 줄에는 공백으로 구분된 두개의 정수 R과 C가 포함되고, R과 C는 검색 그리드 G의 행수 및 각 행 문자열 길이가 된다.
그 다음에는 그리드 G를 나타내는 C 숫자의 문자열이 있는 R줄이 이어집니다.
다음 줄은 공백으로 구분된 두개의 정수 r과 c가 포함되고 r과 c는 패턴 그리드 P의 행수 및 각 행 문자열 길이를 나타낸다.
그 다음에는 패턴 그리드 P를 나타내는 c 숫자의 문자열이 있는 c줄이 이어집니다.
Returns(반환값)
- String : YES 또는 NO
Input Format(입력 형식)
1 ≤ t ≤ 5
1 ≤ R, r, C, c ≤ 1000
1 ≤ r ≤ R
1 ≤ c ≤ C
예제
input | output |
2 10 10 7283455864 6731158619 8988242643 3830589324 2229505813 5633845374 6473530293 7053106601 0834282956 4607924137 3 4 9505 3845 3530 15 15 400453592126560 114213133098692 474386082879648 522356951189169 887109450487496 252802633388782 502771484966748 075975207693780 511799789562806 404007454272504 549043809916080 962410809534811 445893523733475 768705303214174 650629270887160 2 2 99 99 |
YES NO |
구현
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'gridSearch' function below.
*
* The function is expected to return a STRING.
* The function accepts following parameters:
* 1. STRING_ARRAY G
* 2. STRING_ARRAY P
*/
public static String gridSearch(List<String> G, List<String> P) {
// Write your code here
String val = "NO";
for(int i = 0; i <= G.size()-P.size(); i++) {
if(G.get(i).indexOf(P.get(0)) > -1)
{
for(int k = 0; k <= G.get(i).length()-P.get(0).length(); k++){
int chk = G.get(i).substring(k, G.get(i).length()).indexOf(P.get(0));
if(chk == -1){
break;
}
else{
int cnt = 0;
for(int j = 0; j < P.size(); j++){
String Gstr = G.get(i + j).substring(k, k + P.get(j).length());
if(Gstr.equals(P.get(j))){
cnt ++;
}
}
if(cnt == P.size()){
val = "YES";
break;
}
}
}
}
}
return val;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int t = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, t).forEach(tItr -> {
try {
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int R = Integer.parseInt(firstMultipleInput[0]);
int C = Integer.parseInt(firstMultipleInput[1]);
List<String> G = IntStream.range(0, R).mapToObj(i -> {
try {
return bufferedReader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.collect(toList());
String[] secondMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int r = Integer.parseInt(secondMultipleInput[0]);
int c = Integer.parseInt(secondMultipleInput[1]);
List<String> P = IntStream.range(0, r).mapToObj(i -> {
try {
return bufferedReader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.collect(toList());
String result = Result.gridSearch(G, P);
bufferedWriter.write(result);
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}
example에 나온 첫 번째 입력한 값을 토대로 구현한 내용을 설명하자면, 일단 10X10인 G 와 3X4인 P가 있습니다.
패턴(P) 첫줄인 "9505"와 같은 부분을 G에서 찾습니다. G의 5번째 줄(index 4)에서 같은 문자가 있는걸 확인할 수 있습니다.
패턴(P) 첫 줄인 "9505"와 같은 부분을 G에서 찾게 되면 위 그림과 같이 차례대로 패턴(P)과 같은지 확인하고
같기 때문에 YES를 반환하도록 하였습니다.
The Grid Search | HackerRank
Given a 2D array of digits, try to find a given 2D grid pattern of digits within it.
www.hackerrank.com
'코딩테스트' 카테고리의 다른 글
[HackerRank] Day of the Programmer (사용언어 : Python 2) (0) | 2021.08.22 |
---|---|
[HackerRank] Bigger is Greater (사용언어 : JAVA) (0) | 2021.08.22 |
[백준] 1371번 가장 많은 글자 (사용언어 : JAVA) (0) | 2021.08.14 |
[프로그래머스] 2021 KAKAO BLIND RECRUITMENT 신규 아이디 추천 (사용언어 : JAVA) (0) | 2021.08.10 |
[백준] 18406번 럭키 스트레이트 (사용언어 : JAVA) (0) | 2021.08.09 |
댓글