ural 1002. Phone Numbers tire+spfa

1002. Phone Numbers

Time limit: 2.0 second
Memory limit: 64 MB
In the present world you frequently meet a lot of call numbers and they are going to be longer and longer. You need to remember such a kind of numbers. One method to do it in an easy way is to assign letters to digits as shown in the following picture:
1 ij    2 abc   3 def
4 gh    5 kl    6 mn
7 prs   8 tuv   9 wxy
        0 oqz
This way every word or a group of words can be assigned a unique number, so you can remember words instead of call numbers. It is evident that it has its own charm if it is possible to find some simple relationship between the word and the person itself. So you can learn that the call number 941837296 of a chess playing friend of yours can be read as WHITEPAWN, and the call number 2855304 of your favourite teacher is read BULLDOG.
Write a program to find the shortest sequence of words (i.e. one having the smallest possible number of words) which corresponds to a given number and a given list of words. The correspondence is described by the picture above.

Input

Input contains a series of tests. The first line of each test contains the call number, the transcription of which you have to find. The number consists of at most 100 digits. The second line contains the total number of the words in the dictionary (maximum is 50 000). Each of the remaining lines contains one word, which consists of maximally 50 small letters of the English alphabet. The total size of the input doesn't exceed 300 KB. The last line contains call number −1.

Output

Each line of output contains the shortest sequence of words which has been found by your program. The words are separated by single spaces. If there is no solution to the input data, the line contains text “ No solution.”. If there are more solutions having the minimum number of words, you can choose any single one of them.

Sample

input output
7325189087
5
it
your
reality
real
our
4294967296
5
it
your
reality
real
our
-1
reality our
No solution.
Problem Source: Central European Olympiad in Informatics 1999
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) {
		new Task().solve();
	}
}

class Task {
	InputReader in = new InputReader(System.in);
	PrintWriter out = new PrintWriter(System.out);

	char hash(char c) {
		if (c == 'i' || c == 'j') {
			return '1';
		}
		if (c == 'a' || c == 'b' || c == 'c') {
			return '2';
		}
		if (c == 'd' || c == 'e' || c == 'f') {
			return '3';
		}
		if (c == 'g' || c == 'h') {
			return '4';
		}
		if (c == 'k' || c == 'l') {
			return '5';
		}
		if (c == 'm' || c == 'n') {
			return '6';
		}
		if (c == 'p' || c == 'r' || c == 's') {
			return '7';
		}
		if (c == 't' || c == 'u' || c == 'v') {
			return '8';
		}
		if (c == 'w' || c == 'x' || c == 'y') {
			return '9';
		}
		if (c == 'o' || c == 'q' || c == 'z') {
			return '0';
		}
		return '_';
	}
	
	char[] hash(String word){
		int len = word.length() ;
		char[] res = new char[len] ;
		for(int i = 0 ; i < len ; i++){
			res[i] += hash(word.charAt(i)) ;
		}
		return res ; 
	}

	Tire tree = new Tire();
	List<Node>[] adj ;
	String[] name ;
	
	String spfa(int n){
		boolean[] in = new boolean[n+1] ;
		int[] dist = new int[n+1] ;
		int[][] father = new int[n+1][2] ;
		Arrays.fill(in , false) ;
		Arrays.fill(dist , Integer.MAX_VALUE) ;
		Queue<Integer> q = new LinkedList<Integer>() ;
		q.add(0) ;
		dist[0] = 0 ;
		in[0] = true ;
		while(! q.isEmpty()){
			int u = q.poll() ;
			in[u] = false ;
			if(u == n){
				break ;
			}
			for(Node nd : adj[u]){
				int v = nd.first ;
				if(dist[v] > dist[u] + 1){
					dist[v] = dist[u] + 1 ;
					father[v][0] = u ;
					father[v][1] = nd.second ;
					if(! in[v]){
						in[v] = true ;
						q.add(v) ;
					}
				}
			}
		}
		if(dist[n] == Integer.MAX_VALUE){
			return "No solution." ;
		}
		String reslut = "" ;
		int u = n ;
		while(u != 0){
			reslut = name[father[u][1]] + " " + reslut ;
			u = father[u][0] ;
		}
		return reslut.trim() ; 
	}

	void solve() {
		while (true) {
			String text = in.next();
			if("-1".equals(text)){
				break ; 
			}
			tree.clear();
			int textLen = text.length();
			for (int i = 0; i < textLen; i++) {
				for (int j = i; j < textLen; j++) {
					tree.add(text.substring(i, j + 1).toCharArray(), i, j);
				}
			}
			adj = new List[textLen+1] ;
			for(int i = 0 ; i <= textLen ; i++){
				adj[i] = new ArrayList<Node>() ;
			}
			int n = in.nextInt() ; 
			name = new String[n+1] ;
			for(int i = 0 ; i < n ; i++){
				name[i] = in.next() ;
				List<Node> g = tree.find(hash(name[i])) ;
				if(g != null && !g.isEmpty()){
					for(Node nd : g){
						adj[nd.first].add(new Node(nd.second+1 , i)) ;
					}
				}
			}
			out.println(spfa(textLen)) ;
		}
		out.flush() ;
	}

}

