HDU 5972 Regular Number (bitset)

来源:互联网 发布:爱思软件助手 编辑:程序博客网 时间:2024/06/16 17:05

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

43 0 9 72 5 72 2 52 4 509755420524

 

Sample Output

975575540524

 

题意

n 组数,每组数中包含一些一位数,输出所有按顺序从每个组中取出一个数并且这些数组合形成的串是给定串的子串。

 

思路

一开始想到把所有组的数压进一个 bitset 中,其中每个组占 10 位,随后用给定串构造一个类似的 bitset ,然后移动相与求结果,无奈复杂度过高放弃了。


一个更优雅的做法是:我们用一个 bitset 数组记录 [0,9] 它们出现的组号,然后设定一个 ans ,遍历给定串,对于每一位字符 s ,首先左移 ans 并置 ans[0]=1 ,它标记了当前所有连续组的长度,显然 ans&=a[s0] 这一步决定了是否可以扩充一位字符到之前的结果中,如果扩充到长度为 n ,则 ans[n1]=1 ,输出这部分即可。

 

AC 代码

#include<bits/stdc++.h>#define IO ios::sync_with_stdio(false);\    cin.tie(0);\    cout.tie(0);using namespace std;const int maxn = 1e3+10;const int maxm = 5e6+10;bitset<maxn>a[10],ans;char str[maxm];void init(){    for(int i=0; i<10; i++)        a[i].reset();    ans.reset();}int main(){    int n;    while(~scanf("%d",&n))    {        init();        for(int i=0; i<n; i++)        {            int ai;            scanf("%d",&ai);            for(int j=0; j<ai; j++)            {                int x;                scanf("%d%*c",&x);                a[x].set(i);            }        }        gets(str);        int len = strlen(str);        for(int i=0; i<len; i++)        {            ans<<=1;            ans[0]=1;            ans&=a[str[i]-'0'];            if(ans[n-1]==1)            {                char tmp = str[i+1];                str[i+1] = 0;                puts(str+i-n+1);                str[i+1] = tmp;            }        }    }    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 寝室的电闸不亮了怎么办 海绵城市改造只挖不修怎么办 怀孕一个月做了胸透怎么办 怀孕了照了胸透怎么办 刚怀孕照了胸透怎么办 怀孕进了ct室怎么办 消防稳压罐气囊压力不够怎么办 备孕期间拍了ct怎么办 晚上睡不好老做梦怎么办 零线和地线短路怎么办 火花塞拧的过紧怎么办 眼睛被电弧光灼伤怎么办 电脑模拟器微信附近人屏蔽了怎么办 美的电压锅漏气怎么办 电压力锅排气阀漏气怎么办 win10系统上不了网怎么办 手机自拍杆坏了怎么办 车辆遭核生化打击时怎么办? 遭核生化打击时怎么办? 车辆行驶中遭核生化打击时怎么办? 硫酸气体熏到嗓子怎么办 硫酸味儿进嗓子了怎么办 吸入二氧化氯气体后怎么办 衣服84消毒 变色了怎么办 我的身份证被诈骗犯知道了怎么办 母婴店来客人少怎么办 键盘驱动被卸载了怎么办 广州 狗狗随地大小便怎么办 上海 狗狗随地大小便怎么办 服装店人流量少生意差怎么办 2018年服装店生意特别差怎么办 打印机拍的照片打出黑怎么办 租的汽车撞了怎么办 神州租车车坏了怎么办 深圳市公安局办保安员证怎么办? 老婆被车撞了后失忆了怎么办 生气引起的短暂失忆怎么办 win10系统忘记开机密码怎么办 戴尔win10密码忘了怎么办 戴尔电脑win10密码忘了怎么办 出门忘记带身份证了怎么办