任意输入一个字符串,串中出现的相同元素构成的子序列称为平台,试建立一个类str,求出串的长度,并能输出最长平台的子序列,如字符串为jkkkaabbbbbaaa,则
你意思是定义一个类,成员变量是一个字符串,要实现求字符串长度函数,和最长平台子序列函数?
class Str
{
private:
char *str;
char *s;
int maxlen;
public:
Str() {str = NULL;s=NULL;maxlen = 0;}
Str(char *p) {str = p;s = NULL;maxlen = 0;}
~Str() {delete p;p=NULL;s=NULL;}
void print()
{
for(int i=0;i<maxlen;i++)
cout<<*(s+i);
cout<<endl;
}
process()
{
int strlen = 0;
int i,j;
for(i=0;str[i] != 0;i++)
strlen++;
maxlen = 0;
int len = 1;
char *q = p;
char c = str[0];
for(i=1;i<=strlen;i++)
{
if(str[i] != c)
{
c = str[i];
if(len > maxlen)
{
maxlen = len;
s = q;
q = p+i;
len = 1;
}
}
else
len++;
}
}
};
int main()
{
char *s = new char[100];
cin>>s;
Str str(s);
str.process();
str.print();
}