hdu3336(KMP+DP)
2015-08-20 17:56
162 查看
题意:求字符串中所有前缀跟其相同的子串的总个数?
同样是KMP的应用
dp[i]代表的是从 最长前缀到最小前缀的匹配数
同样是KMP的应用
dp[i]代表的是从 最长前缀到最小前缀的匹配数
#include <stdio.h> #include <string.h> #include <iostream> #include <math.h> #include <algorithm> using namespace std; #define N 200010 char s ; int dp ; int nextval ,len; void getnext(const char*s){ int i=0,j=-1; nextval[i]=j; while(i!=len){ if(j==-1||s[i]==s[j]) nextval[++i]=++j; else j=nextval[j]; }//while }//getnext int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d",&len); scanf("%s",s); getnext(s); memset(dp,1,sizeof(dp)); dp[0]=0; int ans=0; for(int i=1;i<=len;i++){ dp[i]=dp[nextval[i]]+1; ans+=dp[i]%10007; }printf("%d\n",ans%1007); }//while return 0; }
相关文章推荐
- UNIX Tutorial Three
- Win 10打击盗版软件的具体方式
- Handler引起的内存泄露问题
- 欢迎使用CSDN-markdown编辑器
- django报错:ImproperlyConfigured和AppRegistryNotReady
- IllegalArgumentException:The observer is null的解决方法
- 设计模式笔记(四)--工厂方法模式、抽象工厂模式
- 使用 Linux 系统调用的内核命令
- C++常见错误及解决方法,持续更新
- 如何给MP4视频加字幕
- Hadoop RCFile存储格式详解(源码分析、代码示例)
- SQL Server 跨网段(跨机房)复制
- 解决Android软键盘弹出引起的各种不适
- MySQL清空表数据并重置Auto_increment字段起始值为1
- 后端分布式系列:分布式存储-概述
- 分布式存储概述
- graphite
- js/jquery 去掉空格.回车.换行
- 多个线程调用同一个处理函数的互斥问题
- Windows 10企业批量部署实战之Logs日志存放