PAT甲级官网 刷题(3)

PAT1130 Infix Expression

  递归,但是官网有个测试点没过,不知道错在哪里,欢迎指出

#include<iostream>

#define ac cin.tie(0);cin.sync_with_stdio(0);

using namespace std;
const int MAXN = 30;
int le[MAXN], ri[MAXN];
string val[MAXN], ans;
bool vis[MAXN];
int n;

void dfs(int root) {
    
    
    if (root == -1)
        return;

    if (le[root] != -1 || ri[root] != -1) {
    
    
        ans += '(';
        dfs(le[root]);
        ans += val[root];
        dfs(ri[root]);
        ans += ')';
    } else {
    
    
        ans += val[root];
    }
}

int main() {
    
    
    ac
    cin >> n;
    for (int i = 1; i <= n; i++) {
    
    
        cin >> val[i] >> le[i] >> ri[i];
        vis[le[i]] = vis[ri[i]] = true;
    }

    int root;
    for (int i = 1; i <= n; i++)
        if (!vis[i])
            root = i;

    dfs(root);

    if (ans[0] == '(')
        cout << ans.substr(1, ans.length() - 2);
    else
        cout << ans;
    return 0;
}

PAT1143 Lowest Common Ancestor

  很经典的一道题目,先建树,由于是BST,当一个节点大于a并且小于b的时候就是第一个根节点了。

#include<iostream>
#include<unordered_set>

#define ac cin.tie(0);cin.sync_with_stdio(0);
using namespace std;
const int MAXN = 10010;
int le[MAXN], ri[MAXN], val[MAXN];
int N, M, root;
unordered_set<int> set;

int insert(int root, int pos, int v) {
    
    
    if (root == -1) {
    
    
        root = pos;
        val[pos] = v;
        le[pos] = ri[pos] = -1;
        return root;
    }
    if (val[root] > v)
        le[root] = insert(le[root], pos, v);
    else
        ri[root] = insert(ri[root], pos, v);
    return root;
}

int main() {
    
    
    ac
    int a, b;

    cin >> M >> N;

    root = -1;
    for (int i = 1; i <= N; i++) {
    
    
        cin >> a;
        set.insert(a);
        root = insert(root, i, a);
    }

    while (M--) {
    
    
        cin >> a >> b;
        if (!set.count(a) && !set.count(b))
            printf("ERROR: %d and %d are not found.\n", a, b);
        else if (!set.count(a))
            printf("ERROR: %d is not found.\n", a);
        else if (!set.count(b))
            printf("ERROR: %d is not found.\n", b);
        else {
    
    
            int tmp = 1;
            while (true) {
    
    
                if (val[tmp] > max(a, b))
                    tmp = le[tmp];
                else if (val[tmp] < min(a, b))
                    tmp = ri[tmp];
                else {
    
    
                    if (val[tmp] == a)
                        printf("%d is an ancestor of %d.\n", a, b);
                    else if (val[tmp] == b)
                        printf("%d is an ancestor of %d.\n", b, a);
                    else
                        printf("LCA of %d and %d is %d.\n", a, b, val[tmp]);
                    break;
                }
            }
        }
    }
    return 0;
}

PAT1076 Forwards on Weibo

  很值得做的一道图论题,先使用vector邻接表存图,然后使用bfs求粉丝数,使用数组来bfs。

#include<iostream>
#include<vector>
#include<cstring>

#define ac cin.tie(0);cin.sync_with_stdio(0);
using namespace std;
const int MAXN = 1010;
vector<int> G[MAXN];
bool vis[MAXN];
int arr[10 * MAXN];
int N, M, K;

int bfs(int root) {
    
    
    memset(vis, false, sizeof(vis));
    int tt = 0, hh = 0, cnt = 0, level = 0;
    arr[0] = root;
    vis[root] = true;
    while (level <= K && tt <= hh) {
    
    
        int size = hh - tt + 1;
        ++level;
        while (size--) {
    
    
            int top = arr[tt++];
            ++cnt;
            for (int i:G[top])
                if (!vis[i]) {
    
    
                    arr[++hh] = i;
                    vis[i] = true;
                }
        }
    }
    //除去自己
    return cnt - 1;
}

int main() {
    
    
    ac
    int len, a;

    cin >> N >> K;
    for (int i = 1; i <= N; i++) {
    
    
        cin >> len;
        while (len--) {
    
    
            cin >> a;
            G[a].push_back(i);
        }
    }

    cin >> M;
    while (M--) {
    
    
        cin >> a;
        cout << bfs(a);
        if (M)
            cout << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Raymond_YP/article/details/110100310