题目详情

阅读下列说明和C代码,回答问题1至问题3,将解答写在答题纸的对应栏内。

【说明】 模式匹配是指给定主串t和子串s,在主串t中寻找子串s的过程,其中s称为模式。如果匹配成功,返回s在t中的位置,否则返回-1 。

KMP算法用next数组对匹配过程进行了优化。KMP算法的伪代码描述如下: 1.在串t和串s中,分别设比较的起始下标i=j=0。 2.如果串t和串s都还有字符,则循环执行下列操作: (1)如果j=-l或者t[i]=s[j],则将i和j分别加1,继续比较t和s的下一个字符; (2)否则,将j向右滑动到next[j]的位置,即j =next[j]。 3.如果s中所有字符均已比较完毕,则返回匹配的起始位置(从1开始);否则返回-1. 其中,next数组根据子串s求解。求解next数组的代码已由get_next函数给出。【C代码】

(1)常量和变量说明

t,s:长度为悯铂Is的字符串

next:next数组,长度为Is

(2)C程序

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

/*求next[]的值*/

void get_next( int *next, char *s, int Is)

{

int i=0,j=-1;

next[0]=-1;/*初始化next[0]*/

while(i < ls){/*还有字符*/

if(j==-1l

ls[i]==s[j]){/*匹配*/

j++;

i++;

if( s[i]==s[j])

next[i]

= next[j];

else

Next[i]

= j;

}

else

j = next[j];

}

}

int kmp( int *next, char *t ,char *s, int

lt, int Is )

{

Int i=

0,j =0 ;

while

(i < lt && (1) ){

if(

j==-1 || (2) ){

i

++ ;

j

++ ;

}

else

(3) ;

}

if (j >= ls)

return (4) ;

else

return -1;

} 【问题1】(8分)

根据题干说明,填充C代码中的空(1)~(4).

【问题2】(2分)

根据题干说明和C代码,分析出kmp算法的时间复杂度为(5)(主串和子串的长度分别为It和Is,用O符号表示)。

【问题3】(5分)

根据C代码,字符串“BBABBCAC”的next数组元素值为(6)(直接写素值,之间用逗号隔开)。若主串为“AABBCBBABBCACCD”,子串为“BBABBCAC”,则函数Kmp的返回值是(7)。

正确答案及解析

正确答案
解析

【问题1】(8分)答:(1):j<ls; (2):t[i]==s[j]; (3):get_next(next, s, ls); j=next[j]; (4):i+1-ls;

【问题2】(2分)答:(5)O(ls+lt)

【问题3】(5分)答:(6)[-1, -1,1, -1, -1, 2, 0, 0](7)6

你可能感兴趣的试题

单选题

Advancements in ( )have contributed to the growth of the automotive industry through the creation and evolution of self-driving vehicles.

  • A.Artificial Intelligence
  • B.Cloud Computing
  • C.Internet of Things
  • D.Big Data
查看答案
单选题

In project human resource management , ( )is not a source of power for the project manager.

  • A.referent power
  • B.expert power
  • C.reward power
  • D.audit power
查看答案
单选题

At the project establishment stage , the feasibility study mainly includes techinical feasibility analysis , ( ), operation environment feasibility analysis and other aspects of feasibility analysis.

  • A.detail feasibility analysis
  • B.opportunity analysis
  • C.economic feasibility analysis
  • D.risk analysis
查看答案
单选题

( )is a grid that shows the project resources assigned to each work package.

  • A.Stakeholder engagement assessment matrix
  • B.Requirements traceability matrix
  • C.Probability and impact matrix
  • D.Responsibility assignment matrix
查看答案
单选题

Xinhua News Agency reported in January 2022,Chian will further promote the developmet of a digital economy during the 14th Five-Year Plan eriod(2021-2025). The plan also emphasized industrial ( )transformation.

  • A.digital
  • B.networking
  • C.intelligentize
  • D.informatization
查看答案

相关题库更多 +