java - 扫描并获取特定 token

标签 java algorithm palindrome

我几天来一直在测试的东西有问题。

全部使用栈和队列

Sample.txt : My mom and dad both think I will do good at my gig tomorrow.

我有一个 GUI,它附加了一个文件并为回文解析文件。例如,我想,

我可以找到 mom 作为回文,并且由于 mom.length() == to 3 我会捕获第三个来自 mom 的 token ,在本例中为 both。我能够正确地捕获所有这些回文,只是不知道如何遍历我还没有“读过”的标记?

我的方法是,

public void fileDecode() throws FileNotFoundException
    {


            while(scanInput.hasNext())
            {
                int counter = 0;
                int nextPalindrome = 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));
                    que.add(token.charAt(i));

                }
                for (int j = 0; j < token.length(); ++j)
                {
                        char tempStk = stk.pop();
                        char tempQue = que.remove();

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

                if (counter == token.length())
                {
                   //build.append(token + " ");  #if i want to see the palindromes
                    nextPalindrome = token.length(); //the length/distance of the token desired
                } 

            }  
        }
    }

最佳答案

just at a loss on how i would traverse tokens that i haven't 'read' in yet?

您阅读并跳过它们。只需使用通常为 0 的局部变量,但是一旦找到回文,就将其设置为回文的长度。然后在检查逻辑之前检查这个变量并在它大于 0 时继续循环。

// Idea: there are 3 cases:
// 1. tokensToSkip is 0: process as normal (check for palindromeness)
// 2. tokensToSkip is 1: decrement it to 0, but process as normal
// 3. tokensToSkip is 2, 3, 4...: decrement it and skip (continue the while)
// Note that tokensToSkip will never be negative.

// Will skip tokensToSkip - 1 tokens
int tokensToSkip = 0;
while(scanInput.hasNext()) {
    String token = scanInput.next();
    // instead of the next two lines we could just have:
    //     if (--tokensToSkip > 0) continue;
    // only once, notice the -- operator in front.
    //
    // This would almost always work, but if tokensToSkip = Integer.MIN_VALUE
    // decrementing it will cause negative overflow and thus tokensToSkip will
    // become Integer.MAX_VALUE! To prevent this we have a check.
    if (tokensToSkip > 0) tokensToSkip--;

    // here tokensToSkip is already decremented, so this check is different
    // from the one above. For example is tokensToSkip == 1 before the line
    // above, now it is tokensToSkip == 0 and this second check fails.
    if (tokensToSkip > 0) continue;
    if (isPalindrome(token)) {
        tokensToSkip = token.length();
    }
    // ... other stuff ...
}

顺便说一下,一个更有效的实现isPalindrome()的方法是:

static boolean isPalindrome(String s) {
    for (int i = 0; i < s.length() / 2; i++) {
        if (s.charAt(i) != s.charAt(s.length() - i - 1)) {
            return false;
        }
    }
    return true;
}

关于java - 扫描并获取特定 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33202981/

相关文章:

c# - 如何确定数组中的哪个字符串与给定字符串最相似?

java - 递归函数 : Check for palindrome in Java

python - 我正在写一个函数来查找数字是否是回文。它在第2行给了我一个运行时错误,这是函数定义的行[closed]

java - 使用 Struts 2 xml 验证的自定义 validator 错误消息

java.sql.Time 异常

algorithm - 如何使多个多边形相交?

Java 回文、方法和抽象类?

java - FTPClient(org.apache.commons.net.ftp.FTPClient) 无法检索大型 xml 文件

通过 ftps 连接 alfresco 的 Java 代码

algorithm - 为未知函数建立线性近似