每日一题 月月查华华的手机

题意:
月月熟知华华搭讪的规则。华华想与某个小姐姐搭讪,当且仅当小姐姐的昵称是他的昵称的子序列。为了方便,华华和小姐姐的昵称只由小写字母构成。为了更加方便,保证小姐姐的昵称长度不会比华华的长。
现在月月要快速的判断出哪些推荐好友要删掉,因为华华快回来了,时间紧迫,月月有点手忙脚乱,所以你赶紧写个程序帮帮她吧!
思路:
序列自动机?

#include<bits/stdc++.h>
using namespace std;
map<char,int>mp;
const int N=1000010;
int dp[N][27];
string s;
int n,m;
bool check(string s)
{
    int i=0;
    int pos=0;
    while(i<s.size())
    {
        if(!dp[pos][s[i]-'a'+1])    return 0;
        else
            pos=dp[pos][s[i]-'a'+1],i++;
    }   
    return 1;
}
int main()
{
    cin>>s;
    n=s.size();
    s=' '+s;
    for(int i=n;i>=0;i--)
    {
        for(int j=1;j<=26;j++)
        {
            dp[i][j]=mp[j];
        }
        mp[s[i]-'a'+1]=i;
    }
    cin>>m;
    while(m--)
    {
        string ss;
        cin>>ss;
        if(check(ss))
            cout<<"Yes\n";
        else
            cout<<"No\n";
    }


    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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