コンテスト152

2019年9月1日20時59分55秒

全体的な経験:最近珍しい何かを扱うときに自身が、彼はTLEに会ったとき、MLEが効果的にデバッグを行使する必要が実施する方法で、特に、十分なコンテストレベルまたは重度の参加を見つけます。

注意点:

1)すべての最初のTLEは、特別な裁判官、解決策は明らかにフィルタを満たしていない、良い実用的な問題に特殊なケースを検討する必要性を解決することができます;第二は、この大会として、最適化されたデータ構造を使用することですビット・コンピューティングを使用します。

2)1E9 + 7は長い/避けるために、二重、現在では、いくつかの方法は、各加算器MODを取って、またはBigIntegerのを使用している使用しないでください。

3)を把握するために十分な時間は、それぞれが時間がないに第三のタイトルを書きました。

  • 1177は、サブストリングから回文を作ることができます

問題の説明:

問題解決:

溶液A:直接TLEその結果、真の特別な場合を返し、K> = 13とは見なされません。プラス特別な文章があまりにもすることができます。しかし、問題は、このソリューションは、最悪時間計算量は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;
    }  

解法二:

 

おすすめ

転載: www.cnblogs.com/TIMHY/p/11444323.html
おすすめ