class Node {
	int first , second;

	Node(int first, int second) {
		this.first = first;
		this.second = second;
	}
}

class Tire {
	final int MAX_NODE = 281700  ;
	final int MAX_AlPHA = 10;
	final int NULL = -1;
	int[][] next = new int[MAX_NODE][MAX_AlPHA];
	List<Node>[] adj = new List[MAX_NODE];
	int totel;
	int root;

	void clear() {
		totel = 0;
		root = newNode();
	}

	int newNode() {
		for (int i = 0; i < MAX_AlPHA; i++) {
			next[totel][i] = NULL;
		}
		adj[totel] = new ArrayList<Node>();
		return totel++;
	}

	void add(char[] word, int l, int r) {
		int cur = root;
		for (char c : word) {
			int i = c - '0';
			if (next[cur][i] == NULL) {
				next[cur][i] = newNode();
			}
			cur = next[cur][i];
		}
		adj[cur].add(new Node(l, r));
	}

	List<Node> find(char[] word) {
		int cur = root;
		for (char c : word) {
			int i = c - '0';
			if (next[cur][i] == NULL) {
				return null;
			}
			cur = next[cur][i];
		}
		return new ArrayList<Node>(adj[cur]);
	}

}

class InputReader {
	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public InputReader(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = new StringTokenizer("");
	}

	private void eat(String s) {
		tokenizer = new StringTokenizer(s);
	}

	public String nextLine() {
		try {
			return reader.readLine();
		} catch (Exception e) {
			return null;
		}
	}

	public boolean hasNext() {
		while (!tokenizer.hasMoreTokens()) {
			String s = nextLine();
			if (s == null)
				return false;
			eat(s);
		}
		return true;
	}

	public String next() {
		hasNext();
		return tokenizer.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public int[] nextInts(int n) {
		int[] nums = new int[n];
		for (int i = 0; i < n; i++) {
			nums[i] = nextInt();
		}
		return nums;
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}

	public BigInteger nextBigInteger() {
		return new BigInteger(next());
	}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 102808 Accepted: 17241 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10. The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows: A, B, and C map to 2 D, E, and F map to 3 G, H, and I map to 4 J, K, and L map to 5 M, N, and O map to 6 P, R, and S map to 7 T, U, and V map to 8 W, X, and Y map to 9 There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010. Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.) Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number. Input The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters. Output Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line: No duplicates. Sample Input 12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279 Sample Output 310-1010 2 487-3279 4 888-4567 3 Source East Central North America 1999 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 102808 Accepted: 17241 Description 企业喜欢用容易被记住的电话号码。让电话号码容易被记住的一个办法是将它写成一个容易记住的单词或者短语。例如,你需要给滑铁卢大学打电话时,可以拨打TUT-GLOP。有时,只将电话号码中部分数字拼写成单词。当你晚上回到酒店,可以通过拨打310-GINO来向Gino's订一份pizza。让电话号码容易被记住的另一个办法是以一种好记的方式对号码的数字进行分组。通过拨打必胜客的“三个十”号码3-10-10-10,你可以从他们那里订pizza。 电话号码的标准格式是七位十进制数,并在第三、第四位数字之间有一个连接符。电话拨号盘提供了从字母到数字的映射,映射关系如下: A, B, 和C 映射到 2 D, E, 和F 映射到 3 G, H, 和I 映射到 4 J, K, 和L 映射到 5 M, N, 和O 映射到 6 P, R, 和S 映射到 7 T, U, 和V 映射到 8 W, X, 和Y 映射到 9 Q和Z没有映射到任何数字,连字符不需要拨号,可以任意添加和删除。 TUT-GLOP的标准格式是888-4567,310-GINO的标准格式是310-4466,3-10-10-10的标准格式是310-1010。 如果两个号码有相同的标准格式,那么他们就是等同的(相同的拨号) 你的公司正在为本地的公司编写一个电话号码薄。作为质量控制的一部分,你想要检查是否有两个和多个公司拥有相同的电话号码。 Input 输入的格式是,第一行是一个正整数,指定电话号码薄中号码的数量(最多100000)。余下的每行是一个电话号码。每个电话号码由数字,大写字母(除了Q和Z)以及连接符组成。每个电话号码中只会刚好有7个数字或者字母。 Output 对于每个出现重复的号码产生一行输出,输出是号码的标准格式紧跟一个空格然后是它的重复次数。如果存在多个重复的号码,则按照号码的字典升序输出。如果输入数据中没有重复的号码,输出一行: No duplicates. Sample Input 12 4873279 ITS-EASY 888-4567 3-10-10-10 888-GLOP TUT-GLOP 967-11-11 310-GINO F101010 888-1200 -4-8-7-3-2-7-9- 487-3279 Sample Output 310-1010 2 487-3279 4 888-4567 3 Source East Central North America 1999 Translator 北京大学程序设计实习2007
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值