链接:

https://vjudge.net/problem/HDU-3336

题意:

It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:

s: "abab"

The prefixes are: "a", "ab", "aba", "abab"

For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6.

The answer may be very large, so output the answer mod 10007.

思路:

计算s前缀出现的次数, 考虑扩展KMP的Nex数组, 从i位置开始从s0开始匹配的长度.

即这些长度的前缀都出现过.累加和即可.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 2e5+10;
const int MOD = 1e4+7; char s1[MAXN], s2[MAXN];
int Next[MAXN], Exten[MAXN]; void GetNext(char *s)
{
int len = strlen(s);
int a = 0, p = 0;
Next[0] = len;
for (int i = 1;i < len;i++)
{
if (i >= p || i+Next[i-a] >= p)
{
if (i >= p)
p = i;
while (p < len && s[p] == s[p-i])
p++;
Next[i] = p-i;
a = i;
}
else
Next[i] = Next[i-a];
}
} void ExKmp(char *s, char *t)
{
int len = strlen(s);
int a = 0, p = 0;
GetNext(t);
for (int i = 0;i < len;i++)
{
if (i >= p || i + Next[i-a] >= p)
{
if (i >= p)
p = i;
while (p < len && s[p] == t[p-i])
p++;
Exten[i] = p-i;
a = i;
}
else
Exten[i] = Next[i-a];
} } int main()
{
int t, n;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
scanf("%s", s1);
GetNext(s1);
int res = 0;
for (int i = 0;i < strlen(s1);i++)
res = (res+Next[i])%MOD;
printf("%d\n", res);
} return 0;
}

HDU-3336-Count the string(扩展KMP)的更多相关文章

  1. HDU 3336 Count the string(KMP的Next数组应用+DP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. hdu 3336 count the string(KMP+dp)

    题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...

  3. HDU 3336 - Count the string(KMP+递推)

    题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...

  4. 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 ...

  5. hdu 3336:Count the string(数据结构,串,KMP算法)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  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. HDU 3336 Count the string 查找匹配字符串

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 3336 Count the string(next数组运用)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. hdu 3336 Count the string -KMP&dp

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

随机推荐

  1. CWMP开源代码研究——git代码工程

    原创作品,转载请注明出处,严禁非法转载.如有错误,请留言! email:40879506@qq.com 声明:本系列涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅 ...

  2. python列表的切片与复制

    切片,即处理一个完整列表中部分数据. 语法 变量[起始索引:终止索引:步长] 首先创建一个字符串列表 >>> cars = ['toyota', 'honda', 'mazda', ...

  3. double write 双写

    Oracle 8KB Postgresql 8KB MySQL Innodb 16KB buffer page block首先,要DML数据,需要先把page读取到index page中,之后对内存中 ...

  4. spring-boot war包部署(二)

    环境 jdk 8 tomcat 8.5 sts 4.4.2 maven 3.6.1 背景 有时候,服务器已经有了,我们必须要使用 war 包进行部署,所以需要 spring boot 支持打包和部署成 ...

  5. Qt5笔记(一)

      1.  只要指定父对象,直接或间接继承于QObect,那么子对象如果是动态分配空间,不需要手动释放内存,系统会自动释放.( Qt的内存回收机制) 2.  想要查看某个函数,可以F1进入帮助文档,连 ...

  6. jenkins+docker+git+etcd实现应用配置文件管理

    两台机器: 一台机器安装gitlab: http://www.cnblogs.com/cjsblogs/p/8716932.html 另一台机器安装etcd+docker+jenkins jenkin ...

  7. 学习笔记--Tarjan算法之割点与桥

    前言 图论中联通性相关问题往往会牵扯到无向图的割点与桥或是下一篇博客会讲的强连通分量,强有力的\(Tarjan\)算法能在\(O(n)\)的时间找到割点与桥 定义 若您是第一次了解\(Tarjan\) ...

  8. C++ STL用法总结(持续更新)

    Vector 动态数组 https://www.cnblogs.com/zhonghuasong/p/5975979.html lower_bound&&upper_bound htt ...

  9. spring AOP的相关术语

    连接点:Joinpoint   其实业务层接口的方法 切入点:Pointcut 被增强的是切入点,没被增强是永远都是连接点.连接点不一定是切入点,切入点一定是连接点 通知:Advice 就是指要增强的 ...

  10. 林大妈的CSS知识清单(二)可见格式化模型(内含margin塌陷与浮动闭合的解决方案)

    盒模型.浮动和定位是CSS中最重要的三个概念.它们共同决定了一个元素在页面中以怎样的形式进行排布与显示. 一.盒模型 1. 定义 盒模型是CSS的核心概念.一个页面中,所有的元素(不管他最终显示是圆形 ...