PTA 《Data Structures and Algorithms (English)》刷题笔记

PTA

Data Structures and Algorithms (English)

持续更新中

7-1 Maximum Subsequence Sum (25 分)

题目描述 https://pintia.cn/problem-sets/16/problems/663

#include <bits/stdc++.h>
using namespace std;
int r[10005]; //原数组
int a[10005]; //dp数组
int main() {
    int n;
    scanf("%d", &n); //读入n
    //读入n个数
    for(int i=0;i<n;++i) {
        scanf("%d", &a[i]);
        // 存储原数组
        r[i] = a[i];
    }
    int maxi = -1; // 最大的子列和的结尾的下标
    for(int i=0;i<n;++i) {
        if(i > 0) {
        	// 状态转移
            a[i] = max(a[i], a[i] + a[i - 1]);
        }
        // 该项的值不为负数, 且最大下标尚未初始化
        if(a[i] >= 0 && maxi < 0) {
            maxi = i;
        }
        // 最大下标已经初始化, 且
        if(a[i] >= 0 && maxi >= 0 && a[maxi] < a[i]) {
            maxi = i;
        }
    }
    // 输出最大值, 如果全为负数, maxi == -1,  仍为0
    printf("%d ", maxi >= 0 ? a[maxi] : 0);
    // 全为负数
    if(maxi == -1) {
        printf("%d %d\n", r[0], r[n - 1]);
        return 0;
    }
    // 最左边界下标
    int left;
    // 最大序列和
    int maxn = a[maxi];
    //找左边界
    for(int i = maxi;i >= 0; --i) {
        maxn -= r[i];
        if(maxn == 0) {
            left = i;
        }
    }
    // 输出value
    printf("%d %d\n",r[left], r[maxi]);
    return 0;
}

7-2 Reversing Linked List (25 分)

题目描述 https://pintia.cn/problem-sets/16/problems/664

/*
author:Deng Wangtao
problem Link:
*/
#include <bits/stdc++.h>
using namespace std;
#define BEGIN signed main(){
#define END return 0;}
#define vcr vector
#define pb push_back
#define IOS std::ios::sync_with_stdio(false);
#define endl "\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
template<typename T>inline void print(vector<T> a){std::copy(a.begin(),a.end(),std::ostream_iterator<T>(std::cout, " "));}
std::ostream& operator<<(std::ostream& out,const pii& p) {out << "{" << p.first << "," << p.second << "}";return out;}
const int maxn = 1e5+5;

struct Node {
	int data;
	string next;
};
string _address[maxn], _next[maxn];
int _data[maxn];
void solve()
{
	string first;
	int n, r;
	cin >> first >> n >> r;
	map<string ,Node> mp;
	string address, next;
	int data;
	for(int i=0;i< n;++ i) {
		cin >> address >> data >> next;
		mp[address] = {data, next};
	}
	int cnt = 0;
	// 按顺序取出链表, 存在三个数组中
	for (string add = first;add != "-1"; add = mp[add].next) {
		_address[cnt] = add;
		_data[cnt] = mp[add].data;
		_next[cnt] = mp[add].next;
		cnt ++;
	}
	if(r > n) {
		r = n;
	}
	if (r > 1) { 
		for(int i=0;i<=cnt; i += r) {
			//cout << _address[i] << " " << _data[i] << " " << _next[i] << endl; 
			if(!i) {
				continue;
			}
			//反转数据和地址 
			for(int j=i-r, k=i-1;j < k;++j, --k) {
				swap(_address[j], _address[k]);
				swap(_data[j], _data[k]);
			}
		}
	
	}
	// 输出 
	for(int i=0;i<cnt; i ++) {
		cout << _address[i] << " " << _data[i] << " "; 
		cout << (i == cnt - 1 ? "-1" : _address[i + 1]) << endl;
	}
	
}

BEGIN
IOS
	signed T = 1;
//	cin >> T;
//	scanf("%d",&T);
	for(int i=1;i<=T;++i) {
		solve();
	}
END

7-3 Pop Sequence (25 分)

题目描述 https://pintia.cn/problem-sets/16/problems/665

/*
author:Deng Wangtao
problem Link:https://pintia.cn/problem-sets/16/problems/665
*/
#include <bits/stdc++.h>
using namespace std;
#define BEGIN signed main(){
#define END return 0;}
#define vcr vector
#define pb push_back
#define IOS std::ios::sync_with_stdio(false);
#define endl "\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
template<typename T>inline void print(vector<T> a){std::copy(a.begin(),a.end(),std::ostream_iterator<T>(std::cout, " "));}
std::ostream& operator<<(std::ostream& out,const pii& p) {out << "{" << p.first << "," << p.second << "}";return out;}
//const int maxn = 1e5+5;
int a[1005];
int m, n, k;
bool mycheck()
{
	stack<int> stk; //模拟过程 
	int top = 0;
	for(int i=1;i<=n;++i) {
		stk.push(i);
		if(stk.size() > m) {
			return false;
		}
		while(stk.size() && top < n && stk.top() == a[top]) {
			top ++;
			stk.pop();
		}
	}
	return stk.size() == 0;
}
void solve()
{
	cin >> m >> n >> k;
	while(k--) {
		for(int i=0;i<n;++i) {
			cin >> a[i];
		}
		cout << (mycheck() ? "YES" : "NO") << endl;
	}
}

