📜  用于电话助记符的Java程序

📅  最后修改于: 2021-05-04 11:36:02             🧑  作者: Mango

在我们的日常生活中,我们必须记住许多电话号码。大多数人都很难记住如此庞大的10位电话号码。一个简单的解决方案是将该电话号码与某个已知单词相关联。
例如,我们可以根据拨号盘中的字符将电话号码32627287与DAMASCUS关联。 (我们可以将DIGIT替换为它在拨号盘上表示的任何相应CHARACTER中的任何一个。例如,我们在拨号盘中的2下提到了ABC。因此,我们可以用A或B或C表示2。类似地,我们可以用D或E或F替换3

编写一个程序,该程序将使用电话号码并打印字典中所有可能的单词。

让我们考虑样本输入23。
这里,
2可以使用A或B或C替换。
3可以用D或E或F代替。
因此,一个有效的组合将是AD,它对应于623。
另一个可以是CD等。
我们正在生成从{A或B或C}开始,然后是{D或E或F}的所有可能排列。但是,仅会打印出字典中的那些排列。这些选定的词将代表我们的示例输出。

但是,DIGITS(如1和0)将被替换为空字符串,因为它们不代表拨号盘上的任何字符,即1和0将代表“”。

例子:

Input : 23
Output : [BE]

Input : 623
Output : [MAD, OCD]

Input : 726
Output : [PAN, PCM, PCO, RAM, SAM]

Input : 2
Output : [A, B, C]

Input : 123
Output : [BE]


天真的方法
该程序使用回溯生成所有可能的单词。单击此处了解更多信息。回溯的概念已在此处进行了解释。

import java.util.*;
  
public class PhoneMnemonics {
    static ArrayList dictionary = new ArrayList();
  
    /* List all the phone mnemonics for the given digit sequence.
     * @param input a digit sequence, like "23432" */
    public static ArrayList listMnemonics(String input)
    {
        ArrayList list = new ArrayList();
        String[] dic = { "A", "B", "C", "D", "E", "F", "G", 
                        "H", "I", "J", "K", "L", "M", "N",
                        "O", "P", "Q", "R", "S", "T", "U", 
                         "V", "W", "X", "Y", "Z", "MAD",
                         "OCD", "PAN", "PCM", "PCO", "RAM", "SAM", "BE" };
        for (String i : dic)
            dictionary.add(i);
        listMnemonics("", input, list);
        return list;
    }
  
    /* Helper recursive method to list phone mnemonics. This works by finding
     * all possibilities for the first digit, then recursing on the rest of the
     * string. For example, if prefix is "AB" and remaining is "2", then the
     * mnemonics "ABA", "ABB", and "ABC" would be printed.
     * 
     * @param prefix : the part of the mnemonic that has already been built
     * @param remaining :  the remaining digits to include in the mnemonic  */
    private static void listMnemonics(String prefix, String remaining, 
                                                ArrayList list)
    {
        // Base case: when there are no more characters to process,
        // just print out the mnemonic that we've already built.
        if (remaining.isEmpty()) {
            if (!list.contains(prefix) && dictionary.contains(prefix))
                list.add(prefix);
            return;
        }
  
        // Recursive case.
        if (remaining.charAt(0) == '1' || remaining.charAt(0) == '0') {
            listMnemonics(prefix, remaining.substring(1), list);
        }
  
        String digits = digitLetters(remaining.charAt(0));
        for (int i = 0; i < digits.length(); i++) {
            String newPrefix = prefix + digits.charAt(i);
            String newRemaining = remaining.substring(1);
            listMnemonics(newPrefix, newRemaining, list);
        }
    }
  
    /**
     * Get the letters appearing on a given key of a standard phone keypad.
     * @param ch the character representation of a digit on the phone keypad
     *            (like '2')
     * @return a string containing the letters on the given key, or the empty
     *         string if the key is not recognized
     */
    private static String digitLetters(char ch)
    {
        switch (ch) {
        case '2':
            return "ABC";
        case '3':
            return "DEF";
        case '4':
            return "GHI";
        case '5':
            return "JKL";
        case '6':
            return "MNO";
        case '7':
            return "PQRS";
        case '8':
            return "TUV";
        case '9':
            return "WXYZ";
        }
        return "";
    }
  
    public static void main(String[] args)
    {
        String str = "123";
        ArrayList list = listMnemonics(str);
        System.out.println(list);
    }
}
输出:
[BE]

注意:一旦我们添加单词词典及其使用频率,该程序将为用户带来更多便利。使用词典,以下输出可能已被优化以根据其频率来打印MAD,OCD等。由用户使用。查看此链接:https://phonespell.org/。

C++实现的相关文章:
打印电话号码中所有可能的单词


参考 :

埃里克罗伯茨的递归思考