1.哈希应用-开放定址法
2.字符串匹配-哈希算法
3.加油站问题
/*
你需要驾驶一辆卡车行驶L单位距离。最开始时,卡车上有P单位的汽油。卡车每开1单位距离
需要消耗1单位的汽油。如果在途中车上的汽油耗尽,卡车就无法继续前行,因而无法到达终点。
在图中一共有N个加油站。第i个加油站在距离起点Ai单位距离的地方,最多可以给卡车加Bi单位
汽油。假设卡车的燃料箱的容量是无限大的。请问卡车能否到达终点?如果可以最小需要加多少次油,
否则输出-1;
思路:我们先假设卡车在有油的情况下一直跑,知道油耗尽的时候,再考虑在经过的加油站中,
能加油最大的加油站加油,所以需要一个优先队列,没经过一个加油站则放进队列中
*/
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int L, P, N;
cin >> L >> P >> N;
int *A = new int[N + 1];
int *B = new int[N + 1];
A[N] = L;
B[N] = 0;
for (int i = 0; i<N; i++)
{
cin >> A[i];
cin >> B[i];
}
//记录油箱里面到油到单位
int tank = P;
//答案
int ans = 0;
//记录当前位置
int pos = 0;
priority_queue<int> que;
for (int i = 0; i < N + 1; i++)
{
//距离下个加油站到距离
int dis = A[i] - pos;
while (tank < dis)
{
if (que.empty())
{
cout << "-1" << endl;
system("pause");
return 0;
}
tank += que.top();
que.pop();
ans++;
}
tank -= dis;
pos = A[i];
que.push(B[i]);
}
cout << ans << endl;
system("pause");
}
4.快速排序
#include <iostream>//快速排序
#include <vector>
using namespace std;
void sort(vector<int> &a, int left, int right) {
if (left >= right) //表明数据只有一个元素了,完成
return;
int i = left;
int j = right;
int key = a[left];
while (i < j) { /*控制在当组内寻找一遍*/
while (i < j && key <= a[j])
j--;
a[i] = a[j];
while (i < j && key >= a[i])
i++;
a[j] = a[i];
}
a[i] = key;/*当在当组内找完一遍以后就把中间数key回归*/
sort(a, left, i - 1); //左边数组继续排序,i是不用排的
sort(a, i + 1, right); //右边数组继续排序,i是不用排的
}
int main()
{
int n;
cin >> n;
int num;
for (int i = 0; i < n; i++) {
vector<int> vec;
while (cin >> num) {
vec.push_back(num);
if (cin.get() == '\n')
break;
}
sort(vec, 0, vec.size()-1);
cout << "[";
for (int j = 0; j < vec.size()-1; j++) {
cout << vec[j] << ", ";
}
cout << vec[vec.size() - 1] << "]" << endl;
}
}
5.旅行商问题-最短距离
#include<iostream>//这道题应该是转载的,但找不到出处了,嘤
#include<iomanip>
#include<string>
#include<vector>
#include<sstream>
#include<cmath>
//第一行输入旅行城市的数目m。(对应的城市编号为0~m-1)
//第二行输入城市间的距离矩阵。
//输出最短距离
using namespace std;
#define MAX_IN 10
class Tsp
{
private:
int city_number; //城市个数
int **distance; //城市距离矩阵
int **process; //求最短路径的过程矩阵
public:
Tsp(int city_number); //构造函数
void correct(); //矫正输入的城市代价矩阵
void printCity(); //打印城市的距离矩阵
void getShoretstDistance(); //动态规划法求最短路径
void printProcess(); //打印过程矩阵
};
//构造函数
Tsp::Tsp(int city_num)
{
int i=0,j=0;
city_number=city_num;
//初始化城市距离矩阵
distance=new int*[city_number];
//cout<<"请输入"<<city_number<<"个城市之间的距离"<<endl;
for(i=-1;i<city_number;i++)
{
distance[i]=new int[city_number];
string s;//读取以逗号为间隔的连续数字
vector<int> v;
getline(cin, s);
istringstream is(s);
int inter;
char ch;
while (is >> inter)
{
v.push_back(inter);
//is >> ch;
if(is.peek()==',')
is.ignore();
}
for(int j=0;j<v.size();j++){
distance[i][j]=v[j];
//cout<<v[j]<<" "<<distance[i][j]<<endl;
}
}
//cout<<distance[0][0]<<endl;
//cout<<distance[0][2]<<endl;
//cout<<distance[1][2]<<endl;
//cout<<distance[2][2]<<endl;
//
//生成过程矩阵
process=new int*[city_number];
for(i=0;i<city_number;i++)
{
process[i]=new int[1<<(city_number-1)];
}
}
//纠正用户输入的城市代价矩阵
void Tsp::correct()
{
int i;
for(i=0;i<city_number;i++)
{
distance[i][i]=0;
}
}
//打印城市距离
//void Tsp::printCity()
//
//{
//
// int i,j;
//
// //打印代价矩阵
//
// cout<<"您输入的城市距离如下"<<endl;
//
// for(i=0;i<city_number;i++)
//
// {
//
// for(j=0;j<city_number;j++)
//
// cout<<setw(3)<<distance[i][j];
//
// cout<<endl;
//
// }
//
//}
//动态规划法求最短路径
void Tsp::getShoretstDistance()
{
int i,j,k;
//初始化第一列
for(i=0;i<city_number;i++)
{
process[i][0]=distance[i][0];
}
//初始化剩余列
for(j=1;j<(1<<(city_number-1));j++)
{
for(i=0;i<city_number;i++)
{
process[i][j]=0x7ffff;//设0x7ffff为无穷大
//对于数字x,要看它的第i位是不是1,通过判断布尔表达式 (((x >> (i - 1) ) & 1) == 1的真值来实现
if(((j>>(i-1))&1)==1)
{
continue;
}
for(k=1;k<city_number;k++)
{
//不能达到k城市
if(((j>>(k-1))&1)==0)
{
continue;
}
if(process[i][j]>distance[i][k]+process[k][j ^ (1 << (k - 1))])
{
process[i][j]=distance[i][k]+process[k][j ^ (1 << (k - 1))];
//cout<<i<<"行"<<j<<"列为:"<<process[i][j]<<endl;
}
}
}
}
//cout<<"最短路径为"<<process[0][(1<<(city_number-1))-1]<<endl;
cout<<process[0][(1<<(city_number-1))-1];
}
//打印过程矩阵
//void Tsp::printProcess()
//
//{
//
// int i,j;
//
// for(j=0;j<1<<(city_number-1);j++)
//
// {
//
// cout<<setw(3)<<j;
//
// }
//
// cout<<endl;
//
// for(i=0;i<city_number;i++)
//
// {
//
// for(j=0;j<1<<(city_number-1);j++)
//
// {
//
// if(process[i][j]==0x7ffff)
//
// process[i][j]=-1;
//
// cout<<setw(3)<<process[i][j];
//
// }
//
// cout<<endl;
//
//
//
// }
//
//}
//主函数
int main()
{
//cout<<"输入城市个数";
int city_number;
cin>>city_number;
Tsp tsp(city_number); //初始化城市代价矩阵
tsp.correct(); //纠正用户输入的代价矩阵
//tsp.printCity(); //打印城市
tsp.getShoretstDistance(); //求出最短路径
//tsp.printProcess(); //打印计算矩阵
//system("pause");
}
6.旅行商问题TSP
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
#define MAX_IN 10
//旅行商问题TSP
class Tsp
{
private:
int city_number; //城市个数
int **distance; //城市距离矩阵
int **process; //求最短路径的过程矩阵
public:
Tsp(int city_number); //构造函数
void correct(); //矫正输入的城市代价矩阵
void printCity(); //打印城市的距离矩阵
void getShoretstDistance(); //动态规划法求最短路径
void printProcess(); //打印过程矩阵
};
//构造函数
Tsp::Tsp(int city_num)
{
int i=0,j=0;
city_number=city_num;
//初始化城市距离矩阵
distance=new int*[city_number];
for(i=0;i<city_number;i++)
{
char t;
distance[i]=new int[city_number];
for(j=0;j<city_number-1;j++)
cin>>distance[i][j]>>t;
cin>>distance[i][city_number-1];
}
//生成过程矩阵
process=new int*[city_number];
for(i=0;i<city_number;i++)
{
process[i]=new int[1<<(city_number-1)];
}
}
//纠正用户输入的城市代价矩阵
void Tsp::correct()
{
int i;
for(i=0;i<city_number;i++)
{
distance[i][i]=0;
}
}
//打印城市距离
void Tsp::printCity()
{
int i,j;
//打印代价矩阵
cout<<"您输入的城市距离如下"<<endl;
for(i=0;i<city_number;i++)
{
for(j=0;j<city_number;j++)
cout<<setw(3)<<distance[i][j];
cout<<endl;
}
}
//动态规划法求最短路径
void Tsp::getShoretstDistance()
{
int i,j,k;
//初始化第一列
for(i=0;i<city_number;i++)
{
process[i][0]=distance[i][0];
}
//初始化剩余列
for(j=1;j<(1<<(city_number-1));j++)
{
for(i=0;i<city_number;i++)
{
process[i][j]=0x7ffff;//设0x7ffff为无穷大
//对于数字x,要看它的第i位是不是1,通过判断布尔表达式 (((x >> (i - 1) ) & 1) == 1的真值来实现
if(((j>>(i-1))&1)==1)
{
continue;
}
for(k=1;k<city_number;k++)
{
//不能达到k城市
if(((j>>(k-1))&1)==0)
{
continue;
}
if(process[i][j]>distance[i][k]+process[k][j ^ (1 << (k - 1))])
{
process[i][j]=distance[i][k]+process[k][j ^ (1 << (k - 1))];
//cout<<i<<"行"<<j<<"列为:"<<process[i][j]<<endl;
}
}
}
}
cout<<process[0][(1<<(city_number-1))-1]<<endl;
}
//打印过程矩阵
void Tsp::printProcess()
{
int i,j;
for(j=0;j<1<<(city_number-1);j++)
{
cout<<setw(3)<<j;
}
cout<<endl;
for(i=0;i<city_number;i++)
{
for(j=0;j<1<<(city_number-1);j++)
{
if(process[i][j]==0x7ffff)
process[i][j]=-1;
cout<<setw(3)<<process[i][j];
}
cout<<endl;
}
}
//主函数
int main(void)
{
int city_number;
while(cin>>city_number)
{
Tsp tsp(city_number); //初始化城市代价矩阵
tsp.correct(); //纠正用户输入的代价矩阵
//tsp.printCity(); //打印城市
tsp.getShoretstDistance(); //求出最短路径
//tsp.printProcess(); //打印计算矩阵
}
return 0;
}
7.图的广度优先遍历-BFS
#include <iostream>//数据结构---图的广度优先遍历
#include <queue>//BFS问题无法通过递归求解
using namespace std;
//本题只考虑无向连通图
//由于图的广度优先遍历不唯一,我们约定对于多邻点的顶点的遍历,按边添加的顺序进行
//广度优先遍历,并且每次从顶点一开始
#define MAX 100
#define START 1
int visited[MAX];//未访问过为0,访问过则visited[i]=1
int matrix[MAX][MAX];//邻接矩阵,有连线则为一
int a[MAX] = {0}, b[MAX] = {0};
void bfs(int start, int n) {
queue<int> q;
int q_top;
cout << start << " ";
visited[start] = 1;
for (int i = 1; i <= n; i++) {
if (matrix[start][b[i]] == 1 && visited[b[i]] == 0) {
q.push(b[i]);
visited[b[i]] = 1;
//cout << b[i] << endl;
//cout << b[n] << endl;
}
}
//与1相邻的顶点入列
while (!q.empty()) {
q_top = q.front();
q.pop();//第一个顶点出列
cout << q_top << " ";//输出
for (int i = 1; i <= n; i++) {
//important
//可能在a中先出现,也可能在b中先出现
//cout << b[5]<< b[6] << endl;
//cout << "[" << i << "]";
if (q_top==a[i]&&matrix[q_top][b[i]] == 1 && visited[b[i]] == 0) {
//q_top==a[i]要保证一一对应,不能随便找个数比较
q.push(b[i]);//找到q.front的邻点,入列
//cout << b[i] << endl;
visited[b[i]] = 1;//访问过则visited变为1
}
if (q_top==b[i]&&matrix[q_top][a[i]] == 1 && visited[a[i]] == 0) {
q.push(a[i]);
//cout << a[i] << endl;
visited[a[i]] = 1;
}
}
}
}
int main() {
int num_vex, num_edge, x, y;
//cout << "Input number of nodes and edges >> ";
cin >> num_vex >> num_edge;
for (int i = 0; i<MAX; i++) {
for (int j = 0; j < MAX; j++) {
matrix[i][j] = 0;
}
}
for (int i = 1; i <= num_vex; i++) {
visited[i] = 0;
}
//cout << "Input edges, " << num_edge << " left >> ";
for (int i = 1; i <= num_edge; i++) {
cin >> x >> y;
matrix[x][y] = matrix[y][x] = 1;
//由于是无向连通图,所以[x][y]和[y][x]都要等于1
a[i] = x; b[i] = y;
//cout << b[i] << endl;
//分开储存
//cout << "Input edges, " << (num_edge - i) << " left >> ";
}
bfs(START, num_edge);//调用函数时应为num_edge,即调用边数
system("pause");
}
//input
//8
//10
//1 6
//1 3
//1 8
//2 8
//3 7
//4 6
//4 5
//5 8
//5 7
//5 6
//output
//1 6 3 8 4 5 7 2
8.图的深度优先遍历
#include <iostream>//数据结构图的深度优先遍历
#include <cstdio>
using namespace std;
int book[100], sum, n, matrix[100][100] = { 0 };
void dfs(int cur)
{
int i;
cout << cur << " ";
sum++;//没访问一个节点sum就加一
if (sum == n)return;//所有的顶点已经访问过直接退出
for (int i = 1; i <= n; i++)
//若此时图中仍有未访问的顶点,则另选一个尚未访问的顶点为新的源点重复操作,直至图中所有顶点均被访问
{
//判断当前顶点cur到顶点i是否有边,并判断顶点i是否已经访问过
if (matrix[cur][i] == 1 && book[i] == 0)
{
book[i] = 1;
dfs(i);
}
}
return;
}
int main()
{
int i, j, m, a, b;
char temp;//读取逗号
cin >> n >> m;//n为顶点的数目,m为边数
//初始化二维矩阵
//matrix[101][101] = { 0 }
/*for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (i == j)matrix[i][j] = 0;
else matrix[i][j] = 0x3f;*/
//读入顶点之间的边
for (i = 1; i <= m; i++)
{
cin >> a >>temp >> b;
matrix[a][b] = 1;
matrix[b][a] = 1; //无向图需要将其对应的点的左边也赋值为1
}
//从顶点1出发
book[1] = 1;//标记一号顶点已经被访问
dfs(1);//从1号顶点开始遍历
system("pause");
}
//input
//8
//10
//1, 3
//1, 6
//1, 8
//2, 8
//3, 7
//4, 5
//4, 6
//5, 6
//5, 7
//5, 8
//output
//1 3 7 5 4 6 8 2