设为首页 加入收藏

TOP

2019秋季PAT甲级_C++题解(四)
2019-09-14 00:52:03 】 浏览:191
Tags:2019 秋季 PAT 甲级 题解
igures 1 and 2 correspond to the samples 1 and 2, respectively.

image
image

Output Specification

For each case, print in a line the postfix expression, with parentheses reflecting the precedences of the operators.There must be no space between any symbols.

Sample Input 1

8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1

Sample Output 1

(((a)(b)+)((c)(-(d))*)*)

Sample Input 2

8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1

Sample Output 2

(((a)(2.35)*)(-((str)(871)%))+)

题目思路

  • 题库 1130 Infix Expression (25 分)
  • 输入每个结点的左右孩子来建树,用 bool 数组记录结点是否在其他结点的孩子结点中出现过,都没有出现过的即为根结点
  • 在后序遍历基础上要添加括号来表示优先级
  • 一般是每进入一个子树,就用括号将这个子树括起来,然后递归进入左子树和右子树,递归返回后输出根节点内容
  • 注意:表达式中会出现 正负号,此时不能在子结点返回后才输出正负号——特判:当结点左子树为空而右子树不为空时,先输出根节点内容,再进入右子树

AC代码

#include<iostream>
using namespace std;
struct Node{
    string data;
    int lchild, rchild;
} node[21];
bool occured[21] = {false};
void postorder(int root){
    cout << "(";
    if (node[root].lchild == -1 && node[root].rchild != -1){
        cout << node[root].data;
        postorder(node[root].rchild);
        cout << ")";
    }
    else{
        if (node[root].lchild != -1) postorder(node[root].lchild);
        if (node[root].rchild != -1) postorder(node[root].rchild);
        cout << node[root].data << ")";
    }
}
int main()
{
    int n, root;
    string data;
    scanf("%d", &n);
    for (int i = 1; i < n + 1; i++){
        cin >> node[i].data >> node[i].lchild >> node[i].rchild;
        if (node[i].lchild != -1) occured[node[i].lchild] = true;
        if (node[i].rchild != -1) occured[node[i].rchild] = true;
    }
    for (root = 1; root < n + 1 && occured[root]; root++);
    postorder(root);
    return 0;
}

7-4 Dijkstra Sequence (30 分)

Dijkstra's algorithm is one of the very famous greedy algorithms. It is used for solving the single source shortest path problem which gives the shortest paths from one particular source vertex to all the other vertices of the given graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.

In this algorithm, a set contains vertices included in shortest path tree is maintained. During each step, we find one vertex which is not yet included and has a minimum distance from the source, and collect it into the set. Hence step by step an ordered sequence of vertices, let's call it Dijkstra sequence, is generated by Dijkstra's algorithm.

On the other hand, for a given graph, there could be more than one Dijkstra sequence. For example, both { 5, 1, 3, 4, 2 } and { 5, 3, 1, 2, 4 } are Dijkstra sequences for the graph, where 5 is the source. Your job is to check whether a given sequence is Dijkstra sequence or not.

Input Specification

Each input file contains one test case. For each case, the first line contains two positive integers \(N_v(\le 10^3)\) and \(N_e(\le 10^5)\), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to \(N_v\)

Then \(N_e\) lines follow, each describes an edge by giving the indices of the vertices at the two ends, followed by a positive integer weight (≤100) of the edge. It is guaranteed that the given graph is connected.

Finally the number of queries, K, is given as a positive integer no larger than 100, followed by K lines of sequences, each contains a permutationof the \(N_v\) vertices. It is assumed that the first vertex is the source for each sequence.

All the inputs in a line are separated by a space.

Output Specification

For each of the K seque

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[基础]C++:名字的作用域 下一篇CUDA -- 内存分配

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目