HDU 5972 Regular Number(ShiftAnd+读入优化)
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5972
【题目大意】
给出一个字符串,找出其中所有的符合特定模式的子串位置,符合特定模式是指,该子串的长度为n,并且第i个字符需要在给定的字符集合Si中
【题解】
利用ShiftAnd匹配算法。
bt[i]表示字符i允许在哪些位置上出现,我们将匹配成功的位置保存在dp中,那么就可以用dp[i]=dp[i-1]<<1&bt[s[i]]来更新答案了
利用滚动数组和bitset可以来优化这样的运算,当一个位置的匹配在更新的过程中没有丢失,
即始终在特定模式中直到定长,那么这个位置就是成功匹配位,复杂度为O(nm/64)
由于输入的数据量庞大,因此需要读入和输出优化。
终于AC了,补上大连赛区的遗憾。
【代码】
#include <cstdio>
#include <bitset>
#include <cstring>
using namespace std;
const int M=1010,N=5000010,U=256;
bitset<M> dp[2],bt[U];
int n,m,x,id[U],cnt,l;
char s[N];
namespace fastIO{#define BUF_SIZE 100000#define OUT_SIZE 1000000bool IOerror=0;inline char nc(){static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;if(p1==pend){p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin);if (pend==p1){IOerror=1;return -1;}}return *p1++;}inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}inline int read(char *s){char ch=nc();for(;blank(ch);ch=nc());if(IOerror)return 0;for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;*s=0;return 1;}inline int RI(int &a){char ch=nc(); a=0;for(;blank(ch);ch=nc());if(IOerror)return 0;for(;!blank(ch)&&!IOerror;ch=nc())a=a*10+ch-'0';return 1;}struct Ostream_fwrite{char *buf,*p1,*pend;Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;}void out(char ch){if (p1==pend){fwrite(buf,1,BUF_SIZE,stdout);p1=buf;}*p1++=ch;}void flush(){if (p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}}~Ostream_fwrite(){flush();}}Ostream;inline void print(char x){Ostream.out(x);}inline void println(){Ostream.out('\n');}inline void flush(){Ostream.flush();}char Out[OUT_SIZE],*o=Out;inline void print1(char c){*o++=c;}inline void println1(){*o++='\n';}inline void flush1(){if (o!=Out){if (*(o-1)=='\n')*--o=0;puts(Out);}}struct puts_write{~puts_write(){flush1();}}_puts;
};
void init(){cnt=0;for(int i='0';i<='9';i++)id[i]=cnt++;
}
void ShiftAnd(int n,int m){int cur=1,f=0;dp[0].reset(); dp[0].set(0);for(int i=1;i<=n;i++,cur^=1){dp[cur]=dp[cur^1]<<1&bt[id[s[i]]];dp[cur].set(0);if(dp[cur][m]){for(int j=i-m+1;j<=i;j++)fastIO::print(s[j]);fastIO::println();}}
}
int main(){ //freopen("demo.in","r",stdin);//freopen("demo.out","w",stdout);init();while(fastIO::RI(m)){ for(int i=0;i<cnt;i++)bt[i].reset();for(int i=1;i<=m;i++){fastIO::RI(l);for(int j=1;j<=l;j++){fastIO::RI(x);bt[x].set(i);}}fastIO::read(s+1); n=strlen(s+1);ShiftAnd(n,m);}return 0;
}
转载于:https://www.cnblogs.com/forever97/p/hdu5972.html
HDU 5972 Regular Number(ShiftAnd+读入优化)相关推荐
- HDU 5972 Regular Number
Regular Number http://acm.hdu.edu.cn/showproblem.php?pid=5972 题意: 给定一个字符串,求多少子串满足,子串的第i位,只能是给定的数(小于等 ...
- HDU 3826 Squarefree number:题目解答源码
2019独角兽企业重金招聘Python工程师标准>>> HDU 3826 Squarefree number:题目解答源码 In mathematics,a squarefree n ...
- C++ 读入优化与输出优化 模板
简介 C++ 是一种神奇的编程语言-- 自然,读入和输出也有着许多种形式: 如:scanf(),cin>>,cout<<,printf()scanf(),cin>> ...
- shift-and(HDU 5972 2016ICPC大连 B: Regular Number)
题意: 有一个长度为n的子串,这个子串有多种形态,也就是每一位都可以是几个数的其中一个,还有一个母串str[] 如果某种形态的子串可以和母串匹配上,就说明这个子串是有价值的 输出所有的有价值的形态(按 ...
- 【HDU】1005 Number Sequence (有点可爱)
http://acm.hdu.edu.cn/showproblem.php?pid=1005 A number sequence is defined as follows: f(1) = 1, f( ...
- HDU 5898 odd-even number
题目:odd-even number 链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5898 题意:给一个条件,问l 到r 之间有多少满足条件的 ...
- hdu 2665 Kth number(划分树模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ] 改变一下输入就可以过 http://poj.org/problem? ...
- HDU 4262 Juggler (模拟+线段树优化)
转载请注明出处,谢谢http://blog.csdn.net/acm_cxlove/article/details/7854526 by---cxlove http://acm.hdu.e ...
- 划分树基础 —— HDU 2665 Kth number
对应 HDU 题目 :点击打开链接 Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K ...
最新文章
- 普罗米修斯 监控_完美的分布式监控系统——普罗米修斯
- mysql+8.0+新特性_MySQL 8.0的一些新特性汇总大全
- thinkphp5中资源文件路径的使用
- C++数组与指针回顾总结
- ACM北大暑期课培训第六天
- java基础知识点(2)—— 环境变量的配置
- linux shell基础
- 赛锐信息:ABAP程序优化的一些总结
- tile布局的ButtonBar
- 宋利兵 mysql_MySQL数据库InnoDB存储引擎Log漫游(3)
- linux ios文件是否存在,技术|如何在 Linux 中验证 ISO 镜像
- arduino无源蜂鸣器歌曲编码_Arduino 蜂鸣器音乐代码转化程序开发
- 如何使用IDEA进行协作编码,共享项目,并实时的处理
- 4.2.4 图像色调处理
- ch340t电路_USB转串口CH340各封装官方原理图、PCB参考电路图下载2018-3-23
- 微众银行客服电话是95384 认准官方的人工客服电话
- 爱看小说手机网源码全站带3w数据带采集,ThinkPHP内核小说网站源码带听书等全部插件
- python中forward的作用_Pytorch学习笔记07----nn.Module类与前向传播函数forward的理解
- C语言——冒泡排序(分析详解)
- WPS如何并排放置两张图片_WPS表格:如何批量将所有图片大小修改成一致?
热门文章
- python namedtuple用法_Python的collections模块中namedtuple结构使用示例
- 建立二维数组_二维数组的 DP
- java 泛型 父子,Java泛型-mb601cf8a78cc07的博客-51CTO博客
- jsp简介mysql_个人知识管理系统的设计与实现(JSP,MySQL)(含录像)
- b2b2c源码 java_java B2B2C源码电子商务平台 ---搭建Eureka注册中心
- 外网访问arm嵌入式linux_嵌入式Linux系统编程——文件读写访问、属性、描述符、API
- 基于YOLO的手部检测和计数实现(课程设计,训练和测试)(1)
- 深度学习--TensorFlow(4)BP神经网络(损失函数、梯度下降、常用激活函数、梯度消失梯度爆炸)
- 【机器视觉案例】(8) AI视觉,手势控制电脑鼠标,附python完整代码
- java avl_Java底层实现AVL 平衡二叉树