一串单词的排列(顺序),但之间用逗号分隔

发布于 2024-08-18 01:17:10 字数 2243 浏览 7 评论 0 原文

我在让这段代码在以逗号分隔的字符串上生成多个排列(排序)时遇到一些困难...我可以只做一个常规字符串,并让排列只对字母起作用,但在做时有点困难它用逗号分隔的单词...

为了让程序识别逗号,我使用了 StringTokenizer 方法,并将其放入 arrayList 中,但这实际上是我所得到的...问题又是我'我在排列每个单词时遇到困难...举一个例子,我将其发布在下面,然后将我的代码发布在下面...谢谢大家的帮助! ...并且通过排列我的意思是用逗号分隔的单词的顺序

例如,如果 BufferedReader 上的输入看起来像:

red,yellow

one,two,three

PrintWriter 上的输出应该看起来像:

red,yellow

yellow,red



one,two,three

one,three,two

two,one,three

two,three,one

three,one,two

three,two,one

请注意,输入总共有 3 行,包括“一,二,三”后有一个空行,而输出共有 11 行,其中“黄,红”后有一个空行,“三,二,一”后有两个空行。获得完全正确的格式至关重要,因为测试将是自动化的并且需要这种格式。另请注意,每个问题的输出行顺序并不重要。这意味着输出的前两行也可能是:

yellow,red

red,yellow

这是我到目前为止的代码...我已经注释掉了一些内容,所以不用担心这些部分

import java.io.*;

import java.util.*;

public class Solution

{

 public static void run(BufferedReader in, PrintWriter out)

    throws IOException

{

 String str = new String(in.readLine());

 while(!str.equalsIgnoreCase(""))
 {
 PermutationGenerator generator = new PermutationGenerator(str);
 ArrayList<String> permutations = generator.getPermutations();
 for(String str: permutations)
 {
  out.println(in.readLine());
 }
 out.println();
 out.println();

 }
 out.flush();

}







public class PermutationGenerator
{

 private String word;


public PermutationGenerator(String aWord)
{
 word = aWord;
}



public ArrayList<String> getPermutations()
{
 ArrayList<String> permutations = new ArrayList<String>();
 //if(word.length() == 0)
 //{
  //permutations.add(word);
  //return permutations;

 //}

  StringTokenizer tokenizer = new StringTokenizer(word,",");

     while (tokenizer.hasMoreTokens())
     {
      permutations.add(word);
      tokenizer.nextToken();
     }
 /*     
 for(int i = 0; i < word.length(); i++)
 {
  //String shorterWord = word.substring(0,i) + word.substring(i + 1);
  PermutationGenerator shorterPermutationGenerator = new PermutationGenerator(word);
  ArrayList<String> shorterWordPermutations =     
    shorterPermutationGenerator.getPermutations();

  for(String s: shorterWordPermutations)
  {
   permutations.add(word.readLine(i)+ s);
  }
 }*/
 //return permutations;


 }
}

}

I'm having some difficulty having this code generate a number of permutations(orderings) on a string separated by commas ...I can do just a regular string and have the permutations work on just letters but it is a bit more difficult when doing it with words separated by the commas...

To have the program recognize the commas I used the StringTokenizer method and I'm putting it into an arrayList but that is really as far as I have gotten ...the problem again is I'm having trouble permuting each word...to give an example I'll post it below this and then my code below that...thank you for your help everyone! ...and by permutations I mean orderings of the words separated by the comma's

For example, if the input coming in on the BufferedReader looked like:

red,yellow

one,two,three

the output on the PrintWriter should look like:

red,yellow

yellow,red



one,two,three

one,three,two

two,one,three

two,three,one

three,one,two

three,two,one

Note that the input had 3 lines total, including the blank line after "one,two,three" while the output had 11 lines total, including one blank line after "yellow,red" and two blank lines after "three,two,one". It is vital that you get the format exactly correct as the testing will be automated and will require this format. Also note that the order of the output lines for each problem does not matter. This means the first two lines of the output could also have been:

yellow,red

red,yellow

here is the code I have so far ...I have commented some stuff out so don't worry about those parts

import java.io.*;

import java.util.*;

public class Solution

{

 public static void run(BufferedReader in, PrintWriter out)

    throws IOException

{

 String str = new String(in.readLine());

 while(!str.equalsIgnoreCase(""))
 {
 PermutationGenerator generator = new PermutationGenerator(str);
 ArrayList<String> permutations = generator.getPermutations();
 for(String str: permutations)
 {
  out.println(in.readLine());
 }
 out.println();
 out.println();

 }
 out.flush();

}







public class PermutationGenerator
{

 private String word;


public PermutationGenerator(String aWord)
{
 word = aWord;
}



public ArrayList<String> getPermutations()
{
 ArrayList<String> permutations = new ArrayList<String>();
 //if(word.length() == 0)
 //{
  //permutations.add(word);
  //return permutations;

 //}

  StringTokenizer tokenizer = new StringTokenizer(word,",");

     while (tokenizer.hasMoreTokens())
     {
      permutations.add(word);
      tokenizer.nextToken();
     }
 /*     
 for(int i = 0; i < word.length(); i++)
 {
  //String shorterWord = word.substring(0,i) + word.substring(i + 1);
  PermutationGenerator shorterPermutationGenerator = new PermutationGenerator(word);
  ArrayList<String> shorterWordPermutations =     
    shorterPermutationGenerator.getPermutations();

  for(String s: shorterWordPermutations)
  {
   permutations.add(word.readLine(i)+ s);
  }
 }*/
 //return permutations;


 }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-08-25 01:17:10

您可以使用 String.split() ( http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String) )到将各个单词作为数组放入。您可以单独生成整数 {1..N} 的所有排列,其中 N 是单词数组的大小。然后使用数字排列作为索引遍历单词数组。

You can use String.split() ( http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#split(java.lang.String) ) to get the individual words into as an array. You can separately generate all the permutations on integers {1..N} where N is the size of the word array. Then just walk the word array using the numeric permutations as indices.

画离情绘悲伤 2024-08-25 01:17:10
  1. 将输入行(这是一个以逗号分隔的字符串单词)解析为字符串数组(String[]words)。
  2. 使用一些适用于数组的排列生成器,您可以使用谷歌轻松找到这样的生成器。您想要一个可以使用 Object[] 初始化的生成器,并且具有类似 Object[] nextPermutation() 的方法。
  3. 将其整合到您的解决方案中。

PS U 还可以使用整数排列生成器并生成从 0 到 (words.length - 1) 的所有排列;每个这样的排列都会为您提供一个要打印的 words[] 索引数组。

  1. Parse your input line (which is a comma-separated String ow words) into array of Strings (String[] words).
  2. Use some permutation generator that works on a array, you can easily find such generator using google. U want a generator that can be initialized with Object[], and has a method like Object[] nextPermutation().
  3. Put it together into your solution.

PS U can also use a Integer permutation generator and generate all permutations from 0 to (words.length - 1); each such permutation will give you an array of indexes of words[] to be printed out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文