on
Algorithm - 가장 짧은 문자거리
Algorithm - 가장 짧은 문자거리
public class Main {
public static void main( String [] args) throws IOException {
BufferedReader reader = new BufferedReader( new InputStreamReader( System . in ));
StringTokenizer st = new StringTokenizer(reader.readLine(), " " );
StringBuilder builder = new StringBuilder();
String input = st.nextToken(); // 입력한 문자열
char find = st.nextToken(). charAt ( 0 ); // 문자열에서 찾을 특정 문자
int ltStart[] = new int [input. length ()]; // 왼족에서부터 탐색할 배열
int rtStart[] = new int [input. length ()]; // 오른쪽에서부터 탐색할 배열
int count = 10 ;
// 왼쪽부터 입력한 문자열을 기준으로 반복문 실행
for ( int i = 0 ; i < input. length (); i + + ) {
int x = input. charAt (i);
if (x = = find) {
count = 0 ;
ltStart[i] = 0 ;
} else {
count + + ;
ltStart[i] = count;
}
}
// 오른쪽부터 입력한 문자열을 기준으로 반복문 실행
for ( int i = input. length () - 1 ; i > = 0 ; i - - ) {
int x = input. charAt (i);
if (x = = find) {
count = 0 ;
rtStart[i] = 0 ;
} else {
count + + ;
rtStart[i] = count;
}
}
// 배열 중 더 작은 숫자를 출력하기 위해 반복문 실행
for ( int i = 0 ; i < ltStart. length ; i + + ) {
if (ltStart[i] < rtStart[i]) {
builder.append(ltStart[i] + " " );
} else if (ltStart[i] > rtStart[i]) {
builder.append(rtStart[i] + " " );
} else {
builder.append(rtStart[i] + " " );
}
}
System . out . println (builder);
}
}
from http://kdg-is.tistory.com/198 by ccl(A) rewrite - 2021-09-13 23:27:16