子序列求解(简单的计数DP)

子序列的定义:对于一个序列a=a[1],a[2],……a[n]。
则非空序列a’=a[p1],a[p2]……a[pm]为a的一个子序列,其中1<=p1

#include
#include
#include
#include
#include
using namespace std;
#define maxsize 100010
#define mod 1000000007
#define ll long long int 
ll a[maxsize];
ll dp[maxsize];
int mark[maxsize];

int main()
{
    int n;
    while (~scanf("%d", &n) != EOF)
    {
        for (int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);

        }
        memset(dp, 0, sizeof(dp));
        memset(mark, 0, sizeof(mark));
        dp[0] = 1;
        for (int i = 1; i <= n; i++)
        {
            if (mark[a[i]] == 0)
                dp[i] = (dp[i - 1] * 2) % mod;
            else
                dp[i] = (2 * dp[i - 1] - dp[mark[a[i]] - 1] + mod) % mod;
            mark[a[i]] = i;
        }
        printf("%lld\n", dp[n] - 1);
    }
    return 0;
}

你可能感兴趣的:(子序列求解(简单的计数DP))