0

该代码适用于长度为 6 的单词。但是当我输入一个 7 个字母的单词时,它会抛出这个堆栈溢出错误。有没有办法解决这个问题?在这段代码中,生成器函数将单词的“第 i 个”元素与最后一个元素交换。example->car . 在第一遍中,它将生成'rac',在第二遍中将生成'cra'。现在这两个新单词作为参数传递给'check'函数。需要注意的是,当第一次调用“生成器”函数时,输入的单词会被添加到数组列表中。此后,当且仅当该词不存在于数组列表中时,才将词添加到数组列表中,即仅当生成新词时才调用生成器函数。所以现在,生成了 'rac' 并传递给 'check',它返回 false。这意味着该词不存在并且调用了生成器函数。现在'rac'生成'car'和'rca'但是'car'已经存在。因此它不会作为参数传递给“生成器”函数。arraylist 中存在的单词作为终止条件。

import java.util.*;
import java.io.*;

class progx
{
   static ArrayList<String> word = new ArrayList<String>();
   public static void main(String[] args) throws IOException
   {
      progx ob = new progx();
      ob.input();
   }

   void input() throws IOException // this function takes the input
   { // from user and calls "generator" function
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("enter word");
      String s = in.readLine();
      progx obj = new progx();
      obj.generator(s); // call 'generator' function
   }

   void generator(String s) throws IOException
   {
      progx ob = new progx();
      String output = "";
      boolean c = false;
      word.add(s); // adds the word to arraylist

      int i, j, l = s.length(), l2 = l - 1;
      char temp;
      char[] b = new char[l];
      for (i = 0; i < l; i++)
      {
         b[i] = s.charAt(i); // initializing each element in array
      } // with the ith character of the input string

      i = 0; // re-initializing 'i' for the while loop
      while (i < l)
      {
         temp = b[i]; // swapping ith character with last element
         b[i] = b[l2];
         b[l2] = temp;
         output = (ob.convertArray(b));// storing the new word in 'output'
         c = ob.check(output);// checking whether the elemnt is present in
                              // arraylist

         if (c == false)// if the word is not present, then call the 'generator'
                        // function
         {
            System.out.println(output);
            ob.generator(output);
         }

         for (j = 0; j < l; j++) // re-initialising the array as the swapping
         {
            b[j] = s.charAt(j);
         } // might change the position characters
         i++; // of the original input String "s"
      }
   }

   String convertArray(char[] s) // array converter- converts array to string
   {
      String n = "";
      for (int i = 0; i < s.length; i++)
      {
         n = n + s[i];
      }
      return n;
   }

   boolean check(String output) // function that checks presence
   { // of the generated word in the array
      boolean present = word.contains(output);
      return present;
   }
}
4

2 回答 2

0

您当前生成字谜的方式会给您 Stack Overflow 错误,因为您的方法嵌套太深。在 Java 中,您有一个代表所有方法调用等的堆栈。它的内存量有限,当您达到该上限时,它会溢出。

因为您的方法调用嵌套得太深,(它们被嵌套在单词n!n的字母数是多少)您一遍又一遍地调用方法,但是由于嵌套,它们无法从堆。因此,您最终会遇到内存不足的情况。

我会重新考虑您用来生成这些字谜的算法(除非您可以接受它在某些机器上的上限,否则我的长度为 8)。

于 2013-10-10T13:40:13.070 回答
0
import java.util.*;
public class rec 
{

  char a[] = {
    'a',
    'b',
    'c',
    'd'
  };

  char temp = ' ';

  void Anagram(int i) 
  {
    if (i < a.length) 
    {
      temp = a[0];
      a[0] = a[i];
      a[i] = temp;
      Anagram1(0);
      Anagram(i + 1);
    } else
      return;
  }

  void Anagram1(int r) 
  {
    if (r < a.length - 1) 
    {
      Anagram2(1);
      Anagram1(r + 1);
    } else
      return;
  }
  void Anagram2(int j) 
  {
    if (j < a.length - 1)
    {
      temp = a[j + 1];
      a[j + 1] = a[j];
      a[j] = temp;
      System.out.println(Arrays.toString(a));
      Anagram2(j + 1);
    } else
      return;
  }

 public static void main(String args[]) 
 {
     rec r =new rec();
    r.Anagram(0);
  }
}
于 2017-04-27T18:10:09.330 回答