문제
패턴에서 반복되는 부분을 마디라고 부른다. 문자열을 입력 받아 마디의 길이를 출력하는 프로그램을 작성하라.
입력
3 KOREAKOREAKOREAKOREAKOREAKOREA SAMSUNGSAMSUNGSAMSUNGSAMSUNGSA GALAXYGALAXYGALAXYGALAXYGALAXY |
출력
#1 5 #2 7 #3 6 |
코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Scanner; class Solution { public static void main(String args[]) throws Exception { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0; i<t; i++){ String str = sc.next(); for(int j=1; j<str.length(); j++){ String word1 = str.substring(0 , j); String word2 = str.substring(j, j*2); if(word1.equals(word2)){ System.out.printf("#%d %d\n",(i+1), word1.length()); break; } } } } } | cs |
'SW expert Academy > D2' 카테고리의 다른 글
[SW Expert Academy] 1989번 초심자의 회문 검사 (0) | 2023.04.20 |
---|---|
[SW Expert Academy] 2001번 파리 퇴치 (0) | 2023.04.20 |
[SW Expert Academy] 2005번 파스칼의 삼각형 (0) | 2023.04.20 |
[SW Expert Academy] 1926번 간단한 369게임 (0) | 2023.04.19 |
[SW Expert Academy] 1859번 백만 장자 프로젝트 (0) | 2023.04.19 |