3.2,3.3数据结构瑞格串,广义表

nefu瑞格串,广义表

–题目来源-nefu瑞格

4944

在这里插入图片描述

#include<iostream>
#include<string.h>
using namespace std;

#define defaultSize 128

class AString {
private:
    char *ch;
    int curLength;
	int maxSize;
public:
	AString(int sz=defaultSize);
	AString(const char *init);
	AString(const AString &ob);
	~AString()
	{
		if (ch!=NULL)
			delete []ch;
	}
	int Length() const { return curLength; }//求长度
	AString operator()(int pos,int len);//字符串截取
	int operator==(AString &ob)const //两个字符串相等返回1
	{
		return strcmp(ch,ob.ch)==0;
	}
	int operator!=(AString &ob) const//两个字符串不相等返回1
	{
		return strcmp(ch,ob.ch)!=0;
	}
	int operator!() const//判断字符串是否为空
	{
		return curLength==0;
	}
	AString &operator=(const AString &ob);//重新写字符串
	AString &operator+=(const AString &ob);//在原字符串后头加字符串
	char operator[](int i);//取第i个字符
	char *getString() const//得到字符串首地址
	{
		return this->ch;
	}
	int Find(AString &pat,int k) const;
};

AString::AString(int sz)
{
	maxSize = sz;
	ch = new char[maxSize+1];
	curLength=0;
	ch[0]='\0';
}

AString::AString(const char *init)
{
	int len=strlen(init);
	maxSize = (len>defaultSize)?len:defaultSize;
	ch = new char[maxSize+1];
	curLength = len;
	strcpy(ch,init);
}

AString::AString(const AString &ob)
{
	maxSize = ob.maxSize;
	ch = new char[maxSize+1];
	curLength = ob.curLength;
	strcpy(ch,ob.ch);
}

AString AString::operator ()(int pos,int len)
{
	AString temp;
	if (pos<0||pos+len-1>=maxSize||len<0) {
		temp.curLength = 0;
		temp.ch[0] = '\0';
	}
	else {
		if (pos+len-1>=curLength)
			len = curLength-pos;
		temp.curLength = len;
		int i,j;
		for (i=0,j=pos;i<len;i++,j++)
			temp.ch[i] = ch[j];
		temp.ch[len] = '\0';
	}
	return temp;
}

AString& AString::operator =(const AString &ob)
{
	if (&ob!=this) {
		delete []ch;
		ch = new char[ob.maxSize];
		curLength = ob.curLength;
		strcpy(ch,ob.ch);
	}
	return *this;
}

AString& AString::operator +=(const AString& ob)
{
	char *temp = ch;
	int n = curLength+ob.curLength;
	int m = n+1;
	ch = new char[m];
	maxSize = m;
	curLength = n;
	int i;
	for (i=0;temp[i]!='\0';i++)
		ch[i] = temp[i];
	int j;
	for (j=0;ob.ch[j]!='\0';j++,i++)
		ch[i] = ob.ch[j];
	ch[i] = '\0';
	delete []temp;
	return *this;
}

char AString::operator [](int i)
{
	if (i<0||i>=curLength)
		return ' ';
	return ch[i];
}

//write your code here
int AString::Find(AString &pat,int k) const
{
    int i=0,j=-1,n;
    n=pat.Length();
    int next[n];
    next[0]=-1;
    while(i<pat.Length()-1)
    {
        if(j==-1||pat.operator[](i)==pat.operator[](j))
        {
            ++i;
            ++j;
            next[i]=j;
        }
        else
            j=next[j];
    }
    i=k-1;
    j=0;
    while(i<curLength&&j<pat.Length())
    {
        if(j==-1||ch[i]==pat.operator[](j))
        {
            ++i;
            ++j;
        }
        else
            j=next[j];
    }
    if(j==pat.Length())
        return i-pat.Length();
    else
        return -1;

}


int main()
{
	char t[20];
	cin >> t;
	char pat[10];
	cin >> pat;
	int k;
	cin >> k;
	AString strt(t);
	AString strpat(pat);
	cout << strt.Find(strpat,k) << endl;
	return 0;
}

4943

在这里插入图片描述

#include<iostream>
#include<string.h>
#include<string.h>
using namespace std;

#define defaultSize 128

