www.acmicpc.net/problem/9536

 

9536번: 여우는 어떻게 울지?

각 테스트케이스마다 여우의 울음소리를 한 줄씩, 녹음된 순서대로 출력한다. 여우의 울음소리가 녹음되어 있음이 보장된다. (알려진 것과는 달리, 여우는 모스 부호로 의사소통하지 않는다.)

www.acmicpc.net

 

문제해설

  • 알고리즘 자체보다는 문자열 문제
  • what does the fox say? 가 나올 때 문자열을 종료시켜주는 것
  • StringToken 잘 쓰기. -->

wantairpod.tistory.com/86

 

[Java 입출력] StringTokenizer

StringTokenizer 클래스는 문자열을 우리가 지정한 구분자로 문자열을 쪼개주는 클래스입니다. 그렇게 쪼개어진 문자열을 우리는 토큰(token)이라고 부릅니다. java.util.StringTokenizer int countTokens() 남..

wantairpod.tistory.com

Java

package Z_ShinHanCard_Prepare;
import java.util.*;
import java.io.*;
/*
 * 1
toot woof wa ow ow ow pa blub blub pa toot pa blub pa pa ow pow toot
dog goes woof
fish goes blub
elephant goes toot
seal goes ow
what does the fox say?
 * */
public class D4백준9536_여우는어떻게울지 
{
	

	public static void main(String[] args) throws Exception
	{	String question="what does the fox say?";
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String input = br.readLine();
		int T = Integer.parseInt(input);
		String s[]= new String[T];
		Vector<String> animals = new Vector<String>();
		StringTokenizer st;
		for(int i = 0 ; i <T ; i++)
		{
			animals.clear();
			input = br.readLine();
			s[i]=input; //동물소리 입력받음
			for(int  j= 0 ; j<100 ; j++)
			{
				String temp =br.readLine();
				if(temp.equals(question))
				{
					break;
				}
				st =new StringTokenizer(temp," ");
				int tempCout =st.countTokens();
						
				for(int w= 0 ; w<st.countTokens() ;j++) {
					if(st.nextToken().equals("goes"))
					{
						animals.add(st.nextToken());
					}
				} 
			}
			
			st=new StringTokenizer(s[i]," "); 	
			while (st.hasMoreTokens()) 
			{
				int check=0;
				String animal = st.nextToken();
				
				for (int k=0; k<animals.size(); k++) 
				{
					if (animal.equals(animals.get(k)))
					{
						check=1;
						break;
					}
					else check=0;
				}
				if (check==0) {
					System.out.print(animal+ " ");
				}
			}
		} 
	}

}

+ Recent posts