沈阳师范大学-PTA-数据结构-串的模式匹配

沈阳师范大学 同时被 2 个专栏收录
37 篇文章 6 订阅
51 篇文章 4 订阅

7-3 串的模式匹配 (10分)
给定一个主串S(长度<=106)和一个模式T(长度<=105),要求在主串S中找出与模式T相匹配的子串,返回相匹配的子串中的第一个字符在主串S中出现的位置。

输入格式:
输入有两行: 第一行是主串S; 第二行是模式T.

输出格式:
输出相匹配的子串中的第一个字符在主串S中出现的位置。若匹配失败,输出0.

输入样例:
在这里给出一组输入。例如:

aaaaaba
ba
输出样例:
在这里给出相应的输出。例如:

6

#include <iostream>
#include <string>

typedef struct MyString* StrPoint;
struct MyString{
    char data;
    StrPoint Link;
};
class Array1{
private:
    StrPoint Front,Rear;
    int Length,MaxInit,MaxLength;
public:
    Array1(){
        this->Length=0;
        this->Front=(StrPoint)malloc(sizeof(struct MyString));
        this->Rear=Front;
        this->Front->Link=NULL;
        this->Length=0;
    }
    void FindMaxSequence(){
        if(this->Front->Link==NULL){
            std::cout<<"NULL\n";
            return;
        }
        StrPoint InitP=this->Front;
        this->MaxInit=0;
        this->MaxLength=0;
        int TempInitNum=0;
        int i=0;
        for(i=this->MaxInit;i<this->Length;i++){
            if(InitP->data>=48&&InitP->data<=57){
                TempInitNum++;
                if(TempInitNum>this->MaxLength){
                    this->MaxLength=TempInitNum;
                    this->MaxInit=i-TempInitNum;
                }
            } else{
                TempInitNum=0;
            }
            InitP=InitP->Link;
        }
        std::cout<<this->MaxInit+1<<" "<<this->MaxLength<<"\n";
    }
    StrPoint InputString(){
        char ch1;
        ch1=getchar();
        while(ch1!='\n'){
            this->Rear->data=ch1;
            StrPoint NewStrP=(StrPoint)malloc(sizeof(struct MyString));
            NewStrP->Link=NULL;
            this->Rear->Link=NewStrP;
            this->Rear=NewStrP;
            ch1=getchar();
            this->Length++;
        }
        return this->Front;
    }
    void StrOutPutString(StrPoint P){
        while (P->Link!=NULL){
            std::cout<<P->data<<" \n";
            P=P->Link;
        }
    }
    int GetLength(){
        return this->Length;
    }
    StrPoint GetFrontPoint(){
        return this->Front;
    }
};
class SequenceSaver{
public:
    StrPoint P1;
    StrPoint P2;
    StrPoint OrinPoint;
    StrPoint RearPoint;
    int num;
    int StrNum;
    SequenceSaver(){
        P1=(StrPoint)malloc(sizeof(struct MyString));
        P2=(StrPoint)malloc(sizeof(struct MyString));
        P1->Link=NULL;
        P2->Link=NULL;
        StrNum=0;
    }
    void PrintNode(){
        if(this->StrNum==0){
            std::cout<<"0";
            return;
        }
        std::cout<<this->num<<" "<<P1->data;
    }
};
void FindMaxSequence(Array1* arr1,Array1* arr2){
    Array1* TempPmin=(arr1->GetLength()<arr2->GetLength())?arr1:arr2;
    Array1* TempPmax=(arr1->GetLength()>arr2->GetLength())?arr1:arr2;
    StrPoint StrMin=TempPmin->GetFrontPoint();
    StrPoint StrMax=TempPmax->GetFrontPoint();
    SequenceSaver* SePointer=new SequenceSaver();
    SePointer->StrNum=0;
    for(;StrMin->Link!=NULL;StrMin=StrMin->Link){
        int TempNum=0;
        for(StrPoint StrMax2=StrMax;StrMax2->Link!=NULL;StrMax2=StrMax2->Link){
            StrPoint StrMinCopy=StrMin;
            StrPoint StrMaxCopy=StrMax2;
            StrPoint OrinPoint;
            int StrNum=0;
            TempNum++;
            while (StrMaxCopy->data==StrMinCopy->data){
                StrNum++;
                StrMaxCopy=StrMaxCopy->Link;
                StrMinCopy=StrMinCopy->Link;
                OrinPoint=StrMaxCopy;
                if((StrMaxCopy==NULL&&StrMinCopy==NULL)||(StrMaxCopy->data!=StrMinCopy->data)){
                    if(StrNum>SePointer->StrNum){
                        SePointer->StrNum=StrNum;
                        SePointer->P1=StrMin;
                        SePointer->P2=StrMinCopy;
                        SePointer->num=TempNum;
                    }
                    break;
                }
            }
        }
    }
    SePointer->PrintNode();
}
char GetChar(StrPoint P,int num){
    StrPoint CopyP=P;
    for(int i=0;i<num;i++){
        if(CopyP!=NULL){
            //std::cout<<CopyP->data<<"\n";
            CopyP=CopyP->Link;
        } else{
            std::cout<<"NULL!\n";
            abort();
        }
    }
    return CopyP->data;
}
void Cal_Next(StrPoint Ptr,int *Next,int PtrLen){
    Next[0]=-1;
    int k=-1;
    StrPoint PtrLenCopy=Ptr;
    for(int q=1;q<PtrLen-1;q++){
        while (k>-1&&GetChar(PtrLenCopy,k+1)!=GetChar(PtrLenCopy,q)){
            k=Next[k];
        }
        if(GetChar(PtrLenCopy,k+1)==GetChar(PtrLenCopy,q)){
            k=k+1;
        }
        Next[q]=k;
    }
}
int KMP(StrPoint Str,int StrLen,StrPoint Ptr,int PtrLen){
    StrPoint Str1=Str;
    StrPoint Ptr1=Ptr;
    int* Next=new int[PtrLen];
    Cal_Next(Ptr1,Next,PtrLen);
    int k=-1;
    for(int i=0;i<StrLen;i++){
        while (k>-1&&GetChar(Ptr,k+1)!=GetChar(Str,i)){
            k=Next[k];
        }
        if(GetChar(Ptr,k+1)==GetChar(Str,i)){
            k=k+1;
        }
        if(k==PtrLen-1){
            return i-PtrLen+2;
        }
    }
    return -1;
}
void Cal_Next2(char *Ptr,int *Next,int PtrLen){
    Next[0]=-1;
    int k=-1;
    for(int q=1;q<PtrLen-1;q++){
        while (k>-1&&Ptr[k+1]!=Ptr[q]){
            k=Next[k];
        }
        if(Ptr[k+1]==Ptr[q]){
            k=k+1;
        }
        Next[q]=k;
    }
}
int KMP2(char *Str,int StrLen,char *Ptr,int PtrLen){
    int* Next=new int[PtrLen];
    Cal_Next2(Str,Next,PtrLen);
    int k=-1;
    for(int i=0;i<StrLen;i++){
        while (k>-1&&Ptr[k+1]!=Str[i]){
            k=Next[k];
        }
        if(Ptr[k+1]==Str[i]){
            k=k+1;
        }
        if(k==PtrLen-1){
            return i-PtrLen+2;
        }
    }
    return 0;
}
int main() {
//    Array1* arr1=new Array1();
//    Array1* arr2=new Array1();
//    arr2->InputString();
//    arr1->InputString();
//    std::cout<<KMP(arr2->GetFrontPoint(),arr2->GetLength(),arr1->GetFrontPoint(),arr1->GetLength());
    std::string s1="";
    std::string s2="";
    char *sp1,*sp2;
    std::getline(std::cin,s1);
    std::getline(std::cin,s2);
    sp1=const_cast<char*>(s1.c_str());
    sp2=const_cast<char*>(s2.c_str());
    std::cout<<KMP2(sp1,s1.length(),sp2,s2.length());
}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值