• 网易2018校园招聘编...


    [编程题]游历魔法王国

    https://www.nowcoder.com/questionTerminal/f58859adc39f4edc9cd8e40ba4160339

    给定一棵树,可以走L步,最多能访问多少个节点

    思路:分类讨论,首先贪心走最长链,如果还有多余的步数,可以知道如果从最底端返回上面再访问节点的话,这样不是最优,应该是在最长链的某个点就走进去然后再回来,如果多了k步,那么最多可以访问k / 2个节点,因为一去一回需要2步,相当于回到这个节点

    #include <bits/stdc++.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    const int maxn = 50 + 20;
    struct Node {
        int u, v, tonext;
    } e[maxn * 2];
    int first[maxn], num;
    void addEdge(int u, int v) {
        e[num].u = u, e[num].v = v, e[num].tonext = first[u];
        first[u] = num++;
    }
    int n, L;
    int dp[maxn];
    
    void dfs(int cur, int sel) {
        dp[cur] = sel;
        for (int i = first[cur]; ~i; i = e[i].tonext) {
            dfs(e[i].v, sel + 1);
        }
    }
    
    void work() {
        memset(first, -1, sizeof first);
        cin >> n >> L;
        for (int i = 0; i <= n - 2; ++i) {
            int fa;
            scanf("%d", &fa);
            addEdge(fa, i + 1);
        }
        dfs(0, 1);
        int mx = 0;
        for (int i = 0; i < n; ++i) mx = max(mx, dp[i]);
        L++;
        if (L <= mx) {
            printf("%d
    ", L);
        } else printf("%d
    ", min(n, mx + (L - mx) / 2));
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        work();
        return 0;
    }
    View Code

    [编程题]最长公共子括号序列

    https://www.nowcoder.com/questionTerminal/504ad6420b314e5bb614e1684ad46d4d

    观察到答案肯定是lenstr - 1

    那么暴力移除一个括号,插入到字符序列的任意位置,可以维持原长度,而且左括号 = 右括号

    所以能枚举到每一种LCS = lenstr - 1的情况

    #include <bits/stdc++.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    const int maxn = 50 + 2;
    char str[maxn], sub[maxn];
    int dp[maxn][maxn];
    int calc(char str[], char sub[], int lenstr) {
        memset(dp, -1, sizeof dp);
        dp[0][0] = 0;
        for (int i = 1; i <= lenstr; ++i) {
            for (int j = 1; j <= lenstr; ++j) {
                if (str[i] == sub[j]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                }
                dp[i][j] = max(dp[i][j], dp[i - 1][j]);
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
        }
        return dp[lenstr][lenstr];
    }
    int lenstr;
    int ans;
    char op[2222];
     
    bool check() {
        int lef = 0;
        for (int i = 1; i <= lenstr; ++i) {
            if (sub[i] == '(') lef++;
            else lef--;
            if (lef < 0) return false;
        }
        return lef == 0;
    }
     
    void did(int from, int to) {
        strcpy(sub + 1, str + 1);
        if (from > to) {
            char ch = sub[from];
            for (int i = from; i >= to; --i) {
                sub[i] = sub[i - 1];
            }
            sub[to] = ch;
        } else {
            char ch = sub[from];
            for (int i = from + 1; i <= to; ++i) {
                sub[i - 1] = sub[i];
            }
            sub[to] = ch;
        }
    }
    map<string, bool> mp;
    void work() {
        scanf("%s", str + 1);
        lenstr = strlen(str + 1);
        strcpy(sub + 1, str + 1);
        for (int i = 1; i <= lenstr; ++i) {
            for (int j = 1; j <= lenstr; ++j) {
                if (i == j) continue;
                did(i, j);
                if (check() && mp.find(sub + 1) == mp.end() && calc(str, sub, lenstr) == lenstr - 1) {
                    mp[sub + 1] = true;
                    ans++;
                }
            }
        }
        printf("%d
    ", ans);
    }
     
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        op['('] = ')';
        op[')'] = '(';
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    透视表提取不反复记录(1)-出现值
    ORA-38760: This database instance failed to turn on flashback database
    Android蓝牙串口程序开发
    指尖上的电商---(5)schema.xml配置具体解释
    iOS-UIImage imageWithContentsOfFile 和 imageName 对照
    JSON-RPC轻量级远程调用协议介绍及使用
    POJ 2296 Map Labeler(2-sat)
    接口測试-HAR
    [Leetcode]Combination Sum II
    MarkDown、Vim双剑合璧
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/7754111.html
Copyright © 2020-2023  润新知