hdu3336 kmp】的更多相关文章

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&qu…
题意:求字符串s的所有前缀出现次数之和. http://www.cnblogs.com/jklongint/p/4446117.html 思路:用kmp做,简单且效率高.以前缀结尾的位置分类,令dp[i]为以结尾位置在i的前缀数量,那么dp[i] = cnt(j)(j~i是前缀),而由kmp的next函数的转移性质,可得如下递推方程:dp[i] = dp[next[i]] + 1,把这个递推式不断展开,也就是i = next[i]不断迭代,那么+1的个数就是dp[i] = cnt(j)(j~i是…
题意是问所有前缀出现的次数和,mod10007: 想一想next数组代表什么意思,是从当前失配位置走到上一个匹配位置的后面,next[i]的值说明以当前位置为结尾,长度为next[i]的后缀,与以开头元素为起始,长度为next[i] 的前缀是相同的,那么方法就很容易了,对于每个j = i,沿着next[j]往前走,到0为止,记录走过的次数,就是前缀的出现次数. #include<cstdio> #include<algorithm> #include<cstring>…
Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9588    Accepted Submission(s): 4480 Problem Description It is well known that AekdyCoin is good at string problems as well as nu…
题意:       给你一个串,问你他的所有前缀子串在本串中的出现次数,注释:abc的前缀子串是 a ab abc; 思路:      还是利用了next数组,先对子串求出next数组,再开一个数组dp,初始化全是1,因为每个以当前i结尾的都至少是1,然后从后往前更新,把以i结尾的加到以next[i]结尾的上, 运用的next数组的特点相当于 123123123 最后一个3加到倒数第二个3,倒数第二个3再加到第一个3 那么以3结尾的(123)出现了三次,以3结尾的(123123)出现了两次,以3…
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - HDU3336 题意概括 给T组数据,每组数据给一个长度为n的字符串s.求字符串每个前缀出现的次数和,结果mod 10007. 题解 首先闭着眼睛KMP跑一跑. 然后我们来dp. dp[i]表示以第i位结尾的前缀个数. 那么,根据Next的含义,不难写出dp[i]=dp[Next[i]]+1的转移方程式. 然后就OK了. 代码 #include <cstring> #include <algorit…
题意 计算所有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[…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么dp[i]=dp[next[i]]+1; 代码: #include<stdio.h> #include<string.h> ; ; int dp[MAXN]; char str[MAXN]; int next[MAXN]; void getNext(char *p) { int j,k…
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"…
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&qu…