1202 number of sub-sequences

Source: Internet
Author: User

1202 sub-sequence number base time limit: 1 second space limit: 131072 KB sub-sequence definition: For a sequence a=a[1],a[2],...... a[n]. The non-empty sequence a ' =a[p1],a[p2]......a[pm] is a sub-sequence of a, where 1<=p1<p2<.....<pm<=n. For example, 4,14,2,3 and 14,1,2,3 are all sub-sequences of 4,13,14,1,2,3. For the given sequence a, some sub-sequences may be the same, only 1 are counted here, please output a number of different sub-sequences. Because the answer is large, output mod 10^9 + 7 results. Input
Line 1th: A number N, indicating the length of the sequence (1 <= N <= 100000) 2-n + 1 lines: Elements in the sequence (1 <= a[i] <= 100000)
Output
The number of different sub-sequences of output a mod 10^9 + 7.
Input example
41232
Output example
13
Idea: Dp.dp[i] represents the first I can be composed of a number of different sub-sequences, then consider whether the current value in the previous occurrence, did not appear is dp[i]=2*dp[i-1]+1, appear for 2*dp[i-1]-Dp[id[ans[i]]-1] The last bit is the same as the previous one and the current value.
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <stdlib.h>5#include <queue>6#include <string.h>7#include <Set>8#include <map>9#include <math.h>Ten using namespacestd; One intans[1000005]; A Const intMoD = 1e9+7; -typedefLong LongLL; -LL dp[1000005]; the intid[1000005]; -map<int,int>my; - intMainvoid) - { +         intN; -          while(SCANF ("%d", &n)! =EOF) +         { A                  for(inti =1; I <= N; i++) at                 { -scanf"%d",&ans[i]); -                 } -Memset (DP,0,sizeof(DP)); -memset (ID,0,sizeof(ID)); -                  for(inti =1; I <= N; i++) in                 { -                         if(!Id[ans[i]]) to                         { +Dp[i] =2*dp[i-1]%mod +1; -Dp[i]%=MoD; theId[ans[i]] =i; *                         } $                         ElsePanax Notoginseng                         { -Dp[i] = (2*dp[i-1]%mod-dp[id[ans[i]]-1])%mod+MoD; thedp[i]%=MoD; +Id[ans[i]] =i; A                         } the                 } +printf"%lld\n", Dp[n]); -         } $         return 0; $}

1202 number of sub-sequences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.