ACM训练——KMP+next数组
来源:互联网 发布:linux查看ntp服务器 编辑:程序博客网 时间:2024/06/08 07:19
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3336
题目要求计算一个字符串的所有前缀字符串在整个字符串中出现的次数。
采用next数组来计算最大前后缀字符串,累加next的值(注意:在累加时,只加下个位置不满足next递归规律的位置的next值)
当k,j位置所对应的字符不同时,k不需要退回初始位置,只需要退回next[k]位置。因为next[k]位置的值目前匹配的最大字符串。
#include <stdio.h>#include <iostream>using namespace std;int nex[200002];char s[200002];int n;void get_next(){int j=0,k=-1;nex[0]=-1;while(j<n){if(k==-1||s[k]==s[j]){k++;j++;nex[j]=k;}else{k=nex[k];}}}int main(){int t,i;scanf("%d",&t);while(t--){int sum=0;scanf("%d",&n);scanf("%s",s);get_next();for(i=0;i<=n;i++){if(nex[i]>0&&nex[i]+1!=nex[i+1])sum+=nex[i];}sum+=n;printf("%d\n",sum%10007);}return 0;}
0 0
- ACM训练——KMP+next数组
- KMP —— next数组
- kmp算法(下)——next数组
- kmp——next数组的应用---cout the string
- 字符串匹配——KMP算法中的next数组理解
- KMP算法——快速求解next数组
- poj2752—kmp的next数组的简单应用
- poj2406—KMP next数组的性质求最小周期
- KMP 算法 next数组
- KMP next数组详解
- KMP中的next数组
- KMP 模板 next数组
- KMP算法--next数组
- KMP next[]数组
- KMP与next数组
- KMP next数组
- KMP的next数组
- kmp, next 数组
- 一个线程的独白
- 将一个实体类赋值到另一个实体类
- asp.net调用带账号密码的webservice
- PHP预定义变量
- T-SQL top 1 in group
- ACM训练——KMP+next数组
- Activity生命周期的自测(对网上一些说法进行纠正)
- windows bat脚本常用命令
- glUniform
- ecshop+ectouch LANP伪静态
- LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
- 回溯法的基本框架
- mysql 直接拷贝data 目录下文件
- 系统集成项目管理工程师-项目整体管理