Contest 152

2019-09-01 20:59:55

Overall experience: recent find themselves participating in the contest level or severe enough, especially when dealing with something unusual when he met TLE, MLE is how to effectively conduct Debug need to exercise.

important point:

1) TLE, first of all can be resolved with a special judge, the need to consider the special case in good practical problems, the solution is obviously not meeting the filter; the second is the use of optimized data structure, such as this competition to use bit computing.

2) 1e9 + 7 do not use the long / double to avoid, at present, some methods have each taking adder MOD, or use bigInteger.

3) not enough time to grasp, each wrote a third title to no time.

  • 1177. Can Make Palindrome from Substring

Problem Description:

Problem Solving:

Solution a: not considered k> = 13 when directly returns True special cases, resulting in TLE. Plus special sentence can be too. But the problem is that this solution is the worst time complexity is O (mn).

    public List<Boolean> canMakePaliQueries(String s, int[][] queries) {
        List<Boolean> res = new ArrayList<>();
        char[] chs = s.toCharArray();
        for (int[] q : queries) {
            res.add(helper(chs, q[0], q[1], q[2]));
        }
        return res;
    }

    private boolean helper(char[] chs, int s, int e, int k) {
        if (k >= 13) return true;
        Set<Character> set = new HashSet<>();
        for (int i = s; i <= e; i++) {
            char c = chs[i];
            if (!set.add(c)) set.remove(c);
        }
        return set.size() / 2 <= k;
    }

解法二:比较好的解法是维护一个前缀和,记录前缀的字符出现的次数,之后在查询的时候就不需要再遍历s了,时间复杂度可以降到O(m)。

    public List<Boolean> canMakePaliQueries(String s, int[][] queries) {
        List<Boolean> ans = new ArrayList<>(); 
        int[][] cnt = new int[s.length() + 1][26];
        for (int i = 0; i < s.length(); ++i) {
            cnt[i + 1] = cnt[i].clone(); // copy previous sum.
            ++cnt[i + 1][s.charAt(i) - 'a'];
        }
        for (int[] q : queries) {
            int sum = 0; 
            for (int i = 0; i < 26; ++i) {
                sum += (cnt[q[1] + 1][i] - cnt[q[0]][i]) % 2;
            }
            ans.add(sum / 2 <= q[2]);
        }
        return ans;
    }

 

  • 1178. Number of Valid Words for Each Puzzle

问题描述:

 

问题求解:

解法一:BF,TLE。

    public List<Integer> findNumOfValidWords(String[] words, String[] puzzles) {
        List<Integer> res = new ArrayList<>();
        int n = words.length;
        int m = puzzles.length;
        int[][] freqw = new int[n][26];
        int[][] freqq = new int[m][26];
        for (int i = 0; i < n; i++) {
            for (char c : words[i].toCharArray()) {
                freqw[i][c - 'a']++;
            }
        }
        for (int i = 0; i < m; i++) {
            for (char c : puzzles[i].toCharArray()) {
                freqq[i][c - 'a']++;
            }
        }
        for (int i = 0; i < m; i++) {
            res.add(helper(freqq[i], puzzles[i].charAt(0), freqw, n));
        }
        return res;
    }

    private int helper(int[] freq, char c, int[][] freqw, int n) {
        int res = 0;
        for (int i = 0; i < n; i++) {
            if (freqw[i][c - 'a'] == 0) continue;
            if (isvalid(freq, freqw[i])) res++;
        }
        return res;
    }
    
    private boolean isvalid(int[] freq, int[] freqw) {
        for (int i = 0; i < 26; i++) {
            if (freqw[i] != 0 && freq[i] == 0) return false;
        }
        return true;
    }  

解法二:

 

Guess you like

Origin www.cnblogs.com/TIMHY/p/11444323.html