class AString {
private:
    char *ch;
    int curLength;
	int maxSize;
public:
	AString(int sz=defaultSize);
	AString(const char *init);
	AString(const AString &ob);
	~AString()
	{
		if (ch!=NULL)
			delete []ch;
	}
	int Length() const { return curLength; }
	AString operator()(int pos,int len);
	int operator==(AString &ob)const
	{
		return strcmp(ch,ob.ch)==0;
	}
	int operator!=(AString &ob) const
	{
		return strcmp(ch,ob.ch)!=0;
	}
	int operator!() const
	{
		return curLength==0;
	}
	AString &operator=(const AString &ob);
	AString &operator+=(const AString &ob);
	char operator[](int i);
	char *getString() const
	{
		return this->ch;
	}
};

AString::AString(int sz)
{
	maxSize = sz;
	ch = new char[maxSize+1];
	curLength=0;
	ch[0]='\0';
}

AString::AString(const char *init)
{
	int len=strlen(init);
	maxSize = (len>defaultSize)?len:defaultSize;
	ch = new char[maxSize+1];
	curLength = len;
	strcpy(ch,init);
}

AString::AString(const AString &ob)
{
	maxSize = ob.maxSize;
	ch = new char[maxSize+1];
	curLength = ob.curLength;
	strcpy(ch,ob.ch);
}

AString AString::operator ()(int pos,int len)
{
	AString temp;
	if (pos<0||pos+len-1>=maxSize||len<0) {
		temp.curLength = 0;
		temp.ch[0] = '\0';
	}
	else {
		if (pos+len-1>=curLength)
			len = curLength-pos;
		temp.curLength = len;
		int i,j;
		for (i=0,j=pos;i<len;i++,j++)
			temp.ch[i] = ch[j];
		temp.ch[len] = '\0';
	}
	return temp;
}

AString& AString::operator =(const AString &ob)
{
	if (&ob!=this) {
		delete []ch;
		ch = new char[ob.maxSize];
		curLength = ob.curLength;
		strcpy(ch,ob.ch);
	}
	return *this;
}

AString& AString::operator +=(const AString& ob)
{
	char *temp = ch;
	int n = curLength+ob.curLength;
	int m = n+1;
	ch = new char[m];
	maxSize = m;
	curLength = n;
	int i;
	for (i=0;temp[i]!='\0';i++)
		ch[i] = temp[i];
	int j;
	for (j=0;ob.ch[j]!='\0';j++,i++)
		ch[i] = ob.ch[j];
	ch[i] = '\0';
	delete []temp;
	return *this;
}

char AString::operator [](int i)
{
	if (i<0||i>=curLength)
		return ' ';
	return ch[i];
}

//write your code here
int Find(AString &a,AString &b,int k)
{
    int i=0,j=-1,n;
    n=b.Length();
    int next[n];
    next[0]=-1;
    while(i<b.Length()-1)
    {
        if(j==-1||b.operator[](i)==b.operator[](j))
        {
            ++i;
            ++j;
            next[i]=j;
        }
        else
            j=next[j];
    }
    i=k-1;
    j=0;
    while(i<a.Length()&&j<b.Length())
    {
        if(j==-1||a.operator[](i)==b.operator[](j))
        {
            ++i;
            ++j;
        }
        else
            j=next[j];
    }
    if(j==b.Length())
        return i-b.Length();
    else
        return -1;

}


void replace(AString &s,AString t,AString v)
{
    int l=0;
    char as[100];
    char bs[100];
    AString a(as);
    AString b(bs);
    int l1=t.Length(),l2=s.Length(),l3=v.Length();
    l=Find(s,t,1);
    while(l!=-1)
    {
        a=s.operator()(0,l);
        b=s.operator()(l+l1,l2-l-l1);
        s.operator =(a);
        s.operator +=(v);
        s.operator +=(b);
        l=Find(s,t,l+l3+1);
     }
}


int main()
{
	char cs[30],ct[10],cv[10];
	cin >> cs >> ct >> cv;
	AString s(cs);
	AString t(ct);
	AString v(cv);
	replace(s,t,v);
	cout << s.getString() << endl;
	return 0;
}

4203

在这里插入图片描述

#include<stdio.h>
#include<string.h>

#define TRUE 1
#define FALSE 0

typedef int Status;

Status isSymmetry(char *s)
{
    // write your code here
    int i,j,l;
    l=strlen(s);
    j=l-1;
    while(i<j)
    {
        if(s[i]!=s[j])
        {
            return 0;
        }
        else
        {
            i++;
            j--;
        }
    }
    return 1;
}

