1

我想在 Java 中使用前向遍历生成可能的标记。例如,如果我有一个字符串“这是我的车”。我需要生成令牌

  • “这是我的车”
  • “这是我的”
  • “这是”
  • “这”
  • “是我的车”
  • “是我的”
  • “是”
  • “我的车”
  • “我的”
  • “车”

做这个的最好方式是什么?有什么例子吗?谢谢。

4

4 回答 4

5

这是另一个具有拆分和嵌套循环的解决方案:

public static void main(String[] args) {
    String original = "this is my car";

    String[] singleWords = original.split(" ");  // split the String to get the single words
    ArrayList<String> results = new ArrayList<String>();  // a container for all the possible sentences
    for (int startWord = 0; startWord < singleWords.length; startWord++) {  // starWords start with 0 and increment just until they reach the last word
        for (int lastWord = singleWords.length; lastWord > startWord; lastWord--) { // last words start at the end and decrement just until they reached the first word 
            String next = "";
            for (int i = startWord; i != lastWord; i++) { // put all words in one String (starting with the startWord and ending with the lastWord)
                next += singleWords[i] + " "; 
            }
            results.add(next);  // add the next result to your result list
        }
    }

    // this is just to check the results. All your sentences are now stored in the ArrayList results
    for (String string : results) {
        System.out.println("" + string);
    }
}

这是我测试该方法时的结果:

this is my car 
this is my 
this is 
this 
is my car 
is my 
is 
my car 
my 
car 
于 2012-12-11T15:13:25.423 回答
2

使用番石榴

String yourOriginalString = "This is my car";
final Set<String> originalWords = 
        Sets.newLinkedHashSet(
                Splitter.on(CharMatcher.WHITESPACE).trimResults().split(yourOriginalString));
final Set<Set<String>> variations = Sets.powerSet(originalWords);
for (Set<String> variation : variations) {
    System.out.println(Joiner.on(' ').join(variation));
}

输出:

This
is
This is
my
This my
is my
This is my
car
This car
is car
This is car
my car
This my car
is my car
This is my car
于 2012-12-11T15:00:55.610 回答
1

这是一种可能的方法:

//Just a method that seperates your String into an array of words based on the spaces
//I'll leave that for you to figure out how to make
String[] array = getSeperatedWords(<yourword>);
List<StringBuffer> bufferArray = new ArrayList<StringBuffer>();

for(int i = 0; i < array.length; i++){
    StringBuffer nowWord = array[i];
    for(int j = i; j < array.length; j++{
        nowWord.append(array[j]);
    }
    bufferArray.add(nowWord);
 }
 for(int i = 0; i < bufferArray.length; i++){
    System.out.print(bufferArray.get(i));
 }
于 2012-12-11T15:04:20.620 回答
1
import java.util.Arrays;

public class Test {

    public static void main(String[] args) {

        String var = "This is my car";
        permute(var);

    }

    public static void permute(String var) {

        if(var.isEmpty())
            return;

        String[] arr = var.split(" ");  

        while(arr.length > 0) {
            for(String str : arr) {
                System.out.print(str + " ");
            }
            arr = (String[]) Arrays.copyOfRange(arr, 0, arr.length - 1);
            System.out.println();               
        }       

        String[] original = var.split(" ");
        permute(implodeArray((String[]) Arrays.copyOfRange(original, 1, original.length), " "));

    }

    public static String implodeArray(String[] inputArray, String glueString) {

        String output = "";

        if (inputArray.length > 0) {
            StringBuilder sb = new StringBuilder();
            sb.append(inputArray[0]);

            for (int i=1; i<inputArray.length; i++) {
                sb.append(glueString);
                sb.append(inputArray[i]);
            }

            output = sb.toString();

        }

        return output;

    }        

}

阅读本书,你将成为递归大师:http: //mitpress.mit.edu/sicp/

于 2012-12-11T15:17:47.257 回答