牛客小白月赛12 392J

链接:https://ac.nowcoder.com/acm/contest/392/J
来源:牛客网

题目描述

月月和华华一起去吃饭了。期间华华有事出去了一会儿,没有带手机。月月出于人类最单纯的好奇心,打开了华华的手机。哇,她看到了一片的QQ推荐好友,似乎华华还没有浏览过。月月顿时醋意大发,出于对好朋友的关心,为了避免华华浪费太多时间和其他网友聊天,她要删掉一些推荐好友。但是为了不让华华发现,产生猜疑,破坏了他们的友情,月月决定只删华华有可能搭讪的推荐好友。
月月熟知华华搭讪的规则。华华想与某个小姐姐搭讪,当且仅当小姐姐的昵称是他的昵称的子序列。为了方便,华华和小姐姐的昵称只由小写字母构成。为了更加方便,保证小姐姐的昵称长度不会比华华的长。
现在月月要快速的判断出哪些推荐好友要删掉,因为华华快回来了,时间紧迫,月月有点手忙脚乱,所以你赶紧写个程序帮帮她吧!

输入描述:

第一行输入一个字符串A表示华华的昵称。
第二行输入一个正整数N表示华华的推荐好友的个数。
接下来N行,每行输入一个字符串BiBi表示某个推荐好友的昵称。

输出描述:

输出N行,对于第i个推荐好友,如果华华可能向她搭讪,输出Yes,否则输出No。
注意大写,同时也要注意输出效率对算法效率的影响。
示例1

输入

复制
noiauwfaurainairtqltqlmomomo
8
rain
air
tql
ntt
xiaobai
oiiiooo
orzcnzcnznb
ooooo

输出

复制
Yes
Yes
Yes
Yes
No
Yes
No
No

备注:

1|A|1061≤|A|≤106,1N1061≤N≤106,1Ni=1Bi106



分析
这题实际上和下 hihocoder 94里的第二题几乎完全相同,主要代码几乎没有很大的变化。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cmath>
#include <bitset>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long  ll;
#define mem(A, X) memset(A, X, sizeof A)
#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
#define fori(i,l,u) for(ll (i)=(ll)(l);(i)<=(ll)(u);++(i))
#define ford(i,l,u) for(ll (i)=(ll)(l);(i)>=(ll)(u);--(i))

string s;
int n;
string qs;
int nxtpos[1000005][30];
void preinit(){
    int len=s.size();
    fori(i,0,25) nxtpos[len-1][i]=-1;
    ford(i,len-2,0){
        fori(j,0,25) nxtpos[i][j]=nxtpos[i+1][j];
        nxtpos[i][s[i+1]-'a']=i+1;
    }
}
bool check(){
    int len=qs.size();
    int firstpos=-1;
    fori(i,0,(int)s.size()-1) {
        if(s[i]==qs[0]) {
            firstpos=i;
            break;
        }
    }
    if(firstpos==-1) return false;
    else {
        int cur=firstpos;
        fori(i,1,len-1){
            if(nxtpos[cur][qs[i]-'a']==-1 ) return false;
            else cur=nxtpos[cur][qs[i]-'a'];
        }
    }
    return true;

}
int main()
{
  ios::sync_with_stdio(false);
  //freopen("local.in","r",stdin);
  while(cin>>s){
    preinit();
    cin>>n;
    fori(i,1,n){
        cin>>qs;
        if(check()) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
  }

return 0;
}
View Code
 
 
 




https://www.cnblogs.com/paulzjt/p/10526317.html

猜你喜欢

转载自www.cnblogs.com/paulzjt/p/10526259.html
今日推荐