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. [Linux] PHP程序员玩转Linux系列-自动备份与SVN

    我的代码经常在开发修改,为了代码的安全性,比如哪天误删了文件,或者哪天改错东西了,可以恢复回来,我要搞代码备份.备份代码,我先做最简单的,使用linux的定时机制加shell命令打包文件,每天按日期保 ...

  2. [译]Selenium Python文档:八、附录:FAQ常见问题

    另外一个FAQ:https://github.com/SeleniumHQ/selenium/wiki/Frequently-Asked-Questions 8.1.怎样使用ChromeDriver ...

  3. JavaScript贪食蛇游戏制作详解

    之前闲时开发过一个简单的网页版贪食蛇游戏程序,现在把程序的实现思路写下来,供有兴趣同学参考阅读. 代码的实现比较简单,整个程序由三个类,一组常量和一些游戏逻辑以外的初始化和控制代码组成,总共400多行 ...

  4. C#中ListView易错的方法

    现在有一个ListView(lv1),有2列. ListViewItem lvi = new ListViewItem(); lvi.Text = "语文"; lvi.SubIte ...

  5. mybatis generator 插件安装及使用

    现在Mybatis特别火,但是在开发中却要经常写实体类和配置文件,会不会特别烦人,所以可以利用Mybatis的代码生成插件来生成这部分代码: 1,打开eclipse,点击Help>Softwar ...

  6. JS——操作内容、操作相关元素

    操作内容:普通元素.innerHTML = "值": 会把标记执行渲染普通元素.innerText = "值": 将值原封不动的展示出来,即使里面有标记 var ...

  7. PBXCp Error

    在项目开发中遇到了报PBXcp Error错误 然后我用快捷键清理了下项目中的缓存,直接错误警告消除 多次清理缓存,我编译时用的Xcode 8.1 问题是资源文件中的nib文件找不到,有时能找到 ,有 ...

  8. vue.js如何在标签属性中插入变量参数

    html的标签的属性,比如id.class.href需要动态传递参数,拼接字符串,查了一些资料,并没有找到合适的解决方法,琢磨了一上午,终于试出了方法: v-bind:属性=" '字符串'+ ...

  9. [编织消息框架][JAVA核心技术]动态代理应用4

    基础部份: 接下来讲编译JAVA时,生成自定义class 我们用 javax.annotation.processing.AbstractProcessor 来处理 public abstract c ...

  10. sublimeText3插件安装

    1,官方下载sublimeText 3(百度搜索) 2,安装成功后按Ctrl+`调出console 3,然后输入 import urllib.request,os; pf = 'Package Con ...