hdu 5972---Regular Number(字符串匹配)

题目链接

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

Source

2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)

题意:输入N ,表示有一个长为N的子串,接下来N行,每行输入ai 和ai个数,表示有ai个数,表示子串第i个字符为ai个数中的一个,也就是这个子串的正则式,然后输入一个主串,要求输出这个主串中包含的所有的子串。

思路:使用bitset<N> b[10] ,b[i][j]表示值为i的数可以出现在子串的那些位置,即下标,那么对主串进行遍历 i=0:len-1 。另外定义一个变量bitset<N> ans ,每次先将ans左移一位,然后将最低位置1,然后令k=当前输入的数,将ans=ans&b[k], 如果当前ans[N-1]==1,那么主串s[i-N+1]~s[i]就是合法子串,输出;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <bitset>
using namespace std;
typedef long long LL;
const int M=5*1e6+5;
bitset<1005>b[12];
bitset<1005>ans;
char s[5000005];

int main()
{
    int N;
    while(scanf("%d",&N)!=EOF)
    {
        for(int i=0;i<10;i++) b[i].reset();
        for(int i=0;i<N;i++)
        {
            int n;
            scanf("%d",&n);
            for(int j=1;j<=n;j++)
            {
                int k;
                scanf("%d",&k);
                b[k].set(i);
            }
        }
        getchar();
        gets(s);
        ans.reset();
        int len=strlen(s);
        for(int i=0;i<len;i++)
        {
            ans=ans<<1;
            ans.set(0);
            ans=ans&b[s[i]-‘0‘];
            if(ans[N-1]==1){
               char c=s[i+1];
               s[i+1]=‘\0‘;
               puts(s+i-N+1);
               s[i+1]=c;
            }
        }
    }
    return 0;
}
时间: 2024-10-13 13:04:19

hdu 5972---Regular Number(字符串匹配)的相关文章

hdu 5972 Regular Number

Regular Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 620    Accepted Submission(s): 134 Problem Description Using regular expression to define a numeric string is a very common thing.

HDU 5972 Regular Number(ShiftAnd+读入优化)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5972 [题目大意] 给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串的长度为n,并且第i个字符需要在给定的字符集合Si中 [题解] 利用ShiftAnd匹配算法. bt[i]表示字符i允许在哪些位置上出现,我们将匹配成功的位置保存在dp中,那么就可以用dp[i]=dp[i-1]<<1&bt[s[i]]来更新答案了 利用滚动数组和bitset可以来优化这样的

HDU 5972 Regular Number(字符串shift-and算法)

题目链接  HDU5972 2016 ACM/ICPC 大连区域赛 B题 我们预处理出b[i][j],b[i][j] = 1的意义是数字i可以放在第j位. 然后就开始这个匹配的过程. 假设字符串第一位下标从1开始 我们每一次处理的子串为s[i - n + 1], s[i  - n + 2], s[i - n + 3], ..., s[i] ans[k] = 1的含义是 s[i - k + 1], s[i - k + 2], s[i - k + 3], ..., s[i]这k位可以与t[1], t

HDU 1711 Number Sequence(字符串匹配)

Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10571    Accepted Submission(s): 4814 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1],

HDU 5716 带可选字符的多字符串匹配(ShiftAnd)

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5716 [题目大意] 给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串的长度为n,并且第i个字符需要在给定的字符集合Si中 [题解] 这种串与字符集的匹配称为柔性字符串匹配,采用ShiftAnd的匹配方法. bt[i]表示字符i允许在哪些位置上出现,我们将匹配成功的位置保存在dp中,那么就可以用dp[i]=dp[i-1]<<1&bt[s[i]]来更新答案了

Substrings(hdu1238)字符串匹配

Substrings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7205 Accepted Submission(s): 3255 Problem Description You are given a number of case-sensitive strings of alphabetic characters, find the

POJ3080——Blue Jeans(暴力+字符串匹配)

Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you hav

poj3080--Blue Jeans(字符串匹配)

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12233   Accepted: 5307 Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousa

字符串匹配问题 ---- 算法导论读书笔记

字符串匹配是一个很常见的问题,可以扩展为模式的识别,解决字符串问题的思想被广泛地应用.介绍四种解决该问题的办法,包括:最朴素的遍历法,Rabin-Karp算法,自动机机匹配,Knuth-Morris-Pratt算法即耳熟能详的KMP. 在一开始,先对时间复杂度做出一个总扩(从大到小):[1]朴素法:O( (n-m+1)m ):[2]Rabin-Karp:预处理:O(m),匹配:最坏O( (n-m+1)m ),但是平均和实际中比这个好得多:[3]自动机:预处理O(m|Σ|),匹配O(n):[4]K