题解 | 月月查华华的手机

月月查华华的手机

https://www.nowcoder.com/practice/c2877187b1bb4573927e759a60f5d3e1

p[i][j]预处理一下原字符串字符i在j位置后面第一次出现的下标,因为要尽可能选出查询的字符串,所以要每次选最近的,然后查询的时候跳一下对应位置的就行了,如果某一个位置后面没有对应的字符了就是no,反之匹配完毕就是yes

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
using PII=pair<ll,ll>;
using PIII=pair<int,pair<int,int>>;
const ld ESP = 1e-10;
const ld PI = acosl(-1);
const int N=5e5+10;
const int M=2e5+10;
// const int mod = 1000000007;
const int mod = 998244353;
//随机化
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> Tp(1, 1000000000);
// cout<<fixed<<setprecision(10);

void solve(){
    string s;
    cin>>s;
    int n=s.size();
    vector<vector<int>> p(26,vector<int>(s.size()+10,1e9));
    for(int c=0;c<=25;c++){
        for(int i=n-1;i>=0;i--){
            if(s[i]=='a'+c)p[c][i]=i;
            p[c][i]=min(p[c][i+1],p[c][i]);
        }
    }
    int q;
    cin>>q;
    for(int i=1;i<=q;i++){
        string t;
        cin>>t;
        int cur=0,f=1;
        for(int j=0;j<t.size();j++){
            int nx=p[(int)(t[j]-'a')][cur];
            if(nx==1e9){
                f=0;
                cout<<"No"<<'\n';
                break;
            }else{
                cur=nx+1;
            }
        }
        if(f)cout<<"Yes"<<'\n';
    }

}   
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int _=1;
    // cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

全部评论

相关推荐

求个付费实习岗位:这种就是吃满时代红利又没啥技术水平,只能靠压力学生彰显优越感的老登,别太在意了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务