Count the string (KMP+DP)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} /********************************************************************/ const int maxn = 1e6+;
char s[maxn];
int Next[maxn];
int sum[maxn]; const int mod = ; int main(){
int t; t = read();
while(t--){
int n; n = read();
scanf("%s", s+);
//int len = strlen(s+1);
Next[] = ;;
int k = ;
for(int i = ;i <= n;i++){
while(k > && s[k+] != s[i]){
k = Next[k];
}
if(s[k+] == s[i])
k++;
Next[i] = k;
}
int ans = ;
memset(sum, , sizeof(sum));
for(int i = ;i <= n;i++){
sum[i] = (sum[Next[i]] + )%mod;
ans = (ans + sum[i])%mod;
}
cout << ans << endl;
}
return ;
}
Count the string (KMP+DP)的更多相关文章
- 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 ...
 - hdu3336 Count the string kmp+dp
		
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...
 - 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 ...
 - [HDU 3336]Count the String[kmp][DP]
		
题意: 求一个字符串的所有前缀串的匹配次数之和. 思路: 首先仔细思考: 前缀串匹配. n个位置, 以每一个位置为结尾, 就可以得到对应的一个前缀串. 对于一个前缀串, 我们需要计算它的匹配次数. k ...
 - HDUOJ------3336  Count the string(kmp)
		
D - Count the string Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
 - HDU3336 Count the string  —— KMP next数组
		
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
 - HDU 3336 Count the string ( KMP next函数的应用 + DP )
		
dp[i]代表前i个字符组成的串中所有前缀出现的次数. dp[i] = dp[next[i]] + 1; 因为next函数的含义是str[1]~str[ next[i] ]等于str[ len-nex ...
 - Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP
		
B. Obsessive String Hamed has recently found a string t and suddenly became quite fond of it. He s ...
 - Count the string kmp
		
问题描述众所周知,aekdycoin擅长字符串问题和数论问题.当给定一个字符串s时,我们可以写下该字符串的所有非空前缀.例如:S:“ABAB”前缀是:“A”.“AB”.“ABA”.“ABAB”对于每个 ...
 
随机推荐
- CSS3实现3D木块旋转动画
			
CSS3实现3D木块旋转动画,css3特效,旋转动画,3D,立体效果,CSS3实现3D木块旋转动画是一款迷人的HTML5+CSS3实现的3D旋转动画. 代码下载:http://www.huiyi8.c ...
 - html5--2.6新的布局元素(5)-nav
			
html5--2.6新的布局元素(4)-aside/nav 学习要点 了解aside/nav元素的语义和用法 通过实例理解aside/nav元素的用法 aside元素 aside元素通常用来设置侧边栏 ...
 - CentOS7 默认防火墙firewalld
			
firewalld基础 firewalld是CentOS7源生支持的防火墙,firewalld最大的好处有两个:支持动态更新,不用重启服务:第二个就是加入了防火墙的“zone”概念. firewall ...
 - 基于node.js及express实现中间件,实现post、get
			
首先,当然是有必要的环境,安装node,这个我就不多说了. 依赖模块: "express": "^4.13.4", "request": & ...
 - listen 62
			
The Hangover I'm never drinking again. And this time I mean it! Anyone who's suffered through a bad ...
 - BZOJ_4033_[HAOI2015]树上染色_树形DP
			
BZOJ_4033_[HAOI2015]树上染色_树形DP Description 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 将其他的 ...
 - ACM学习历程——HDU1331 Function Run Fun(锻炼多维dp的打表)
			
Description We all love recursion! Don't we? Consider a three-parameter recursive function w( ...
 - Godot-3D教程-01.介绍3D
			
创建一个3D游戏将是个挑战,额外增加的Z坐标将使许多用于2D游戏的通用技术不再有用.为了帮助变换(transition),值得一提的是Godot将使用十分相似的API用于2D和3D. 目前许多节点是公 ...
 - 媒体格式分析之flv -- 基于FFMPEG
			
本来是应该先写一个媒体文件格式的简单讲解的,还没来得及写,以后再写.今天就先根据ffmpeg的flv.c的flv_demux这个结构体来讲解一下当前比较流行的媒体格式flv. FLV 是FLASH V ...
 - POJ3259(ford判环)
			
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 39078 Accepted: 14369 Descr ...