标签:scan exti rem i+1 sys lower mil hang 部分
InputThe first line contains only one integer T (T<=50), which is the number of test cases. Each test case contains a string S, the length of S is not greater than 1000 and only contains lowercase letters.OutputFor each test case, output the case number first, then output the number of different subsequence of the given string, the answer should be module 10007.Sample Input
4
a
aaaaa
goodafternooneveryone
welcometoooxxourproblems
Sample Output
Case 1: 1
Case 2: 31
Case 3: 421
Case 4: 960
题意:
求字符串有多少回文子序列。(可以不连续)
题解:
用dp[i][j]表示这一段里有多少个回文串,那首先dp[i][j]=dp[i+1][j]+dp[i][j-1],但是dp[i+1][j]和dp[i][j-1]可能有公共部分,即dp[i+1][j-1]是重复的,需要减去。如果s[l]==s[r],那么dp[l][r] = dp[l+1][r-1] + 1 。
1 import java.util.Scanner;
2
3 public class Main {
4 static int casen,mod = 10007;
5 static int [][] dp ;
6 static String s;
7 public static void main(String[] args) {
8 Scanner cin = new Scanner(System.in);
9 casen = cin.nextInt();
10 int ca = 1;
11 while(casen-->0) {
12 s = cin.next();
13 dp = new int [s.length()+1][s.length()+1];
14 for(int i=0;i<s.length();i++) {
15 dp[i][i] = 1;
16 }
17 for(int len=1;len<s.length();len++) {
18 for(int l=0;l+len<s.length();l++) {
19 int r = l+len;
20 if(s.charAt(l)==s.charAt(r)) {
21 dp[l][r] = (dp[l+1][r-1] + 1 + mod) % mod;
22 }
23 dp[l][r] = (dp[l][r] + dp[l][r-1] + dp[l+1][r] - dp[l+1][r-1] + mod) % mod;
24 }
25 }
26 System.out.println("Case "+ ca++ + ": "+dp[0][s.length()-1]);
27 }
28 }
29 }
30
标签:scan exti rem i+1 sys lower mil hang 部分
原文地址:https://www.cnblogs.com/1013star/p/10409839.html