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 ...
随机推荐
- SparkStreaming和Drools结合的HelloWord版
关于sparkStreaming的测试Drools框架结合版 package com.dinpay.bdp.rcp.service; import java.math.BigDecimal; impo ...
- 【Python】Python中in与not in
在python中,要判断特定的值是否存在列表中,可使用关键字in,判断特定的值不存在列表中,可使用关键字not in letters = ['A','B','C','D','E','F','G'] i ...
- HTTP头解读
Http协议定义了很多与服务器交互的方法,最基本的有4种,分别是GET.POST.PUT.DELETE.一个URL地址用于描述一个网络上的资源, 而HTTP中的GET.POST.PUT. DELETE ...
- client交互技术简单介绍
随着网络应用的不断丰富,client交互技术也如雨后春笋一般,遍地开花. 正是这些技术的支持,我们的互联网世界变得更加丰富多彩.一个浏览器上.不用说是简单的动画效果,就是一个Office应用也能顺畅的 ...
- 把握linux内核设计思想(二):硬中断及中断处理
[版权声明:尊重原创.转载请保留出处:blog.csdn.net/shallnet,文章仅供学习交流,请勿用于商业用途] 操作系统负责管理硬件设备.为了使系统和硬件设备的协同工作不减少机器性能.系统和 ...
- HTML5之Canvas绘图(一) ——基础篇
HTML5火的正热,最近有个想法也是要用到HTML的相关功能,所以也要好好学习一把. 好好看了一下Canvas的功能,感觉HTML5在客户端交互的功能性越来越强了,今天看了一下Canvas绘图,下边是 ...
- 使用java+TestNG进行接口回归测试
TestNG是一个开源自动化测试框架,“NG”表示下一代(Next Generation的首字母). TestNG类似于JUnit(特别是JUnit 4),但它不是JUnit框架的扩展,相较于Juni ...
- Google论文BigTable拜读
这周少打点dota2,争取把这篇论文读懂并呈现出来,和大家一起分享. 先把论文搞懂,然后再看下和论文搭界的知识,比如hbase,Chubby和Paxos算法. Bigtable: A Distribu ...
- 【Python基础】之异常
一.常见异常 try: open('abc.txt','r') except FileNotFoundError: print('异常啦!') 输出结果: ======= 异常啦! 我们通过 open ...
- 目标检测之vibe---ViBe(Visual Background extractor)背景建模或前景检测
ViBe算法:ViBe - a powerful technique for background detection and subtraction in video sequences 算法官网: ...