HDU3336 Count the string —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-3336
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11760 Accepted Submission(s): 5479
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.
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.
4
abab
题解:
求每个前缀在字符串中出现(可重叠)的次数之和。next数组的应用。
1.求出该字符串的next数组。
2.枚举每个位置,然后用next数组对其进行回退,且每回退一次,都有一个前缀出现了一次(next数组的特性)。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e5+; char x[MAXN], y[MAXN];
int Next[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d%s", &n, x);
get_next(x, n); int sum = ;
for(int i = ; i<=n; i++)
for(int j = i; j>=; j = Next[j])
sum = (sum+)%; printf("%d\n", sum);
}
}
51Nod 1277 字符串中的最大值
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1277
输入字符串S, (1 <= L <= 100000, L为字符串的长度),S中的所有字符均为小写英文字母。
输出所有前缀中字符长度与出现次数的乘积的最大值。
abababa
10
题解:
与上一题类似,只不过此题用next数组回退的方法会超时,因为假如字符串是“aaaaaaaaaa”,那么next每回退一次只会退一个,所以枚举每个位置然后next数组回退的方法,时间复杂度最坏可达O(n^2),所以容易超。故采用递推的方法,时间复杂度为O(n),详情请看代码。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e5+; char x[MAXN];
int Next[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} int sum[MAXN];
int main()
{
while(scanf("%s", x)!=EOF)
{
int n = strlen(x);
get_next(x, n); memset(sum, , sizeof(sum));
for(int i = n; i>=; i--)
{
sum[i]++;
sum[Next[i]] += sum[i];
}
LL ans = ;
for(int i = ; i<=n; i++)
ans = max(ans, 1LL*i*sum[i]);
printf("%lld\n", ans);
}
}
HDU3336 Count the string —— KMP next数组的更多相关文章
- 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 ...
- 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 ...
- HDUOJ------3336 Count the string(kmp)
D - Count the string Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 3336 Count the string(next数组运用)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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
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 ...
随机推荐
- 导出excel(利用工具类导出excel)
/** * 添加导出功能 * @param creditPageResult * @param request * @param response */ @RequestMapping(value = ...
- Mysql 性能监控及调优
死锁概念: 两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象 1.监控死锁(innotop): (1) 启用 innodb_status_file 在/etc/my.cnf添加如 ...
- jquery:选择器 过滤器
容易理解错误的地方: 1.假如我们想要让一个表格中第八列的所有单元格,都隐藏起来.我们可能会这么写$("table tr td:eq(8)").css("display& ...
- cookie-小总结吧
写入common.js文件,其他页面调用即可: //添加cookie值 function addcookie(name, value, days) { days = days || 0; var ex ...
- Allegro Desgin Compare的用法与网表比较
转:Allegro Desgin Compare的用法与网表比较 Allegro中自带有Design Compare工具,利用它可以比较明了的看到线路的差异.当然也可以通过SKILL进行比较,不过我们 ...
- Struts2学习七----------Struts2后缀
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2后缀 - Struts2默认后缀是action - Struts2使用默认后缀时*.action和*都是同一个请求 - Struts2自 ...
- HDU 2242 考研路茫茫——空调教室(边双连通)
HDU 2242 考研路茫茫--空调教室 题目链接 思路:求边双连通分量.然后进行缩点,点权为双连通分支的点权之和,缩点完变成一棵树,然后在树上dfs一遍就能得出答案 代码: #include < ...
- js中比較好的继承方式
前面说到了原型和原型链,今天就来说说在面向对象中比較好的继承方式吧.先来看看两种基础的继承方式: 一.构造函数型 function People(name) { this.name=name; } P ...
- 【EDAS问题】轻量级EDAS部署hsf服务出现找不到类的解决方案
本地运行轻量级EDAS调用服务的时候报错如下: 2018-01-08 13:16:58.029 WARN [http-bio-8090-exec-8:t.hsf] [RPC Protocol call ...
- mac上利用minikube搭建kubernetes(k8s)环境
友情提示:对于初次接触k8s的同学,强烈建议先看看本文最后的参考文章. 环境: mac os(Mojave) 前提:先安装好kubectl (brew install kubectl) .docker ...