首页 > 代码库 > CSU 1354 Distinct Subsequences 求不同子序列和 dp

CSU 1354 Distinct Subsequences 求不同子序列和 dp

题目链接:点击打开链接

题意:

给定一个长整数

求所有不同的子序列和。

思路:

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


CSU 1354 Distinct Subsequences 求不同子序列和 dp