Reversi

题目描述:

There are N stones arranged in a row. The i-th stone from the left is painted in the color Ci.
Snuke will perform the following operation zero or more times:
Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones.
Find the number of possible final sequences of colors of the stones, modulo 109+7.

Constraints
·1≤N≤2×105
·1≤Ci≤2×105(1≤i≤N)
·All values in input are integers.

输入

Input is given from Standard Input in the following format:

N
C1
:
CN

输出

Print the number of possible final sequences of colors of the stones, modulo 109+7.

样例输入

5
1
2
1
2
2

样例输出

3

提示

We can make three sequences of colors of stones, as follows:
·(1,2,1,2,2), by doing nothing.
·(1,1,1,2,2), by choosing the first and third stones to perform the operation.
·(1,2,2,2,2), by choosing the second and fourth stones to perform the operation.

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=200200;
const ll p=1e9+7;
 
ll a[N],n,b[N],f[N];
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  f[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] == a[i - 1]) {
      f[i] = f[i - 1];
    } else {
      f[i] = (f[i - 1] + b[a[i]]) % p;
      b[a[i]] = f[i];
    }
  }
  printf("%lld
", f[n]);
}

  

原文地址:https://www.cnblogs.com/Accpted/p/11188628.html