当前位置:网站首页>J - Count the string HDU - 3336 (KMP)
J - Count the string HDU - 3336 (KMP)
2022-06-21 21:29:00 【fighting_ yifeng】
J - Count the string HDU - 3336
The question : Let's find the sum of the matching numbers of the prefix of the string .
analysis : Let's think about it next What is stored in the array , Prefix and suffix of the same length .
Give an example to illustrate the key step, that is, the accumulation .
abab next The array value is 0 0 1 2 namely ab matching What we need is the number of matches of all prefixes , The next step is to use a dp Array plus the previous number ,next[i] = 0 Yes, the number of matches is 1 and next[i] > 0 It means that he can be matched many times , The one in the back a Match the a and aba Behind the b Match the ab and abab namely dp[i] = dp[next[i]]+ 1;
#include <iostream>
#include <cstdio>
#include <stack>
#include <cmath>
#include <set>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
const int maxn = 1000010;
const int mod = 10007;
int next1[maxn], n, m, t, dp[maxn];
char x[maxn], y[maxn];
void kmp_pre(int m)
{
int i, j;
j = -1;next1[0] = -1;
i = 0;
while(i < m){
while(-1 != j && x[i] != x[j]) j = next1[j];
next1[++i] = ++j;
}
}
int main()
{
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
scanf("%s", &x);
kmp_pre(n);
int res = 0;
for(int i = 1; i <= n; i++)
{
dp[i] = (dp[next1[i]] + 1) % mod;
res = (res + dp[i]) % mod;
}
printf("%d\n", res);
}
}
边栏推荐
- 有哪些新手程序员不知道的小技巧?
- Operation of 2022 welder (Advanced) examination question bank simulation examination platform
- 提升四大性能!D-Wave发布下一代量子退火机原型
- ARP协议及ARP攻击
- [applet] realize applet and background ASP through request Net data JSON transmission (post protocol text + code)
- The final scheme of adding traceid at the C end
- Mendeley 安装、配置、使用
- Citus 11 for Postgres is completely open source and can be queried from any node (citus official blog)
- Database management: Navicat premium 15
- What is the C language callback function?
猜你喜欢
随机推荐
Tencent global digital ecology Conference - high speed intelligent computing special session!
MySQL数据库---事务
测评 | 在生活中,你是一位什么样的人呢?
Lvs+keepalived high availability cluster deployment
PowerPoint tutorial, how to organize slides into groups in PowerPoint?
J - Count the string HDU - 3336 (KMP)
[Internet of things development] punctual atom STM32 warship v3+ smart cloud aiot+app control
Leecode435 non overlapping interval
ASP.Net Core创建Razor页面上传多个文件(缓冲方式)
PowerPoint 教程,如何在 PowerPoint 中将幻灯片整理成组?
Citus 11 for Postgres is completely open source and can be queried from any node (citus official blog)
On the charm of code language
Dedecms dream weaving background mailbox verification sending mail configuration tutorial
[Patents and papers-19]: Notice on electronic information application of Nanjing City, Jiangsu Province in 2022 (medium and advanced)
How functions are declared
Xshell7+Xftp7免费版下载
提升四大性能!D-Wave发布下一代量子退火机原型
2016 ICLR | Adversarial Autoencoders
Mysql database transaction
[microservices 7] in depth analysis of bestavailablerule source code of ribbon load balancing strategy









