字符串应用
B
有s1,s2,s3....sn这n个字符串
字符串相似程度是指两个字符串最长连续公共子串的长度
请这一群字符串中找出最不相似的字符串
问题分析:
定义两个数组,
第一个数组s用来存储输入的字符串
第二个数组same可以看成n阶矩阵,用于计数,里面的元素a(i,j)对应第i个元素与第j个元素的相似值,就是它们最长连续公共子串的长度;最不相似就是值最小的了
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int same_m(string s,string t)//求两个子字符串的公共子串的长度
{
int lens=s.size();
int lent=t.size();
int same=0;
for(int i=0;i<lens+1;i++)
{
for(int j=0;j<lent+1;j++)
{
if(s[i]==t[j])//stop
{ int c=0;
int k=i;
int f=j;
for(k,f;f<lent&&k<lens;f++,k++)
{
if(s[k]==t[f])
{
c++;
}
}
if(c>same){same=c;}
}
}
}
return same;
}
int main()
{
string s[100];
int count=0;
cin>>s[count];
while(cin.get()!='\n')
{
count++;
cin>>s[count];
}
const int n=count+1;
int same[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
same[i][j]=same_m(s[i],s[j]);
}
}
int min=same[0][0];
int hang=0,lie=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
//cout<<s[i]<<'\t'<<s[j]<<':'<<same[i][j]<<'\t';
if(same[i][j]<min)
{
min=same[i][j];
hang=i;
lie=j;
}
}
cout<<endl;
}
cout<<s[hang]<<endl<<s[lie]<<endl;、
return 0;
}
重点在于构造一个找公共子字符串的函数
在构造这个函数的时候,对于函数,要牢记以下几点
1,for循环的时候最后赋值一定要注意在循环体里面还是在外面
2,赋值需要有个比较的结果,因为子字符串不止一个,而且由于我们需要的只是最大的那个
所以这有个比较的过程
类与对象
A
设计以下四个类,即学生类、本科生类、硕士生类和博士生类,并定义后三个类的对象各一
个,并调用各自的函数,显示对应的返回结果。
1
、学生类为一个抽象类,没有成员变量,包括一种构造函数,一种析构函数,用于显示的
纯虚函数,计算总分的纯虚函数。
2
、本科生类继承了学生类,成员变量包括学号、姓名、班级、课程总分以及创新总分,包
括两种构造函数,一种析构函数,用于显示各个成员变量的函数,计算总分的函数。
3
、硕士生类继承了本科生类,成员变量包括科研总分,包括两种构造函数,一种析构函数,
用于显示各个成员变量的函数,计算总分的函数。
4
、博士生类继承了硕士生类,成员变量包括基金总分,包括两种构造函数,一种析构函数,
用于显示各个成员变量的函数,计算总分的函数。
其中,本科生类对象的总分
=
课程总分
+
创新总分;硕士生类对象的总分
=
课程总分
+
创新总分
+
科研总分;博士生类对象的总分
=
课程总分
+
创新总分
+
科研总分
+
基金总分
#include<iostream>
#include<cstring>
using namespace std;
class student
{
public:
student(){}
virtual void show()
{}
virtual double calculate()
{}
~student(){}
};
class undergraduate:public student
{
protected:
char id[10];
char name[10];
char class_n[10];
double c_score;
double i_score;
public:
undergraduate()
{
strcpy(id,"XXX");
strcpy(name,"XXX");
strcpy(class_n,"000");
c_score=0;
i_score=0;
}
undergraduate(char*id,char*name,char*class_n,double c_score,double i_score)
{
strcpy(this->id,id);
strcpy(this->name,name);
strcpy(this->class_n,class_n);
this->c_score=c_score;
this->i_score=i_score;
}
void show()
{
cout<<"id:"<<id<<endl;
cout<<"name:"<<name<<endl;
cout<<"class_n:"<<class_n<<endl;
cout<<"c_score:"<<c_score<<endl;
cout<<"i_score:"<<i_score<<endl;
}
double calculate()
{
double sum=0;
sum=c_score+i_score;
return sum;
}
~undergraduate(){}
};
class bachelor:public undergraduate
{
protected:
double r_score;
public:
bachelor(){
//undergraduate::undergraduate();
r_score=0;
}
bachelor(char*id,char*name,char*class_n,double c_score,double i_score,double r_score){
this->r_score=r_score;
strcpy(this->id,id);
strcpy(this->name,name);
strcpy(this->class_n,class_n);
this->c_score=c_score;
this->i_score=i_score;
//undergraduate::undergraduate(id,name,class_n,c_score,i_score);
}
void show()
{
undergraduate::show();
cout<<"r_score:"<<r_score<<endl;
}
double calculate()
{
double sum;
sum=undergraduate::calculate()+r_score;
return sum;
}
~bachelor(){}
};
class doctoral:public bachelor
{
protected:
double f_score;
public:
doctoral(){
//bachelor::bachelor();
f_score=0;
}
doctoral(char*id,char*name,char*class_n,double c_score,double i_score,double r_score,double f_score)
{
this->f_score=f_score;
this->r_score=r_score;
strcpy(this->id,id);
strcpy(this->name,name);
strcpy(this->class_n,class_n);
this->c_score=c_score;
this->i_score=i_score;
//bachelor::bachelor(id,name,class_n,c_score,i_score,r_score);
}
void show()
{
bachelor::show();
cout<<"f_score:"<<f_score<<endl;
}
double calculate()
{
double sum;
sum=f_score+bachelor::calculate();
return sum;
}
~doctoral(){}
};
int main()
{
undergraduate p1;
p1.show();
bachelor p2_1,p2("123","wuyang","1",90,90,95);
p2_1.show();
p2.show();
doctoral p3("123","wuyang","1",90,90,95,97);
p3.show();
cout<<p3.calculate();
return 0;
}
//
基类里面初始化的构造函数不需要再调用了 ,不需要不需要!!!如果调用就错了