ACM: Problem 7391

pdf of problem


/*
 * Problem 7391
Sample:
input:
2
7
saskatoon
toronto
winnipeg
toronto
vancouver
saskatoon
toronto
3
edmonton
edmonton
edmonton

output:
4
1
*/


import java.io.*;
import java.util.*;

class Main
{
    static String ReadLn (int maxLg)  // utility function to read from stdin
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;
        String line = "";

        try
        {
            while (lg < maxLg)
            {
                car = System.in.read();
                if ((car < 0) || (car == '\n')) break;
                lin [lg++] += car;
            }
        }
        catch (IOException e)
        {
            return (null);
        }

        if ((car < 0) && (lg == 0)) return (null);  // eof
        return (new String (lin, 0, lg));
    }

    public static void main (String args[])  // entry point from OS
    {
        Main myWork = new Main();  // create a dinamic instance
        myWork.Begin();            // the true entry point
    }

	void Begin()
	{
		String input;
		StringTokenizer idata;
		int travels = 0;
		int cities=0;
		int count = 0;
		
		
		while ((input = Main.ReadLn (255)) != null)
		{
			idata = new StringTokenizer (input);
			if(count == 0){
				travels = Integer.parseInt (idata.nextToken());
				count++;
				continue;
			}
			
			if(count == travels+1)
				break;
				
			count++;
			cities = Integer.parseInt (idata.nextToken()); 
			HashSet<String> hset = 
               new HashSet<String>();
               	
			for(int i = 0; i < cities; i++){
				input = Main.ReadLn (255);
				hset.add(input);
			}
			
			System.out.println (hset.size());
		}
	}
}

Leave a Reply