最大重复字串(mss)&& 二叉树(binary tree)

本文介绍了一个寻找字符串中最大重复子串的算法,并实现了一个从字符串表达式构建二叉树的过程。通过具体代码示例展示了如何确定最长重复序列的位置与长度,以及如何解析字符串来创建对应的二叉树结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream> using namespace std; const int SIZE=100; struct sqString{ char ch[SIZE]; int len; }; sqString * MSS(sqString s){ sqString *sp; int index=0,length=0,length1,i=0,j,k; while(i<s.len){ j=i+1; while(j<s.len){ if(s.ch[i]==s.ch[j]){ length1=1; for(k=1;s.ch[i+k]==s.ch[j+k];++k) length1++; if(length1>length){ index=i; length=length1; } j+=length1; //j++; //j++ 当然对 但是就算可以匹配也不是最大串了 } else j++; } i++; } sp=(sqString *)malloc(sizeof(sqString)); sp->len=length; for(int i=0;i<length;++i) sp->ch[i]=s.ch[index+i]; cout<<index<<endl; return sp; } void assign (sqString &s, char t[]){ int i=0; while(t[i]){ s.ch[i]=t[i]; ++i; } s.len=i; } void display(sqString s){ for(int i=0;i<s.len;++i) cout<<s.ch[i]; cout<<endl; } int main(){ char str[SIZE]; sqString s,*sp; cin>>str; assign(s,str); display(s); sp=MSS(s); cout<<"original:"<<endl; display(s); cout<<"MSS"<<endl; display(* sp); } //a(b(d,e(h(j,k(l,m(,n))))),c(f,g(,i))) #include<iostream> using namespace std; const int SIZE=100; struct tnode{ char data; tnode *lchild,*rchild; }; void createBtree(tnode * &bt,char *str){ tnode* st[SIZE],*p=NULL; int top=-1,k,j=0; char ch; bt=NULL; ch=str[j]; while(ch){ switch(ch){ case '(':top++;st[top]=p;k=1; break; case ')':top--; break; case ',':k=2;break; default: p=(tnode *)malloc(sizeof(tnode)); p->data=ch;p->lchild=p->rchild=NULL; if(bt==NULL) bt=p; else{ switch(k){ case 1:st[top]->lchild=p;break; case 2:st[top]->rchild=p;break; } } } j++; ch=str[j]; } } void display(tnode * bt){ if(bt!=NULL){ cout<<bt->data; if(bt->lchild!=NULL || bt->rchild!=NULL){ cout<<"("; display(bt->lchild); if(bt->rchild!=NULL) cout<<","; display(bt->rchild); cout<<")"; } } //cout<<endl; } int main(){ char str[SIZE]; cin>>str; tnode * bt; createBtree(bt,str); display(bt); cout<<endl; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值