URAL1002

来源:互联网 发布:荆州网络电视 编辑:程序博客网 时间:2024/05/21 12:46

一道动态规划,时间复杂度:O(N)

公式可以简单表示为:

//list[i]:list of string which is phone number's substring, and its index in phone number is ifor (String s : list[i]) {  int newLen = 1 + f(i + len);      if (newLen < f(i)) {       f(i) = newLen;    }}


import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.List;/** * DP * list[i]:list of string which is phone number's substring, and its index in phone number is i *  for (String s : list[i]) { *   if (1 + f(i + len) < f(i)) { *        f(i) = 1 +  f(i + len); *    } *   } * @author fytain * */public class URAL1002 {static {        //System.setIn(URAL1004.class.getResourceAsStream("/URAL1002.txt"));    }private static final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));private static final PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));        //22233344115566070778889990    private static char[] CHAR_TO_NUM_MAP = {'2','2','2','3','3','3','4','4','1','1','5','5','6',                                             '6','0','7','0','7','7','8','8','8','9','9','9','0'};    private static  List<List<String>> words = new ArrayList<List<String>>(100);//store list of words    static {    for (int i = 0; i < 100; i++) {    words.add(new ArrayList<String>());    }    }        private static String[] RESULT = new String[100];    private static int[] LEN_OF_RESULT  = new int[101];        private static  String phoneNumber;    private static int LEN;        private static char[] buff = new char[50];    private static final int MAX_LEN = 200;            public static void main(String[] args) throws Exception {    phoneNumber = stdin.readLine();    while (!"-1".equals(phoneNumber)) {readData();dp();printResult();phoneNumber = stdin.readLine();}        }        private static void printResult() {    if (LEN_OF_RESULT[0] <= LEN) {    pw.println(RESULT[0].trim());    } else {    pw.println("No solution.");    }        pw.flush();    }        private static void dp() {    Arrays.fill(LEN_OF_RESULT, MAX_LEN);    Arrays.fill(RESULT, "");    LEN_OF_RESULT[LEN] = 0;        if (words.get(LEN - 1).size() > 0) {    //exist a word with a single char which is the last char of phone number    LEN_OF_RESULT[LEN - 1] = 1;    RESULT[LEN - 1] = words.get(LEN - 1).get(0);    } else {    LEN_OF_RESULT[LEN - 1] = MAX_LEN;    RESULT[LEN - 1] = "";    }                for (int i = LEN - 2; i >= 0; i--) {        List<String> wList = words.get(i);        for (String s : wList) {        if (i + s.length() == LEN) {        LEN_OF_RESULT[i] = 1;        RESULT[i] = s;        break;        } else {        int newLen = LEN_OF_RESULT[i + s.length()] + 1;        if (newLen < LEN_OF_RESULT[i]) {        LEN_OF_RESULT[i] = newLen;        RESULT[i] = s + " " + RESULT[i + s.length()];        }        }        }        }    }        private static void readData() throws Exception {// initfor (List<String> list : words) {list.clear();}LEN = phoneNumber.length();int count = readInt();for (int i = 0; i < count; i++) {String w = stdin.readLine();String num = transWordToNum(w);if (num != null) {int index = phoneNumber.indexOf(num);while (index >= 0) {words.get(index).add(w);index = phoneNumber.indexOf(num, index + 1);}}}}            // trans word to num,etc: it->18    private static String transWordToNum(String w) {    if (w.length() > LEN) {    return null;    }        for (int i = 0; i < w.length(); i++) {    char c = w.charAt(i);    buff[i] = CHAR_TO_NUM_MAP[c - 'a'];    }        return String.valueOf(buff, 0, w.length());    }        private static int readInt() throws Exception {        String line = stdin.readLine().trim();        while (line.length() == 0) {            line = stdin.readLine().trim();        }        return Integer.parseInt(line);    }}


原创粉丝点击