hdu3336 Counting the string kmp的next数组的应用
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3336/
题意就是要求一个字符串的所有前缀在字符串中出现的次数之和,我们容易想到kmp中的next数组,next[i]=j,表示存在一个长度为j的前缀与长度为j的后缀相同,本题的结果就是要对每一位的next数组都向前回退,回退的次数就是长度不同的前缀出现的次数。还可以采用dp的策略。
代码如下:
- #include<bits/stdc++.h>
- using namespace std;
- typedef unsigned int ui;
- typedef long long ll;
- typedef unsigned long long ull;
- #define pf printf
- #define mem(a,b) memset(a,b,sizeof(a))
- #define prime1 1e9+7
- #define prime2 1e9+9
- #define pi 3.14159265
- #define lson l,mid,rt<<1
- #define rson mid+1,r,rt<<1|1
- #define scand(x) scanf("%llf",&x)
- #define f(i,a,b) for(int i=a;i<=b;i++)
- #define scan(a) scanf("%d",&a)
- #define dbg(args) cout<<#args<<":"<<args<<endl;
- #define inf 0x3f3f3f3f
- #define maxn 1000010
- int n,m,t;
- int nxt[maxn];
- char s[maxn];
- void getnxt()
- {
- int i=-,j=;
- nxt[]=-;
- while(j<n)
- {
- if(i==-||s[i]==s[j])
- {
- i++,j++;
- nxt[j]=i;//不能用kmp优化的nxt,那样会损失数量
- }
- else i=nxt[i];
- }
- }
- int main()
- {
- //freopen("input.txt","r",stdin);
- //freopen("output.txt","w",stdout);
- std::ios::sync_with_stdio(false);
- scan(t);
- while(t--)
- {
- scan(n);
- scanf("%s",s);
- getnxt();
- int ans=;
- f(i,,n)//查看以i-1为截至位置的公共前缀后缀
- {
- int j=i;
- while(j>)
- {
- ans++;//初始时本身还要算一次
- if(ans>)ans-=;
- j=nxt[j];
- }
- }
- pf("%d\n",ans);
- }
- }
dp:
- #include <cstdio>
- #include <cstring>
- #include <iostream>
- #include <time.h>
- #include <cstdlib>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- int const MOD = ;
- int const MAXN = ;
- char s[MAXN];
- int next[MAXN],dp[MAXN];
- inline void Get_Next(int n){
- memset(next,,sizeof(next));
- for(int i = ;i < n;i++){
- int j = next[i];
- while(j && s[i] != s[j]) j = next[j];
- if(s[i] == s[j]) next[i + ] = j + ;
- else next[i + ] = ;
- }
- }
- int main(){
- int T;
- while(~scanf("%d",&T)){
- while(T--){
- int n;
- scanf("%d",&n);
- scanf("%s",s);
- Get_Next(n);
- for(int i = ;i < n;i++){
- dp[i] = ;
- }
- dp[] = ;
- int sum = ;
- for(int i = ;i <= n;i++){
- dp[i] = dp[next[i]] + ;
- sum = (sum + dp[i]) % MOD;
- }
- printf("%d\n",sum);
- }
- }
- return ;
- }
hdu3336 Counting the string kmp的next数组的应用的更多相关文章
- HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336 Count the string Time Limit: 2000/1000 MS (Java/Others) ...
- hdu3336 Count the string kmp+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...
- HDU3336 Count the string KMP 动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...
- HDU3336 Count the string(kmp
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- [KMP][HDU3336][Count the string]
题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...
- Count the string[KMP]HDU3336
题库链接http://acm.hdu.edu.cn/showproblem.php?pid=3336 这道题是KMP的next数组的一个简单使用,首先要理解next数组的现实意义:next[i]表示模 ...
- 求最长公共前缀和后缀—基于KMP的next数组
KMP算法最主要的就是计算next[]算法,但是我们知道next[]求的是当前字符串之前的子字符串的最大前后缀数,但是有的时候我们需要比较字符串中前后缀最大数,比如 LeetCode的shortest ...
- KMP(next数组的更新理解)Codeforces Round #578 (Div. 2)--Compress Words
题目链接:https://codeforc.es/contest/1200/problem/E 题意: 有n串字符串,让你连起来:sample please ease in out ---> ...
随机推荐
- 对RLC重排序窗口大小的一点讨论
在LTE协议栈的PDCP层和RLC层,都有一个重排序窗口(reordering window),主要用来保证数据的可靠传输,PDCP层的重排序窗口主要用于handover时保证数据的可靠传输,这里暂且 ...
- mac电脑终端使用scp上传/下载文件/文件夹
1.从服务器下载文件到本地电脑 1 scp -r remote_username@remote_ip:remote_folder local_folder 例如: 1 scp -r root@106. ...
- APP倒闭:你充值的钱会蒸发吗?
有一句说到吐,但却又不得不说的话:资本大潮退去,才知道谁在裸泳.随着资本寒冬的来临,互联网上众多看起来狂飙突进的项目却呈现迅速萎靡态势.尤其是众多具有互联网元素的油卡.洗衣.保洁等成为重灾区,其中不少 ...
- Flutter Widgets 之 RichText
注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 基础用法 应用程序离不开文字的展示,因此文字的排版非常重要 ...
- 【视频+图文】带你快速掌握Java中含break语句的双重for循环
双重for循环掌握后,我们就一起来看看双重for循环的进阶内容一之带break语句的双重for循环. 双重for循环[视频+图文]讲解传输门:点击这里可去小乔的哔哩哔哩观看~ 带continue语句的 ...
- java反序列化-ysoserial-调试分析总结篇(6)
前言: 这篇记录CommonsCollections6的调试,外层也是新的类,换成了hashset,即从hashset触发其readObject(),yso给的调用链如下图所示 利用链分析: 首先在h ...
- Mac使用brew安装MongoDB
之前一直使用以下命令安装MongoDB,但是一直安装不上 brew install mongodb 后来看了官网,安装方法如下 brew tap mongodb/brew //这步不知道需不需要 br ...
- 1024程序员节最新福利之2018最全H5前端资料集
前言 有好久没有写博客了,主要这段时间都沉迷学习无法自拔了,哈哈.自吹一波. 前两天不是1024节吗,所以就有很多福利出现了,当然每个人能都获得的信息都有所不同,这就是所谓的信息差.秉着好东西需要分享 ...
- fsLayuiPlugin数据字典使用
概述 数据字典主要解决下拉框数据填充和数据表格转义处理,一个数据字典可以多处使用. 1.多个页面下拉框使用同样的数据,改一个地方需要把所有页面都要修改 2.数据表格转义代替自己手动写templet解析 ...
- CSS+JS相应式导航菜单
响应式导航菜单 响应式导航菜单就是当网页在其他不同视口的样式,不同的设备需要不同的样式 需要掌握的知识 - 掌握媒体查询,如果你不是很懂那就看我写的CSS响应式布局 掌握CSS重的display:no ...