鱼C论坛

 找回密码
 立即注册
查看: 4085|回复: 2

[已解决]kmp算法遇到一些小问题

[复制链接]
发表于 2016-5-13 11:47:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
打算输出匹配的位置,结果程序运行时打出一个很长的数字,即乱码 求帮助
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. #define OK 1
  5. #define ERROR 0
  6. #define MAXLEN  200

  7. typedef struct
  8. {
  9.         char ch[MAXLEN+1];
  10.         int length;

  11. }SString;

  12. int index_KMP(SString S,SString T,int pos)
  13. {
  14.         int i=pos; int j=1; int next[100];
  15.         while(i<=S.length && j<=S.length)
  16.         {
  17.                 if(j==0||S.ch[i]==T.ch[j])
  18.                 {
  19.                         ++i;
  20.                         ++j;
  21.                 }
  22.                 else j=next[j];
  23.         }
  24.         if(j>T.length) return i-T.length;
  25.         else return 0;
  26. }

  27. void get_next(SString T,int next[])
  28. {
  29.         int i=1; next[1]=0;int j=0;
  30.         while(i<T.length)
  31.         {
  32.                 if(j==0||T.ch[i]==T.ch[j])
  33.                 {
  34.                         ++i;
  35.                         ++j;
  36.                         next[i]=j;
  37.                 }
  38.                 else j=next[j];
  39.         }
  40. }


  41. int main()
  42. {
  43.         SString S,T;
  44.         int pos;
  45.         cin>>S.ch;
  46.         cin>>T.ch;
  47.         pos=index_KMP(S,T,1);
  48.         cout<<pos<<endl;     //   输出pos时的值不符合常规
  49.         system("pause");
  50.         return 0;
  51. }
复制代码


最佳答案
2016-5-31 14:40:52
c语言里面表示长度要用strlen(“S")或者sizeof("S"),不同的是使用sizeof多了一个字符串结束符\0
你这里面试定义了一个结构体,所以应用strlen(S.ch)
如果直接用S.length,返回的是一个地址,所以会跳过你的18句直接跳到27句执行,所以你的一大段"乱码"其实是1-(-地址码)得到的结果。
另外上面正常的话执行到25句也会出错,因为你的next[j]数组没有初始化
所以需要先加上调用get_next()函数
另外next[]数组是从next[0]开始存放数据的,所以把i和j赋值为1会导致双方第一次匹配时第一个字符串匹配不到。
以下是我的代码:
  1. 应用程序的入口点。
  2. //测试数据:字符串:cddcdc
  3. //子串:cdc
  4. // 输出结果:4

  5. #include "stdafx.h"

  6. #include<iostream>
  7. using namespace std;
  8. #include<string>
  9. #define OK 1
  10. #define ERROR 0
  11. #define MAXLEN  200

  12. typedef struct
  13. {
  14.         char ch[MAXLEN+1];
  15.         int length;

  16. }SString;

  17. void get_next(SString T,int *next)
  18. {
  19.         int i=1; next[1]=0;int j=0;
  20.                 while(i<strlen(T.ch))
  21.         {
  22.                 if(j==0||T.ch[i]==T.ch[j])
  23.                 {
  24.                         ++i;
  25.                         ++j;
  26.                         next[i]=j;
  27.                 }
  28.                 else j=next[j];
  29.         }
  30. }

  31. int index_KMP(SString S,SString T,int pos)
  32. {
  33.         int i=pos; int j=0; int next[100];
  34.                 get_next(T,next);
  35.         while(i<=strlen(S.ch)&& j<=strlen(S.ch))
  36.         {
  37.                 if(j==0||S.ch[i]==T.ch[j])
  38.                 {
  39.                         ++i;
  40.                         ++j;
  41.                 }
  42.                 else j=next[j];
  43.         }
  44.         if(j>strlen(T.ch)) return (i-strlen(T.ch));
  45.         else return 0;
  46. }




  47. int _tmain(int argc, _TCHAR* argv[])
  48. {
  49.          SString S,T;
  50.         int pos=0;
  51.         cin>>S.ch;
  52.         cin>>T.ch;
  53.         pos=index_KMP(S,T,pos);
  54.         cout<<pos<<endl;     //   输出pos时的值不符合常规
  55.         system("pause");
  56.         return 0;
  57. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-5-16 20:31:28 | 显示全部楼层
好文章 感谢share
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-5-31 14:40:52 | 显示全部楼层    本楼为最佳答案   
c语言里面表示长度要用strlen(“S")或者sizeof("S"),不同的是使用sizeof多了一个字符串结束符\0
你这里面试定义了一个结构体,所以应用strlen(S.ch)
如果直接用S.length,返回的是一个地址,所以会跳过你的18句直接跳到27句执行,所以你的一大段"乱码"其实是1-(-地址码)得到的结果。
另外上面正常的话执行到25句也会出错,因为你的next[j]数组没有初始化
所以需要先加上调用get_next()函数
另外next[]数组是从next[0]开始存放数据的,所以把i和j赋值为1会导致双方第一次匹配时第一个字符串匹配不到。
以下是我的代码:
  1. 应用程序的入口点。
  2. //测试数据:字符串:cddcdc
  3. //子串:cdc
  4. // 输出结果:4

  5. #include "stdafx.h"

  6. #include<iostream>
  7. using namespace std;
  8. #include<string>
  9. #define OK 1
  10. #define ERROR 0
  11. #define MAXLEN  200

  12. typedef struct
  13. {
  14.         char ch[MAXLEN+1];
  15.         int length;

  16. }SString;

  17. void get_next(SString T,int *next)
  18. {
  19.         int i=1; next[1]=0;int j=0;
  20.                 while(i<strlen(T.ch))
  21.         {
  22.                 if(j==0||T.ch[i]==T.ch[j])
  23.                 {
  24.                         ++i;
  25.                         ++j;
  26.                         next[i]=j;
  27.                 }
  28.                 else j=next[j];
  29.         }
  30. }

  31. int index_KMP(SString S,SString T,int pos)
  32. {
  33.         int i=pos; int j=0; int next[100];
  34.                 get_next(T,next);
  35.         while(i<=strlen(S.ch)&& j<=strlen(S.ch))
  36.         {
  37.                 if(j==0||S.ch[i]==T.ch[j])
  38.                 {
  39.                         ++i;
  40.                         ++j;
  41.                 }
  42.                 else j=next[j];
  43.         }
  44.         if(j>strlen(T.ch)) return (i-strlen(T.ch));
  45.         else return 0;
  46. }




  47. int _tmain(int argc, _TCHAR* argv[])
  48. {
  49.          SString S,T;
  50.         int pos=0;
  51.         cin>>S.ch;
  52.         cin>>T.ch;
  53.         pos=index_KMP(S,T,pos);
  54.         cout<<pos<<endl;     //   输出pos时的值不符合常规
  55.         system("pause");
  56.         return 0;
  57. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 14:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表