【问题标题】:Palindromes using Stacks and Queues使用堆栈和队列的回文
【发布时间】:2015-10-18 17:20:09
【问题描述】:

我正在测试我的新示例文本:我妈妈是个好厨师。虽然有时在中午左右她会离开,忘记给我做午餐和一些汽水。 @旧作业再次变得重要

我的问题是我没有得到正确的输出,因为我的方法只打印 *Mom a noon i

这都是基于 GUI 的。我正在读取一个文件并检查回文,然后使用堆栈和队列将它们打印到我的 JTextArea 中。

问题是,所有这些都有效,当我启动程序并附加文本文件时,我只得到第一个回文。所以它会打印“妈妈”,这是我的第一个测试用例,但它不会进入它之后的任何其他回文?

我以为我可能在某个时候陷入了代码阻塞的困境,但在修补了一天之后,我现在有点卡住了。

编辑 1:我现在得到更多结果

我的方法是,

public void fileDecode() throws FileNotFoundException
    {

        if (fileChoice.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
        {
            file = fileChoice.getSelectedFile();

            scanInput = new Scanner(file);

            while(scanInput.hasNext())
            {

                int nextPalindrome = 0;
                int counter = 0;
                String token = scanInput.next();
                Stack<Character> stk = new Stack<Character>();
                Queue<Character> que = new LinkedList<Character>();
                for (int i = 0; i < token.length(); ++i)
                {
                    stk.push(token.charAt(i));  //pushing all char's onto the stack/queue
                    que.add(token.charAt(i));

                }
                for (int j = 0; j < token.length(); ++j)
                {
                        char tempStk = stk.pop(); //removing them one at a time and checking if they are equal and incrementing counter
                        char tempQue = que.remove();

                        if (tempStk == tempQue)
                        {
                            counter++;
                        }
                }

                if (counter == token.length())
                {
                   build.append(token + " "); //if the counter # was the same as the token's length, than it is indeed a palindrome and we append it into our StringBuilder that we pass to the JTextArea
                   nextPalindrome = token.length();
                } 
            }  
        }
    }

【问题讨论】:

    标签: java algorithm stack queue


    【解决方案1】:

    您在 while 循环之外将 counter 设置为 0,因此第二个单词的计数将从第一个单词的计数开始。将 counter = 0 移动到 while 循环中,它应该可以工作。

    另外,nextPalindrome 似乎没有被使用,即使是,如果你将它设置在循环的底部,它会立即在顶部设置为 0,所以它只会保持非零,如果最后一个词是回文。

    另外,想想第二个for 循环中发生了什么。您正在遍历所有字符并比较堆栈中的字符和队列中的字符。如果它们不同,你就知道你没有回文,所以一旦你发现不同之处,继续循环就没有意义了。你也已经有了一个循环计数器j,所以你不需要另一个。所以我会重写第二个循环和以下条件:

                for (int j = 0; j < token.length(); ++j)
                {
                        char tempStk = stk.pop(); //removing them one at a time and checking if they are equal and incrementing counter
                        char tempQue = que.remove();
    
                        if (tempStk != tempQue)
                            break;
                }
    
                if (j == token.length())
    

    这是可行的,因为在循环完成后,j 可以等于 token.length() 的唯一方法是循环完成,这只有在没有不相等的字符对时才会发生(换句话说,所有字符对是平等的,这是你想要的)。

    【讨论】:

    • 将它移到 while 循环中确实给我带来了更多的结果。然而,我现在打印了ia 之类的东西,以及真正的回文noonmom,我知道我必须进入并创造更多条件。你能再解释一下你的第三条信息吗?删除第二个 for 循环部分。
    • 作为旁注,nextPalindrome 变量是因为我需要做类似的事情,mom 是回文,length() 等于 3。所以你会从mom 这将是秘密信息的一部分,只是还没走到那一步哈哈
    • 嗯,ia 是回文。 :-) 但是,如果您不想要这些,只需检查 token.length() == 1 并循环。我将编辑我的答案以扩展我的最后一段。
    • @SenjuXo 好的,扩展了我的第三段。
    • 谢谢! @blm 我现在一切正常。我现在所要做的就是弄清楚如何获取回文长度,然后像我在旁注第二条评论中提到的那样以该长度打印令牌。
    猜你喜欢
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2010-11-20
    • 1970-01-01
    • 2012-10-11
    • 2014-07-21
    相关资源
    最近更新 更多