hihocoder53

http://hihocoder.com/contest/offers53/problems
题目1 : 继承顺位
建树,然后前序遍历

package l531;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        Set dead=new HashSet();
        Map> child = new HashMap>();
        String k=sc.next();
        for(int i=0;i());
                child.get(t2).add(t3);
            } else {
                dead.add(t2);
            }
        }
        
        Listres=new ArrayList();
        dfs(child,k,res);
        for(String t:res) {
            if(!k.equals(t) && !dead.contains(t))
                System.out.println(t);
        }
    }

    private static void dfs(Map> child, String k, List res) {
        res.add(k);
        if(!child.containsKey(k)) return;
        for(String t:child.get(k)) {
            dfs(child,t,res);
        }
    }
}

题目2 : hiho字符串3
递归

package l532;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        for(int i=0;i

题目3 : 最长一次上升子序列
把原问题分解成两个子问题,求0到i的最长下降子序列,和i到n的最长下降子序列,最长下降子序列用最小末尾辅助数组优化到NlogN
一直WA,不知bug在何处

package l533;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        int[]a=new int[n];
        for(int i=0;ihelper[p]) {
                helper[++p] = a[i];
                res[i] = p;
            } else {
                int t=Arrays.binarySearch(helper, 0, p, a[i]);
                int pos=t>0?t:-(t+1);
                helper[pos] = a[i];
                res[i] = pos;
            }
        }
        return res;
    }
}

你可能感兴趣的:(hihocoder53)