zoukankan      html  css  js  c++  java
  • 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<<":
    ";
        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;
    }
    
    
    
    
  • 相关阅读:
    Unix domain sockets
    python异常处理
    php注册登录源代码
    div,css命名规范!
    html、css和js注释的规范用法
    PHPstrom的Disable Power Save Mode
    开通了博客园
    O(1)时间删除链表中的节点 13
    打印1到最大的n位数 12
    自己实现一个数的整数次方 11
  • 原文地址:https://www.cnblogs.com/sz-wcc/p/12335290.html
Copyright © 2011-2022 走看看