[KMP][HDU3336][Count the string]
题意
计算所有S的前缀在S中出现了几次
思路
跟前缀有关的题目可以多多考虑KMP的NEXT数组
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
char S[2000000];
int NEXT[2000000];
int dp[2000000];//dp[i] 表示 以i结尾的子串与前缀相等的个数 d[i]=d[next[i]]+1;
//一开始还以为是 d[i]=next[i]+1; 在abab这个样例中 d[3]=3 显然有重复计算了
int len;
int sum=0;
void get_next()
{
for(int i=1;i<=len;i++)
{
int p=i-1;
while(S[i]!=S[NEXT[p]+1]&&p!=0) p=NEXT[p];
if(p!=0) NEXT[i]=NEXT[p]+1; //这种不else 的写法注意清空NEXT
//else NEXT[i]=0;
}
}
int main()
{
// freopen("a.in","r",stdin);
int T;
cin>>T;
while(T--)
{
sum=0;
cin>>len;
memset(NEXT,0,sizeof(NEXT));
scanf("%s",S+1);
get_next();
for(int i=1;i<=len;i++)
{
dp[i]=dp[NEXT[i]]+1;
sum=(sum+dp[i])%10007;
}
cout<<sum<<endl;
}
}
[KMP][HDU3336][Count the string]的更多相关文章
- 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 ...
- 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
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- 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 ...
- HDU3336 Count the string 题解 KMP算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 题目大意:找出字符串s中和s的前缀相同的所有子串的个数. 题目分析:KMP模板题.这道题考虑 n ...
- HDU3336 Count the string
居然一A了,说明对朴素的KMP还是有一定理解. 主要就是要知道next数组的作用,然后就可以计算每个i结尾的满足题意的串个数. #include<cstdio> #include<c ...
- 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 ...
随机推荐
- [Hapi.js] Using the response object
When you use reply method: let resp = reply('hello world') It actually return an response object. By ...
- Firebase远程更新应用
能打造出色的应用不意味着一定能在商业上取得成功,两者之间还有许多工作要做,绝不能简单发布应用后就宣告“收工”.您需要能迅速根据用户反馈作出调整.测试新功能,以及向用户提供他们最关注的内容. Fireb ...
- python-布尔值
布尔只有两个值:0,1 1或0 真或假 下面的值在作为布尔表达式的时候,会被解释器看作假(false) False None 0 "" () [] ...
- jquery selector
jquery的选择器功能 1 :lt(index) selector 一组元素选择index之前的元素,若index<0 则倒着选过来 http://api.jquery.com/lt-sele ...
- JavaScript ----------------- 寄生式继承
寄生式继承 寄生式继承是于原型式继承紧密相关的一种思路.寄生式基础的思路与寄生构造函数和工厂模式类似,既创建一个仅用于封装继承过程的函数,该函数内部以某种方式来增强对象,最后再像真地是它做了所有工作一 ...
- Android中使用shape来定义控件
本文章转接于:http://kofi1122.blog.51cto.com/2815761/521605 Android中常常使用shape来定义控件的一些显示属性,今天看了一些shape的使用,对s ...
- weblogic开机启动脚本
1.在/home/bea/startBeaAll目录内创建一个startBeaAll.sh文件,加入如下内容(把相应目录与命令修改即可,红字部分为修改地方): #!/bin/sh echo " ...
- 三维偏序-二维LIS
Another Longest Increasing Subsequence Problem 有两种思路. 思路一: 考虑到如果只有一维,那么可以用f[s]表示长度为s时,最后一个数是多少,把这个想法 ...
- Python学习笔记4(函数与模块)
1.Python程序的结构 Python的程序由包(package).模块(module)和函数组成. 模块是处理一类问题的集合,由函数和类组成. 包是由一系列模块组成的集合.包是一个完成特定任务的工 ...
- NOIP前模板整理
图 最短路径 #include <queue> #define N 1000 typedef long long ll; using namespace std; int d[N], w[ ...