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(1≤ai≤10)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 Input

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

Sample Output

9755
7554
0524题意:有n组数,同组数之间的关系是 | ,然后输入一行字符串,问字符串有哪些子串能够用这n组数来表示??

Input:
先输入n,然后输入n组数,每组数的第一个(ai)表示这组数的长度,然后输入ai个数,他们之间的关系是 | ,然后输入一行字符串。
Output:
输出满足条件的子串。

思路:开一个规格为10的bitset数组和一个bitset类型的test,然后根据输入存放bitset数组。输入字符串后对字符串进行遍历当test[n]==1时输出字符串[i-n+1,i]。输出是可以把下一个变成‘\0’输出字符串s+i-n+1,然后再变回来。

AC代码:

 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 }

时间: 2024-08-29 12:50:18

Regular Number hdu-5972(bitset+Shift-And算法)的相关文章

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 4007 Dave (基本算法-水题)

Dave Problem Description Recently, Dave is boring, so he often walks around. He finds that some places are too crowded, for example, the ground. He couldn't help to think of the disasters happening recently. Crowded place is not safe. He knows there

hdu 1533 Going Home (KM算法)

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2715    Accepted Submission(s): 1366 Problem Description On a grid map there are n little men and n houses. In each unit time, every

hdu 2680 最短路径(dijkstra算法+多源最短路径单源化求最小值)

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7062    Accepted Submission(s): 2301 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

HDU 4911 Inversion(基本算法-排序)

Inversion Problem Description bobo has a sequence a1,a2,-,an. He is allowed to swap two adjacent numbers for no more than k times. Find the minimum number of inversions after his swaps. Note: The number of inversions is the number of pair (i,j) where

HDU 4022 Bombing(基本算法-水题)

Bombing Problem Description It's a cruel war which killed millions of people and ruined series of cities. In order to stop it, let's bomb the opponent's base. It seems not to be a hard work in circumstances of street battles, however, you'll be encou

HDU 4864 Task(基本算法-贪心)

Task Problem Description Today the company has m tasks to complete. The ith task need xi minutes to complete. Meanwhile, this task has a difficulty level yi. The machine whose level below this task's level yi cannot complete this task. If the company

hdu 4568 spfa 最短路算法+旅行商问题

http://acm.hdu.edu.cn/showproblem.php?pid=4568 Problem Description One day, a hunter named James went to a mysterious area to find the treasures. James wanted to research the area and brought all treasures that he could. The area can be represented a

hdu 1269 迷宫城堡(Targin算法)

---恢复内容开始--- 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Swwubmission(s): 10884    Accepted Submission(s): 4878 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单