题意

计算所有S的前缀在S中出现了几次

思路

跟前缀有关的题目可以多多考虑KMP的NEXT数组

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
char S[2000000];
int NEXT[2000000];
int dp[2000000];//dp[i] 表示 以i结尾的子串与前缀相等的个数 d[i]=d[next[i]]+1;
//一开始还以为是 d[i]=next[i]+1; 在abab这个样例中 d[3]=3 显然有重复计算了
int len;
int sum=0;
void get_next()
{
for(int i=1;i<=len;i++)
{
int p=i-1;
while(S[i]!=S[NEXT[p]+1]&&p!=0) p=NEXT[p];
if(p!=0) NEXT[i]=NEXT[p]+1; //这种不else 的写法注意清空NEXT
//else NEXT[i]=0;
}
}
int main()
{
// freopen("a.in","r",stdin);
int T;
cin>>T;
while(T--)
{
sum=0;
cin>>len;
memset(NEXT,0,sizeof(NEXT));
scanf("%s",S+1);
get_next();
for(int i=1;i<=len;i++)
{
dp[i]=dp[NEXT[i]]+1;
sum=(sum+dp[i])%10007;
}
cout<<sum<<endl;
}
}

[KMP][HDU3336][Count the string]的更多相关文章

  1. kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  2. HDU3336 Count the string —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others)     ...

  3. hdu3336 Count the string 扩展KMP

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  4. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  5. HDU3336 Count the string KMP 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...

  6. HDU3336 Count the string(kmp

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  7. HDU3336 Count the string 题解 KMP算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...

  8. HDU3336 Count the string

    居然一A了,说明对朴素的KMP还是有一定理解. 主要就是要知道next数组的作用,然后就可以计算每个i结尾的满足题意的串个数. #include<cstdio> #include<c ...

  9. hdu 3336 Count the string KMP+DP优化

    Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...

随机推荐

  1. EffectiveC#01--避免返回内部类对象的引用

    此篇是对00中第3点的再一次阐述. 1.如果一个属性返回一个引用类型,那么调用者就可以访问这个对象的公共成员,也包括修改这些属性的状态. public class MyBusinessObject { ...

  2. android——ImageLoader添加缓存

    //给图片加入到缓存中.            DisplayImageOptions options = new DisplayImageOptions.Builder().cacheOnDisc( ...

  3. Android Studio添加应用作为依赖时报错Error:Dependency MonthText:xlistview:unspecified on project app resolves to an APK archive which is not supported as a compilation dependency. File: 及其解决方案

    Error:Dependency MonthText:xlistview:unspecified on project app resolves to an APK archive which is ...

  4. JwPlayer播放器【去除Logo、去除版本信息】

    效果图: <html> <head> <title>JwPlayer播放器@杯中红茶</title> <script type="tex ...

  5. 加载为应用程序池‘DefaultAppPool'提供服务的进程失败,应用程序池被禁用【解决方法】

    iis应用程序池不能启动2008年03月13日 星期四 15:29iis应用程序池不能启动.WINDOWS2003 ENT SERVER(64位)环境下(测试过32位系统一样操作无此问题),当打开“应 ...

  6. iOS开发 数据库FMDB

    iOS开发  数据库FMDB 1.简介 需求作用: 如果需要保存大量的结构较为复杂的数据时候, 使用数据库, 例如交规考试项目 常用的数据库: (1)Microsoft SQL Server 2000 ...

  7. c#窗体的传值方法

    了解了窗体的显示相关知识,接着总结一下窗体的传值方法:  .通过构造函数  特点:传值是单向的(不可以互相传值),实现简单 实现代码如下: 在窗体Form2中         int value1;  ...

  8. codeforces 336C Vasily the Bear and Sequence(贪心)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Vasily the Bear and Sequence Vasily the b ...

  9. jetty 8.1.8 PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required

    应该是jdk和jre配置问题,建议看看这个博客:http://blog.csdn.net/nba_2011/article/details/7219750里边查看配置方法很清楚

  10. centos 6.4从源码安装mysql 5.6笔记

    上周在安装mysql时遇到了些许麻烦,今天整理下. 在代码目录建立obj文件夹. 在obj目录下,执行cmake .. -DXXX  // XXX表示一些参数,详见http://dev.mysql.c ...