UVA – 417 – Word Index

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

	static HashMap<String, Integer> map = new HashMap<String, Integer>();

	public static void main(String[] args) {

		generatee();
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {
			Integer a = map.get(in.next());
			System.out.println(a == null ? 0 : a);
		}
	}

	private static void generatee() {

		int Index = 1;
		Queue<String> que = new LinkedList<String>();

		for (Character c = 'a'; c <= 'z'; map.put(c.toString(), Index++), c++)
			que.add(c.toString());

		while (!que.isEmpty()) {
			String s = que.poll();
			char last = s.charAt(s.length() - 1);
			if (s.length() == 5 || last == 'z')
				continue;
			for (char next = ++last; next <= 'z'; que.add(s + next), next++)
				map.put(s + next, Index++);
		}

	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: