1. 문제
https://www.acmicpc.net/problem/2179
2. 풀이
키워드
- 구현
풀이
- 문자열들을 배열에 담고 반복문 2개를 돌려가면서 서로 매칭되는 단어의 최대 수를 구한다.
- 최대 수에 해당하는 단어들의 인덱스를 저장하고 출력한다.
3. 소스코드
package org.hanghae; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BG_비슷한단어 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] strings = new String[n]; for(int i = 0; i < n; i++) { strings[i] = br.readLine(); } int max = 0; int one = 0; int two = 0; for(int i = 0; i < n-1; i++) { for(int j = 1; j < n; j++) { if(i == j) continue; int len = Math.min(strings[i].length(), strings[j].length()); int count = 0; for(int k = 0; k < len; k++) { if(strings[i].charAt(k) != strings[j].charAt(k)) { break; } count++; } if(max < count) { max = count; one = i; two = j; } } } System.out.println(one); System.out.println(two); } }
'알고리즘' 카테고리의 다른 글
[백준] 2056 작업 (java) (2) | 2024.11.13 |
---|---|
[백준] 2660 회장찾기 (java) (0) | 2024.10.30 |
[백준] 1389 케빈 베이컨의 6단계 법칙 (java) (0) | 2024.10.29 |
[백준] 11403 경로찾기 (java) (0) | 2024.10.28 |
[프로그래머스 level 03] - n으로 표현 (c++) (0) | 2020.01.06 |