2504 是子序列的个数

Problem

小b有一个字符串S和n个字符串words[1...n],现在她想知道有多少个i满足words[i]是S的子序列。

S的长度≤50000,1≤n≤5000,words[i]长度≤50.

样例解释

a,acd,ace都是abcde的子序列,但bb不是。

Solution

存一下s各个字母的位置,比较时二分查找。

Code

#include<stdio.h>
#include<set>
#include<iostream>
#include<stack>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
typedef long long ll;
typedef long double ld;
typedef double db;
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int mod=998244353;
int mo(ll a,int p){
    return a>=p?a%p:a;
}
inline int rd() {
    int x = 0, f = 1;
    char ch;
    while (ch < '0' || ch > '9') {
        if (ch == '-')f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return f * x;
}
int ans,n;
string s,x;
int a[27][500020];
bool fun(string &cur){
    //cout<<cur<<":\n";
    int las=0,l,r,mid,anss;
    for(char i : cur){
        l=1,r=a[i-'a'][0],anss=0;
        while(l<=r){
            mid=l+r>>1;
            if(a[i-'a'][mid]>las){
                r=mid-1;
                anss=mid;

            }
            else{
                l=mid+1;
            }
        }
        if(!anss){
            return false;
        }
        las=a[i-'a'][anss];//cout<<las<<endl;
    }
    return true;
}
int main(){
    io_opt;
    cin>>s>>n;
    for(int i=0;i<s.size();i++){
        a[s[i]-'a'][++a[s[i]-'a'][0]]=i+1;
    }
    for(int i=1;i<=n;i++){
        cin>>x;
        if(fun(x)){
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}


猜你喜欢

转载自www.cnblogs.com/sz-wcc/p/12335290.html