1002. Phone Numbers
Time limit: 2.0 second
Memory limit: 64 MB
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());
}
}