当前位置:网站首页>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);
}
}
边栏推荐
- Cocoapods installation (after xcode8.0, the infinite card is in the setting up cocoapods master repo)
- Libtorch video memory management example
- Delaying patient self-help guide | "I have 100 excuses for not delaying, but I am not willing to take a step"
- ACM. HJ61 放苹果 ●
- Modify the UE4 cache path to relieve the pressure on the C disk
- F - Phone List HDU - 1671 (字典树查前缀)
- NewOJ Week 6
- 智力题整理总结
- ARP protocol and ARP attack
- ASP.Net Core创建Razor页面上传多个文件(缓冲方式)
猜你喜欢

Delaying patient self-help guide | "I have 100 excuses for not delaying, but I am not willing to take a step"

The first in the industry! Krypton app has obtained the authoritative certification of China Network Security Review Technology and Certification Center

十一、美化界面

Database management: Navicat premium 15

matplotlib plt. Details of subplots()

总结了嵌入式开发中几种常见的设备通信协议

With what to save you? My attention

matplotlib plt.subplots()详解

【物联网开发】正点原子STM32战舰v3+机智云AIoT+APP控制

晶合集成通过注册:拟募资95亿 合肥建投与美的是股东
随机推荐
Uibutton implements left text and right picture
在程序退出,defer 不执行是为什么
Why does defer not execute after the program exits
[Internet of things development] punctual atom STM32 warship v3+ smart cloud aiot+app control
如何解决织梦文章列表自动更新点击次数
JS里的数据类型(基础)
30组户外旅行游玩VLOG记录LUTs调色预设Moody Travel LUTs
The final scheme of adding traceid at the C end
[OWT] P2P signaling server running
集群一---LVS负载均衡集群NAT模式及LVS负载均衡实战部署
PowerPoint 教程,如何在 PowerPoint 中将幻灯片整理成组?
Lvs+keepalived high availability cluster deployment
Golang learning notes - pointer
2022年焊工(高级)考试题模拟考试题库模拟考试平台操作
Operation of 2022 welder (Advanced) examination question bank simulation examination platform
[microservices 7] in depth analysis of bestavailablerule source code of ribbon load balancing strategy
有哪些新手程序員不知道的小技巧?
全新混合架构iFormer!将卷积和最大池化灵活移植到Transformer
11、 Beautify the interface
js中的for.....in函数