The Preliminary Contest for ICPC China Nanchang National Invitational Subsequence (序列自动机)

版权声明:转载请标明出处 https://blog.csdn.net/weixin_41190227/article/details/89459019

ive a string SS and NN string T_iTi​ , determine whether T_iTi​ is a subsequence of SS.

If ti is subsequence of SS, print YES,else print NO.

If there is an array \lbrace K_1, K_2, K_3,\cdots, K_m \rbrace{K1​,K2​,K3​,⋯,Km​} so that 1 \le K_1 < K_2 < K_3 < \cdots < K_m \le N1≤K1​<K2​<K3​<⋯<Km​≤N and S_{k_i} = T_iSki​​=Ti​, (1 \le i \le m)(1≤i≤m), then T_iTi​ is a subsequence of SS.

Input

The first line is one string SS,length(SS) \le 100000≤100000

The second line is one positive integer N,N \le 100000N,N≤100000

Then next nn lines,every line is a string T_iTi​, length(T_iTi​) \le 1000≤1000

Output

Print NN lines. If the ii-th T_iTi​ is subsequence of SS, print YES, else print NO.

样例输入复制

abcdefg
3
abc
adg
cba

样例输出复制

YES
YES
NO

题目大意:给一个文本串,判断接下来n个模式串是否为文本串的子序列。

解题思路:建立序列自动机,对于每个模式串扫一遍即可。

题目链接:https://nanti.jisuanke.com/t/38232

两种写法:

/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
const int Maxn = 1e5 + 10 ;

string s, str ;
int ch[26][Maxn] ;
int last[26], Next[Maxn] ;
int cnt = 0, len ;

inline void init(int c) {
    cnt++ ;
    Next[cnt] = last[c] ;
    for (int i = 0; i < 26; i++){
        for (int j = last[i]; j != -1 && ch[c][j] == 0; j = Next[j]){
            ch[c][j] = cnt ;
        }
    }
    last[c] = cnt ;
}

inline bool check (int u, int x) {
    if (x == len) return true ;
    int c = str[x] - 'a' ;
    if (ch[c][u] == 0) return false ;
    return check(ch[c][u], x + 1 ) ;
}

int main (){
    ios_base::sync_with_stdio(false) ;
    cin.tie(0) ;
    cout.tie(0) ;
    Next[0] = -1 ;
    cin >> s ;
    for (auto i : s) init(i - 'a') ;
    int n ;
    cin >> n ;
    for (int i = 0; i < n; i++){
        cin >> str ;
        len = str.size() ;
        if (check(0, 0)) cout << "YES" << endl ;
        else cout << "NO" << endl ;
    }
    return 0 ;
}
/*
@Author: Top_Spirit
@Language: C++
*/
#include <bits/stdc++.h>
using namespace std ;
const int Maxn = 1e5 + 10 ;

int Next[Maxn][26] ;
int pre[Maxn] ;
string s, str ;

void init(){
    memset(pre, -1, sizeof(pre)) ;
    int len = s.size() ;
    for (int i = len - 1; i >= 0; i--){
        for (int j = 0; j < 26; j++){
            Next[i][j] = pre[j] ;
        }
        pre[s[i] - 'a'] = i ;
    }
}

int main (){
    ios_base::sync_with_stdio(false) ;
    cin.tie(0) ;
    cout.tie(0) ;
    cin >> s ;
    init() ;
    int n ;
    cin >> n ;
    for (int i = 0; i < n; i++){
        cin >> str ;
        int len = str.size() ;
        bool flag = false ;
        int ch = pre[str[0] - 'a'] ;
        if (ch == -1) cout << "NO" << endl ;
        else {
            for (int i = 1; i < len; i++){
                ch = Next[ch][str[i] - 'a'] ;
                if (ch == -1) {
                    flag = true ;
                    break ;
                }
            }
            if (flag) cout << "NO" << endl ;
            else cout << "YES" << endl ;
        }
    }
    return 0 ;
}

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/89459019
今日推荐