PEAK6 2020校招OA

PEAK6 2020.8.31 OA
两题都做出来。

  1. 情景问题

有一个机场,只有1条无限长的跑道。一个list存飞机降落时间,另一个list存飞机起飞时间。飞机至少停靠1分钟(如果两个list中存在相同时间,则肯定不是同一架飞机)。有一个常数M。如果飞机在跑道上停靠时间超过M,则必须使用一个停机位。另外有一个常数S,表示一开始已经有S个停机位被占用。每当有飞机起飞,如果它之前占用停机位,则该停机位可以立刻再被其他飞机使用。

返回最少需要几个停机位。

我当时使用greedy来解的。第一题做了好久,不知道会不会挂掉平台不包含的隐层case。

  1. String问题。

有三个list。第一个list是需要处理的词。第二个list是同义词,第三个list是反义词。第二个第三个list中每一个string遵循如下格式"key:str1,str2,str3…",表示str1、str2等等是key的同义词或反义词。

定义kangaroo包含如下:给定String a和b。
如果a中不连续包含b,则视为kangaroo包含。
比如"team" kangaroo包含"em".
如果是连续包含,则不能成为kangaroo包含。
比如"team"不kangaroo包含"eam".
但是如果a中可以通过拆分后不连续包含得到b,则视为kangaroo包含。
比如"teamatem" kangaroo包含"eam".

有三种得分方式。第一个list中的单词,kangaroo包含其同义词,加1分;kangaroo包含其反义词,减掉1分;如果一个在第二个list中出现的词(不是key)被两个不同的第一个list中单词kagaroo包含,每出现一次这种pair,加一分。

返回第一个list中单词的总得分。

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;


class a {

    /*
     * Complete the 'findKangarooScore' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts following parameters:
     *  1. STRING_ARRAY words
     *  2. STRING_ARRAY wordsToSynonyms
     *  3. STRING_ARRAY wordsToAntonyms
     */

    public static int findKangarooScore(List<String> words, List<String> wordsToSynonyms, List<String> wordsToAntonyms) {
        int res = 0;
        int n = words.size();
        if (n <= 0) return 0;
        HashMap<String, List<String>> synonyms = new HashMap();
        HashMap<String, List<String>> antonyms = new HashMap();
        for (String s: wordsToSynonyms) {
            String[] arr = s.split(":|,");
            List<String> neighbor = new ArrayList();
            for (int i = 1; i < arr.length; i++)
                neighbor.add(arr[i].toLowerCase());
            synonyms.put(arr[0].toLowerCase(), neighbor);
        }
        for (String s: wordsToAntonyms) {
            String[] arr = s.split(":|,");
            List<String> neighbor = new ArrayList();
            for (int i = 1; i < arr.length; i++)
                neighbor.add(arr[i].toLowerCase());
            antonyms.put(arr[0].toLowerCase(), neighbor);
        }
        HashMap<String, Integer> cousin_cnt = new HashMap();
        for (int i = 0; i < n; i++) {
            String key = words.get(i).toLowerCase();
            if (synonyms.containsKey(key)) {
                List<String> neighbors = synonyms.get(key);
                for (String nei: neighbors) {              
                    if (contains(key, nei)) {
                        res++;
                        int cousin_score = cousin_cnt.getOrDefault(nei, 0);
                        cousin_cnt.put(nei, cousin_score+1);
                        res += cousin_score;
                    }
                }
            }
            if (antonyms.containsKey(key)) {
                List<String> neighbors = antonyms.get(key);
                for (String nei: neighbors) {              
                    if (contains(key, nei)) {
                        res--;
                    }
                }
            }
        }
        return res;
    }

    public static boolean contains(String s, String pattern) {
        int m = s.length();
        int n = pattern.length();
        if (m <= n) return false;
        int p = 0;
        int q = 0;
        while (p < m && q < n) {
            if (s.charAt(p) == pattern.charAt(q)) {
                p++;
                q++;
            } else
                p++;
        }
        if (p >= m && q < n) return false;
        int idx = s.indexOf(pattern);
        if (idx < 0) return true;
        System.out.println("idx is " + idx);
        int lastChar_lastIdx = s.lastIndexOf(pattern.charAt(n-1));
        System.out.println("lastChar_lastIdx is " + lastChar_lastIdx);
        if (idx+n-1 == lastChar_lastIdx)
            return false;
        return true;
    }
    
    
}
public class Main {
    public static void main(String[] args) throws IOException {
        boolean flag = a.contains("devilishly", "evil");
        System.out.println(flag);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int wordsCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> words = IntStream.range(0, wordsCount).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        int wordsToSynonymsCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> wordsToSynonyms = IntStream.range(0, wordsToSynonymsCount).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        int wordsToAntonymsCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> wordsToAntonyms = IntStream.range(0, wordsToAntonymsCount).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        int result = a.findKangarooScore(words, wordsToSynonyms, wordsToAntonyms);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40136685/article/details/108335194