当前位置: 首页 > 知识库问答 >
问题:

使用Java使用“n choose k”的字符串字母组合

阎知
2023-03-14
public class ReadFile {

    public static void main(String[] args) throws IOException {

        String file_name = "C:/Users/Shane/Documents/College/Classes/PurchaseTable.txt";

        extract(file_name, 50);


    }

        private String path;

        public ReadFile(String file_path) {
            path= file_path;
        }

        public String[] OpenFile() throws IOException {
            FileReader fr = new FileReader(path);
            BufferedReader textReader = new BufferedReader(fr);

            int numberOfLines = readLines();
            String[] textData = new String[numberOfLines];

            int i;

            for(i=0; i < numberOfLines; i++) {
                textData[i] = textReader.readLine();
            }

            textReader.close();
            return textData;
        }

        int readLines() throws IOException {

            FileReader file_to_read = new FileReader(path);
            BufferedReader bf = new BufferedReader(file_to_read);

            String aLine;
            int numberOfLines = 0;

            while(( aLine = bf.readLine()) != null) {
                numberOfLines++;
            }
            bf.close();

            return numberOfLines;
        }

    public static void extract(String filename, int threshold) {

        String file_name = filename;
        ArrayList<String> temp = new ArrayList<String>();
        ArrayList<String> products = new ArrayList<String>();
        HashMap<Integer, String> productsPerDate = new HashMap<Integer, String>();
        //HashMap<Integer, String> allCombinations = new HashMap<Integer, String>();

        try {
            ReadFile file = new ReadFile(file_name);
            String[] aryLines = file.OpenFile();
            int i;
            for (i=1; i < aryLines.length; i++) { //excludes header section of any table as shown in assignment
                temp.add(aryLines[i]);              
            }
        }
        catch (IOException e) {
            System.out.println( e.getMessage() );
        }

        System.out.println(temp);
        System.out.println(temp.get(0));
        System.out.println(temp.size());

        int i; int j; int l;
        for (i=0; i<temp.size(); i++) {
            String str = temp.get(i);
            StringBuilder sb = new StringBuilder(str);
            int k =0;
            for (j=0; j<=sb.length(); j++) {
                if(sb.charAt(j) == '\"' && k==0) {
                    sb.delete(0, j+1);
                    k++;
                }
                if(sb.charAt(j) == '\"' && k!=0) {
                    sb.delete(j, sb.length());
                    String line = null;
                    System.out.println(sb);
                    for( l=0; l<sb.length(); l++) {
                         String string = Character.toString(sb.charAt(l));
                         if(string.equals(",")) {

                         }
                         else if (l ==0) {
                             products.add(string);
                             line = string;
                         }
                         else {
                             products.add(string);
                             line = line + string;
                         }
                    }
                    productsPerDate.put(i, line);
                    //System.out.println(products);
                    break;
                }
            }
        }

        System.out.println(products);
        System.out.println(productsPerDate.entrySet()); //Hashmap set to string of 1 letter characters for products per date

        Set<String> removeDup = new HashSet<>();
        removeDup.addAll(products);
        products.clear();
        products.addAll(removeDup);

        System.out.println(products);

        int maxLength = productsPerDate.get(0).length();
        for(int m = 0; m < productsPerDate.size(); m++) { //determine max length of string in hashmap
            if(maxLength < productsPerDate.get(m).length()) {
                maxLength = productsPerDate.get(m).length();
            }
        }

以下是在上面的代码中创建的内容的输出:

1,"A,B,C,N",1/3/2013
4
A,B,C,N
B,C,D,A,F
A,C,V,N,J
A,C,J,D
[A, B, C, N, B, C, D, A, F, A, C, V, N, J, A, C, J, D]
[0=ABCN, 1=BCDAF, 2=ACVNJ, 3=ACJD]
[A, B, C, D, F, V, J, N]

因此,基本上,我试图编写代码,使长度为5的所有可能组合的字符串使用包含在上一个输出中显示的数组列表中的字母字符串。

共有1个答案

姜德泽
2023-03-14

这里有一个小方法,它返回长度为k的所有字母组合的列表(顺序不重要),给定长度为n的输入字符串:

public static ArrayList<String> combinations(String nChars, int k) {
    int n = nChars.length();
    ArrayList<String> combos = new ArrayList<String>();
    if (k == 0) {
        combos.add("");
        return combos;
    }
    if (n < k || n == 0)
        return combos;
    String last = nChars.substring(n-1);
    combos.addAll(combinations(nChars.substring(0, n-1), k));
    for (String subCombo : combinations(nChars.substring(0, n-1), k-1)) 
        combos.add(subCombo + last);

    return combos;
}

public static void main(String[] args) {
    String nChars = "ABCDE";
    System.out.println(combinations(nChars, 2));
}

output: [AB, AC, BC, AD, BD, CD, AE, BE, CE, DE]

我使用字符串作为输入和输出,因为它们是不可变的,并且在切片方面比列表表现得更好。但是如果您的列表只包含1个字母的字符串,那么转换应该很容易。

我不知道这个递归实现是否有效,但它很好地反映了帕斯卡三角形的数学性质:(n choose k)=(n-1 choose k-1)+(n-1 choose k)

 类似资料:
  • 问题内容: 使用java如何生成随机的字母数字字符串? 问题答案: 算法 要生成随机字符串,请连接从可接受的符号集中随机抽取的字符,直到字符串达到所需的长度为止。 实例 这是一些相当简单且非常灵活的代码,用于生成随机标识符。阅读以下信息以获取重要的应用笔记。 用法示例 为8个字符的标识符创建不安全的生成器: 为会话标识符创建一个安全的生成器: 创建具有易于阅读的代码的生成器以进行打印。字符串比完整

  • 问题内容: 我只需要从中提取数值。如何从Android中的字母数字字符串中提取数字? 问题答案: 更新:

  • 尝试查找正则表达式以获取字符串的最后一个字符String=“a76b62zf63” 通过正则表达式查找最后一个字符返回“F”

  • 我有一个单词列表(编程语言),我想找出字母表中的哪个字母出现在这些单词中,然后将这些单词的字符串总长度相加,最后返回一个字母,返回与这些单词匹配的最长字符串。这是我到目前为止所得到的,我几乎没有进一步的进展。 这是我试图更好地理解java流的一个练习。 我期望的最终输出(结果)类似于“p:17” 由于字符p出现在单词php、python、perl、lisp中,它对这些单词求和并返回字符串长度为17

  • 问题内容: 我有下表: 我要订购这样的产品ID(从最小到最大): 这是我尝试过的方法,但是不起作用: 我该怎么做呢? 以及如何获得最大的product_id?我试过了,但是没有用: 问题答案: 尝试执行以下查询以按预期顺序获取结果。 小提琴演示

  • 问题内容: 我正在做一个作业,在该作业中,我必须编写程序以读取用户的字符串,并打印出字符串中出现次数的字母。 例如,“ Hello world”应该打印出“ h = 1 e = 1 l = 3 o = 2 …等”,但是我只写“ hello world”和字母总数。 我不能使用hashmap函数,只能使用数组。有人可以给我一两个提示,提示如何从下面的书面代码继续进行操作以获得我的首选功能?我不完全了