实验一 STL 的熟悉与使用
一、实验目的和要求
1.掌握C++中STL的容器类的使用;
2.掌握C++中STL的算法类的使用.
二、实验预习内容
1.预习ICPC讲义,大致了解STL的相关内容。
2.了解STL中一些类 vector list类的使用方法
3.了解泛型算法的使用
三、实验项目摘要
1.练习 vector 和 list 的使用。
定义一个空的 vector,元素类型为 int,生成 10 个随机数插入到 vector 中,用迭代器遍历 vector 并输出其中的元素值。在 vector 头部插入一个随机数,用迭代器遍历 vector并输出其中的元素值。用泛型算法 find 查找某个随机数,如果找到便输出,否则将此数插入 vector 尾部。用泛型算法 sort 将 vector 排序,用迭代器遍历 vector 并输出其中的元素值。删除 vector 尾部的元素,用迭代器遍历 vector 并输出其中的元素值。将 vector 清空。
定义一个 list,并重复上述实验,并注意观察结果。
2.练习泛型算法的使用。
定义一个 vector,元素类型为 int,插入 10 个随机数,使用 sort 按升序排序,输出每个元素的值,再按降叙排序,输出每个元素的值。练习用 find 查找元素。用 min 和max 找出容器中的最小元素个最大元素,并输出。
#include<list>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int main(void)
{
list<int> a;
list<int>::iterator it;
int r;
for(int i=0;i<10;i++)
{
r = rand();
a.push_back(r);
}
cout<<"输出元素:"<<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<(*it)<<' ';
}
r = rand();
a.insert(a.begin(),r);
cout<<endl<<"在头部插入一个随机数后输出元素:"<<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<(*it)<<' ';
}
r = rand();
it = find(a.begin(),a.end(),r);
if(it!=a.end())
{
cout<<endl<<"已经查找到"<<r<<endl;
}
else
{
a.push_back(r);
}
a.sort();
cout<<endl<<"排序后输出元素:"<<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<(*it)<<' ';
}
a.pop_back();
cout<<endl<<"删除尾部元素后输出元素:"<<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<(*it)<<' ';
}
cout<<endl;
a.clear();
return 0;
}
#include<vector>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int main(void)
{
vector<int> a;//定义一个空的 vector,元素类型为 int
vector<int>::iterator it;
int r;
for(int i=0;i<10;i++)//生成 10 个随机数插入到 vector 中
{
r = rand();
a.push_back(r);
}
cout<<"输出元素:"<<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<(*it)<<' ';//用迭代器遍历 vector 并输出其中的元素值
}
r = rand();
a.insert(a.begin(),r);//在 vector 头部插入一个随机数
cout<<endl<<"在头部插入一个随机数后输出元素:"<<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<(*it)<<' ';//用迭代器遍历 vector 并输出其中的元素值
}
r = rand();//用泛型算法 find 查找某个随机数
it = find(a.begin(),a.end(),r);
if(it!=a.begin()+a.size())//如果找到便输出
{
cout<<endl<<"已经查找到"<<r<<endl;
}
else//否则将此数插入 vector 尾部
{
a.push_back(r);
}
sort(a.begin(),a.end());//用泛型算法 sort 将 vector 排序
cout<<endl<<"排序后输出元素:"<<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<(*it)<<' ';//用迭代器遍历 vector 并输出其中的元素值
}
a.pop_back();//删除 vector 尾部的元素
cout<<endl<<"删除尾部元素后输出元素:"<<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<(*it)<<' ';//用迭代器遍历 vector 并输出其中的元素值
}
cout<<endl;
a.clear();//将 vector 清空
return 0;
}
#include<vector>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int main(void)
{
vector<int> a;
vector<int>::iterator it;
int r;
for(int i=0;i<10;i++)
{
r = rand();
a.push_back(r);
}
sort(a.begin(),a.end());
cout<<"升序输出元素:"<<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<(*it)<<' ';
}
sort(a.rbegin(),a.rend());
cout<<endl<<"降序输出元素:"<<endl;
for(it=a.begin();it!=a.end();it++)
{
cout<<(*it)<<' ';
}
cout<<endl<<"最大元素:"<<*max_element(a.begin(),a.end())<<endl;
cout<<endl<<"最小元素:"<<*min_element(a.begin(),a.end())<<endl;
}
实验二 搜索算法的实现
一、实验目的和要求
1.掌握宽度优先搜索算法。
2.掌握深度优先搜索算法。
二、实验预习内容
1.预习ICPC讲义中的搜索的内容
2. 了解什么是深度优先搜索和广度优先搜索。
三、实验项目摘要
1、 将书上的走迷宫代码上机运行并检验结果,并注意体会搜索的思想。
2、八皇后问题:在一个国际象棋棋盘上放八个皇后,使得任何两个皇后之间不相互攻击,求出所有的布棋方法。上机运行并检验结果。
3、骑士游历问题:在国际棋盘上使一个骑士遍历所有的格子一遍且仅一遍,对于任意给定的顶点,输出一条符合上述要求的路径。
4.倒水问题:给定2 个没有刻度容器,对于任意给定的容积,求出如何只用两个瓶装出L 升的水,如果可以,输出步骤,如果不可以,请输出No Solution。
#include<iostream>
#include<vector>
using namespace std;
int maze[4][6] = { {1, 1, 0, 0, 0, 0},
{0, 1, 1, 1, 0, 0},
{1, 1, 0, 1, 0, 0},
{0, 0, 0, 1, 0, 0}};
vector<pair<int, int> > path;
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void printvector(vector<pair<int, int> > path)
{
vector<pair<int , int > >::iterator pit;
for(pit=path.begin();pit != path.end();pit++)
{
cout<<pit->first<<","<<pit->second<<" -> ";
}
cout<<"3,3"<<endl;
}
void search(vector<pair<int, int> > tpath, int x, int y)
{
if(x < 0 || y < 0 || x > 5 || y > 3)
return;
if(x == 3 && y == 3)
{
path = tpath;
printvector(path);
return;
}
for(int ix = 0; ix < 4; ix++)
{
if(maze[x+dir[ix][0]][ y+dir[ix][1]] == 1)
{
tpath.push_back(make_pair(x, y));
maze[x][y] = 2;
search(tpath, x+dir[ix][0], y+dir[ix][1]);
tpath.pop_back();
}
}
}
int main()
{
vector<pair<int, int> > tpath;
search(tpath, 0, 0);
cout<<endl;
}
#include<iostream>
using namespace std;
int table[8][8];
int queen[8];
int last = -1;
int ss = 0;
int find_where(int i,int j)
{
for(;j<8;j++)
{
if(table[i][j]==0)
{
return j;
}
}
return -1;
}
void set_where(int i,int j)
{
queen[i] = j;
table[i][j]=-1;
for(int m=0;m<8;m++)//横纵
{
if(table[i][m]!=-1)
{
table[i][m]++;
}
if(table[m][j]!=-1)
{
table[m][j]++;
}
}
int si,sj;
for(si=i+1,sj=j+1;si<8 && sj<8;si++,sj++)//右上
{
table[si][sj]++;
}
for(si=i-1,sj=j-1;si>=0 && sj>=0;si--,sj--)//左下
{
table[si][sj]++;
}
for(si=i+1,sj=j-1;si<8 && sj>=0;si++,sj--)//右下
{
table[si][sj]++;
}
for(si=i-1,sj=j+1;si>=0 && sj<8;si--,sj++)//左上
{
table[si][sj]++;
}
}
void take_queen(int i,int j)
{
j = queen[i];
for(int m=0;m<8;m++)//横纵
{
if(table[i][m]!=-1)
{
table[i][m]--;
}
if(table[m][j]!=-1)
{
table[m][j]--;
}
}
int si,sj;
for(si=i+1,sj=j+1;si<8 && sj<8;si++,sj++)//右上
{
table[si][sj]--;
}
for(si=i-1,sj=j-1;si>=0 && sj>=0;si--,sj--)//左下
{
table[si][sj]--;
}
for(si=i+1,sj=j-1;si<8 && sj>=0;si++,sj--)//右下
{
table[si][sj]--;
}
for(si=i-1,sj=j+1;si>=0 && sj<8;si--,sj++)//左上
{
table[si][sj]--;
}
table[i][j]=0;
}
int main(void)
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
table[i][j] = 0;
}
queen[i] = -1;
}
for(i=0;;i++)
{
j = find_where(i,last+1);
if(j==-1)
{
if(i==0)
{
break;
}
i=i-1;
take_queen(i,queen[i]);
last = queen[i];
i=i-1;
}
else
{
last = -1;
set_where(i,j);
if(i==7)
{
ss++;
cout<<ss;
for(int k=0;k<8;k++)
{
cout<<"\t"<<k<<","<<queen[k];
}
cout<<endl;
take_queen(i,queen[i]);
last = j;
i--;
}
}
}
return 0;
}
#include<iostream>
using namespace std;
int movx[]={-2,-2,-1,-1,1,1,2,2};
int movy[]={-1,1,-2,2,-2,2,-1,1};
int table[8][8];
bool out(int x,int y)
{
if(x<0 || x>7 || y<0 || y>7)
{
return true;
}
else
{
return false;
}
}
bool visited(int x,int y)
{
if(table[x][y]!=0)
{
return true;
}
else
{
return false;
}
}
bool next(int &x,int &y,int count)
{
int x1,y1;
switch(count)
{
case 0:
{
x1=x+1,y1=y-2;
if(visited(x1,y1)==false && out(x1,y1)==false)
{
x=x1,y=y1;
return true;
}
break;
}
case 1:
{
x1=x+2,y1=y-1;
if(visited(x1,y1)==false && out(x1,y1)==false)
{
x=x1,y=y1;
return true;
}
break;
}
case 2:
{
x1=x+2,y1=y+1;
if(visited(x1,y1)==false && out(x1,y1)==false)
{
x=x1,y=y1;
return true;
}
break;
}
case 3:
{
x1=x+1,y1=y+2;
if(visited(x1,y1)==false && out(x1,y1)==false)
{
x=x1,y=y1;
return true;
}
break;
}
case 4:
{
x1=x-1,y1=y+2;
if(visited(x1,y1)==false && out(x1,y1)==false)
{
x=x1,y=y1;
return true;
}
break;
}
case 5:
{
x1=x-2,y1=y+1;
if(visited(x1,y1)==false && out(x1,y1)==false)
{
x=x1,y=y1;
return true;
}
break;
}
case 6:
{
x1=x-2,y1=y-1;
if(visited(x1,y1)==false && out(x1,y1)==false)
{
x=x1,y=y1;
return true;
}
break;
}
case 7:
{
x1=x-1,y1=y-2;
if(visited(x1,y1)==false && out(x1,y1)==false)
{
x=x1,y=y1;
return true;
}
break;
}
default :
{
break;
}
}
return false;
}
bool knight_tour(int x,int y,int tag=1)//坐标和步数
{
int count=0;
bool flag=false;
int x1,y1;
table[x][y]=tag;
if(tag==64)//说明走完了
{
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
cout<<table[i][j]<<"\t";
}
cout<<"\n";
}
return true;
}
while(count<8)//应该是表示有几种选择
{
flag=false;
x1=x;
y1=y;
while(flag==false && count<8)
{
flag=next(x1,y1,count++);
}
if(flag==true && knight_tour(x1,y1,tag+1)==true)
{
return true;
}
}
if(count==8)
{
table[x][y]=0;
return false;
}
}
int main(void)
{
int x,y;
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
table[i][j]=0;
}
}
cout<<"Input x and y\n";
cin>>x>>y;
if(knight_tour(x,y)==false)
{
cout<<"Error!!";
}
return 0;
}
#include<iostream>
#define maxlen 100
using namespace std;
int aw=0,bw=0;
int times=0;
int av[maxlen],bv[maxlen];
void put_water(int a,int b,int l)
{
if(aw==l || bw==l)
{
for(int i=0;i<times;i++)
{
cout<<av[i]<<"\t"<<bv[i]<<"\n";
}
return;
}
else if(aw==0)
{
aw = a;
av[times] = aw;
bv[times] = bw;
}
else if(aw!=0 && bw==0)
{
bw = aw;
aw = 0;
av[times] = aw;
bv[times] = bw;
}
else if(aw!=0 && bw!=0)
{
int sum=bw+aw;
if(sum>b)
{
bw = b;
aw = sum-b;
}
else
{
bw = sum;
aw = 0;
}
av[times] = aw;
bv[times] = bw;
}
times++;
if(times>100)
{
cout<<"No Solution"<<endl;
return;
}
put_water(a,b,l);
}
int main(void)
{
int a,b,l;
cout<<"输入两个容器容积和要求的水的量:(小的容器在前,大的容器在后)\n";
cin>>a>>b>>l;
if(l>a+b || a==b)
{
cout<<"No Solution"<<endl;
return 0;
}
put_water(a,b,l);
return 0;
}
实验三 计算几何算法的实现
一、实验目的和要求
1.理解线段的性质、叉积和有向面积。
2.掌握寻找凸包的算法。
3.综合运用计算几何和搜索中的知识求解有关问题。
二、实验预习内容
1.预习ICPC讲义,了解有关计算几何算法。
2.理解线段的性质、叉积和有向面积。
三、实验项目摘要
1.将讲义第三章第三节中的凸包代码上机运行并检验结果。
2.完成讲义第三章的课后习题,上机运行并检验结果。
3.思考:
判线段相交时,如果有个线段的端点在另一条线段上,注意可能与另一条线段上的端点重合,思考这样的情况怎么办。
4.房间最短路问题:
给顶一个内含阻碍墙的房间,求解出一条从起点到终点的最最短路径。房间的边界固定在 x=0,x=10,y=0 和 y=10。起点和重点固定在(0,5)和(10,5)。房间里还有 0 到 18 个墙,每个墙有两个门。输入给定的墙的个数,每个墙的 x 位置和两个门的 y 坐标区间,输出最短路的长度。
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<double,double> POINT;
// function dirction determines the direction that the segment
//p1p turns to p2p with respect to point p
//if return value is positive, means clockwise;
//if return value is negative, menas counter-clockwise;
// naught means on the same line;
double direction(POINT p,POINT p1,POINT p2)
{
POINT v1,v2;
v1.first =p2.first -p.first ;
v1.second=p2.second-p.second;
v2.first =p1.first -p.first;
v2.second=p1.second-p.second;
return v1.first*v2.second-v1.second*v2.first;
}
//function on_segment determines whether the point p is on the segment p1p2
bool on_segment(POINT p,POINT p1,POINT p2)
{
double min_x=p1.first <p2.first ?p1.first :p2.first ;
double max_x=p1.first >p2.first ?p1.first :p2.first ;
double min_y=p1.second<p2.second?p1.second:p2.second;
double max_y=p1.second>p2.second?p1.second:p2.second;
if (p.first >=min_x && p.first <=max_x && p.second>=min_y && p.second<=max_y) return true;
else return false;
}
//point startPoint is the polor point that is needed for comparing two other points;
POINT startPoint;
//function sortByPolorAngle provides the realizing of comparing two points, which support
//the STL function sort();
bool sortByPolorAngle(const POINT & p1, const POINT & p2)
{
double d=direction(startPoint, p1, p2);
if (d<0) return true;
if (d>0) return false;
if (d==0 && on_segment(startPoint, p1, p2) )return true;
if (d==0 && on_segment(p2,startPoint,p1) ) return true;
return false;
}
//here realizes the process of finding convex hull
void find_convex_hull(vector<POINT> & point)
{
POINT p0=point[0];
int k=0;
for (int i=1;i<point.size();i++)
{
if (point[i].second<p0.second || point[i].second==p0.second && point[i].first<p0.first)
{
p0=point[i];
k=i;
}
}
point.erase(point.begin()+k);
point.insert(point.begin(),p0);
vector<POINT> convex_hull;
do {
convex_hull.push_back(point[0]);
startPoint=point[0];
point.erase(point.begin());
sort(point.begin(),point.end(),sortByPolorAngle);
if (point[0]==convex_hull[0]) break;
point.push_back(convex_hull[convex_hull.size()-1]);
} while (1);
for (int i=0;i<convex_hull.size();i++)
{
cout<<convex_hull[i].first<<' ' <<convex_hull[i].second<<endl;
}
}
int main(void)
{
vector<POINT> p;
int i;
double x,y;
cout<<"输入10个点的坐标\n"<<endl;
for(i=0;i<10;i++)
{
cin>>x>>y;
p.push_back(make_pair(x,y));
}
cout<<endl;
find_convex_hull(p);
return 0;
}
#include<iostream>
#include<stdio.h>
#include<math.h>
#define maxlen 1000
using namespace std;
struct point
{
double x;
double y;
}p[maxlen];
double in_same_line(point a,point b,point c)
{
return ((c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x));
}
bool if_cross(point a,point b,point c,point d)
{
double f1 = in_same_line(a,b,c);
double f2 = in_same_line(a,b,d);
if(f1*f2>=0)
{
return false;
}
else
{
return true;
}
}
bool if_have_cross(int n)
{
for(int i=2;i<n-1;i++)
{
for(int j=1;j<i;j++)
{
if(if_cross(p[i],p[i+1],p[j-1],p[j])==true)
{
return true;
}
}
}
for(int i=1;i<n-2;i++)
{
if(if_cross(p[0],p[n-1],p[i],p[i+1])==true)
{
return true;
}
}
return false;
}
double area(int n)
{
double area = 0;
area=p[0].y*(p[n-1].x-p[1].x);
for(int i=1;i<n;i++)
{
area+=p[i].y*(p[i-1].x-p[(i+1)%n].x);
}
return fabs(area)/ 2.0;
}
int main(void)
{
int n;
int test = 1;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
break;
}
double x,y;
for(int i=0;i<n;i++)
{
scanf("%lf %lf",&x,&y);
p[i].x = x;
p[i].y = y;
}
if(n<3)
{
printf("Figure %d: Impossible\n", test);
test++;
printf("\n");
continue;
}
if(if_have_cross(n)==true)
{
printf("Figure %d: Impossible\n", test);
test++;
printf("\n");
continue;
}
else
{
printf("Figure %d: %.2lf",test,area(n));
test++;
printf("\n");
}
}
return 0;
}
#include<iostream>
#include<math.h>
using namespace std;
struct Wall
{
double x;
double y1;
double y2;
}wall[6];
struct Point
{
double x;
double y;
};
typedef Point point;
void put_in_wall(Wall wall[6])
{
for(int i=0;i<3;i++)
{
wall[i].x = 4;
wall[i+3].x = 7;
}
wall[0].y1 = 0,wall[0].y2 = 4;
wall[1].y1 = 7,wall[1].y2 = 8;
wall[2].y1 = 9,wall[2].y2 = 10;
wall[3].y1 = 0,wall[3].y2 = 3;
wall[4].y1 = 4.5,wall[4].y2 = 6;
wall[5].y1 = 7,wall[5].y2 = 10;
}
double dist(point a,point b)
{
double x = a.x-b.x;
double y = a.y-b.y;
double s = sqrt(x*x+y*y);
return s;
}
double cross_wall(Point a,Point b,Wall w)
{
double wx = w.x;
double y1 = w.y1;
double y2 = w.y2;
if(a.x==b.x)
{
return true;
}
if((wx>a.x && wx<b.x)||(wx<b.x && wx>a.x))
{
double k = (a.y-b.y)/(a.x-b.x);
double b = a.y-k*a.x;
double s = k*wx+b;
if((s>y1 && s<y2)||(s<y1 && s>y2))
{
return s;
}
else
{
return -1;
}
}
else
{
return -1;
}
}
int main(void)
{
Point st,ed,m;
double x,y,sum;
st.x = 0,st.y = 5;
ed.x = 10,ed.y = 5;
put_in_wall(wall);
for(int i=0;i<6;i++)
{
m = ed;
double s = cross_wall(st,ed,wall[i]);
if(s!=-1)
{
ed.x = wall[i].x;
if(fabs(s-wall[i].y1)<fabs(s-wall[i].y2))
{
ed.y = wall[i].y1;
}
else
{
ed.y = wall[i].y2;
}
sum+=dist(st,ed);
st = ed;
ed = m;
i = -1;
}
}
sum+=dist(st,ed);
cout<<"×î¶Ì¾àÀëΪ£º"<<sum<<endl;
return 0;
}
实验四 动态规划算法的实现
一、实验目的和要求
1.理解动态规划的基本思想、动态规划算法的基本步骤。
2.掌握动态规划算法实际步骤。
二、实验预习内容
1.预习ICPC讲义,大致了解动态规划算法的相关内容。
2.掌握动态规划算法基本步骤。
三、实验项目摘要
1.求两个字符串的最长公共子序列。
X 的一个子序列是相应于 X 下标序列{1, 2, …, m}的一个子序列,求解两个序列的所有子序列中长度最大的,例如输入:pear, peach 输出:pea。
2. 给定两个字符串 a 和 b,现将串 a 通过变换变为串 b,可用的操作为,删除串 a 中的一个字符;在串 a 的某个位置插入一个元素;将串 a 中的某个字母换为另一个字母。对于任意的串 a 和串 b,输出最少多少次能够将串变为串 b。
思考:输出变换的步骤。
3.输入一个矩阵,计算所有的子矩阵中和的最大值。
例如,输入
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
输出为:15
思考:当矩阵很大时,比如 100*100 的矩阵,你的程序还能够很快的得出结果吗,如果不能,请思考如何用动态规划的思想解决。
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int leng(string s1,string s2,vector<vector<int> > a)
{
if(s1==""||s2=="")
{
return 0;
}
for(int i=0;i<=s1.length();i++)
{
a[i][0] = 0;
}
for(int i=0;i<=s2.length();i++)
{
a[0][i] = 0;
}
for(int i=1;i<=s1.length();i++)
{
for(int j=1;j<=s2.length();j++)
{
if(s1[i-1]==s2[j-1])
{
a[i][j] = a[i-1][j-1]+1;
}
else
{
if (a[i-1][j]>=a[i][j-1])
{
a[i][j] = a[i-1][j];
}
else
{
a[i][j] = a[i][j-1];
}
}
}
}
return a[s1.length()][s2.length()];
}
int main(void)
{
string s1,s2;
getline(cin,s1);
getline(cin,s2);
vector<vector<int> > a(s1.length()+1,vector<int>(s2.length()+1));
int s = leng(s1,s2,a);
cout<<"最长公共子序列:"<<s<<endl;
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
int get_min(int a,int b,int c)
{
if(a<b)
{
if(a<c)
{
return a;
}
else
{
return c;
}
}
else
{
if(b<c)
{
return b;
}
else
{
return c;
}
}
}
void get_way(string s1,string s2,vector<vector<int> > a)
{
int i,j;
int con,del,ins,sub;
a[0][0] = 0;
for(i=1;i<=s1.length();i++)
{
a[i][0] = i;
}
for(j=1;j<=s2.length();j++)
{
a[0][j] = j;
}
for(i=1;i<=s1.length();i++)
{
for(j=1;j<=s2.length();j++)
{
if(s1[i-1]==s2[j-1])
{
con = 0;
}
else
{
con = 1;
}
del = a[i-1][j]+1;
ins = a[i][j-1]+1;
sub = a[i-1][j-1]+con;
a[i][j] = get_min(del,ins,sub);
}
}
cout<<"The least times\n"<<a[s1.length()][s2.length()]<<endl;
}
int main(void)
{
string s1,s2;
cout<<"Input the first string"<<endl;
cin>>s1;
cout<<"Input the second string"<<endl;
cin>>s2;
vector<vector<int> > a(s1.length()+1,vector<int>(s2.length()+1));
get_way(s1,s2,a);
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
int get_max(vector<int> a,int n)
{
int ma=0,b=0;
for(int i=0;i<n;i++)
{
if(b>0)
{
b = b+a[i];
}
else
{
b = a[i];
}
if(b>ma)
{
ma = b;
}
}
return ma;
}
int main(void)
{
int n;
int i,j,k;
int m1,m2;
cout<<"输入横纵坐标长度"<<endl;
cin>>n;
vector<vector<int> > a(n,vector<int>(n));
vector<int> s(n);
cout<<"输入坐标:"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
m1 = 0;
m2 = 0;
for(i=0;i<n;i++)
{
for(k=0;k<n;k++)
{
s[k]=0;
}
for(j=i;j<n;j++)
{
for(k=0;k<n;k++)
{
s[k] = s[k]+a[j][k];
}
m2 = get_max(s,n);
if(m2>m1)
{
m1 = m2;
}
}
}
cout<<"最大子矩阵和为\n"<<m1<<endl;
return 0;
}