HDU5972 Regular Number(bitset)

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組數,從每組裏選取出來一個數字,按順序構成的新的串是合法的,其餘均是不合法的。又給出一個串,問當中合法的字串有哪些
這個題需要使用bitset,將每個數字可以出現的位置所對應的二進制的相應的位置爲1;
b[0]=1 0 0 0 0 0 
b[1]=0 0 0 0 0 0
b[2]=0 0 1 0 0 0
b[3]=0 0 0 0 0 0
b[4]=0 0 0 1 0 0
b[5]=0 1 1 1 0 0
b[6]=0 0 0 0 0 0
b[7]=1 1 0 0 0 0
b[8]=0 0 0 0 0 0
b[9]=1 0 0 0 0 0
比如b[0]表示0這個數字只能出現在第0位是合法的......
在使用二進制數,每次將這個數字右移一位,並將最低爲置爲1,和對應的母串的字符相與,以確定對應的數字是否合法。
直至1出現在n-1的位置上時,找到成功的字符串。
#include <stdio.h>
#include <string.h>
#include <bitset>
using namespace std;
const int N=1005;
bitset<N> ans;
bitset<N> dic[N];
char str[5000005];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            int k;
            scanf("%d",&k);
            for(int j=0;j<k;j++)
            {
                int tmp;
                scanf("%d",&tmp);
                dic[tmp].set(i);
            }
        }
        getchar();
        gets(str);
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            ans=ans<<1;
            ans.set(0);
            ans=ans&dic[str[i]-'0'];
            if(ans[n-1]==1)
            {
                char s=str[i+1];
                str[i+1]='\0';
                puts(str+i-n+1);
                str[i+1]=s;
            }
        }
    }
    return 0;
}





發佈了36 篇原創文章 · 獲贊 14 · 訪問量 4836
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章