【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]二分得到 ...
随机推荐
- ### About Multi-Object Tracking
点击查看Evernote原文. #@author: gr #@date: 2014-10-17 #@email: forgerui@gmail.com Multi_Object Tracking Fr ...
- CocoaLumberjack+XcodeColor(输出带有颜色的日志)在安装过程中遇到的问题
在安装的时候遇到了各种坑,(在这里用到的pch文件的使用以及解决无法引入的问题,可以参考上午的文章) 一(XcodeColor的安装).在github上下载XcodeClolor的插件,并且安装,Xc ...
- java集合 collection-list-LinkedList 模拟一个堆栈或者队列数据结构。
/* 使用LinkedList模拟一个堆栈或者队列数据结构. 堆栈:先进后出 如同一个杯子. 队列:先进先出 First in First out FIFO 如同一个水管. */ import jav ...
- CUDA_矢量相加
#include<iostream> #define N 10 _ _global_ _ void add(*a,*b,*c) { int tid=blockIdx.x; if(tid&l ...
- LIS
五:LIS 概念 最长上升子序列(Longest Increasing Subsequence,LIS),在计算机科学上是指一个序列中最长的单调递增的子序列.比如一个序列31 2 6 3 8,他的最长 ...
- Sharepoint 2010 Workflow 发布
1.首先需要有一个已经创建好的WorkFlow 2.然后在Sharepoint中打开这个WorkFlow,点击Save as Template,系统会自动将这个Workflow保存在Site Asse ...
- .NET高端职位招聘要求
系统架构师: 1.硕士及以上学历,博士有项目成果者优先: 2.五年以上工作经验,三年以上互联网经验,一年以上大型软件项目总体设计.分析.架构经验,有移动互联网或云计算虚拟化系统设计开发经验者优先: 3 ...
- .NET研发人员面试题(一)
1.简述javascript中的“=.==.===”的区别? =赋值 ==比较是否一般相等 "3"==3 //会做类型的隐式转换,true ===比较是否严格相等 " ...
- php结合jquery异步上传图片(ajaxSubmit)
php结合jquery异步上传图片(ajaxSubmit),以下为提交页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi ...
- ZeroMQ/jzmq安装使用
环境: No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 12.04.2 LTS Release: 12 ...