Codeforces Round #210 (Div. 1) problem C Levko and Strings
来源:互联网 发布:蒋中正 知乎 编辑:程序博客网 时间:2024/04/28 00:36
http://codeforces.com/contest/360/problem/C
极好的一道动态规划, 给定一个小写字母的字符串 S, 求其相同长度的串T,T中恰包含K个子串字典序大于S中相同位置的子串,求T有几种,记 DP[ i ] [ j ][ 0或1 ] 为 第i个位置为止已有J个子串字典序大于S中相对应为止的子串,最后一维表示相同与否,我在这里用0表示相同,及T[i]=s[i],否则T[i]!=s[i] 我固定位置i, 向前遍历 当到达位置t时, 令 (S[t] = T[t] ,S[t+1] = T[t+1]) S[i] 与T[i] 的关系分别讨论。此时
若T[i]>S[i], t 每向前移动一位 , 满足条件的子串就增加 n-i+1 个,我们若枚举 j,则只需枚举 j/(n-i+1) 次,整体复杂度为n*n*log(n)
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<queue>#include<set>#include<cstring>#include<vector>#include<bitset>#include<iterator>#include<list>#include<map>#include<cstdlib>#include<ctime>#include<utility>using namespace std;#define L p->ch[0]#define R p->ch[1]#define KeyTree root->ch[1]->ch[0]typedef long long LL;typedef pair<char, int> PII;typedef vector<int> VI;const int maxn = 2111;const int INF = 1 << 30;const int mod = (int) (1e9 + 7);const LL X = 123;template<typename T>T gcd(T a, T b) {return b ? gcd(b, a % b) : a;}LL dp[maxn][maxn][2];char s[maxn];int ch[maxn];LL P[maxn];int main() {ios::sync_with_stdio(false);int n, k, i, j, t, r;cin >> n >> k;cin >> (s + 1);dp[0][0][1] = 1;dp[0][0][0] = 0;P[0] = 1;for (i = 1; i <= n; ++i) {ch[i] = (s[i] ^ 0x60);}r = n;for (i = 1, r = n; i <= n; ++i, --r) {for (j = 0; j <= k; ++j) {dp[i][j][0] = dp[i][j][1] = dp[i - 1][j][0] + dp[i - 1][j][1];LL& tv = dp[i][j][1];tv = dp[i][j][1] * (ch[i] - 1) % mod;for (t = i - 1; t >= 0 && j - (i - t) * r >= 0; --t) {tv += (dp[t][j - (i - t) * r][1]) * (26 - ch[i]);}tv %= mod;}}LL ans = (dp[n][k][0] + dp[n][k][1]) % mod;cout << ans << endl;return 0;}
- Codeforces Round #210 (Div. 1) problem C Levko and Strings
- Codeforces Round #210 (Div. 1) C. Levko and Strings
- Codeforces Round #210 (Div. 1)——Levko and Array
- Codeforces 360C Levko and Strings (dp)
- Codeforces 360C Levko and Strings dp
- CF-Codeforces Round #210 (Div. 1)-A-Levko and Array Recovery
- Codeforces Round #210 (Div. 1) B. Levko and Array(dp 思维)
- Codeforces Round #210 (Div. 2) C. Levko and Array Recovery && Codeforces 360 A 详解(思维+维护理论值)
- Codeforces Round #112 (Div. 2) C Another Problem on Strings
- Codeforces Round #112 (Div. 2) C. Another Problem on Strings
- Codeforces Round #112 (Div. 2) C Another Problem on Strings
- Codeforces 360C - Levko and Strings 组合数学+DP
- Codeforces Round #138 (Div. 1), problem: (B) Two Strings
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
- Codeforces Round #136 (Div. 2) C. Little Elephant and Problem
- Codeforces Round #292 (Div. 2) Problem C - Drazil and Factorial
- Codeforces Beta Round #85 (Div. 1 Only), problem: (C) Petya and Spiders 状态压缩
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend(贪心+DP)
- half years old
- MySQL 百万级数据查询优化技巧
- POJ 1113 Wall
- 数据结构与算法系列-树-二叉树存储结构
- heartsadorable adorable tired heart tired tired tired hearts adorable adorable
- Codeforces Round #210 (Div. 1) problem C Levko and Strings
- /proc文件系统详解
- mysql 注册、删除服务
- paradise paradise
- Codeforces 211 div2 D. Renting Bikes
- myeclipse中安装svn
- Spring并发访问的线程安全性问题
- 常用统计分析 SQL 在 AWK 中的实现
- Mysql 取得距离目前最近的一条数据(自己的)