算法导论(problems 6-3 Young tableaus)

Here is the solution for the problem.As for the (e) of this problem,the process is easy to implement.We just insert the orginal element into the tableaus and then extract the minimum.When the tableaus is empty,we could get the sorted lists.


#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<xfunctional>
using namespace std;

typedef struct tableau{
	int row;
	int col;
	int t_size;
	vector<vector<int>> elem;
}tableau;

pair<int,int> search(tableau& tab,int data){
	int i = tab.row - 1;
	int j = 0;
	pair<int, int> res;
	while (i >= 0 && j < tab.col){
		if (tab.elem[i][j] == data){
			res.first = i;
			res.second = j;
			return res;
		}
		else if (tab.elem[i][j]>data) i--;
		else j++;
	}
	res.first = -1;
	res.second = -1;
	return res;
}

void read_data(tableau tab){
	for (int i = 0; i < tab.row; i++){
		for (int j = 0; j < tab.col; j++){
			if (tab.elem[i][j] == INT_MAX){
				cout << "inf" << " ";
			}
			else cout << tab.elem[i][j] << " ";
		}
		cout << endl;
	}
}

int Extract_min(tableau &tab){
	if (tab.t_size == 0) return -1;
	tab.t_size--;
	int i = 0;
	int j = 0;
	int temp = tab.elem[i][j];
	while (i<tab.row-1&&j<tab.col-1){
		if (tab.elem[i + 1][j]<tab.elem[i][j + 1]){
			tab.elem[i][j] = tab.elem[i+1][j];
			i++;
		}
		else{
			tab.elem[i][j] = tab.elem[i][j+1];
			j++;
		}
	}
	if (i == tab.row - 1){
		while (j < tab.col - 1){
			tab.elem[i][j] = tab.elem[i][j+1];
			j++;
		}
	}
	if (j == tab.col - 1){
		while (i < tab.row - 1){
			tab.elem[i][j] = tab.elem[i+1][j];
			i++;
		}
	}
	tab.elem[i][j] = INT_MAX;
	return temp;
}

void insert(tableau& tab,int t){
	if (tab.t_size >= tab.col*tab.row) return;
	int i = tab.row - 1;
	int j = tab.col - 1;
	tab.elem[i][j] = t;
	while (i >0 && j >0){
		if (tab.elem[i - 1][j] > tab.elem[i][j - 1]){
			if (t <= tab.elem[i - 1][j]){ 
				tab.elem[i][j] = tab.elem[i - 1][j];
				i--; 
			}
			else break;
		}
		else{
			if (t <= tab.elem[i][j - 1]){
				tab.elem[i][j] = tab.elem[i][j-1];
				j--;
			}
			else break;
		}
	}
	if (i == 0){
		while (j > 0){
			if (t < tab.elem[i][j-1]){
				tab.elem[i][j] = tab.elem[i][j-1];
				j--;
			}
			else break;
		}
	}
	if (j == 0){
		while (i > 0){
			if (t < tab.elem[i-1][j]){
				tab.elem[i][j] = tab.elem[i-1][j];
				i--;
			}
			else break;
		}
	}
	tab.elem[i][j] = t;
}

int main(){
	tableau tab;
	cout << "Input the row and col of the matrix:";
	cin >> tab.row >> tab.col;
	tab.elem = vector<vector<int>>(tab.row, vector<int>(tab.col, INT_MAX));
	cout << "Input the amount of the data:";
	int amount;
	cin >> amount;
	tab.t_size = amount;
	vector<int> temp;
	for (int i = 0; i < amount; i++){
		int t;
		cin >> t;
		temp.push_back(t);
	}
	sort(temp.begin(),temp.end());
	int cur = 0;
	for (int i = 0; i < tab.row; i++){
		for (int j = 0; j < tab.col; j++){
			tab.elem[i][j] = temp[cur];
			cur++;
			if (cur == amount) break;
		}
		if (cur == amount) break;
	}
	cout << endl;
	cout << "before extract:" << endl;
	read_data(tab);
	cout << endl;
	int a = Extract_min(tab);
	cout << "after extract:" << endl;
	read_data(tab);
	cout << endl;
	cout << "after insert 100"<< endl;
	insert(tab,100);
	read_data(tab);
	cout << endl;
	cout << "Input the number you want to search:";
	int data;
	cin >> data;
	pair<int, int> res;
	res = search(tab,data);
	if (res.first == -1){
		cout << "Not found" << endl;
	}
	else{
		cout << "position: " << res.first << " " << res.second << endl;
	}
	system("pause");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值