原创,记录自己学习数据结构时的理解与算法代码
第一题:请基于BF算法和KMP算法,设计一个病毒基因算法(要求至少5组输入和5组输出
#include
#include
using namespace std;
#define MAXSIZE 200
typedef struct {
char *ch;
int length;
}String;
int inistring(String &str,char ch[]){
if(str.ch!=NULL){
cout<<"String串不为空,故释放空间"<<endl;
str.ch=NULL;
str.length=0;
}
char *data=ch;
int length_new=0;
while(*data){
++length_new;
++data;
}
cout<<"串的长度为:"<<length_new<<endl;
if(length_new==0){
cout<<"data为空,初始化串为空串"<<endl;
str.ch=NULL;
str.length=0;
}
else{
str.ch=(char *)malloc(sizeof(char)*(length_new+1));
data=ch;
for(int i=0;i<=length_new;i++,++data){
str.ch[i]=*data;
}
str.length=length_new;
//cout<<"串chuan的长度为:"<
}
}
int output(String &str){
for(int i=0;str.ch[i]!='\0';i++){
cout<<str.ch[i]<<endl;
}
return 0;
}
//BF___________________________________________________________________
int index_bf(String &s1,String &s2,int pos){
int cc=0;
int i=pos,j=0;
while (i<=s1.length && j<=s2.length)
{
if (s1.ch[i]==s2.ch[j]){
++i;
++j;
if (j>=s2.length)
break;
}
else{
i=i-j+1;
j=0;
cc++;
}
}
cout<<"BF算法匹配次数"<<cc<<endl;
if (j>=s2.length)
{
return i-s2.length;
}
else
{
cout<<"BF算法匹配失败"<<endl;
return -1;
}
}
//BF___________________________________________________________________
//KMP___________________________________________________________________
void get_next(String &s2,int next[]){
int i=1,j=0;
next[0]=0;
next[1]=0;
while (i<s2.length){
if(0==j||s2.ch[i] == s2.ch[j])
{
if(0==j&&s2.ch[i] != s2.ch[j])
{next[++i] = j;}
else
{
next[++i] = ++j;
}
}else{
j=next[j];
}
}
}
int index_kmp(String &s1,String &s2,int pos){
int c=0;
int * next = new int[s2.length];
get_next(s2,next);
int i=pos,j=0;
while (i<s1.length && j<s2.length) {
if (s1.ch[i] == s2.ch[j]) {
j++;
i++;
} else {
if (j == 0)
i++;
else
j = next[j];
c++;
}
}
cout<<"KMP算法匹配次数"<<c<<endl;
if (j>=s2.length){
return i-s2.length;
} else{
return -1;
}
}
//KMP___________________________________________________________________
int main() {
int res, gene,kmp,numbers=1;
char ch0[] = "AATAATCCCGGGAATAATGTGCCCTTTAATAACCCGGTCGTGTAATAAGGA";
cout << "现有人体基因为:AATAATCCCGGGAATAATGTGCCCTTTAATAACCCGGTCGTGTAATAAGGA" << endl;
while (numbers<6) {
//char ch1[] = "AG";
char ch1[MAXSIZE];
cout << "请输入病毒碱基序列(A、T、C、G、U)" << endl;
cin>>ch1;
String s1, s2;
//output(s1);
cout << "<<<<<<<<<<<<<<<<<<<<<<" << endl;
//output(s2);
cout << "若为DNA病毒,则输入1,若为RNA病毒,则输入0" << endl;
cin >> gene;
if (gene == 0) {
for (int i = 0; ch1[i] != '\0'; ++i) {
switch (ch1[i]) {
case 'U':
ch1[i] = 'A';
break;
case 'A':
ch1[i] = 'T';
break;
case 'C':
ch1[i] = 'G';
break;
case 'G':
ch1[i] = 'C';
break;
default:
break;
}
}
} else {
for (int i = 0; ch1[i] != '\0'; ++i) {
switch (ch1[i]) {
case 'T':
ch1[i] = 'A';
break;
case 'A':
ch1[i] = 'T';
break;
case 'C':
ch1[i] = 'G';
break;
case 'G':
ch1[i] = 'C';
break;
}
}
}
inistring(s1, ch0);
inistring(s2, ch1);
//cout << ch1 << endl;
kmp = index_kmp(s1,s2,0);
cout<<"从0开始,匹配结果如下:"<<endl;
cout<<"KMP——success:index= "<<kmp<<endl;
// cout << ch1 << endl;
res = index_bf(s1, s2, 0);
cout<<"从0开始,匹配结果如下:"<<endl;
cout << "BF——success:index=" << res << endl;
if(res==kmp){
if (res != -1) {
cout << "检测出病毒" << numbers << endl;
} else {
cout << "未检测出病毒" << numbers << endl;
}
}
else
{
cout<<"KMP结果与BF结果不一样,请检查程序";
}
numbers++;
}
}
第二题:请用线性表和链表,设计已知线性表LA,LB,表中的数据皆为整数,现在要求将LA和LB合并成一个新的线性表LC。要求:(1)1. LA和LB皆为顺序表,且LA和LB数目分别是7和10;2. 2. LA和LB皆为链表,且LA和LB数目分别是6和8
//
// Created by 86182 on 2021-04-14.
//
#include
#include
using namespace std;
#define MAXSIZE 100
typedef int ElemType;
typedef struct {
ElemType data[MAXSIZE];
int length;
}List;
void initList(List &L){
L.length=0;
}
void create_List(List &L){
int i,j;
cout<<"请输入要创建的顺序表的长度,最长为200"<<endl;
cin>>j;
if (j<200){
for (i = 0; i < j; ++i) {
int a;
cout<<"请输入顺序表数据,要求为整数,按回车结束一次输入"<<endl;
cin>>a;
L.data[i]=a;
L.length++;
}
} else{
cout<<"长度超过200,创建失败"<<endl;
}
}
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
void initLNode(LinkList &L){
L = new LNode;
L->next=NULL;
};
void create_LNode(LinkList &L){
int i,j;
LinkList p,r;
r = L;
cout<<"请输入要创建的单链表的长度"<<endl;
cin>>j;
for (i = 0; i < j; ++i) {
p = new LNode;
cout<<"请输入链表的数据,要求为整数,按回车结束一次输入"<<endl;
cin>>p->data;
p->next=NULL;
r->next=p;
r=p;
}
}
//求两顺序表的集合
void Merge_List(List &l1,List &l2,List &l3){
int e,a=l1.length,b=l2.length;
int a1=a-1;
for (int i = 0; i < b; ++i) {
e = l2.data[i];
for (int j = 0; j < a; ++j) {
if (l1.data[j]==e)
break;
if (j==a1)
l1.data[l1.length++]=e;
}
}
l3=l1;
for (int i = 0; i<l3.length; ++i) {
cout<<"集合后的顺序表:"<<l3.data[i]<<endl;
}
}
void MergeLNode(LinkList &l1,LinkList &l2,LinkList &l3){
LinkList pa,pb,pc;
pa = l1->next;
pb = l2->next;
l3 = l1;
pc = l3;
while (pa&&pb){
if (pa->data<=pb->data){
//pc = new LNode;
pc->next=pa;
pc=pa;
pa=pa->next;
cout<<"AAAAAAAAA"<<pc->next<<endl;
}
else{
//pc = new LNode;
pc->next=pb;
pc=pb;
pb=pb->next;
cout<<"wadawdwdwad"<<pc->next<<endl;
}
}
pc->next=pa?pa:pb;
}
int main(){
//顺序表的集合
List s1,s2,s3;
initList(s1);
initList(s2);
create_List(s1);
create_List(s2);
Merge_List(s1,s2,s3);
//有序链表的合并
LinkList l1,l2,l3;
initLNode(l1);
initLNode(l2);
create_LNode(l1);
create_LNode(l2);
MergeLNode(l1,l2,l3);
while (l3->next != NULL){
cout<<l3->next->data<<endl;
l3 = l3->next;
}
}
第三题:假设有如下程序片段:输入3个整数a, b, c,分别作为三角形的三条边,通过程序判断这3条边是否能构成三角形?如果能,则判断三角形的类型(等边三角形、等腰三角形、一般三角形)。
要求输入3个整数a、b、c,必须满足以下条件:1≤a≤200; 1≤b≤200; 1≤c≤200。要求:用cin>>输入,用cout>>….<//输入三个整数a,b,c,判断是否可以构成三角形,若可以组成三角形求出其面积
// Created by 86182 on 2021-04-15.
//
#include