剪气球串(360公司2017春招真题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/m1f2c3/article/details/99644174

在这里插入图片描述

千万不要忘记数据溢出这件事

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]);
    }
}

猜你喜欢

转载自blog.csdn.net/m1f2c3/article/details/99644174