HDU 5972 Regular Number(Shift-And)

原题链接

Problem Description

Using regular expression to define a numeric string is a very common thing. Generally, use the shape as follows:
(0|9|7) (5|6) (2) (4|5)
Above regular expression matches 4 digits:The first is one of 0,9 and 7. The second is one of 5 and 6. The third is 2. And the fourth is one of 4 and 5. The above regular expression can be successfully matched to 0525, but it cannot be matched to 9634.
Now,giving you a regular expression like the above formula,and a long string of numbers,please find out all the substrings of this long string that can be matched to the regular expression.

Input

It contains a set of test data.The first line is a positive integer N (1 ≤ N ≤ 1000),on behalf of the regular representation of the N bit string.In the next N lines,the first integer of the i-th line is ai(1≤ai≤10),representing that the i-th position of regular expression has ai numbers to be selected.Next there are ai numeric characters. In the last line,there is a numeric string.The length of the string is not more than 5 * 10^6.

Output

Output all substrings that can be matched by the regular expression. Each substring occupies one line

Sample Input

4
3 0 9 7
2 5 7
2 2 5
2 4 5
09755420524

Sample Output

9755
7554
0524

题目大意

字符串匹配问题,给出模式串每个位允许出现的数字,再给出目标串,输出所有可能的匹配方案。

解题思路

第一次接触Shift-And算法,参照了网上的资料。算法本身还需要更深理解。
以及令人惊叹的Shift-And/Shift-Or
大致意思就是首先根据模式串预处理出每个数字可以出现的位置,dp循环数组用来记录当前的状态。这里Shift-And算法核心在于

dp[cur]<<=1;
dp[cur].set(0);
dp[cur^1]=dp[cur]&bt[in[i]-'0'];

bt[in[i]-‘0’]中储存in[i]所有允许出现的位置,这个按位与运算巧妙地更新了状态。
判断是否匹配上只需要看dp[cur^1][outlen-1]是否为1,其中outlen为输出串的长度。
听说这题需要输入输出外挂?同样从网上直接搞来了模板。

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<vector>
#include<bitset>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define db double
//#define rep(i,n) for(int i = 0;i < n; i++)
//#define rep(i,n) for(int i = 0;i < n; i++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
//#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define mp make_pair
#define exp 2.7182818
#define PI 3.141592653589793238462643383279502884
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define eps 1e-6
#define MOD 1000000007ll
using namespace std;

namespace fastIO{
    #define BUF_SIZE 100000
    #define OUT_SIZE 1000000
    bool IOerror=0;
    inline char nc(){
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend){
            p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if (pend==p1){IOerror=1;return -1;}
        }return *p1++;
    }
    inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
    inline int read(char *s){
        char ch=nc();
        for(;blank(ch);ch=nc());
        if(IOerror)return 0;
        for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;
        *s=0;
        return 1;
    }
    inline int RI(int &a){
        char ch=nc(); a=0;
        for(;blank(ch);ch=nc());
        if(IOerror)return 0;
        for(;!blank(ch)&&!IOerror;ch=nc())a=a*10+ch-'0';
        return 1;
    }
    struct Ostream_fwrite{
        char *buf,*p1,*pend;
        Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;}
        void out(char ch){
            if (p1==pend){
                fwrite(buf,1,BUF_SIZE,stdout);p1=buf;
            }*p1++=ch;
        }
        void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}}
        ~Ostream_fwrite(){flush();}
    }Ostream;
    inline void print(char x){Ostream.out(x);}
    inline void println(){Ostream.out('\n');}
    inline void flush(){Ostream.flush();}
    char Out[OUT_SIZE],*o=Out;
    inline void print1(char c){*o++=c;}
    inline void println1(){*o++='\n';}
    inline void flush1(){if (o!=Out){if (*(o-1)=='\n')*--o=0;puts(Out);}}
    struct puts_write{
        ~puts_write(){flush1();}
    }_puts;
};

using namespace fastIO;

const int MAXN=1010;

bitset<MAXN> dp[2];
bitset<MAXN> bt[15];
char in[5000010];

void shiftand(int outlen,int inlen)
{
    int cur=0;
    for(int i=0;i<inlen;++i)
    {
        dp[cur]<<=1;
        dp[cur].set(0);
        dp[cur^1]=dp[cur]&bt[in[i]-'0'];
        if(dp[cur^1][outlen-1])
        {
            for(int j=i-outlen+1;j<=i;++j)
            {
                print(in[j]);
            }
            println();
        }
        cur^=1;
    }
}
int main(void) 
{
    //freopen("input.in","r",stdin);
    //freopen("output.out","w",stdout);
    int t;
    int n;
    int x;
    while(scanf("%d",&t)!=EOF)
    {
        for(int i=0;i<15;++i) bt[i].reset();
        dp[0].reset();dp[1].reset();

        for(int i=0;i<t;++i)
        {
            RI(n);
            for(int j=1;j<=n;++j)
            {
                RI(x);
                bt[x].set(i);
            }

        }
        read(in);
        shiftand(t,strlen(in));   

    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/xj949967574/article/details/77986008