It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example: 
s: "abab" 
The prefixes are: "a", "ab", "aba", "abab" 
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6. 
The answer may be very large, so output the answer mod 10007. 

InputThe first line is a single integer T, indicating the number of test cases. 
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters. 
OutputFor each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.Sample Input

1
4
abab

Sample Output

6
题意:找每个前缀在字符串中的出现次数
题解:kmp的next数组处理,刚开始从后往前遍历,每个next数值往前递归加上次数,最后就是结果,交了之后发现tle(没有发现最差的时间复杂度居然有O(n*n)),于是乎只能改进一下,用num数组来存次数,每次递归的时候就把当前num++,这样只需要遍历一遍就行了
时间复杂度变成了O(n)果然不会Tle了
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 10007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; ll num[N];
int Next[N],slen;
string str; void getnext()
{
int k=-;
Next[]=-;
for(int i=;i<slen;i++)
{
while(k>-&&str[k+]!=str[i])k=Next[k];
if(str[k+]==str[i])k++;
Next[i]=k;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
int t,n;
cin>>t;
while(t--){
cin>>n>>str;
slen=str.size();
getnext();
ll ans=;
memset(num,,sizeof num);
for(int i=slen-;i>=;i--)
{
ll j=i;
while(j!=-)num[j]++,j=Next[j];
}
for(int i=;i<slen;i++)ans=ans%mod+num[i]%mod;
cout<<ans<<endl;
}
return ;
}

hdu3336 kmp的更多相关文章

  1. [hdu3336]kmp(后缀数组)

    题意:求字符串s的所有前缀出现次数之和. http://www.cnblogs.com/jklongint/p/4446117.html 思路:用kmp做,简单且效率高.以前缀结尾的位置分类,令dp[ ...

  2. HDU3336——KMP算法

    题意是问所有前缀出现的次数和,mod10007: 想一想next数组代表什么意思,是从当前失配位置走到上一个匹配位置的后面,next[i]的值说明以当前位置为结尾,长度为next[i]的后缀,与以开头 ...

  3. HDU3336 KMP+DP

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. hdu3336 KMP + DP 前缀数组出现的次数

    题意:       给你一个串,问你他的所有前缀子串在本串中的出现次数,注释:abc的前缀子串是 a ab abc; 思路:      还是利用了next数组,先对子串求出next数组,再开一个数组d ...

  5. HDU3336 Count the string KMP 动态规划

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mo ...

  6. [KMP][HDU3336][Count the string]

    题意 计算所有S的前缀在S中出现了几次 思路 跟前缀有关的题目可以多多考虑KMP的NEXT数组 #include <cstdio> #include <cstring> #in ...

  7. hdu3336 Count the string kmp+dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...

  8. hdu3336 Count the string 扩展KMP

    It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...

  9. 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 ...

随机推荐

  1. WP8.1小梦词典开发2:百度翻译API使用

    原文出自:http://www.bcmeng.com/api2/ 小梦昨天和大家分享了WP8.1金山词霸API使用方法,今天继续分享windows phone 8.1中百度翻译API的使用方法.和昨天 ...

  2. android中在java代码中设置Button按钮的背景颜色

    android中在java代码中设置Button按钮的背景颜色 1.设置背景图片,图片来源于drawable: flightInfoPanel.setBackgroundDrawable(getRes ...

  3. maridb安装审计audit插件

    1.下载插件 https://mariadb.com/kb/en/mariadb-audit- plugin/ 比较新的mariadb版本audit插件直接内嵌在版本里,可以直接安装 INSTALL ...

  4. css3特效

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

  5. java实现八种排序算法并测试速度(详细)

    算法代码: /** * Created by CLY on 2017/3/17. */ package pers.cly.sorting; /** * 排序工具类,里面包含各种排序方法 */ publ ...

  6. IOS简单画板实现

    先上效果图 设计要求 1.画笔能设置大小.颜色 2.有清屏.撤销.橡皮擦.导入照片功能 3.能将绘好的画面保存到相册 实现思路 1.画笔的实现,我们可以通过监听用户的 平移手势 中创建 UIBezie ...

  7. SQL Server 获取本周,本月,本年等时间内记录

    datediff(week,zy_time,getdate())=0 //查询本周 datediff(month,zy_time,getdate())=0 //查询本月 本季:select * fro ...

  8. Oracle ASM数据库故障数据恢复过程

    一.故障描述 ASM磁盘组掉线 ,ASM实例不能mount.ASM磁盘组有4个500G的磁盘组成,数据库和ASM软件为10.2.0.1,急需恢复oracle数据库.二.故障分析   分析组成ASM磁盘 ...

  9. Zookeeper3.4.9分布式集群安装

    一.依赖文件安装 1.1 JDK 参见博文:http://www.cnblogs.com/liugh/p/6623530.html 二.文件准备 2.1 文件名称 zookeeper-3.4.9.ta ...

  10. Netd学习笔记

    service netd /system/bin/netd     class main     socket netd stream 0660 root system     socket dnsp ...