HDU 3336 Count the string(KMP)
来源:互联网 发布:淘宝联盟 百度百科 编辑:程序博客网 时间:2024/06/05 10:41
KMP中的f数组相当于一个状态机。
我们如果能求出处理到第i个字符时,有多少从第1个字符开始的前缀和以i为结尾的后缀完全相同,那么扫一遍这个串然后求和就行了。
从第1个字符开始的前缀和以i为结尾的后缀和f数组意义很像,当沿着失配边一直走(j=f[j]),到0之前,经过多少个节点就是有多少个前缀等于后缀。
所以递推维护每个f[i]到0之间有多少节点。然后扫一遍。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define mod 10007using namespace std;char s[200005];int f[200005];int c[200005];int main(){ int T; scanf("%d",&T); while(T--){ int len; scanf("%d%s",&len,s); c[0]=c[1]=0; int res=1; memset(c,0,sizeof(c)); for(int i=1;i<len;i++){ res++; int j=f[i]; while(j&&s[i]!=s[j]) j=f[j]; if(s[i]==s[j]){ f[i+1]=j+1; c[i+1]=c[j+1]+1; res+=c[i+1]; } else f[i+1]=0; res%=mod; } printf("%d\n",res); } return 0;}
0 0
- hdu 3336 Count the string(KMP)
- hdu 3336 Count the string(KMP)
- HDU 3336 Count the string(kmp)
- HDU 3336 Count the string(KMP)
- HDU 3336 Count the string(KMP)
- 【hdu 3336】Count the string(kmp)
- 【HDU 3336】Count the string(kmp)
- 【HDU-3336-count the string】(KMP)
- hdu 3336 Count the string KMP
- KMP Hdu-3336 Count the string
- HDU 3336 Count the string(KMP 理解)
- HDU 3336 Count the string KMP
- HDU 3336 Count the string(KMP+dp)
- [HDU 3336]Count the String[kmp]
- [HDU 3336]Count the String[kmp][DP]
- HDU 3336 Count the string KMP+DP
- hdu 3336 Count the string(KMP)
- hdu 3336 Count the string KMP+DP
- solr查询语法
- Problem 2072 Count
- Linux下的查找命令使用指导
- Baidu Map开发示例
- 【手势交互】6. 微动VID
- HDU 3336 Count the string(KMP)
- HDU 3046 Pleasant sheep and big big wolf(最小割)
- Hibernate ehcache配置二级缓存
- iOS int long NSInteger 入门与兼容问题讲解,让你秒懂
- 配置文件工具类
- UILable写出类似Cell的UITableViewCellStyleSubtitle状态下效果
- JBoss调优(一)JVM调优
- sizeof(void*)
- Ctemplate的简介