[TC SRM 708 lev 3] PalindromicSubseq2

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 typedef long long LL;
 6
 7 const int MOD = 1e9 + 7;
 8
 9 class PalindromicSubseq2 {
10 public:
11     int solve(string s) {
12         int n = s.size();
13         string t(s.rbegin(), s.rend());
14         vector<vector<int>> dp(3001, vector<int>(3001, 0));
15         for (int i = 0; i <= n; i++) {
16             dp[i][0] = 1;
17             dp[0][i] = 1;
18         }
19         for (int i = 1; i <= n; i++) {
20             // 虽然dp[i - 1][j] + dp[i][j - 1]肯定包含dp[i - 1][j - 1],但是取模后前两者可能小于后者导致结果为负,因此切记要+MOD
21             for (int j = 1; j <= n; j++) {
22                 if (s[i - 1] == t[j - 1]) dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD;
23                 else dp[i][j] = ((dp[i - 1][j] + dp[i][j - 1]) % MOD - dp[i - 1][j - 1] + MOD) % MOD;
24             }
25         }
26         int res = 0;
27         for (int i = 0; i < n; i++) {
28             res ^= (i + 1) * (long long)dp[i][n - i - 1] % MOD;
29         }
30         return res;
31     }
32 };
时间: 2024-12-13 04:15:52

[TC SRM 708 lev 3] PalindromicSubseq2的相关文章

[TC SRM 697 div1 lev1] DivisibleSetDiv1

Tutorial:https://apps.topcoder.com/wiki/display/tc/SRM+697#DivisibleSetDiv1 Note:证明过程值得一看. 主要内容:寻找[x1,x2,...,xn]使得满足bi * xi >= S - xi,其中S = x1 + x2 + ... + xn.

TC srm 673 300 div1

TC srm.673 300 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 Description 给你n(n<=50)匹马和n个人,一匹马和一个人能够组成一个骑兵,这个骑兵的战斗力等于马的战斗力乘以人的战斗力,问你有多少种组合方式满足其他骑兵的战斗力都不超过第0号骑兵. Input Output Sample Input Sample Output HINT 题意 题解: 大概就暴力枚举哪匹马和第0号人匹配,后面的骑兵我们按照战斗力从大到小排序之后,

TC SRM 665 DIV2 A LuckyXor 暴力

LuckyXorTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description A lucky number is a positive integer consisting of only the digits 4 and 7.Given an int a, return an int b strictly greater than a, such that a XOR b is a lucky number. (See Notes fo

TC SRM 663 div2 A ChessFloor 暴力

ChessFloor Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description Samantha is renovating a square room. The floor of the room is an N times N grid of unit square tiles. Each tile has some color. You are given the current colors of all tiles in a

TC SRM 663 div2 B AABB 逆推

AABB Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 TC Description One day, Jamie noticed that many English words only use the letters A and B. Examples of such words include "AB" (short for abdominal), "BAA" (the noise a sheep makes), &

TC SRM 638 DIV1 600 NarrowPassage2

Problem Statement 在一个狭长的通道中有$n$只狼,每只狼的体积为$size_i$,如果相邻的两只狼体积不超过$m$,则可以交换位置,求可以有多少种排列. $n≤50,1≤m≤1,000,000,000$ Tutorial 每次选出区间中体积最大的狼,如果其他狼可以越过它,那么即可在整个区间里自由移动,而不能越过它的狼则被隔离到两个区间里,可以分治. 区间总共有$n$只狼,能自由移动的狼个数为$cnt$,那么最后结果等于$$solve(l)*solve(r)*A_{n}^{cnt

TC SRM 664 div2 AB

#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; class BearCheats{ public: string eyesight(int A, int B){ char t[256]; string s1; sprintf(t, "%d", A); s1 = t; string s2; cha

tc srm 632 500 (规律)

We have a sequence of N positive integers: a[0] through a[N-1]. You do not know these integers. All you know is the number of trailing zeros in their binary representations. You are given a vector <int> d with N elements. For each i, d[i] is the num

TC SRM 397 2 250

BreakingTheCode Problem Statement You have been given a secret mission where you must break the enemy's code. You have already figured out that they encode messages using the following method. Each letter between 'a' and 'z', inclusive, is assigned a