leetcode 2564. 子字符串异或查询[题解]

链接子字符串异或查询
思路:题目说 valfirst=second

可得val=secondfirst

题目变成从01串中找到最先出现的val的二进制表示,注意是105次询问。原来认为是AC自动机类的东西,但仔细一想,数字最多30位,那么字符串s存在的数字数量仅为105级别,那么我们可以预处理出所有数字的所在位置。

Code

复制class Solution {
public:
    vector<vector<int>> substringXorQueries(string s, vector<vector<int>>& queries) {
        #define ll int 
        map<ll,pair<int,int > >S;
        int sz = s.size() - 1;
        for(int len=1;len<=30;++len){
            for(int i=0;;++i){
                if(s[i] == '0' and len != 1)continue;
                if(i+len-1>sz)break;
                ll now = 0;
                for(int j=i;j<=i+len-1;++j){
                    now <<= 1;
                    if(s[j] == '1')now += 1ll;
                }
                if(S.count(now))continue;
                S[now] = {i,i+len-1};
            }
        }
        vector<vector<int> > ans;
        int tot = -1;
        for(auto i:queries){
            ll res = (ll)i[0] ^ i[1];
            if(S.count(res)){
                ans.push_back({S[res].first,S[res].second});
            }else {
                ans.push_back({-1,-1});
            }
        }
        return ans ;
    }
};
posted @   xiaodangao  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示