본문 바로가기
카테고리 없음

백준) 1181 단어정렬 -JAVA

by 신드로 2020. 8. 18.

https://www.acmicpc.net/problem/1181

 

1181번: 단어 정렬

첫째 줄에 단어의 개수 N이 주어진다. (1≤N≤20,000) 둘째 줄부터 N개의 줄에 걸쳐 알파벳 소문자로 이루어진 단어가 한 줄에 하나씩 주어진다. 주어지는 문자열의 길이는 50을 넘지 않는다.

www.acmicpc.net

1) 단어를 배열에 입력받음

2) 배열을 알파벳 순으로 정렬

3) length를 0인것부터 차례로 list에 담기 (+중복 제거 처리 )

package solution;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Bakjun_1181{
	public static void main(String[] args) throws Exception {

		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
	
		String[] a=new String[T];
		
		String ent= sc.nextLine();
		
		for(int i=0;i<T;i++) {
			
			 a[i]= sc.nextLine();
			
			
		}
		
		Arrays.sort(a);
		ArrayList<String> list = new ArrayList<String>();
		
		for(int i=0;i<51;i++) {
		for(int j=0;j<T;j++) {
		if(i==a[j].length()&&!list.contains(a[j])) {			
			list.add(a[j]);
		}			
		}
		}
		
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));
		}
		
		
		sc.close();
	}
}