1

これが私の最初の質問です。私はこの問題に何時間も取り組んできました。再帰を使用して、文字列内の文字のすべてのサブセット (必ずしも substringsではない) を生成することになっています。あなたが私の考えを見て、うまくいけば私が間違っているところを教えてくれるように、私はたくさんコメントしました. それが違いを生む場合、私はEclipseをIDEとして使用しています。

 import java.util.ArrayList;

//Generates subsets of a string
public class SubsetGenerator
{
    private String original;
    private String remaining;
    private ArrayList<String> subsets;
    //Constructs a subset generator
    //@param input string to have subsets generated
    public SubsetGenerator(String input)
    {
        original = input;
        remaining = original;
        subsets = new ArrayList<String>();
    }

    public void printSubsets()
    {
        System.out.print(subsets);
    }
    //gets subsets
    public void generateSubsets()
    {       
        //if the string is empty, it has no subsets
        if(remaining.length() == 1)
        {
            subsets.add(remaining);
            return;
        }
        else
        {
            //remove the first character and hold onto it
            String removed = remaining.substring(0,1);
            remaining = remaining.substring(1);
            //recursion. Eventually it should add the last character in the string to the ArrayList and return
            generateSubsets();
            //Take each element that is in the ArrayList, add the removed character to it, add this back to the list
            for (int i = 0; i < subsets.size(); i++)
            {
                String temp = removed + subsets.get(i);
                subsets.add(temp);
            }
            subsets.add(removed);//add the removed character by itself
            return;
        }
    }

}

これらは私が得ているエラーです:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOfRange(Arrays.java:3221)
    at java.lang.String.<init>(String.java:233)
    at java.lang.StringBuilder.toString(StringBuilder.java:447)
    at SubsetGenerator.generateSubsets(SubsetGenerator.java:41)
    at SubsetGenerator.generateSubsets(SubsetGenerator.java:37)
    at SubsetGenerator.generateSubsets(SubsetGenerator.java:37)
    at SubsetGenerator.generateSubsets(SubsetGenerator.java:37)
    at SubsetGeneratorTester.main(SubsetGeneratorTester.java:7)

私はこのコードでそれをテストしました:

public class SubsetGeneratorTester 
{
    public static void main(String[] args) 
    {
        SubsetGenerator s = new SubsetGenerator("world");
        s.generateSubsets();
        s.printSubsets();
    }

}
4

4 に答える 4

1

どうぞ:

public class SubsetGenerator {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<String> list = new ArrayList<String>();
        StringBuilder myBuilder = new StringBuilder();
        String original = "World";


        for(int i=0; i<original.length(); ++i)
            genSubs(original, myBuilder, list, i);

        System.out.println(list.toString());


    }

static void genSubs(String original, StringBuilder current, ArrayList<String> myList, int index){

    current.append(original.charAt(index));

    System.out.println(current.toString() + index);

    myList.add(current.toString());


    for(int i=index+1; i<original.length(); ++i)
        genSubs(original, current, myList, i);

    current.deleteCharAt(current.toString().length()-1);


    return;
}

}

出力:

[W, Wo, Wor, Worl, World, Word, Wol, Wold, Wod, Wr, Wrl, Wrld, Wrd, Wl, Wld, Wd, o, or, orl, orld, ord, ol, old, od, r, rl, rld, rd, l, ld, d]
于 2013-07-08T06:24:56.723 に答える