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

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. 

InputIt 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)ai(1≤ai≤10),representing that the i-th position of regular expression has aiai numbers to be selected.Next there are aiainumeric characters. In the last line,there is a numeric string.The length of the string is not more than 5 * 10^6.OutputOutput all substrings that can be matched by the regular expression. Each substring occupies one lineSample Inputios

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

Sample Outputgit

9755
7554
0524
題意:有n組數,同組數之間的關係是 | ,而後輸入一行字符串,問字符串有哪些子串可以用這n組數來表示??

Input:
先輸入n,而後輸入n組數,每組數的第一個(ai)表示這組數的長度,而後輸入ai個數,他們之間的關係是 | ,而後輸入一行字符串。
Output:
輸出知足條件的子串。express

 思路:開一個規格爲10的bitset數組和一個bitset類型的test,而後根據輸入存放bitset數組。輸入字符串後對字符串進行遍歷當test[n]==1時輸出字符串[i-n+1,i]。輸出是能夠把下一個變成‘\0’輸出字符串s+i-n+1,而後再變回來。數組

AC代碼:ide

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <algorithm>
 6 #include <map>
 7 #include <set>
 8 #include <stdlib.h>
 9 #include <stack>
10 #include <vector>
11 #include <cmath>
12 #include <bitset>
13 #define ll long long
14 using namespace std;
15 const int maxn=1005;
16 bitset<maxn>vec[10];
17 bitset<maxn>dp;
18 char s[5000005];
19 int main()
20 {
21     int n,m,x;
22     while(~scanf("%d",&n))
23     {
24         for(int i=0;i<=9;i++)
25             vec[i].reset();
26         for(int i=0;i<n;i++)
27         {
28             scanf("%d",&m);
29             for(int j=0;j<m;j++)
30             {
31                 scanf("%d",&x);
32                 vec[x].set(i);
33             }
34         }
35         scanf("%s",s);
36         int len=strlen(s);
37         dp.reset();
38         for(int i=0;i<len;i++)
39         {
40             dp=dp<<1;
41             dp=dp.set(0);
42             dp=dp&vec[s[i]-'0'];
43             if(dp[n-1]==1)
44             {
45                 char c=s[i+1];
46                 s[i+1]='\0';
47                 printf("%s\n",s+i-n+1);
48                 s[i+1]=c;
49             }
50         }
51     }
52     return 0;
53 }
View Code
相關文章
相關標籤/搜索