HDU4455 Substrings(DP)
Posted 松子茶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU4455 Substrings(DP)相关的知识,希望对你有一定的参考价值。
Substrings
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2918 Accepted Submission(s): 895
Problem Description
XXX has an array of length n. XXX wants to know that, for a given w, what is the sum of the distinct elements’ number in all substrings of length w. For example, the array is 1 1 2 3 4 4 5 When w = 3, there are five substrings of length 3. They are (1,1,2),(1,2,3),(2,3,4),(3,4,4),(4,4,5)
The distinct elements’ number of those five substrings are 2,3,3,2,2.
So the sum of the distinct elements’ number should be 2+3+3+2+2 = 12
Input
There are several test cases.Each test case starts with a positive integer n, the array length. The next line consists of n integers a 1,a 2…a n, representing the elements of the array.
Then there is a line with an integer Q, the number of queries. At last Q lines follow, each contains one integer w, the substring length of query. The input data ends with n = 0 For all cases, 0<w<=n<=10 6, 0<=Q<=10 4, 0<= a 1,a 2…a n <=10 6
Output
For each test case, your program should output exactly Q lines, the sum of the distinct number in all substrings of length w for each query.Sample Input
7 1 1 2 3 4 4 5 3 1 2 3 0
Sample Output
7 10 12
Source
2012 Asia Hangzhou Regional Contest#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
const int MAXN=1000010;
int a[MAXN];//1-n输入的数列
int f[MAXN];//f[i]表示a[i]在前面最近出现的位置,f[i]==0表示从左到右第一次出现
int s[MAXN];//s[i]表示 t-f[t]==i,1<=t<=n的t的个数,即离上一个相等元素的距离为i的个数
long long dp[MAXN];//需要输出的结果
int ss[MAXN];//ss[i]表示最后的i个数含有的不同元素的个数
int main()
int n;
int m;
while(scanf("%d",&n)==1 && n)
memset(f,0,sizeof(f));
memset(s,0,sizeof(s));
//顺着求s数组
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
s[i-f[a[i]]]++;
f[a[i]]=i;
memset(f,0,sizeof(f));//f数组标记在后面是否出现过
ss[1]=1;
f[a[n]]=1;
for(int i=2;i<=n;i++)
if(f[a[n-i+1]]==0)
f[a[n-i+1]]=1;
ss[i]=ss[i-1]+1;
else ss[i]=ss[i-1];
dp[1]=n;
int sum=n;
//从dp[i-1]扩展到dp[i]就是去掉最后一个区间的个数,把前面的区间长度增加1,
//加上相应增加的种类数
for(int i=2;i<=n;i++)
dp[i]=dp[i-1]-ss[i-1];//减掉最后一个区间的种类数
sum-=s[i-1];
dp[i]+=sum;//加上前面的区间增加一个长度后增加的种类数
scanf("%d",&m);
int t;
while(m--)
scanf("%d",&t);
printf("%I64d\\n",dp[t]);
return 0;
以上是关于HDU4455 Substrings(DP)的主要内容,如果未能解决你的问题,请参考以下文章