- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在努力解决 Spoj 上的运行时错误 (NZEC) 问题, http://www.spoj.com/problems/NHAY/
我从我这边尝试了很多案例,每次它在 eclipse 中给出正确的输出,但在 Spoj 上提交时无法找出运行时错误的原因,任何人都可以帮我解决这个错误。
这是我的代码,
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
class KMP {
public int[] lps(String needle, int needleLength)
{
int lps[] = new int[needleLength];
int j=0,i=1;
lps[0]=0;
while(i<needle.length())
{
if(needle.charAt(j) == needle.charAt(i))
{
lps[i] = j+1;
i++;
j++;
}
else
{
if(j != 0)
{
j = lps[j-1];
}
lps[i] = 0;
i++;
}
}
return lps;
}
public List<Integer> KMPalgo(String hayStack, String needle, int needleLengh)
{
int lps[] = lps(needle, needleLengh);
int i=0;
int j=0;
List<Integer> position = new ArrayList<Integer>();
while(i<hayStack.length())
{
if(hayStack.charAt(i) == needle.charAt(j))
{
i++;
j++;
}
else
{
if(j !=0)
{
j = lps[j-1];
}
else
i++;
}
if(needle.length() == j)
{
position.add(i-j);
if(j !=0)
j = lps[j-1];
}
}
return position;
}
public static void main(String[] args) throws NumberFormatException, IOException {
KMP o = new KMP();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
while(true)
{
String needlLength = bf.readLine().trim();
if(needlLength == null || needlLength.equals(""))
break;
int lNeedle = Integer.parseInt(needlLength);
String needle = bf.readLine().trim();
String haystack = bf.readLine().trim();
List<Integer> result= o.KMPalgo(haystack, needle, lNeedle);
System.out.println();
for(Integer itr : result)
System.out.println(itr);
}
}
}
最佳答案
运行时错误的原因是:
String needlLength = bf.readLine().trim();
if(needlLength == null || needlLength.equals(""))
break;
在检查 needlLength
是否为 null
之前调用 trim()
。
但看起来您至少还有一个错误。你应该更换
if(j != 0)
{
j = lps[j-1];
}
lps[i] = 0;
i++;
与
if(j != 0)
{
j = lps[j-1];
}
else
{
lps[i] = 0;
i++;
}
因为现在你在计算前缀函数时最多做一次跳跃,这是不正确的。
关于java - Spoj 上的代码提交出现运行时错误 (NZEC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45423319/
以下是 SPOJ GCD2 的代码.它在我的机器和 Ideone 上运行良好,但在 SPOJ 上出现运行时错误 (SIGFPE)。我已经检查了 spojtoolkit.com 上也提供的所有测试用例。
如果正整数在从左到右和从右到左读取时在十进制系统中的表示相同,则称为回文。给定一个不超过1000000位的正整数K,写出大于K的最小回文的值输出。显示的数字始终不带前导零。 输入 第一行包含整数 t,
我一直在努力解决这个问题 SPOJ www.spoj.com/problems/PRHYME/?几天了,但没有成功。这是问题的简要说明: Given is a wordlist L, and a
我是编码初学者。我在向 spoj 提交质数生成代码时收到 NZEC 错误。但代码在我的桌面上运行得很好。请帮助我。这就是我编写的代码。 import java.util.*; import java.
#include #include #include main() { int n, m, i, j, k; char a[100], b[100]; scanf("%d
我遇到了一个问题 SPOJ . 我检查了所有通过所有测试用例,但我仍然在 spoj 上得到“WA”。 我知道它可以使用动态编程来解决,但我正在练习内存。 这是我的代码: #include #inclu
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
有关确切的问题,请参阅此 link 这里我定义了三个函数,将它们相互调用。函数调用尚未完成 #include int primegen(int x1,int x2); int isprime(int
Peter 想生成一些 prime numbers对于他的密码系统。帮助他!您的任务是生成两个给定数字之间的所有素数! 输入 输入以单行中测试用例的数量t开始(t int main() {
http://www.spoj.com/problems/FCTRL2/ 我的代码在 Spoj 中显示编译错误,尽管在我的编译器中运行准确。 **IDE - 代码块 ** int cal(int );
我正在尝试将我的代码提交给 SPOJ 上的“小阶乘”问题。它在我的 IDE 上成功运行,但在 SPOJ 上显示运行时错误 (NZEC)。请提出解决方案。 import java.util.Scanne
我正在尝试解决 SPOJ 中的下一个回文问题。我在下面的 Java 代码中收到超出时间限制的错误。 “如果一个正整数在十进制中从左到右和从右到左读的表示相同,则称为回文。对于给定不超过 1000000
我在 spoj 上解决了一个名为 Ambiguous Permutations(http://www.spoj.com/problems/PERMUT2/)的简单问题,当我测试小输入时它工作正常,但在
我正在尝试关于 SPOJ 的问题,其中我们必须简单地找到 Longest Increasing Sub-sequence 的长度给定数组 A. 我使用动态规划 O(n^2) 算法解决了这个问题,解决方
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我刚开始接触竞争性编程。我有点坚持这个素数。 SPOJ 上的生成器问题。代码在 GeeksforGeeks IDE 上运行良好,但在 SPOJ 上它会出现运行时错误。问题是这样的: Peter 想为他
好吧,这让我发疯了。我在 spoj 上解决了一个名为 MIXTURES ( http://www.spoj.com/problems/MIXTURES/) 的问题。我不知道为什么我总是遇到段错误。该问
我已经为下一个回文问题编写了一个蛮力解决方案,并希望获得 Time Limit Exceeded 。但是当我测试了一些测试用例时它工作正常但是当我在 spoj 中提交代码时我得到了错误的答案。这是我的
我正在尝试解决 NGON问题。我在这里使用自下而上的动态编程。递归函数为: f(a,b) = f(a-1,b) + f(a-1,b-1)*ai +f(a-1,b-2)*ai*(ai-1)/2, a>0
我正在解决这个 spoj 问题。 http://www.spoj.com/problems/MAXSUB/我所有的测试用例都正常工作,但我在 spoj 中得到了错误的答案。我试图改变我的代码很多但仍然
我是一名优秀的程序员,十分优秀!