HDU3336(KMP + dp)
Count the string
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6875 Accepted Submission(s): 3191
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.
/*
ID: LinKArftc
PROG: 3336.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
const int MOD = ;
int dp[maxn];
char str[maxn];
int Next[maxn]; void getNext(char *p) {
int i = , j = -;
int len = strlen(p);
Next[] = -;
while (i < len) {
if (j == - || p[i] == p[j]) {
i ++; j ++;
Next[i] = j;
} else j = Next[j];
}
} int main() {
int T, n;
scanf("%d", &T);
while (T --) {
scanf("%d", &n);
scanf("%s", str);
getNext(str);
dp[] = ;
int ans = ;
for (int i = ; i <= n; i ++) {
dp[i] = (dp[Next[i]] + ) % MOD;
ans = (dp[i] + ans) % MOD;
}
printf("%d\n",ans);
}
return ;
}
HDU3336(KMP + dp)的更多相关文章
- 2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP)
2021.11.09 P3426 [POI2005]SZA-Template(KMP+DP) https://www.luogu.com.cn/problem/P3426 题意: 你打算在纸上印一串字 ...
- [HDOJ5763]Another Meaning(KMP, DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意:给定两个字符串a和b,其中a中的字符串如果含有子串b,那么那部分可以被替换成*.问有多少种 ...
- hdu_3336: Count the string(KMP dp)
题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...
- HDU5763 another meaning -(KMP+DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5763 思路:dp[i]表示前i个字符组成的字符串所表示的意思数量,则当匹配时dp[i]=dp[i-1] ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- HDU 5763 Another Meaning (kmp + dp)
Another Meaning 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 Description As is known to all, ...
- hdu3689(kmp+dp)
题意:问随机生成一个长度为m(m<=1000)长度的字符串,出现某个子串s的概率是多少. 解法:dp+kmp优化.ans[i][j]表示i长度,走到了s的j位置的概率,当然这是在i之前没有出现s ...
- 【HDU 3336】Count the string(KMP+DP)
Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...
- hdu 6068--Classic Quotation(kmp+DP)
题目链接 Problem Description When online chatting, we can save what somebody said to form his ''Classic ...
随机推荐
- python进制转换(二进制、十进制和十六进制)及注意事项
使用内置函数实现进制转换实现比较简单,主要用到以下函数: bin().oct().int().hex() 下面分别详解一下各个函数的使用(附实例) 第一部分:其他进制转十进制 1.二进制转十进制 使用 ...
- 微信小程序—setTimeOut定时器的坑
原文地址: http://fanjiajia.cn/2018/06/27/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E2%80%94setTimeOu ...
- springmvc项目搭建五-postgresql+easyui的数据显示
上一篇虽然完成了页面的显示,但是是假数据,本篇添加了postgresql的数据库,将登陆的校验和数据的显示都通过数据库来完成. 我是在本地搭建了一个postgre的数据库,就先新建两张表吧,一个用于用 ...
- 【WebService】——契约优先
相关博客: [WebService]--入门实例 [WebService]--SOAP.WSDL和UDDI 前言: 我们先来看一个契约优先的开发实例,通过熟悉他的开发流程,最后再和代码优先的方式进行比 ...
- SQL select 和SQL where语句
一.SQL SELECT语句 用于从表中选取数据,结果被存储在一共结果表中(称为结果集) 1.语法: SELECT 列名称 FROM 表名称 以及: SELECT * FROM 表名称 注:SQ ...
- Oracle解决索引碎片功能
我们开始时向一个空的带索引的表中插入大量数据后,是不会产生碎片问题的,但是,数据库经过很长一段时间的增删改查后,难免会出现碎片问题,影响数据库的性能,Oracle对于这一问题有自己的解决方案. 下面介 ...
- Div+Css中transparent制作奥运五环
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [bzoj4860] [BeiJing2017]树的难题
Description 给你一棵 n 个点的无根树.树上的每条边具有颜色. 一共有 m 种颜色,编号为 1 到 m.第 i 种颜色的权值为 ci.对于一条树上的简单路径,路径上经过的所有边按顺序组成一 ...
- JavaScript十大经典排序算法
排序算法说明 (1)排序的定义:对一序列对象根据某个关键字进行排序: 输入:n个数:a1,a2,a3,…,an输出:n个数的排列:a1’,a2’,a3’,…,an’,使得a1’ 再讲的形象点就是排排坐 ...
- linux管理(二)---网络使用情况的监控
我们经常在监控服务器或者排查程序性能瓶颈时需要知道 网络带宽的使用情况,看看带宽是不是瓶颈. linux系统中监控网络的工具和命令很多. 但其实主要分2种,一种是实时监控带宽情况(速度如何),一种是 ...