HDU5972 LA7724 Regular Number位运算+字符串匹配

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5972 LA7724 Regular Number位运算+字符串匹配相关的知识,希望对你有一定的参考价值。

Regular Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3262 Accepted Submission(s): 956

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亚洲区大连站-重现赛(感谢大连海事大学)

问题链接HDU5972 LA7724 Regular Number
问题简述:给定一个n,对于n位数指定每一位可能的数字(即规则)。后面跟着n行,开头是k,代表这一行后面有k个数,表示第i位只能是这k个数字之一。最后给定一个数字字符串,问有哪些跟上述规则匹配长度为n的的子串?
问题分析:字符串匹配问题,用位运算来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* HDU5972 LA7724 Regular Number */

#include <bits/stdc++.h>

using namespace std;

const int N = 1000;
const int A = 10;
bitset<N> b[A + 1], ans;
char s[5000000 + 1];

int main()
{
    int n, k;
    scanf("%d", &n);

    ans.reset();
    for (int i = 0; i <= A; i++) b[i].reset();

    for (int i = 0; i < n; i++) {
        scanf("%d", &k);
        for (int j = 0; j < k; j++) {
            int a;
            scanf("%d", &a);
            b[a].set(i);
        }
    }
    scanf("%s", s);

    char end = '\\0';
    for (int i = 0; s[i]; i++) {
        ans <<= 1;
        ans.set(0);
        ans &= b[s[i] - '0'];
        if (ans[n - 1] == 1) {
            swap(s[i + 1], end);
            puts(s + i - n + 1);
            swap(s[i + 1], end);
        }
    }

    return 0;
}

以上是关于HDU5972 LA7724 Regular Number位运算+字符串匹配的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5972 Regular Number

hdu 5972 Regular Number

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

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

Regular Number hdu-5972(bitset+Shift-And算法)

Shift - And字符串快速处理 hdu5972+cf