HDU-3336-Count the string(扩展KMP)
链接:
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)的更多相关文章
- 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) ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- HDU 3336 - Count the string(KMP+递推)
题意:给一个字符串,问该字符串的所有前缀与该字符串的匹配数目总和是多少. 此题要用KMP的next和DP来做. next[i]的含义是当第i个字符失配时,匹配指针应该回溯到的字符位置. 下标从0开始. ...
- 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 ...
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu3336 Count the string 扩展KMP
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- HDU 3336 Count the string 查找匹配字符串
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdoj 3336 Count the string【kmp算法求前缀在原字符串中出现总次数】
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 ...
随机推荐
- 2019牛客暑期多校训练营(第五场)- B generator 1 (齐次线性递推+矩阵快速幂)
题目链接:https://ac.nowcoder.com/acm/contest/885/B 题意:已知齐次线性式xn=a*xn-1+b*xn-2,已知a,b,x0,x1,求xn,n很大,n<= ...
- setsockopt用法详解
最近做的一个程序用到了IOCP通信模型,里面用到了setsockopt对套接字进行设置,看源代码的时候最setsockopt函数很不理解,看了msdn以后还是不太明白这个函数的用法,于是就到网上找了一 ...
- django进阶版2
目录 批量插入数据 自定义分页器 创建多表关系的3种方法 全自动 全手动 半自动 form组件 如何渲染页面 第一种方式 第二种方式 第三种方式 如何显示错误信息 forms组件钩子函数 局部钩子 全 ...
- iview给布局MenuItem标签绑定点击事件
@click.native="menuHandleClick"
- RSA加密-解密以及解决超长内容加密失败解决
加解密(没有使用到证书):https://blog.csdn.net/qy20115549/article/details/83105736 生成证书网站:https://blog.csdn.net/ ...
- 安装consul
概述consul是google开源的一个使用go语言开发的服务发现.配置管理中心服务.内置了服务注册与发现框架.分布一致性协议实现.健康检查.Key/Value存储.多数据中心方要依赖其他工具(比如Z ...
- c# 获取屏幕图片
Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty)); using (Bitmap bitmap = new Bitma ...
- BZOJ2456-mode题解--一道有趣题
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2456 瞎扯 这是今天考的模拟赛T2交互题的一个30分部分分,老师在讲题时提到了这题.考 ...
- JavaScript和Java是不同公司开发的不同产品
首先,JavaScript和Java是不同公司开发的不同产品.javascript是Netscape的产品.它的目的是扩展Netscape Navigator功能,开发一种可以嵌入到网页中的对象和事件 ...
- element-ui 表单自定义日期输入校验
methods: { validateDate(rule, value, callback){ if (value) { let timestamp = new Date(value).getTime ...