HDU - 5972 Regular Number (bitset)

本文介绍了一种使用正则表达式进行数字字符串匹配的方法,通过比特操作实现高效的子串匹配,适用于特定格式的数字验证场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Regular Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1645    Accepted Submission(s): 446


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(1ai10) ,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
 

Source

题意:
给出N位上每一个位可以匹配成功的数字。然后给出一个N位数。求可以匹配成功的每一个字串。

这题看网上的做法才知道用bitwet这样神奇的操作。
思路:
用二进制表示,存下每一个数字在第i位可以匹配成功则置第i位为1,否则为0。
然后ans的第i位为1表示的是从第1-i位都能匹配成功的情况,否则为0。通过不断的左移可以让给出的N位完成对每一位的匹配。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <bitset>
using namespace std;
bitset<1005> s[15];
bitset<1005> ans;
char str[5000005];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int x,tmp;
        for(int i=0;i<15;i++)
            s[i].reset();
        ans.reset();
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            for(int j=1;j<=x;j++)
            {
                scanf("%d",&tmp);
                s[tmp].set(i);
            }
        }
        scanf("%s",str);
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            ans<<=1;
            ans[0]=1;
            ans=ans&s[str[i]-'0'];
            if(ans[n-1]==1)
            {
                char tmp=str[i+1];
                str[i+1]='\0';
                puts(str+i-n+1);
                str[i+1]=tmp;
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值