BEGIN
IOS
	signed T = 1;
//	cin >> T;
//	scanf("%d",&T);
	for(int i=1;i<=T;++i) {
		solve();
	}
END


7-4 List Leaves (25 分)

题目描述 https://pintia.cn/problem-sets/16/problems/666

/*
author:Deng Wangtao
problem Link:
*/
#include <bits/stdc++.h>
using namespace std;
#define BEGIN signed main(){
#define END return 0;}
#define vcr vector
#define pb push_back
#define IOS std::ios::sync_with_stdio(false);
#define endl "\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
template<typename T>inline void print(vector<T> a){std::copy(a.begin(),a.end(),std::ostream_iterator<T>(std::cout, " "));}
std::ostream& operator<<(std::ostream& out,const pii& p) {out << "{" << p.first << "," << p.second << "}";return out;}
const int maxn = 105;
int n;
int _left[maxn], _right[maxn];
bool isExist[maxn];
int tree[maxn];
int maxIdx = 1;

bool isNumber(char c) {
	return (c >= '0' && c <= '9');
}

int createTree(int root, int idx) {
	tree[idx] = root;
	maxIdx = max(maxIdx, idx);
//	printf("创建%d, %d\n", idx, root);
	if (_left[root] >= 0) {
		createTree(_left[root], idx << 1);
	}
	if(_right[root] >= 0) {
		createTree(_right[root], idx << 1 | 1);
	}
	return 1;
}

//二叉树层序遍历 
void levelTraversal () {
	vi output;
	for(int i=1;i<=maxIdx;++i) {
		if(tree[i] >= 0 && tree[i << 1] == -1 && tree[i << 1 | 1] == -1) {
			output.pb(tree[i]);
		}
	}
	for(int i=0;i<output.size(); ++ i) {
		if(i==0) {
			cout << output[i];
			continue;
		}
		cout << " " << output[i];
	}
}

void solve()
{
	// 全部填充-1 
	memset(_left, -1, sizeof _left);
	memset(_right, -1, sizeof _right);
	memset(tree, -1, sizeof tree);
	cin >> n;
	char l, r;
	for(int i=0;i<n;++i) {
		cin >> l >> r;
		if(isNumber(l)) {
			_left[i] = l - '0';
			// 标记, l- '0'一定不是根节点 
			isExist[l - '0'] = 1;
		}
		if(isNumber(r)) {
			_right[i] = r- '0';
			isExist[r - '0'] = 1;
		}
	}
	// 找根节点 
	int root = -1;
	for(int i=0;i<n;++i) {
		if(!isExist[i]) {
			root = i;
			break;
		}
	}
	//创造树 
	createTree(root, 1);
	levelTraversal();
}

BEGIN
IOS
	signed T = 1;
//	cin >> T;
//	scanf("%d",&T);
	for(int i=1;i<=T;++i) {
		solve();
	}
END


7-5 Tree Traversals Again (25 分)

题目描述 https://pintia.cn/problem-sets/16/problems/667

/*
author:Deng Wangtao
problem Link:
*/
#include <bits/stdc++.h>
using namespace std;
#define BEGIN signed main(){
#define END return 0;}
#define vcr vector
#define pb push_back
#define IOS std::ios::sync_with_stdio(false);
#define endl "\n"
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
template<typename T>inline void print(vector<T> a){std::copy(a.begin(),a.end(),std::ostream_iterator<T>(std::cout, " "));}
std::ostream& operator<<(std::ostream& out,const pii& p) {out << "{" << p.first << "," << p.second << "}";return out;}
//const int maxn = 1e5+5;
int n;
int tree[10005];
vi res;
void postorder(int idx) {
	if(tree[idx << 1] != -1) {
		postorder(idx << 1);
	}
	if(tree[idx << 1 | 1] != -1) {
		postorder(idx << 1 | 1);
	}
	res.pb(tree[idx]);
}

void solve()
{
	memset(tree, -1, sizeof tree);
	string op;
	cin >> n;
	int a;
	int idx = 0, maxIdx = -1;
	stack<int> stk;
	for(int _ = 0; _ < n * 2; ++ _) {
		cin >> op;
		if(op == "Push") {
			//Push a
			cin >> a;
			if(idx == 0) {
				idx = 1;
			} else {
				idx <<= 1;
				if(tree[idx] != -1) {
					idx |= 1;
				} 
			}
			maxIdx = max(maxIdx, idx);
			tree[idx] = a;
			stk.push(idx);
		} else {
			//Pop
			idx = stk.top();
			stk.pop();
		}
	}
	//	for(int i=1;i<=maxIdx; ++i) {
	//		cout << tree[i] << " ";
	//	}
	postorder(1);
	for(int i=0;i<res.size(); ++i) {
		if(!i) {
			cout << res[i];
		} else {
			cout << " " << res[i];
		}
	}
}

BEGIN
IOS
	signed T = 1;
//	cin >> T;
//	scanf("%d",&T);
	for(int i=1;i<=T;++i) {
		solve();
	}
END


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值