class PasswordExtractor {
String keylog = "319 680 180 690 129 620 762 689 762 318 368 710 720 710 629 168 160 689 716 731 736 729 316 729 729 710 769 290 719 680 318 389 162 289 162 718 729 319 790 680 890 362 319 760 316 729 380 319 728 716";
String[] keylogArr;
Node root;
public PasswordExtractor() {
keylogArr = keylog.split(" ");
// System.out.println("keylogArr.length : "+keylogArr.length);
root = new Node('x', keylogArr[0].charAt(1), 'x');
}
public void printPassword() {
// System.out.print("password : ");
root.print();
System.out.println();
}
// 순서 체크
public boolean isAscending(char prev, char next) {
// System.out.println("isAscending : "+prev+", "+next);
int prevIndex = -1;
int nextIndex = -1;
for(int i=0; i<keylogArr.length; i++) {
prevIndex = keylogArr[i].indexOf(prev);
nextIndex = keylogArr[i].indexOf(next);
if( prevIndex >= 0 && nextIndex >= 0 ) {
// System.out.println(prevIndex+", "+nextIndex+"] isAscending : "+(prevIndex < nextIndex));
return prevIndex < nextIndex ? true : false;
}
}
// System.out.println("isAscending : "+false);
return false;
}
public char findNumber(char prev, char next) {
char[] digits;
int index = -1;
boolean[] isCompared = {false,false,false,false,false,false,false,false,false,false };
// System.out.println("findNumber : "+prev+", "+next);
for(int i=0; i<keylogArr.length; i++) {
// System.out.print("keylogArr["+i+"] : "+keylogArr[i]+" - ");
digits = keylogArr[i].toCharArray();
if(prev == 'x' && next != 'x') { // 무조건 앞에 문자 찾음
index = keylogArr[i].indexOf(next);
// System.out.println("무조건 앞에 문자 찾음 index : "+index);
if(index > 0) {
return digits[index-1];
}
} else if(prev != 'x' && next == 'x') { // 무조건 뒤에 문자 찾음
index = keylogArr[i].indexOf(prev);
// System.out.println("무조건 뒤에 문자 찾음 index : "+index);
if(index >= 0 && index < 2) {
return digits[index+1];
}
} else if(prev != 'x' && next != 'x') { // prev 보다 뒤 next 보다 앞의 문자 찾음
index = keylogArr[i].indexOf(prev);
// System.out.println("prev 보다 뒤 next 보다 앞의 문자 찾음 index : "+index);
if(index >= 0 && index < 2) {
for(int j=index+1; j<digits.length; j++) {
// System.out.println(j+"] isCompared : "+isCompared[digits[j]-48]);
if( !isCompared[digits[j]-48] ) {
if( digits[j] != next && isAscending(digits[j], next)) {
return digits[j];
}
}
isCompared[digits[j]-48] = true;
}
}
}
}
return 'x';
}
class Node {
char prevDigit,currDigit,nextDigit;
Node head, tail;
public Node(char prev, char curr, char next) {
prevDigit= prev;
currDigit= curr;
nextDigit= next;
// System.out.println("prevDigit : "+prevDigit);
// System.out.println("currDigit : "+currDigit);
// System.out.println("nextDigit : "+nextDigit);
addHead();
addTail();
}
public void addHead() {
// currDigit 보다 앞에 위치하는 수
if(head==null) {
char digit = findNumber(prevDigit, currDigit);
// System.out.println("digit : "+digit);
if(digit != 'x') head = new Node(prevDigit, digit,currDigit);
}
}
public void addTail() {
// currDigit 보다 뒤에 위치하는 수
if(tail==null) {
char digit = findNumber(currDigit, nextDigit);
// System.out.println("digit : "+digit);
if(digit != 'x') tail = new Node(currDigit, digit, nextDigit);
}
}
public void print() {
if(head != null) head.print();
System.out.print(currDigit+", ");
if(tail != null) tail.print();
}
}
}
public static void findShortPassword() {
Long startTime = System.nanoTime();
PasswordExtractor ep = new PasswordExtractor();
Long endTime = System.nanoTime();
ep.printPassword();
System.out.println("수행시간 : "+ (endTime - startTime) + " ns");
}