求数组的所有排列组合数
2017-12-06 16:07
323 查看
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
代码:
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 ; 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()); }
相关文章推荐
- 排列组合-打印出一个数组的元素的所有排列方式-算法
- 求一个长度为n的数组中长度为m的所有排列组合。
- 形如数组1223打印所有不同的组合排列java
- [leetcode 46] Permutations------数组中元素的所有排列组合集合
- 输出数组的所有元素的排列组合(递归)
- php数组全排列,元素所有组合
- php求数组全排列,元素所有组合的方法
- 每日一省之————递归法计算数组的所有排列组合
- 【C++】求数组中所有元素的排列或组合
- 列出多个数组所有排列组合
- N个数组中所有元素的排列组合(笛卡尔积)算法
- javascript数组全排列,数组元素所有组合
- N个数组中所有元素的排列组合(笛卡尔积)算法
- php求数组全排列,元素所有组合的方法总结
- php求数组全排列,元素所有组合的方法
- iOS 从allCount个元素中取出myCount个元素的所有排列组合
- 从数组中取出n个元素的所有组合(递归实现)
- 生成某字符串的所有排列组合
- 多个数组间元素排列组合问题求解(Java实现)
- 给定一个正整数数组,找所有元素组合起来最大的值(答案)