求数组的所有排列组合数
来源:互联网 发布:胡为乎遑遑欲何之的乎 编辑:程序博客网 时间:2024/05/21 12:46
Problem Description
How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, …., sn} ? For example, we assume that S = {1, 2, 3}, and you can find seven nondecreasing subsequences, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}.
Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n that is the length of the sequence S, the next line contains n integers {s1, s2, s3, …., sn}, 1 <= n <= 100000, 0 <= si <= 2^31.
Output
For each test case, output one line containing the number of nondecreasing subsequences you can find from the sequence S, the answer should % 1000000007.
Sample Input
3
1 2 3
Sample Output
7
代码:
static List<String> list = new ArrayList<String>(); /** * 求a中count个数的组合 * @param a 存放组合的数 * @param restCount 当前还需多少个数构成一个组合 * @param count 在a中挑选出count个数 * @param b 存放当前组合 * @param pos 指向a中的元素, */ public static void combination(int []a,int restCount,int count,int[] b,int pos){ if(restCount <= 0 || count > a.length){ return; } for(int i = pos;i >= restCount - 1;i-- ){ StringBuilder sb = new StringBuilder(); b[restCount - 1] = a[i]; if(restCount - 1 == 0){ for(int j:b){ sb.append(j); } if (!list.contains(sb.toString())) { list.add(sb.toString()); } } combination(a, restCount-1, count, b, i-1); } } public static void main(String[] args) { int n ; Scanner in = new Scanner(System.in); n = in.nextInt(); int[] s = new int [n]; for (int j = 0; j < s.length; j++) { s[j] = in.nextInt(); } for (int i = 0; i < s.length; i++) { int[] b = new int[i+1]; combination(s, i+1, i+1, b, s.length - 1); } System.out.println(list.size()); }
- 求数组的所有排列组合数
- 求一个数组元素的所有排列组合
- 【求一个数组元素的所有排列组合】
- 求1~9的的所有排列组合
- 求几个数的全排列组合
- C#求排列组合数
- java求排列组合数
- 求数组中和为固定数的所有对
- 求数组中和为给定数所有的组合
- 求一数组前k大的所有数
- 求所有的水仙花数
- 求所有的水仙花数
- 排列组合(求排列数)
- 排列组合(求排列数)
- [IOS 开发] 穷举数组字符的所有排列组合的可能性
- 输出数组的所有元素的排列组合(递归)
- iOS 借助排列组合求赛事投注的注数问题。
- 求数组中区间中最小数*区间所有数和的最大值
- 黄有龙才是真戏精!法律武器捍卫赵薇,鱼肉股民却否认违法事实
- electron-packager 打包过程
- 为tornado自定义session
- Perl 获取当前目录下所有文件名
- Dataset的用法
- 求数组的所有排列组合数
- linux命令-mount
- 桥接模式-Bridge Pattern
- QT QMap介绍与使用
- CUICatalog: Invalid asset name supplied: '(null)'
- 滚动到对应的高度与导航栏吸顶
- fftshift详解
- 编程工具
- Java中HashSet集合如何控制 元素唯一性 总结