CSU 1354Distinct Subsequences 求不相同子序列的和 dp
来源:互联网 发布:excel怎样做数据透视表 编辑:程序博客网 时间:2024/06/07 02:23
题目链接:点击打开链接
Description
Give a positive number, count the sum of the distinct subsequence of it, moreover, any subsequence should not contain leading zeroes except it is zero.
For example, if the number is 1022, the answer is 1 + 0 + 2 + 10 + 12 + 22 + 102 + 122 + 1022 = 1293.
Input
The first line has an integer T, means there are T test cases.
For each test case, there is only one line with a positive number, the number of the digits of it is in range [1, 105].
The size of the input file will not exceed 5MB.
Output
For each test case, print the desired answer in one line. Because the answer may be very large, you just need to print the remainder of it divided by 1000000007 instead.
Sample Input
3710221000000001
Sample Output
71293222222223
思路:
dp[i]表示以数字i结尾的子序列的和,num[i]表示以i结尾的子序列的个数
import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.Iterator;import java.util.LinkedList;import java.util.PriorityQueue;import java.util.Scanner;import java.util.TreeSet;import java.util.Queue;public class Main {static int mod = 1000000007;static int N = 100010;long[] dp = new long[10], num = new long[10];String s;void work() {int T = cin.nextInt();while(T-- > 0) {s = cin.next();int len = s.length();for(int i = 0; i < 10; i++)dp[i] = num[i] = 0;for(int i = 0; i < len; i++){int x = s.charAt(i)-'0';long sum = 0, n = 0;for(int j = 0; j < 10; j++){sum += dp[j];n += num[j];}if(x>0)n++;dp[x] = sum*10%mod + n*x%mod;num[x] = n%mod;}long ans = 0;for(int i = 0; i < 10; i++)ans = (ans + dp[i])%mod;out.println(ans);}}Main() {cin = new Scanner(System.in);out = new PrintWriter(System.out);}public static void main(String[] args) {Main e = new Main();e.work();out.close();}public Scanner cin;public static PrintWriter out;int max(int x, int y) {return x > y ? x : y;}int min(int x, int y) {return x < y ? x : y;}double max(double x, double y) {return x > y ? x : y;}double min(double x, double y) {return x < y ? x : y;}long max(long x, long y) {return x > y ? x : y;}long min(long x, long y) {return x < y ? x : y;}static double eps = 1e-8;int abs(int x) {return x > 0 ? x : -x;}double abs(double x) {return x > 0 ? x : -x;}long abs(long x) {return x > 0 ? x : -x;}boolean zero(double x) {return abs(x) < eps;}}
- CSU 1354Distinct Subsequences 求不相同子序列的和 dp
- CSU 1354 Distinct Subsequences 求不同子序列和 dp
- <LeetCode>115.Distinct Subsequences 求相同子序列数 Tag:DP, string
- Distinct Subsequences 不同的子序列个数
- LeetCode 115 Distinct Subsequences (子序列计数 dp)
- Distinct Subsequences 不同子序列
- UVa Problem 10069 Distinct Subsequences (不同的子序列)
- LeetCode: Distinct Subsequences(不同子序列的个数)
- LeetCode | Distinct Subsequences(不同的子序列)
- 118.Distinct Subsequences-不同的子序列(中等题)
- Leetcode 115 Distinct Subsequences(不同的子序列)
- UVA - 10069 Distinct Subsequences 不同子序列
- Spoj694(Distinct Substrings)求字符串不相同的子串个数(后缀数组)
- SPOJ - DISUBSTR Distinct Substrings(后缀数组求不相同的子串个数)
- 求一个数列(数列中的每个数不相同)的最长不下降子序列。
- leetCode 115.Distinct Subsequences(子序列距离) 解题思路和方法
- 字符串中某子序列的个数 Distinct Subsequences
- uva 10069 Distinct Subsequences 不同的序列
- 借谷歌推未来软件发展趋势
- ssize_t与size_t的前世今生
- CSU 1350To Add Which? 给序列增加最少的值使得相邻数差<=D 优先队列+贪心
- iOS 文件学习笔记
- 【已解决】windows下,安装python的chardet
- CSU 1354Distinct Subsequences 求不相同子序列的和 dp
- Best Practices for Speeding Up Your Web Site
- struts2.0升级到struts2.3.20
- <C/C++算法>九度OJ题目1201--1358解题练习(六)
- POJ2377 Bad Cowtractors【Kruskal】【求最大生成树】
- php 关于namespace的一下总结
- OneNet
- Web性能优化的十个技巧
- js去掉字符串前后空格