【HDOJ】4455 Substrings
5000ms的时限,还挺长的。算法是DP。
思路是找到ans[1..n]的结果,然后Query就容易做了。问题是怎么DP?
考虑:
1 1 2 3 4 4 5w=1: 7, 7 = 1 * 7
w=2: 10,10 = |{1,1}|+|{1,2}|+|{2,3}|+|{3,4}|+|{4,4}|+|{4,5}|=1+2+2+2+1+2=10
w=3: 12, 12 = |{1,1, 2}|+|{1,2, 3}|+|{2,3, 4}|+|{3,4, 5}|+|{4,4, 5}|
...
观察w=3如何在w=2的基础上求得,首先需要减去|{4,5}|,然后考虑2,3,4,5,5(每个集合的最后一个数)是否distinct。
因此,O(n^2)是可解得。不过n的范围是1e6,肯定超时。
那么,简化思路,分成三个部分。
1. ans[n-1];
2. 后k([1..n])个数中,distinct的数目可以在O(n)内求得。
3. 而考虑a[w..n]是否对数量增加1,可以换个思路考虑。
考虑1 1 2 3 4 4 5中的3,因为前面没有出现3,因此w=[1..4]时,这个3都会提供增量1。
考虑1 3 2 3* 4 4 5中的3*,因为前面有个3,两者间距2,因此w=[1..2]时,这个3会提供增量1。
可见弄个delta数组,两个O(n)循环可以确定针对w=[1..n]时,这个增量delta是多少。
最后合成一下就可以了。
/* 4455 */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 1e6+;
int a[maxn], b[maxn];
int pre[maxn];
__int64 delta[maxn], ans[maxn];
bool mark[maxn]; int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int n, q, x;
int l, tmp;
int i, j, k; while (scanf("%d", &n)!=EOF && n) {
for (i=; i<=n; ++i) {
scanf("%d", &a[i]);
++a[i];
}
memset(delta, , sizeof(delta));
memset(pre, , sizeof(pre));
memset(mark, , sizeof(mark)); // handle last i number
for (i=n,j=; i>; --i,++j) {
if (mark[a[i]]) {
b[j] = b[j-];
} else {
b[j] = b[j-]+;
mark[a[i]] = true;
}
}
// handle pre
for (i=; i<=n; ++i) {
l = i - pre[a[i]];
// [1, l]++
delta[]++;
delta[l+]--;
pre[a[i]] = i;
}
ans[] = n;
for (i=; i<=n; ++i) {
delta[i] += delta[i-];
ans[i] = ans[i-] + delta[i] - b[i-];
} scanf("%d", &q);
while (q--) {
scanf("%d", &x);
printf("%I64d\n", ans[x]);
}
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【HDOJ】4455 Substrings的更多相关文章
- 【HDOJ】1238 Substrings
深搜+剪枝,简单字符串. #include <stdio.h> #include <string.h> #define MAXLEN 105 #define MAXNUM 10 ...
- 【SPOJ】Distinct Substrings(后缀自动机)
[SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...
- 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)
[SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...
- 【CF316G3】Good Substrings 后缀自动机
[CF316G3]Good Substrings 题意:给出n个限制(p,l,r),我们称一个字符串满足一个限制当且仅当这个字符串在p中的出现次数在[l,r]之间.现在想问你S的所有本质不同的子串中, ...
- 【SPOJ】Distinct Substrings
[SPOJ]Distinct Substrings 求不同子串数量 统计每个点有效的字符串数量(第一次出现的) \(\sum\limits_{now=1}^{nod}now.longest-paren ...
- 【UVA10829】 L-Gap Substrings (后缀数组)
Description If a string is in the form UVU, where U is not empty, and V has exactly L characters, we ...
- 【POJ3415】 Common Substrings(后缀数组|SAM)
Common Substrings Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤i≤ ...
- 【spoj705】 Distinct Substrings
[题目描述] 给定一个字符串,计算其不同的子串个数. [输入格式] 一行一个仅包含大写字母的字符串,长度<=50000 [输出格式] 一行一个正整数,即不同的子串个数. [样例输入] ABABA ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
随机推荐
- 用dubbo+zookeeper+spring搭建一个简单的http接口程序
dubbo是一个分布式服务框架,是阿里巴巴开发的一个解决RPC远程调用优化的核心框架,包含负载均衡算法,能提高分布式系统的性能. zookeeper是hadoop的一个子项目,主要用来解决分布式系统的 ...
- CocoaPods的使用详解
CocoaPods是什么 当我们开发 iOS 项目时候,会经常使用到第三方类库,并且会使用很多.大家的做法基本上都是到 GitHub 上下载一个一个的类库,然后导入到工程中,并且引入各种的类库,做各种 ...
- ios错误修改了系统头文件
一.打开终端 二.进入Xcode 输入命令: cd /Users/apple/Library/Developer/Xcode/ 三.打开当前 输入命令: open . 四.将DerivedData ...
- HDU 4422 The Little Girl who Picks Mushrooms(简单题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4422 题目大意:小姑娘背着5个包去山上采蘑菇,每座山上只能用一个背包采集.三个小精灵会要她3个背包,其 ...
- (poj)3159 Candies
题目链接:http://poj.org/problem?id=3159 Description During the kindergarten days, flymouse was the monit ...
- leetcode problem 33 -- Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- SQL Server2008数据库导入导出兼容性处理
使用场景:SQL Server 的高版本数据库恢复到低版本则可能会有兼容性问题,为了解决此类问题进行数据库脚本模式处理,数据库结构,及数据存储更换版本等. 1. 选择要导出的数据库,右键任务,生成脚 ...
- Mac 平台下安装 OpenVC
opencv for Mac I 背景 Mac(Ios 9.0), 时间: 2015年11月. 本人也是小白,在网上爬了几天才安装好. II 安装OpenCV for Mac 首先下载op ...
- PHP LINUX Notice: undefined $_GET完美解决方法
PHP Notice: undefined 平时用$_GET[‘xx’] 取得参数值时,如果之前不加判断在未传进参数时会出现这样的警告: PHP Notice: undefined index xxx ...
- VB6-系统打印常识
在一次做图片打印的时候,对位置的调整老是不得法,后来通过CBM666老师的帮助才解决问题,分享以下他给的帮助. , , picA.Width , picA.Height Printer.End ...