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

来源:互联网 发布:巫师3和老滚5 知乎 编辑:程序博客网 时间:2024/06/05 14:51

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