| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 526 人关注过本帖
标题:我有一个作业不会做了,请你们帮帮忙!
只看楼主 加入收藏
hu280468593
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-11-16
结帖率:0
收藏
已结贴  问题点数:10 回复次数:3 
我有一个作业不会做了,请你们帮帮忙!
实验内容:
比较BF算法和KMP算法的优劣

实验基本要求:
1.采用定长顺序显示表示串长的结构来存储串(结构定义见课件第15张幻灯片)

2.实现BF算法(即朴素算法),BF函数头如下:(参见课件第21张幻灯片)
int Index_BF(SString S, SString T) {}// 返回子串T在主串S中第0个字符之后的位置。若不存在,返回-1

3.实现getnext函数和kmp算法函数,参见课件第38和第45张幻灯片,函数头分别如下:
int Index_KMP(SString S, SString T){}//功能同Index_BF
void get_next(SString T, int next[]){}

4,编写主函数,分别定义SSsting类型的两个变量,并通过键盘输入串值,并为串设置串长,并分别验证两种匹配算法。

实验附加要求:
1,修改以上的两种算法函数,使之可以统计字符的比较次数,并最终输出类似以下信息:
主串长??,模式串长??
BF算法,比较次数为??
KMP算法,比较次数为??其中求next值比较的次数为??

2,编写一个过滤函数函数头如下,将字符串中的所有指定子串删除,如果不存在指定子串,则函数返回0,否则返回删除的子串数。并在主函数中进行测试。
int str_del(SString &S, SString T){}//删除S中的所有T子串,如S为”abcdabcdefa”,T为“abc”,则函数的返回为2,S变为“ddefa”

3,编写一个替换函数,将字符串中的指定子串用另外一个子串进行替换,如果不存在指定子串,则函数返回0,否则返回替换的子串数。并在主函数中进行测试。
int str_rep(SString &S, SString T, String P){}//将S中的所有T子串替换为P,如S为”abcdabcdefa”,T为“abc”,P为“dc”则函数的返回为2,S变为“dcddcdefa”

其中第一个我的源程序为:(有错误,但我不知道怎么改)
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

#define MAXSTRKEN 20

int k_BF=0,k_KMP=0,k_next=0;

typedef struct
{
    char ch[MAXSTRKEN];
    int length;
}SString;  

void get_next(SString T, int next[])
{
    int j = 0,i = -1;

    next[0] = -1;     
   
    while (j<T.length)
    {            
        if ((i==-1) || T.ch[i] == T.ch[j])
        {
            ++i;
            ++j;
            next[j]=i;
        }
        else
            i = next[i];

        k_next++;
    }
}

int Index_BF(SString S, SString T)
{
    int i=0,j=0;
    while (i <= S.length-1 && j <= T.length-1)
    {
      if (S.ch[i] == T.ch[j])
      {
          ++i;  
          ++j;
      }
      else
      {
          i = i-j+1;   
          j = 0;
      }

      k_BF++;

    }

   if (j =T.length)  
       return  i-T.length;
   else
       return -1;
}
                              
int Index_KMP(SString S, SString T)
 {
     int i = 0,j = 0;

     while (i <= S.length -1 && j <= T.length -1)
    {
         if (j == -1 || S.ch[i] == T.ch[j])
         {
             ++i;  
             ++j;
         }                              
         else  
             j = next[j];
     }

     k_KMP++;

    if (j =T.length)  
        return  i-T.length;
    else
        return 0;
} // Index_KMP

 int main()
 {
     int i,j;
     SString S,T;

     S.length=0;
     T.length=0;

     cout<<"&Ccedil;&euml;&Ecirc;&auml;&Egrave;&euml;&Ouml;÷&acute;&reg;&pound;&ordm;S=";
     while(S.ch[i]=getchar()!='\n')
     {
         i++;
         S.length++;
     }

     cout<<"&Ccedil;&euml;&Ecirc;&auml;&Egrave;&euml;&Auml;&pound;&Ecirc;&frac12;&acute;&reg;&pound;&ordm;T=";
     while(T.ch[i]=getchar()!='\n')
     {
         i++;
         T.length++;
     }

     Index_BF(S,T);
     Index_KMP(S,T);

     cout<<"主串长:"<<strlen(S.ch)<<endl;
     cout<<"模式串长:"<<strlen(T.ch)<<endl;
     cout<<"BF算法比较的次数:"<<k_BF<<endl;
     cout<<"KMP比较的次数:"<<k_KMP*k_next<<endl;
     cout<<"next比较的次数:"<<k_next<<endl;
 }
搜索更多相关主题的帖子: 作业 
2010-11-16 23:48
MrBluer
Rank: 4
等 级:业余侠客
威 望:1
帖 子:120
专家分:263
注 册:2010-10-23
收藏
得分:5 
(⊙o⊙)…好难啊,帮不了你
2010-11-17 18:57
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:5 
确实不简单

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2010-11-17 21:46
hu280468593
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-11-16
收藏
得分:0 
不过,我还是谢谢你们,我查出错了,正确的代码为:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

#define MAXSTRKEN 20

int k_BF=0,k_KMP=0,k_next=0;

typedef struct
{
    char ch[MAXSTRKEN];
    int length;
}SString;  

void get_next(SString T, int next[])
{
    int j = 0,i = -1;

    next[0] = -1;     
   
    while (j<T.length)
    {            
        if ((i==-1) || T.ch[i] == T.ch[j])
        {
            ++i;
            ++j;
            next[j]=i;
        }
        else
            i = next[i];

        k_next++;
        k_KMP++;
    }
}

int Index_BF(SString S, SString T)
{
    int i=0,j=0;

    while (i <= S.length-1 && j <= T.length-1)
    {
      if (S.ch[i] == T.ch[j])
      {
          ++i;  
          ++j;
      }
      else
      {
          i = i-j+1;   
          j = 0;
      }

      k_BF++;

    }

   if (j ==T.length)  
       return  i-j;
   else
       return -1;
} // Index`   (朴素算法)                              


 int Index_KMP(SString S, SString T)
 {
     int i = 0,j = 0,next[20];

     get_next(T,next);

     while (i <= S.length -1 && j <= T.length -1)
    {
         if (j == -1 || S.ch[i] == T.ch[j])
         {
             ++i;  
             ++j;
         }                              
         else  
             j = next[j];

         k_KMP++;
     }


    if (j ==T.length)  
        return  i-j;
    else
        return -1;
} // Index_KMP

 void main()
 {
     int i=0,j=0,N_BF,N_KMP;
     SString S,T;

     S.length=0;
     T.length=0;

     cout<<"请输入主串:S=";
     gets(S.ch);
     S.length=strlen(S.ch);
     
     cout<<"请输入模式串:T=";
     gets(T.ch);
     T.length=strlen(T.ch);

     N_BF=Index_BF(S,T);
     N_KMP=Index_KMP(S,T);

     cout<<"主串长:"<<S.length<<endl;
     cout<<"模式串长:"<<T.length<<endl;
     if(N_BF!=-1&&N_KMP!=-1)
     {
         cout<<"BF算法比较次数为:"<<k_BF<<endl;
         cout<<"KMP算法比较次数为:"<<k_KMP<<endl;
         cout<<"next比较次数为:"<<k_next<<endl;
         cout<<"模式串出现的位置为:"<<N_BF<<endl;
         cout<<"模式串出现的位置为:"<<N_KMP<<endl;
     }
     else
         cout<<"模式串不是主串里面的子串!"<<endl;
     
     system("pause");
 }
2010-11-17 22:48
快速回复:我有一个作业不会做了,请你们帮帮忙!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019423 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved