如何修复我为给定的测试案例实现的KMP算法中的“越界”错误?

huangapple go评论88阅读模式
英文:

How to fix 'out of bound' error in my KMP algorithm implementation for a given testcase?

问题

以下是您要翻译的内容:

  1. 我刚刚阅读了KMP算法,然后自己编写了算法,对一些测试用例有效,但在给定的测试用例中,编译器报告了一些越界/溢出类型的错误。
  2. 测试用例:haystack="mississippi" needle="issipi"
  3. 我检查了lps的输出=0 0 0 1 -1 1,它应该在索引2处打印1,并且-1甚至不应该存在。请告诉我我犯了什么错误?

请注意,我已经删除了代码部分,只返回翻译好的内容。

英文:

i just read the KMP algorithm and then i wrote the algorithm myself, it worked for few testcases but at the given testcase the compiler gave some out of bound/overlfow kind of error.

testcase: haystack="mississippi" needle="issipi".
i checked that the output of lps=0 0 0 1 -1 1 , it should print 1 at index=2 and -1 shouldn't even be there.pls tell what mistake i did?

  1. `string haystack="mississippi";
  2. string needle="issipi";
  3. int n=haystack.length();
  4. int m=needle.length();
  5. if(m>n) cout<<"no occurence";
  6. vector<int> lps(m,-1);
  7. lps[0]=0;
  8. int len=0;
  9. for(int i=1;i<m;i++){
  10. if(needle[i]==needle[len]){
  11. len++;
  12. lps[i]=len;
  13. }
  14. else{
  15. if(len>0) len=lps[len-1];
  16. else lps[i]=0;
  17. }
  18. }
  19. for(auto it:lps){cout<<it<<" ";}
  20. int i=0;
  21. int j=0;
  22. while(i<n && j<m){
  23. if(haystack[i]==needle[j]){
  24. i++;
  25. j++;
  26. if(j==m){
  27. cout<<"occurence at: "<<i-j<<endl;
  28. j=lps[j-1];
  29. }
  30. }
  31. else{
  32. if(j>0) j=lps[j-1];
  33. else i++;
  34. }
  35. }`

答案1

得分: 0

这部分代码的问题似乎出现在以下部分:

  1. while (i < n && j < m) {
  2. if (haystack[i] == needle[j]) {
  3. i++;
  4. j++;
  5. if (j == m) {
  6. cout << "occurence at: " << i - j << endl;
  7. j = lps[j - 1];
  8. }
  9. }
  10. else {
  11. if (j > 0) j = lps[j - 1];
  12. else i++;
  13. }

while循环迭代中,当i = 9时,执行了j = lps[j - 1]这一行代码。由于在这个循环迭代中j = 5,所以j被设置为lps中索引为5的值,即-1。在下一次循环迭代中,代码尝试使用负值索引needle[j],这导致了错误。

英文:

I don't recall the details of KMP, but just stepping through your code the problem appears to stem from this section:

  1. while (i &lt; n &amp;&amp; j &lt; m) {
  2. if (haystack[i] == needle[j]) {
  3. i++;
  4. j++;
  5. if (j == m) {
  6. cout &lt;&lt; &quot;occurence at: &quot; &lt;&lt; i - j &lt;&lt; endl;
  7. j = lps[j - 1];
  8. }
  9. }
  10. else {
  11. if (j &gt; 0) j = lps[j - 1];
  12. else i++;
  13. }

On the iteration of this while loop when i = 9 the line j = lps[j - 1] executes. Since j = 5 on this loop iteration, j is getting set to what lps holds at index 5, namely, -1. On the next iteration of the while loop the code attempts to index needle[j] with a negative value which causes the error.

答案2

得分: 0

Here is the translated content:

"不要使用for循环,改用while循环,当len = lps[len-1]时不要增加i,因为在这一步我们没有为lps[i]分配任何值。

  1. while(i<m) {
  2. if(needle[i]==needle[len]) {
  3. len++;
  4. lps[i]=len;
  5. i++;
  6. }
  7. else {
  8. if(len>0) {
  9. len=lps[len-1]; // 在这里修改了len,但没有给lps[i]赋值
  10. } else {
  11. lps[i]=0;
  12. i++;
  13. }
  14. }
  15. }
  16. ```"
  17. <details>
  18. <summary>英文:</summary>
  19. Instead of using a for loop, use a while loop and don&#39;t increment i when len = lps[len-1] since we are not assigning any value to lps[i] at this step
  20. while(i&lt;m) {
  21. if(needle[i]==needle[len]) {
  22. len++;
  23. lps[i]=len;
  24. i++;
  25. }
  26. else {
  27. if(len&gt;0) {
  28. len=lps[len-1]; // len is modified here but no value is assigned to lps[i] here
  29. } else {
  30. lps[i]=0;
  31. i++;
  32. }
  33. }
  34. }
  35. </details>

huangapple
  • 本文由 发表于 2023年6月5日 01:45:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定