1
struct elemtype{
Keytype key;
};
struct SStable{
elemtype *elem;
int length;
};
SStable L;
int search_result(SStable L,Keytype key)
{
int low=1;
int high=L.length;
while(low<=high)
{
mid=(low+high)/2;
if(L.elem[mid].key==key)
return mid;
else if(key<L.elem[mid].key)
high=mid-1;
else
low=mid+1;
}
return 0;
}
2
void LinkList_clear(LinkList &L,int min,int max)
{
node *q,*t,*p,*r;
p = L->next;
while (p!=NULL&& p->date <= min)
{
t = p;
p = p->next;
}
if (p!=NULL)
{
while (p && p->date < max)
{
p = p->next;
}
q = t->next;
t->next = p;
while (q != p)
{
r = q->next;
free(q);
q = r;
}
}
}
3
#include <bits/stdc++.h>
using namespace std;
typedef struct
{
char data[MaxSize];
int length;
} SqString;
void GetNext(SqString t,int next[])
{
int j,k;
j=0;k=-1;
next[0]=-1;
while (j<t.length-1)
{
if (k==-1 || t.data[j]==t.data[k])
{
j++;k++;
next[j]=k;
}
else
{
k=next[k];
}
}
}
int KMP(SqString s,SqString t)
{
int next[MaxSize],i=0,j=0;
GetNext(t,next);
while (i<s.length && j<t.length)
{
if (j==-1 || s.data[i]==t.data[j])
{
i++;j++;
}
else j=next[j];
}
if (j>=t.length)
return(1);
else
return(-1);
}
int main()
{
int a=KMP(STR1,Pattern);
int b=KMP(STR,Pattern);
if((a+b))==2)
printf("模式串Pattern是字符串STR1和STR2的公共子串");
return 0;
}
4
int count=0;
int findcount(tree root)
{
if(root==NULL)
count=0;
else if(!root->l&&!root->r)
return 1;
else
return findcount(root->l)+findcount(root->r);
}
5 压轴题
int getheight(tree t) {
if (!t) return -1;
else {
int lheight = getheight(t->left);
int rheight = getheight(t->right);
return max(lheight, rheight) + 1;
}
}
tree Lrotation(tree A) {
tree B = A->left;
A->left = B->right;
B->right = A;
A->height = getheight(A);
B->height = getheight(B);
return B;
}
tree Rrotation(tree A) {
tree B = A->right;
A->right = B->left;
B->left = A;
A->height = getheight(A);
B->height = getheight(B);
return B;
}
tree LRrotation(tree A)
{
A->left = Rrotation(A->left);
return Lrotation(A);
}
tree RLrotation(tree A)
{
A->right = Lrotation(A->right);
return Rrotation(A);
}
tree solve(tree p,int )
{
if(getheight(p->letf)-getheight(p->right)==2)
{
if(getheight(p->left->left)-getheight(p->left->right)==1)
{
p=Lrotation(p);
}
if(getheight(p->left->left)-getheight(p->left->right)==-1)
{
p=LRrotation(p);
}
}
if(getheight(p->right)-getheight(p->left)==2)
{
if(getheight(p->right->right)-getheight(p->right->left)==1)
{
p=Rrotation(p);
}
if(getheight(p->right->right)-getheight(p->right->left)==-1)
{
p=RLrotation(p);
}
}
}