首页 > 其他分享 >Weekly Contest 390

Weekly Contest 390

时间:2024-03-24 21:24:18浏览次数:23  
标签:node num Contest int TrieNode length 390 new Weekly

Problem A

每个字符最多出现两次的最长子字符串

思路

双指针,使用一个数组记录每个字符的出现次数,当出现次数大于2时l往左收缩 其余情况往右划

代码

class Solution {
    public int maximumLengthSubstring(String s) {
        int n = s.length();
        int[] cnt = new int[26];
        int ans = 1;
        int l = 0;
        int r = 1;
        cnt[s.charAt(0)-'a']++;
        while(r<n){
            char c = s.charAt(r);
            cnt[c-'a']++;
            while(cnt[c-'a']>2&&l<r){
                cnt[s.charAt(l++)-'a']--;
            }
            ans = Math.max(ans,r-l+1);
            r++;
        }
        return ans;
    }
}

Problem B

执行操作使数据元素之和大于等于 K

思路

贪心 最优解为先将初始值增加到一定数值,然后进行复制 贪心枚举增加次数 计算复制次数
不保证正确的证明
假设上述为错的 那在复制后一定存在一个增加的操作 但是在复制后增加一定不如在复制前增加得到的数大。

代码

class Solution {
    public int minOperations(int k) {
        int ans = k-1;
        for(int i = 0;i<k;++i){
            int temp = i+1;
            int d = k%temp==0?k/temp:k/temp+1;
            ans = Math.min(ans,i+d-1);
        }
        return ans;
    }
}

Problem C

最高频率的 ID

思路

数据结构题 优先队列加延迟更新

代码

class Solution {
    public long[] mostFrequentIDs(int[] nums, int[] freq) {
        Map<Long,Long> map = new HashMap<>();
        int n = nums.length;
        PriorityQueue<Long []> q = new PriorityQueue<>(new Comparator<Long[]>(){
            public int compare(Long[] a,Long[] b){
                return b[0].compareTo(a[0]);
            }
        });
        long[] ans = new long[n];
        for(int i = 0;i<n;++i){
            long l= nums[i];
            long r = freq[i];
            map.put(l,map.getOrDefault(l,0L)+r);
            q.offer(new Long[]{map.get(l),l});
            while(!q.isEmpty()){
                Long[] temp = q.peek();
                if(map.get(temp[1])!=temp[0]){
                    q.poll();
                }
                else{
                    break;
                }
            }
            Long[] temp = q.peek();
            ans[i] = temp[0];
            
        }
        return ans;
    }
}
        

Problem D

最长公共后缀查询

思路

字典树板子 逆序建立wordsContainer的字典树 然后查询即可 需要更新的点是每次访问到某个节点是要比较下当前字符串和之前字符串的长度

代码

补题代码

class Trie{
    private int SIZE=26;
    private TrieNode root;
    private String[] words;
    Trie(String[] word){
        root = new TrieNode();
        words =word;
    }
    private class TrieNode{
        private int num;
        private TrieNode[] son;
        private boolean isEnd;
        private char val;
        TrieNode(){
            num = 0;
            son = new TrieNode[SIZE];
            isEnd = false;
           
        }
    }
    public void insert(String str,int index){
        if(str==null||str.length()==0){
            return ;
        }
        TrieNode node = root;
        char[] letters = str.toCharArray();
        for(int i = str.length()-1,len =0;i>=len;--i){
            int pos = letters[i]-'a';
            if(node.son[pos]==null){
                node.son[pos] = new TrieNode();
                node.son[pos].num = index;
            }
             if(words[node.num].length()>words[index].length()){
            node.num = index;
        }
            node = node.son[pos];
        }
        if(words[node.num].length()>words[index].length()){
            node.num = index;
        }
    }
    public int getSum(String str){
        TrieNode node = root;
        char[] letters = str.toCharArray();
        for(int i = letters.length-1,len =0;i>=len;--i){
            int pos = letters[i]-'a';
            if(node.son[pos]==null){
                return node.num;
            }else{
                node = node.son[pos];
            }
        }
        return node.num;
    } 
}
class Solution {
    public int[] stringIndices(String[] wordsContainer, String[] wordsQuery) {
        Trie tree = new Trie(wordsContainer);
        int n = wordsContainer.length;
        int m = wordsQuery.length;
        int[] ans  = new int[m];
        for(int i = 0;i<n;++i){
            tree.insert(wordsContainer[i],i);
        }
        for(int i = 0;i<m;++i){
            ans[i] = tree.getSum(wordsQuery[i]);
        }
        return ans;

    }
}

