链接:

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. Oracle 中 decode 函数用法(字段A,B, C,A不为空则取A,否则取B,B为空则取C)

    –decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值) SELECT DECODE(TEMP.SCORE, ‘A’, ‘90-100’, ‘B’, ‘80-89’, ‘C’, ...

  2. IE浏览器的脚本文本框监听事件

    一.IE8的文本框监听事件 由于在IE8所以input和propertychange都不能用,终于皇天不负有心人让我找到了这个:https://github.com/sophiebits/jquery ...

  3. spark教程(18)-sparkSQL 自定义函数

    sparkSQL 也允许用户自定义函数,包括 UDF.UDAF,但没有 UDTF 官方 API class pyspark.sql.UDFRegistration(sparkSession)[sour ...

  4. python-open函数操作实例

    一.这个是源配置文件: global               log 127.0.0.1 local2        daemon        maxconn 256        log 12 ...

  5. 安装calico

    安装docker:https://www.cnblogs.com/cjsblogs/p/8717304.html 安装etcd集群:https://www.cnblogs.com/cjsblogs/p ...

  6. .Net高级工程师面试题

    ----------高级开发工程师岗位职责: 1.完成平台系统新功能模块开发,维护现有产品,独立地设计.开发.实现和测试关键系统: 2.负责公司项目核心代码的编写: 3.根据产品需求进行业务功能的开发 ...

  7. 帝国cms 加载更多的实现(父栏目以及子栏目都可以实现)

    1. <div class="pagelist"> <span id="loadmore" class="btn" sty ...

  8. CentOS7安装node环境

    1 找到官网复制链接地址 2 再自己Linux的按照路径,wget下载软件包并解压 wget https://nodejs.org/dist/v10.16.1/node-v10.16.1-linux- ...

  9. 3、Rsync备份服务实战

    1.Rsync基本概述 rsync是一款开源的备份工具,可以在不同主机之间进行同步,可实现全量备份与增量备份,因此非常适合用于架构集中式备份或异地备份等应用. rsync官方地址:传送门http:// ...

  10. 第十五章·Kibana深入-Dev Tools及Lucene语法

    Dev Tools介绍 Dev Tools 页面包含开发工具,您可以使用这些Dev Tools与Kibana中的数据进行交互. 原先的交互式控制台Sense,使用户方便的通过浏览器直接与Elastic ...