Cutting balloon string (360 companies recruit Zhenti Spring 2017

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m1f2c3/article/details/99644174

Here Insert Picture Description

Do not forget about it overflow data

import java.util.*;

public class Main {
    public static void main(String args[]) {
        int mod = 1000000007;
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for(int i = 0; i < n; ++i) a[i] = sc.nextInt();

        long[] dp = new long[n];
        int[] color = new int[10];
        dp[0] = 1;
        ++color[a[0]];
        for(int i = 1; i < n; ++i){
            if(color[a[i]] != 0){
                int[] temp_c = new int[10];
                ++temp_c[a[i]];
                int temp_i = i - 1;
                dp[i] += dp[i - 1];
                while(temp_c[a[temp_i]] == 0){
                    dp[i] += dp[temp_i - 1];
                    ++temp_c[a[temp_i]];
                    --temp_i;
                }
            }else{
                dp[i] = dp[i - 1] * 2;
                ++color[a[i]];
            }
            dp[i] %= mod;
        }
        System.out.println(dp[n - 1]);
    }
}

Guess you like

Origin blog.csdn.net/m1f2c3/article/details/99644174