总结

这场比上场简单,因为是伪AK所以先写了这场的题解。 学了下字典树的板子。

标签:node,num,Contest,int,TrieNode,length,390,new,Weekly
From: https://www.cnblogs.com/baihualiaoluan/p/18093081

相关文章

  • 第 390 场周赛记录-快手
    1.每个字符最多出现两次的最长子字符串给你一个字符串s,请找出满足每个字符最多出现两次的最长子字符串,并返回该子字符串的最大长度。示例1:输入:s="bcbbbcba"输出:4解释:以下子字符串长度为4,并且每个字符最多出现两次:"bcbbbcba"。示例2:输入:s="aaaa"输出:2解......
  • AtCoder Beginner Contest 346
    AtCoderBeginnerContest346最刺激的一集。尝试挑战手速极限,用了57s做A。但是好像还是很慢。然后做B,仍然想挑战手速。结果一眼出思路只要把wbwbwwbwbwbw多重复几遍就可以代替「无限长」。很快就写完了。然后交了三发罚时。后来发现我复制若干遍wbwbwwbwbwbw的时候......
  • AtCoder Beginner Contest 346
    A-AdjacentProduct(abc346A)题目大意给定\(n\)个数,依次输出相邻俩数的乘积。解题思路按照题意模拟即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=longlong;intmain(void){ios::sync_with_stdio(false);cin.tie(0);c......
  • AtCoder Beginner Contest 345
    A-Leftrightarrow(abc345A)题目大意给定一个字符串,问是不是形如<======...====>的字符串。解题思路根据长度构造出期望的字符串,再判断是否相等即可。神奇的代码s=input()print("Yes"ifs=="<"+"="*(len(s)-2)+">"else"No")B-Inte......
  • The 14th Jilin Provincial Collegiate Programming Contest
    The14thJilinProvincialCollegiateProgrammingContest-Codeforces队友太猛了,我整场就只写了D,其他题给队友开完了,预计补一下M,FProblemD.Trie(AC自动机+树状数组)大概就是给定一颗Trie树操作一是给Trie树的fail树上一个集合中的点的所有子节点打上一个......
  • 390_xxl-job 定时任务执行失败
    执行失败时情况错误原因:::info定时任务执行器端口配置为:2+项目端口,生成了6位数无效端口,导致错误:::解决方法:::info定时任务执行器端口配置为指定端口:::正常时情况......
  • JB Loves Math(The 19th Zhejiang Provincial Collegiate Programming Contest)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • JB Wants to Earn Big Money(The 19th Zhejiang Provincial Collegiate Programming Co
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • JB Loves Comma(The 19th Zhejiang Provincial Collegiate Programming Contest)
    #include<bits/stdc++.h>#defineendl'\n'usingll=longlong;typedefunsignedlonglongull;usingnamespacestd;voidGordenGhost();signedmain(){#ifdefGordenfreopen("in.txt","rt",stdin);freopen......
  • AtCoder Beginner Contest 345
    A-Leftrightarrow#include<bits/stdc++.h>usingnamespacestd;usingi32=int32_t;usingi64=longlong;usingldb=longdouble;#defineintlonglongusingvi=vector<int>;usingpii=pair<int,int>;constintmod=1e9+7;......