int main()
{
	char a[20];
	gets(a);
	char *str = a;
	if (isSymmetry(str))
		printf("YES\n");
	else
		printf("NO\n");
	return 0;
}

4941

在这里插入图片描述

#include<iostream>
#include<string.h>
using namespace std;

//write your code here
int stringToInt(char *s)
{
    int l=strlen(s);
    if(l==1)
        return s[0]-'0';
    else if(l==2)
        return (s[0]-'0')*10+s[1]-'0';
    else
    {
        int i;
        char a[l];
        for(i=0;i<l-1;i++)
        {
            a[i]=s[i];
        }
        a[l-1]='\0';
        return stringToInt(a)*10+s[l-1]-'0';
    }
}


int main()
{
	char a[20],b[20];
	cin >> a >> b;
	int m = stringToInt(a);
	int n = stringToInt(b);
	cout << m+n << endl;
	return 0;
}

4940

在这里插入图片描述

#include<iostream>
#include<string.h>
using namespace std;

#define defaultSize 128

class AString {
private:
    char *ch;
	int curLength;
	int maxSize;
public:
	AString(int sz=defaultSize);
	AString(const char *init);
	AString(const AString &ob);
	~AString()
	{
		if (ch!=NULL)
			delete []ch;
	}
	int Length() const { return curLength; }
	AString operator()(int pos,int len);
	int operator==(AString &ob)const
	{
		return strcmp(ch,ob.ch)==0;
	}
	int operator!=(AString &ob) const
	{
		return strcmp(ch,ob.ch)!=0;
	}
	int operator!() const
	{
		return curLength==0;
	}
	AString &operator=(const AString &ob);
	AString &operator+=(const AString &ob);
	char operator[](int i);
};

//write your code here
AString::AString(int sz)
{
	maxSize = sz;
	ch = new char[maxSize+1];
	curLength=0;
	ch[0]='\0';
}

AString::AString(const char *init)
{
	int len=strlen(init);
	maxSize = (len>defaultSize)?len:defaultSize;
	ch = new char[maxSize+1];
	curLength = len;
	strcpy(ch,init);
}

AString::AString(const AString &ob)
{
	maxSize = ob.maxSize;
	ch = new char[maxSize+1];
	curLength = ob.curLength;
	strcpy(ch,ob.ch);
}

AString AString::operator ()(int pos,int len)
{
	AString temp;
	if (pos<0||pos+len-1>=maxSize||len<0) {
		temp.curLength = 0;
		temp.ch[0] = '\0';
	}
	else {
		if (pos+len-1>=curLength)
			len = curLength-pos;
		temp.curLength = len;
		int i,j;
		for (i=0,j=pos;i<len;i++,j++)
			temp.ch[i] = ch[j];
		temp.ch[len] = '\0';
	}
	return temp;
}

AString& AString::operator =(const AString &ob)
{
	if (&ob!=this) {
		delete []ch;
		ch = new char[ob.maxSize];
		curLength = ob.curLength;
		strcpy(ch,ob.ch);
	}
	return *this;
}

AString& AString::operator +=(const AString& ob)
{
	char *temp = ch;
	int n = curLength+ob.curLength;
	int m = n+1;
	ch = new char[m];
	maxSize = m;
	curLength = n;
	int i;
	for (i=0;temp[i]!='\0';i++)
		ch[i] = temp[i];
	int j;
	for (j=0;ob.ch[j]!='\0';j++,i++)
		ch[i] = ob.ch[j];
	ch[i] = '\0';
	delete []temp;
	return *this;
}

char AString::operator [](int i)
{
	if (i<0||i>=curLength)
		return ' ';
	return ch[i];
}


int main()
{
	AString str1(20);
	str1 = "Hello World!";
	AString str2("I am a programmer.");
	AString str3(str2);
	cout << str1.Length() << endl;
	if (str2==str3)
		str1+=str2;
	int i;
	for (i=0;i<str1.Length();i++)
		cout << str1[i];
	cout << endl;
	str2 = str3(7,20);
	cout << str2[4] << endl;
	return 0;
}

4929

在这里插入图片描述B

4191

在这里插入图片描述C

4192

在这里插入图片描述B

4194

在这里插入图片描述A

4195

在这里插入图片描述A

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值