Off white cow month season 12 J month investigation of China and China's mobile phone (automatic machine template title sequence)

Links: https://ac.nowcoder.com/acm/contest/392/J
Source: Cattle-off network

Title Description
month and China and China together to dinner. During Chihuahua something out for a while, he did not carry a cell phone. For the month of mankind's most simple curiosity, opened the China and China's mobile phone. Wow, she saw a friend's recommendation QQ, China and China seems to have not visited. Month suddenly jealous, out of concern for good friends, in order to avoid wasting too much time China and China and other friends to chat, she recommended to delete some of your friends. But in order not to Chihuahua found, suspicions, destroyed their friendship, month and decided to delete only China and China is likely to strike up a friend's recommendation.
Month familiar with China and China strike rules. China and China want to strike up a conversation with a little sister, little sister if and only if the nickname is a subsequence of his nickname. For convenience, Hua Hua and little sister nickname consists only of lowercase letters. For added convenience, guarantee nickname length is not longer than the little sister of China and China.
Now month to quickly determine which recommend a friend should be deleted, since China and China come back, and time is running out, month and a bit confused, so you can quickly write a program to help her!
Input Description:
The first line of the input string A represents a Chihuahua nickname.
The second line of the input N represents a positive integer number huahua recommended friends.
Next N lines of input string B_iB
I represents a friend recommended nickname. Output Description: The output N lines, i-th recommendation for a friend, if China and China could strike up to her, output Yes, otherwise the output No. Note the capital, but also the impact on the efficiency of the algorithm's output efficiency. Example 1 Input Copy noiauwfaurainairtqltqlmomomo . 8 Rain AIR TQL NTT














xiaobai
oiiiooo
orzcnzcnznb
ooooo
输出
复制
Yes
Yes
Yes
Yes
No
Yes
No
No
备注:
1\le|A|\le10^61≤∣A∣≤10
6
,1\le N\le10^61≤N≤10
6
,1\le\sum_{i=1}^NB_i\le10^61≤∑
i=1
N

B
i

≤10
6

Ideas:

This title is an automatic machine template title sequence,

To explain the sequence automaton is a something,

Is actually an array
nxt [| str |] [| S |] | str | is the maximum length of the string, and | S | is the number of elements in the set of characters.

We pretreated nxt array wear against the base,

NXT [i] [j] denotes the nearest subscript j i-th character in the rear position. If that is not zero.

Obtained by the method is very simple, we only need to sweep forward from the rear side of the dp method similar to what can be transferred.

When a sub string to try to match the sequence of the letter string, only you can transfer along the array nxt, when the characters are not required, i.e., to match the index is 0 (since the time read from the letter string subscript 1 to start reading characters), which matches a failure.

Failure to return.

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

int nxt[maxn][30];
char s[maxn];
int len;
void init()
{
    for(int i=len;i>=1;--i)
    {
        for(int j=0;j<=25;++j)
        {
            nxt[i-1][j]=nxt[i][j];
        }
        nxt[i-1][s[i]-'a']=i;
    }
}
char t[maxn];
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    
    scanf("%s",s+1);
    len=strlen(s+1);
    int q;
    init();
    scanf("%d",&q);
    while(q--)
    {
        scanf("%s",t);
        int len1=strlen(t);
        int isok=1;
        for(int now=0,i=0;i<len1;++i)
        {
            now=nxt[now][t[i]-'a'];
            if(!now)
            {
                isok=0;
                break;
            }
        }
        if(isok)
        {
            puts("Yes");
        }else
        {
            puts("No");
        }

    }
    
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11355472.html
Recommended