[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 ...
随机推荐
- java.util.concurrent.ExecutionException
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start com ...
- ORACLE数据库常用查询二
ORACLE数据库常用查询 1.查看表空间对应数据文件情况: SQL MB,AUTOEXTENSIBLE FROM DBA_DATA_FILES; TABLESPACE_NAME FILE_NAME ...
- mycat源码分析
http://www.cnblogs.com/fernandolee24/p/5196367.html
- EffectiveC#2--为你的常量选择readonly而不是const
1.对于常量,C#里有两个不同的版本: 编译时常量--效率相比更高些,但可维护性不好,保留的目的是为了性能.const关键字申明 public const int _Millennium = 2000 ...
- 如何利用 Bootstrap 进行快速 Web 开发
原文出处: IBM developerworks 了解如何使用 Bootstrap 快速开发网站和 Web 应用程序(包括移动友好型应用程序).Bootstrap 以 LESS 项目为基础,由 Twi ...
- 在asp.net中导出表格Excel数据
第一步:需要引用org.in2bits.MyXls程序集到使用页面 第二步:前台代码 <asp:Button ID="LeadingOut" runat="serv ...
- javascript父窗口与子窗口通信
在父窗口的js代码 //回调的函数 function alt(temp){ alert(temp); }; $("#Btn").click(function (obj) { //声 ...
- POJ 2289 Jamie's Contact Groups (二分+最大流)
题目大意: 有n个人,可以分成m个组,现在给出你每个人可以去的组的编号,求分成的m组中人数最多的组最少可以有多少人. 算法讨论: 首先喷一下这题的输入,太恶心了. 然后说算法:最多的最少,二分的字眼. ...
- POJ 1723 SOLDIERS (中位数)
题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...
- JSON的parse()方法
JSON方法也可以接受另外的一个参数,作为还原函数. 实例: var book = { title:"JavaScript Learn", author:["wang&q ...