=0),其中,s是串的名称,用双引号(也可以使用单引号)括起来的字符序列是串的值,注意引号不是串的内容。ai(i_数据结构中的串是什么">
当前位置:   article > 正文

数据结构—串的详细解释(含KMP算法)_数据结构中的串是什么

数据结构中的串是什么

1.1串的定义

串:串是由零个或多个字符组成的有限序列,又叫字符串(其的存储结构包含顺序表存储、单链表存储的形式。)

一般记为s="a1a2a3....an"(n>=0),其中,s是串的名称,用双引号(也可以使用单引号)括起来的字符序列是串的值,注意引号不是串的内容。ai(i<=i<=n)可以是字母、数字或者其他字符,i就是该字符在串中的位置。串中的字符数目n称为串的长度,定义中谈到"有限"是指长度为n是一个有限的数值。零个字符的串称为空串,它的长度为0,可以直接用两个双引号表示,也可以用其他的字符表示空串。所谓的序列说明串的相邻字符之间具有前驱和后继的关系。

(1)空格串,空格串是有长度的串内容为空格。

(2)子串与主串,串中任意个数的连续字符组成的子序列为该串的子串,包含子串的串即为主串.

子串在主串的位置就是子串第一个字符在主串的位置的序号。

1.2串的比较

串的比较实际上就是判断字母前后的ASCII码的大小。比较两个串相等时,必须是它们串的长度以及它们各个位置上的内容完全一致才为相等。

当串在比大小时,串中内容都相同但有一个串的长度比其大一点则长度大的就是比另一个串大,相反长度小的则比另一个串小了。

1.3串的抽象数据类型

串的一些操作函数(需要自己去书写):

串(string)

DATA

串中元素仅由一个字符组成,相邻元素具有前驱和后继的关系

Operation

        StrAssign(T,*chars):生成一个其值等于字符串常量chars的串T

        StrCopy(T,S):串S存在,由串S复制得串T

        ClearString(S):将串S清空

        StringEmpty(S):若串为空则返回true,不为空则返回false

        StringLength(S):返回串S的元素个数,即串的长度

        StrCompare(S,T):若S>T,返回值>0,若S=T,返回0,若S<T,返回值<0

        Concat(T,S1,S2):用T存储S1和S2连接后的串

        SubString(Sub,S,pos,len):串S存在1<=pos<=Stringlength(s),   且 0<=len<=StringLength(S)-pos-1,用Sub返回串S第pos起长度为len的子串。

        Index(S,T,pos):用串S和T存在,T非空,若S中存在与T相同的串则返回它在pos后第一个出现的位置

        Repalce(S,T,V):用V替换主串S中所有出现与T相同的不重叠的子串

        StrInsert(S,pos,T):在串s的pos处插入子串T

        StrDelete(S,pos,len):删除串s的pos处长len的子串

 (1)StrCopy函数

代码如下:(还可以将字符数组更改为string类型)

  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. void StrCopy(char *T,char *S);
  5. int main()
  6. {
  7. char s[Max],T[Max];
  8. cin>>s;
  9. cout<<"S:";
  10. cout<<s<<endl;
  11. cout<<"T:";
  12. StrCopy(T,s);
  13. cout<<T;
  14. }
  15. void StrCopy(char *T,char *S) //传递指针指向字符数组的首地址
  16. {
  17. int i;
  18. for(i=0;S[i]!='\0';i++) //根据主串长度赋值给串T
  19. T[i]=S[i];
  20. T[i]='\0';
  21. }
  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. void StrCopy(string &T,string &S);
  5. int main()
  6. {
  7. string s,T;
  8. cin>>s;
  9. cout<<"S:";
  10. cout<<s<<endl;
  11. cout<<"T:";
  12. StrCopy(T,s);
  13. cout<<T;
  14. }
  15. void StrCopy(string &T,string &S)
  16. {
  17. T=S;
  18. }

运行结果如下图所示:

 (2)StrAssign()函数

 代码如下:

  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. StrAssign (string &T,char *s)
  5. {
  6. T=s;
  7. }
  8. int main()
  9. {
  10. string T;
  11. StrAssign(T,"love");
  12. cout<<"T:"<<T;
  13. return 0;
  14. }

运行结果如下图所示:

(3)ClearString()函数

代码:

  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. void Clear(string &S)
  5. {
  6. int i;
  7. for(i=0;S[i]!='\0';i++)
  8. S[i]='\0';
  9. }
  10. int main()
  11. {
  12. string S;
  13. cin>>S;
  14. cout<<"S:"<<S<<endl;
  15. Clear(S);
  16. cout<<S;
  17. return 0;
  18. }

运行结果如下图:

 (4)StringEmpty()函数

代码:

  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. bool StrEmpty(string S)
  5. {
  6. if(S[0]=='\0')
  7. return true;
  8. else
  9. return false;
  10. }
  11. int main()
  12. {
  13. string S;
  14. cin>>S;
  15. cout<<"S:"<<S<<endl;
  16. if(StrEmpty(S))
  17. cout<<"S字符串为空!";
  18. else
  19. cout<<"S字符串不为空!";
  20. return 0;
  21. }

运行结果如下:

(5)StrLength()函数

代码:

  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. int StrLength(string S)
  5. {
  6. int i=0;
  7. while(S[i]!='\0')
  8. i++;
  9. return i;
  10. }
  11. int main()
  12. {
  13. string S;
  14. int n;
  15. cin>>S;
  16. cout<<"S:"<<S<<endl;
  17. n=StrLength(S);
  18. cout<<"S的长度为:"<<n;
  19. return 0;
  20. }

运行结果如下:

 (6)StrCompare()函数

代码:

  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. int StrCompare(string S,string T)
  5. {
  6. int i=0;
  7. while(S[i]!='\0'&&T[i]!='\0')
  8. {
  9. if(S[i]>T[i])
  10. return 1;
  11. else if(S[i]<T[i])
  12. return -1;
  13. i++;
  14. }
  15. if(S[i]!='\0')
  16. return 1;
  17. if(T[i]!='\0')
  18. return -1;
  19. if(T[i]=='\0'&&S[i]=='\0')
  20. return 0;
  21. }
  22. int main()
  23. {
  24. string S,T;
  25. int n;
  26. cin>>S>>T;
  27. cout<<"S:"<<S<<endl;
  28. cout<<"T:"<<T<<endl;
  29. n=StrCompare(S,T);
  30. if(n>0)
  31. cout<<"S大于T."<<endl;
  32. else if(n==0)
  33. cout<<"S等于T."<<endl;
  34. else
  35. cout<<"S小于T."<<endl;
  36. return 0;
  37. }

运行结果:

 (7)Concat()函数

在string中有重载的运算符+表示串的连接所以直接使用即可,在字符数组中则用指针赋值的方法先找到一个串的尾部再让第二个串赋值给第一个串尾即可

代码:

  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. void Concat(string &T,string S1,string S2)
  5. {
  6. T=S1+S2;
  7. }
  8. int main()
  9. {
  10. string S1,T,S2;
  11. cin>>S1>>S2;
  12. cout<<"S1:"<<S1<<endl;
  13. cout<<"S2:"<<S2<<endl;
  14. Concat(T,S1,S2);
  15. cout<<"T:"<<T;
  16. return 0;
  17. }
  1. #include<stdio.h>
  2. #define Max 20
  3. char * my_strcat(char * dest,char * src)
  4. {
  5. char *ret = dest; //定义一个ret来保存dest的值;
  6. while(* dest!='\0') //判断是否是第一个字符串数组的元素是否是末尾
  7. {
  8. dest++; //使指向下一个元素
  9. }
  10. * dest = *src; //将第二个数组的首元素赋给‘\0’
  11. while(* src != '\0') //判断是否是第二个字符串数组的元素是否是末尾
  12. {
  13. * dest++ = *src++; //将原来第二个数组中各元素赋给第一个数组
  14. }
  15. *src++ = '\0'; //在最后一个数组后面赋上‘\0’
  16. return ret;
  17. }
  18. int main()
  19. {
  20. char str1[Max]={"lo"};
  21. char str2[Max]={"ve"};
  22. my_strcat(str1,str2); //调用my_stract函数
  23. printf("%s\n",str1);
  24. return 0;
  25. }

运行结果:

 

 (8)SubString()函数

返回主串中pos位置处长度为len的子串到sub中,即将找到的位置加加赋值给sub【0.1.2......n】即可让sub最后一个值为、0意味着字符串结束

代码:

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. void SubString(string &Sub,string S1,int pos,int len)
  5. {
  6. int i=0;
  7. for(i=0;i<len;i++)
  8. {
  9. Sub[i]=S1[pos];
  10. pos++;
  11. }
  12. Sub[i+1]='\0';
  13. }
  14. int main()
  15. {
  16. string S1,T; //要保存子串首先要给T开空间
  17. int pos,len;
  18. T=" ";
  19. cout<<"请输入字符串->";
  20. cin>>S1;
  21. cout<<"请输入位置和长度:";
  22. cin>>pos>>len;
  23. cout<<"S1:"<<S1<<endl;
  24. SubString(T,S1,pos,len);
  25. cout<<"子串:"<<T;
  26. return 0;
  27. }

运行结果:

(9)Index()函数

代码如下:

先计算主串和子串的长度,计算成功后判断位置是否输入正确,正确后从主串pos位置处取出子串长度的字符,再与子串进行判断是否相等,相等则输出位置。

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. void SubString(string &Sub,string S1,int pos,int len)
  5. {
  6. int i=0;
  7. for(i=0;i<len;i++)
  8. {
  9. Sub[i]=S1[pos];
  10. pos++;
  11. }
  12. }
  13. int StrCompare(string S,string T)
  14. {
  15. int i=0;
  16. while(S[i]!='\0'&&T[i]!='\0')
  17. {
  18. if(S[i]>T[i])
  19. return 1;
  20. else if(S[i]<T[i])
  21. return -1;
  22. i++;
  23. }
  24. if(S[i]!='\0')
  25. return 1;
  26. if(T[i]!='\0')
  27. return -1;
  28. if(T[i]=='\0'&&S[i]=='\0')
  29. return 0;
  30. }
  31. int StrLength(string S)
  32. {
  33. int i=0;
  34. while(S[i]!='\0')
  35. i++;
  36. return i;
  37. }
  38. int Index(string S,string T,int pos)
  39. {
  40. int n,m,i;
  41. string sub;
  42. n=StrLength(S);
  43. m=StrLength(T);
  44. i=pos;
  45. cout<<"子串中有"<<m<<"个数据\n";
  46. cout<<"请给sub开辟与子串相同的空间输入任意字符即可\n";
  47. cin>>sub;
  48. if(pos>0)
  49. {
  50. while(i<n-m+1)
  51. {
  52. SubString(sub,S,i,m);
  53. if(StrCompare(sub,T)!=0)
  54. i++;
  55. else
  56. return i;
  57. }
  58. }
  59. return 0;
  60. }
  61. int main()
  62. {
  63. string S1,T; //要保存子串首先要给T开空间
  64. int pos,i;
  65. cout<<"请输入字符串->";
  66. cin>>S1>>T;
  67. cout<<"请输入位置:";
  68. cin>>pos;
  69. i=Index(S1,T,pos);
  70. if (i!=0)
  71. cout<<"找到子串的位置为:"<<i;
  72. else
  73. cout<<"没有找到子串!";
  74. return 0;
  75. }

运行结果如下:

(10)Replace()函数

代码如下所示:

 不包含删除函数的替代函数,只可以替换与子串相同长度的串,有了删除函数则可以进行先删除后进行替换也就是插入某个位置即可。

  1. #include<iostream>
  2. using namespace std;
  3. void SubString(string &Sub,string S1,int pos,int len)
  4. {
  5. int i=0;
  6. for(i=0;i<len;i++)
  7. {
  8. Sub[i]=S1[pos];
  9. pos++;
  10. }
  11. }
  12. int StrCompare(string S,string T)
  13. {
  14. int i=0;
  15. while(S[i]!='\0'&&T[i]!='\0')
  16. {
  17. if(S[i]>T[i])
  18. return 1;
  19. else if(S[i]<T[i])
  20. return -1;
  21. i++;
  22. }
  23. if(S[i]!='\0')
  24. return 1;
  25. if(T[i]!='\0')
  26. return -1;
  27. if(T[i]=='\0'&&S[i]=='\0')
  28. return 0;
  29. }
  30. int StrLength(string S)
  31. {
  32. int i=0;
  33. while(S[i]!='\0')
  34. i++;
  35. return i;
  36. }
  37. int Index(string S,string T,int pos)
  38. {
  39. int n,m,i;
  40. string sub;
  41. n=StrLength(S);
  42. m=StrLength(T);
  43. i=pos;
  44. cout<<"子串中有"<<m<<"个数据\n";
  45. cout<<"请给sub开辟与子串相同的空间输入任意字符即可\n";
  46. cin>>sub;
  47. if(pos>=0)
  48. {
  49. while(i<n-m+1)
  50. {
  51. SubString(sub,S,i,m);
  52. if(StrCompare(sub,T)!=0)
  53. i++;
  54. else
  55. return i;
  56. }
  57. }
  58. return 0;
  59. }
  60. void Replace(string &S,string T,string V)
  61. {
  62. int i,n;
  63. i=Index(S,T,0);
  64. n=StrLength(V);
  65. int j=i,k=0;
  66. while(k<n)
  67. {
  68. S[j]=V[k];
  69. j++;
  70. k++;
  71. }
  72. }
  73. int getlen(string V,string T)
  74. {
  75. int n=V.size();
  76. int m=T.size();
  77. return n-m;
  78. }
  79. int main()
  80. {
  81. string S1,T,V; //要保存子串首先要给T开空间
  82. int pos,i;
  83. cout<<"请输入字符串->";
  84. cin>>S1;
  85. cout<<"请输入子串:";
  86. cin>>T;
  87. cout<<"请输入替代的串:";
  88. cin>>V;
  89. if(n>0)
  90. {
  91. while(n--)
  92. S1+='1';
  93. }
  94. Replace(S1,T,V);
  95. cout<<S1;
  96. return 0;
  97. }
  1. #include <iostream>
  2. using namespace std;
  3. int StrLength(string S)
  4. {
  5. int i=0;
  6. while(S[i]!='\0')
  7. i++;
  8. return i;
  9. }
  10. int StrCompare(string S,string T)
  11. {
  12. int i=0;
  13. while(S[i]!='\0'&&T[i]!='\0')
  14. {
  15. if(S[i]>T[i])
  16. return 1;
  17. else if(S[i]<T[i])
  18. return -1;
  19. i++;
  20. }
  21. if(S[i]!='\0')
  22. return 1;
  23. if(T[i]!='\0')
  24. return -1;
  25. if(T[i]=='\0'&&S[i]=='\0')
  26. return 0;
  27. }
  28. void SubString(string &Sub,string S1,int pos,int len)
  29. {
  30. int i=0;
  31. for(i=0;i<len;i++)
  32. {
  33. Sub[i]=S1[pos];
  34. pos++;
  35. }
  36. }
  37. int Index(string S,string T,int pos)
  38. {
  39. int n,m,i;
  40. string sub;
  41. n=StrLength(S);
  42. m=StrLength(T);
  43. i=pos;
  44. cout<<"子串中有"<<m<<"个数据\n";
  45. cout<<"请给sub开辟与子串相同的空间输入任意字符即可\n";
  46. cin>>sub;
  47. if(pos>=0)
  48. {
  49. while(i<n-m+1)
  50. {
  51. SubString(sub,S,i,m);
  52. if(StrCompare(sub,T)!=0)
  53. i++;
  54. else
  55. return i;
  56. }
  57. }
  58. return 0;
  59. }
  60. void StrInsert(string &S,int pos,string T)
  61. {
  62. int n,m,i=0;
  63. n=StrLength(T);
  64. for(int j=pos;j<pos+n;j++)
  65. {
  66. S[j+n]=S[j];
  67. }
  68. while(i<n)
  69. {
  70. S[pos]=T[i];
  71. i++;
  72. pos++;
  73. }
  74. }
  75. void StrDelete(string &S,int pos,int len)
  76. {
  77. int i,m;
  78. m=StrLength(S);
  79. for(i=pos;i<m-len+1;i++)
  80. {
  81. S[i]=S[i+len];
  82. S[i+len]=' ';
  83. }
  84. }
  85. void Replace(string &S,string T,string V)
  86. {
  87. int i,n,m;
  88. m=StrLength(T);
  89. i=Index(S,T,0);
  90. StrDelete(S,i,m);
  91. StrInsert(S,i,V);
  92. }
  93. int getlen(string V,string T)
  94. {
  95. int n=V.size();
  96. int m=T.size();
  97. return n-m;
  98. }
  99. int main()
  100. {
  101. string S1,T,V; //要保存子串首先要给T开空间
  102. int pos,i,n;
  103. cout<<"请输入字符串->";
  104. cin>>S1;
  105. cout<<"请输入子串:";
  106. cin>>T;
  107. cout<<"请输入替代的串:";
  108. cin>>V;
  109. n=getlen(V,T);
  110. if(n>0)
  111. {
  112. while(n--)
  113. S1+='1';
  114. }
  115. Replace(S1,T,V);
  116. cout<<S1;
  117. return 0;
  118. }

运行结果如下:

(11) StrInsert(S,pos,T)函数

该函数需要给string预留足够多的空间来保存T长度的后移要不然就相当于用子串将其替代了,相对如此可以使用字符数组来使用更简单一点。也可以直接给串开辟足够大的空间

代码如下:

  1. #incldue<iostream>
  2. using namespace std;
  3. int StrLength(string S)
  4. {
  5. int i=0;
  6. while(S[i]!='\0')
  7. i++;
  8. return i;
  9. }
  10. void StrInsert(string &S,int pos,string T)
  11. {
  12. int n,m,i=0;
  13. n=StrLength(T);
  14. for(int j=pos;j<pos+n;j++) //后移目标存储的位置
  15. {
  16. S[j+n]=S[j];
  17. }
  18. while(i<n) //给目标位置赋值
  19. {
  20. S[pos]=T[i];
  21. i++;
  22. pos++;
  23. }
  24. }
  25. int main()
  26. {
  27. string S1,T; //要保存子串首先要给T开空间
  28. int pos;
  29. cout<<"请输入子串:";
  30. cin>>T;
  31. cout<<"子串长度为"<<StrLength(T)<<"请预留位置\n";
  32. cout<<"请输入字符串:";
  33. cin>>S1;
  34. cout<<"请输入要插入的位置:";
  35. cin>>pos;
  36. StrInsert(S1,pos,T);
  37. cout<<S1;
  38. return 0;
  39. }

运行的结果如下所示:

 (12)StrDelete(S,pos,len)的函数

计算S串的长度然后用长度减去要删除的长度+1为后续要前移的元素,然后将后面的元素进行前移操作,再将原来后面的元素置为空即可,字符串的做法与其类似只不过不用置空直接将长度减去要删除的长度即可。

代码如下:

  1. #include<iostream>
  2. using namespace std;
  3. int StrLength(string S)
  4. {
  5. int i=0;
  6. while(S[i]!='\0')
  7. i++;
  8. return i;
  9. }
  10. void StrDelete(string &S,int pos,int len)
  11. {
  12. int i,m;
  13. m=StrLength(S);
  14. for(i=pos;i<m-len+1;i++)
  15. {
  16. S[i]=S[i+len];
  17. S[i+len]=' '; //将后面元素置空
  18. }
  19. }
  20. int main()
  21. {
  22. string S1;
  23. int pos,len;
  24. cout<<"请输入字符串:";
  25. cin>>S1;
  26. cout<<"请按顺序输入删除位置与删除长度:";
  27. cin>>pos>>len;
  28. StrDelete(S1,pos,len);
  29. cout<<S1;
  30. return 0;
  31. }

运行结果如图所示:

 1.4朴素的模式匹配算法

子串的定位操作通常被称为串的模式匹配。

简单来说就是对主串的每一个字符作为子串的开头,要与匹配的字符进行匹配。对主串做大循环,每个字符开头做T的长度的小循环,直到匹配完成或者全部遍历完成为止。

我先把代码放在下面以及运行结果:

代码如下:

  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. int StrLength(char *S)
  5. {
  6. int i=0;
  7. while(S[i]!='\0')
  8. i++;
  9. return i;
  10. }
  11. int Index(char *S,char *T,int pos)
  12. {
  13. int i=pos;
  14. int j=0,m,n;
  15. m=StrLength(S);
  16. n=StrLength(T);
  17. while(i<=m&&j<n)
  18. {
  19. if(S[i]==T[j]) //相同则同时进行后移
  20. {
  21. ++i;
  22. ++j;
  23. }
  24. else //不相同且未超过子串长度则返回主串下一个位置,子串置0
  25. {
  26. i=i-j+1;
  27. j=0;
  28. }
  29. }
  30. if(j>=n)
  31. return i-n; //返回在主串中的位置即主串匹配完成的位置减去子串长度
  32. else
  33. return 0;
  34. }
  35. int main()
  36. {
  37. char S[Max],T[Max];
  38. int pos,i;
  39. cout<<"请输入字符串:";
  40. cin>>S;
  41. cout<<"请输入要查找的子串:";
  42. cin>>T;
  43. cout<<"请输入要开始查找的位置:";
  44. cin>>pos;
  45. i=Index(S,T,pos);
  46. cout<<"返回的位置为:"<<i;
  47. return 0;
  48. }

 运算过程如下:

 1.5KMP模式匹配算法

1.5.1匹配算法原理

按照朴素模式匹配算法,应该是如上图(2),(3),(4),(5),(6)。即主串S中当i=2、3、4、5、6时,首字符与子串T的首字符均不相等。因为子串中首字符a不与其子串后的任意一个元素相等所以只运行(1),(6)即可,因此我们要提高该算法的匹配能力则需要记录子串中是否存在相同的字符。KMP模式匹配算法就是为了让这没必要的回溯不发生。由上图可以看出j的大小取决于当前字符之前的串的前后缀的相似度。即可以用一个数组来保存其回溯的位置。

1.5.2next数组值的推导

具体如何推导出一个串的next的数组值呢?我们看看如下几个例子再进行总结 。

如果前后缀的字符相同则next数组在原本的数值基础上+1,如果不相同则进行回溯操作直到找到相同的数值,然后令其next的数值+1等于当前next的值。

 1.5.3KMP算法实现

代码如下所示:

  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. int StrLength(char *S)
  5. {
  6. int i=0;
  7. while(S[i]!='\0')
  8. i++;
  9. return i;
  10. }
  11. void get_next(char *T,int *next)
  12. {
  13. int i,k;
  14. i=1;
  15. k=0;
  16. next[1]=0;
  17. while(i<StrLength(T)) //直到大于等于长度退出
  18. {
  19. //k=0时是第一个字符所以先给其分配数值=1,相当于字符前现只有一个值
  20. if(k==0||T[i]==T[k]) //如果相同则回溯时可以一个一个与前串相比
  21. {
  22. ++i;
  23. ++k;
  24. next[i]=k; //相同则加一即可 可理解为在子串中回溯的位置靠后一个
  25. }
  26. else
  27. k=next[k]; //如果与当前的字符不同则可以在于前一个字符进行比较,如果相同则next的数组值+1
  28. }
  29. }
  30. int Index(char *S,char *T,int pos)
  31. {
  32. int i=pos;
  33. int j=1,m,n;
  34. m=StrLength(S);
  35. n=StrLength(T);
  36. int next[Max];
  37. get_next(T,next);
  38. while(i<=m&&j<n)
  39. {
  40. if(j==0||S[i]==T[j])
  41. {
  42. ++i;
  43. ++j;
  44. }
  45. else //回溯到应该的位置
  46. j=next[j];
  47. }
  48. if(j>=n)
  49. return i-n;
  50. else
  51. return 0;
  52. }
  53. int main()
  54. {
  55. char S[Max],T[Max];
  56. int pos,i,next[Max];
  57. cout<<"请输入字符串:";
  58. cin>>S;
  59. cout<<"请输入要查找的子串:";
  60. cin>>T;
  61. cout<<"请输入要开始查找的位置:";
  62. cin>>pos;
  63. i=Index(S,T,pos);
  64. cout<<"返回的位置为:"<<i;
  65. return 0;
  66. }

运行的结果如下图所示:

 1.5.4KMP算法改进

对next数组的寻找合适位置进行了改进,如果字符串中前缀中相同字符太多,则会减慢运行速度,因为next的值仍然会让其一个一个回溯一个已经不匹配的字符,因此我们要在其基础上进行改进。

代码如下 所示:

  1. #include<iostream>
  2. using namespace std;
  3. #define Max 20
  4. int StrLength(char *S)
  5. {
  6. int i=0;
  7. while(S[i]!='\0')
  8. i++;
  9. return i;
  10. }
  11. void get_nextval(char *T,int *nextval)
  12. {
  13. int i,k;
  14. i=1;
  15. k=0;
  16. nextval[1]=0;
  17. while(i<StrLength(T)) //直到大于等于长度退出
  18. {
  19. //k=0时是第一个字符所以先给其分配数值=1,相当于字符前现只有一个值
  20. if(k==0||T[i]==T[k]) //如果相同则回溯时可以一个一个与前串相比
  21. {
  22. ++i;
  23. ++k;
  24. if(T[i]!=T[k]) //如果串的字符不相等即以及回溯到了顶端时让其+1且赋给nextval
  25. nextval[i]=k;
  26. else
  27. nextval[i]=nextval[k];
  28. }
  29. else
  30. k=nextval[k]; //如果与当前的字符不同则可以在于前一个字符进行比较,如果相同则next的数组值+1
  31. }
  32. }
  33. int Index(char *S,char *T,int pos)
  34. {
  35. int i=pos;
  36. int j=1,m,n;
  37. m=StrLength(S);
  38. n=StrLength(T);
  39. int nextval[Max];
  40. get_nextval(T,nextval);
  41. while(i<=m&&j<n)
  42. {
  43. if(j==0||S[i]==T[j])
  44. {
  45. ++i;
  46. ++j;
  47. }
  48. else //回溯到应该的位置
  49. j=nextval[j];
  50. }
  51. if(j>=n)
  52. return i-n;
  53. else
  54. return 0;
  55. }
  56. int main()
  57. {
  58. char S[Max],T[Max];
  59. int pos,i,next[Max];
  60. cout<<"请输入字符串:";
  61. cin>>S;
  62. cout<<"请输入要查找的子串:";
  63. cin>>T;
  64. cout<<"请输入要开始查找的位置:";
  65. cin>>pos;
  66. i=Index(S,T,pos);
  67. cout<<"返回的位置为:"<<i;
  68. return 0;
  69. }

运行的结果如下图:

这篇博客是应某个人的要求书写的,同时也提供给大家观看,其中包含了很多详细的代码,不理解的部分可以在评论区找我,或者V我我都会进行解答,希望看完这篇博客大家都有所收获!

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号