PTA团体程序设计天梯赛篇(二)----数据结构

数据结构

这是二叉搜索树吗?

题目链接
解题思路:
此题我们知道了二叉搜索树的性质:左子树小于根,右子树大于等于根。
且输入的是前序遍历,则对一个二叉树[l,r]:a[l]是根,[l+1,r]是左右子树范围。
其中,前x项若都小于根,剩下的都大于等于根:则从l+1开始的前x个就是左子树,剩下的都是右子树。如此就分出了左右子树[l1,r1][l2,r2],然后再对左右子树递归即可。

由于输出要后序遍历,则我们只需:递归左子树,递归右子树,存根 (按照后序遍历的顺序)即可。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;

const int N = 10010;

int n, flag;
int t[N];
vector<int>ans;

void solve(int l, int r) {
   
	if (r < l)
		return ;
	int i = l + 1, j = r;

	if (!flag) {
   
		while (i <= r && t[i] < t[l]) // 最后停止在大于等于的位置,比正确位置后移了一位
			i++;
		while (j > l && t[j] >= t[l]) // 最后停在小于的位置,比正确位置前移了一位
			j--;
		if (i - j != 1) // 只有当两者相差1也就是中间没有元素的时候才正确
			return ;
		solve(l + 1, i - 1);
		solve(j + 1, r);
		ans.push_back(t[l]); 
	} else {
   
		while (i <= r && t[i] >= t[l])
			i++;
		while (j > l && t[j] < t[l])
			j--;
		if (i - j != 1)
			return ;
		solve(l + 1, i - 1);
		solve(j + 1, r);
		ans.push_back(t[l]);
	}
}

int main() {
   
	int f = 0;
	cin >> n;
	for (int i = 1 ; i <= n ; ++i)
		cin >> t[i];
	solve(1, n);
	if (ans.size() == n) {
   
		puts("YES");
		for (auto x : ans) {
   
			if (f)
				cout << " ";
			cout << x;
			f++;
		}
	} else {
   
		ans.clear();
		flag = 1;
		solve(1, n);
		if (ans.size() == n) {
   
			puts("YES");
			for (auto x : ans) {
   
				if (f)
					cout << " ";
				cout << x, f++;
			}
		} else
			puts("NO");
	}
	return 0;
}

树的遍历

题目链接

解题思路:

思路:(1)通过后序遍历找到根结点,再通过根结点在中序遍历的位置找出左子树、右子树。(2)根据左子树在后序遍历结果的顺序,找到左子树的根结点,视左子树为一棵独立的树,转步骤(1)。(3)根据右子树在后序遍历结果的顺序,找到右子树的根结点,视右子树为一棵独立的树,转步骤(1)。

注意点:
函数参数需要一个创建二叉树的指针 ,和后序的左右指针,以及中序的左右指针,5个参数。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdlib>
using namespace std;

const int N = 1010;

int n ;
int in[N], nx[N];

typedef struct node {
   
	int val;
	struct node *l, *r;
} BitNode, *BiTree;


void build(BiTree &T, int L1, int R1, int L2, int R2) {
   
	T = (BiTree)malloc(sizeof (BitNode));
	T->val = nx[R1];
	int in_root ;
	for (int i = L2 ; i <= R2 ; ++i)
		if (in[i] == nx[R1]) {
   
			in_root = i ;
			break;
		}

	if ( in_root - L2 != 0 ) {
   
		build(T->l, L1, L1 + in_root - L2 - 1,  L2,  in_root - 1);
	} else
		T->l = NULL;

	if (R2 - in_root != 0) {
   
		build(T->r, R1 - (R2 - in_root), R1 - 1, in_root + 1, R2 );
	} else
		T->r = NULL;
}

void bfs(BiTree T) {
   
	queue<BiTree>q;
	q.push(T);
	int f = 0;
	while (q.size()) {
   
		auto u = q.front();
		q.pop();
		if (f)
			cout << " ";
		cout << u->val;
		f++;
		if (u->l)
			q.push(u->l);
		if (u->r)
			q.push(u ->r);
	}
}

int main() {
   
	BiTree t;
	cin >> n;
	for (int i = 1 ; i <= n ; ++i)
		cin >> nx[i];
	for (int j = 1; j <= n ; ++j
关于 PTA 团体程序设计天梯赛 L2-01 的具体题目及其解答,虽然当前引用未直接提及该题目的具体内容[^1],但从以往的经验来看,L2 级别的题目通常涉及较为复杂的算法逻辑或数据结构应用。 以下是基于经验推测可能的 L2-01 类型题目及解决方案: --- ### 可能的 L2-01 题目描述 假设 L2-01 是一道与 **字符串处理** 或 **动态规划 (DP)** 相关的问题。例如,给定一段文字和若干关键词,要求统计这些关键词在文本中的出现次数,并返回按频率排序的结果列表。 #### 输入格式 - 第一行是一个正整数 N 表示关键词的数量。 - 接下来 N 行每行为一个关键词。 - 最后是一段由多个句子组成的文本。 #### 输出格式 按照关键词出现频次降序排列输出每个关键词及其对应的频次。如果频次相同,则按输入顺序输出。 --- ### 解决方案代码实现 以下提供一种 Python 实现方式来解决上述问题: ```python from collections import Counter def count_keywords(keywords, text): word_list = text.split() # 将文本拆分为单词列表 counter = Counter(word_list) # 使用 Counter 统计词频 result = [] for keyword in keywords: freq = counter[keyword] result.append((keyword, freq)) # 按照频次降序排序,若频次相等则保持原输入顺序 sorted_result = sorted(result, key=lambda x: (-x[1], keywords.index(x[0]))) return sorted_result if __name__ == "__main__": n = int(input().strip()) # 关键词数量 keywords = [input().strip() for _ in range(n)] # 获取关键词 text = input().strip() # 获取文本 output = count_keywords(keywords, text) for item in output: print(f"{item[0]} {item[1]}") ``` 此代码通过 `collections.Counter` 来高效计算关键词在文本中的出现次数,并利用自定义排序规则满足题目需求[^2]。 --- ### 注意事项 1. 如果存在大小写敏感的情况,在预处理阶段可以统一转换为小写字母形式再进行匹配。 2. 对于特殊字符干扰(如标点符号),需提前清理掉无关字符以确保准确性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落春只在无意间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值