알고리즘 풀이/백준

[백준][Java] 9933번 민균이의 비밀번호

배게 2019. 3. 24. 13:51
728x90

lac <-> cal 처럼 쌍을 이루는 경우 이외에


abcba 처럼 단어하나가 회문인 경우도 통과시켜주어야함 ( 다른 사람 코드보고 알 수 있었음 )


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;



public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		sc.nextLine();
		
		String[] words = new String[N];
		
		for(int i=0; i<N; i++) {
			words[i]=sc.nextLine();
		}
		
		int result1=0;
		char result2 ='0';
		
		
		for(int i=0; i<N; i++) {
			String reverse = (new StringBuffer(words[i])).reverse().toString();
			
			for(int j=i; j<N;j++) {
				if(reverse.equals(words[j])) {
					result1=reverse.length();
					result2=reverse.charAt( (reverse.length()/2) );
					break;
				}				
			}
			if(result2 != '0') break;
		}
		
		System.out.println(result1+" "+result2);
	 
	}
}