3670: [Noi2014]动物园
2016-04-30 08:32
281 查看
kmp维护两个指针,记录cnt表示能向前跳几步
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1000005; ll ans=1;int p[maxn],cnt[maxn],T,n;char s[maxn]; int main(){//freopen("in.in","r",stdin); scanf("%d",&T); while(T--){ans=1; scanf("%s",s+1); p[0]=p[1]=cnt[0]=0;cnt[1]=1; n=strlen(s+1); int f=0,ff=0; for(int i=2;i<=n;i++){ while(f && s[f+1]!=s[i])f=p[f]; if(s[f+1]==s[i])f++; // p[i]=f; cnt[i]=cnt[f]+1; while(ff && s[ff+1]!=s[i])ff=p[ff]; if(s[ff+1]==s[i])ff++; while(ff && (ff<<1)>i)ff=p[ff]; ans*=(cnt[ff]+1); ans%=1000000007; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- 自定义控件
- Elasticsearch 聚合作用域(Scoping Aggregations)
- 五、JAVA数组
- bzoj4459 丢番图
- 【笔试/面试】—— 数学找规律题
- 开源新闻速递(160430):GNOME 3.22 开始开发
- matplotlib作图中文显示问题
- curl 模拟登陆
- NoSQL数据库技术
- 5-48 银行排队问题之单窗口“夹塞”版 (30分)
- C语言:关于程序运行时的内存管理
- Elasticsearch 聚合 - 时间数据处理(Looking at Time)
- Jetbrains IntelliJ IDEA破解方法
- Java类和对象及实例
- XML
- LeetCode 338. Counting Bits(统计比特数)
- 安装 phpredis 扩展
- bzoj3930 [CQOI2015]选数
- COGS 1834. [国家集训队2011]采矿
- Jquery之获取和设置元素内容