Codeforces 645E (构造 DP)
来源:互联网 发布:gitlab for mac客户端 编辑:程序博客网 时间:2024/05/19 05:02
After getting kicked out of her reporting job for not knowing the alphabet, Bessie has decided to attend school at the Fillet and Eggs Eater Academy. She has been making good progress with her studies and now knows the first k English letters.
Each morning, Bessie travels to school along a sidewalk consisting of m + n tiles. In order to help Bessie review, Mr. Moozing has labeled each of the first m sidewalk tiles with one of the first k lowercase English letters, spelling out a string t. Mr. Moozing, impressed by Bessie's extensive knowledge of farm animals, plans to let her finish labeling the last n tiles of the sidewalk by herself.
Consider the resulting string s (|s| = m + n) consisting of letters labeled on tiles in order from home to school. For any sequence of indices p1 < p2 < ... < pq we can define subsequence of the string s as string sp1sp2... spq. Two subsequences are considered to be distinct if they differ as strings. Bessie wants to label the remaining part of the sidewalk such that the number of distinct subsequences of tiles is maximum possible. However, since Bessie hasn't even finished learning the alphabet, she needs your help!
Note that empty subsequence also counts.
The first line of the input contains two integers n and k (0 ≤ n ≤ 1 000 000, 1 ≤ k ≤ 26).
The second line contains a string t (|t| = m, 1 ≤ m ≤ 1 000 000) consisting of only first k lowercase English letters.
Determine the maximum number of distinct subsequences Bessie can form after labeling the last n sidewalk tiles each with one of the first klowercase English letters. Since this number can be rather large, you should print it modulo 109 + 7.
Please note, that you are not asked to maximize the remainder modulo 109 + 7! The goal is to maximize the initial value and then print the remainder.
1 3ac
8
0 2aaba
10
设dp[i]表示以字母i结尾的不同子序列个数,dp[j] = sigma (dp[i],i!=j)+1;
构造可以在末尾加上一个上次出现的最前面的字母;
#include <bits/stdc++.h>using namespace std;#define mod 1000000007#define maxn 2111111char s[maxn];int pre[33];//字母的前一个位置long long dp[33]; //以i结尾的子序列int main () { int n, m, k; scanf ("%d%d", &n, &k); scanf ("%s", s); m = strlen (s); memset (pre, -1, sizeof pre); for (int i = 0; i < m; i++) { pre[s[i]-'a'] = i; } for (int i = m; i < n+m; i++) {//构造 int pos = maxn; char ch; for (int j = 0; j < k; j++) { if (pos > pre[j]) pos = pre[j], ch = 'a'+j; } s[i] = ch; pre[ch-'a'] = i; } //s[n+m] = '\n'; cout << s << endl; memset (dp, 0, sizeof dp); dp[s[0]-'a'] = 1; for (int i = 1; i < n+m; i++) { dp[s[i]-'a'] += 1; for (int j = 0; j < k; j++) { if (s[i]-'a' == j) continue; dp[s[i]-'a'] += dp[j]; dp[s[i]-'a'] %= mod; } } long long ans = 0; for (int i = 0; i < k; i++) { ans += dp[i]; ans %= mod; } cout << (ans+1)%mod << endl; return 0;}
- Codeforces 645E (构造 DP)
- CodeForces 645E Intellectual Inquiry(构造+贪心+dp)
- CodeForces 417E (构造)
- codeforces 508E (构造)
- Codeforces 645E Intellectual Inquiry (贪心+DP)
- Codeforces 645E Intellectual Inquiry [贪心] [DP]
- 【dp】codeforces 83E
- codeforces-731E-dp
- Codeforces 748E dp
- Codeforces 722E [DP]
- Codeforces 416E DP
- codeforces 526E E. Transmitting Levels(dp)
- Codeforces 838E E. Convex Countour DP
- codeforces 215E 数位DP
- Codeforces 14E Camels (DP)
- codeforces 23E 树形DP
- CodeForces 215E 数位DP
- codeforces 23E 树形DP
- Hama0.7.1 执行
- iOS开发调试技巧总结
- 怎样成为一个ACMer
- mysql 安装后报 ERROR 1045 (28000): Access denied for user 'root'@'localhost'
- IT行业导览-目录
- Codeforces 645E (构造 DP)
- servlet相关介绍及乱码问题的解决方案
- 20160322 HDU4704 Sum(费马小定理+快速幂)
- 项目记录31--unity 简单分析LuaFramework_UGUI
- BZOJ_P3594 [Scoi2014]方伯伯的玉米田(动态规划+二维树状数组优化)
- 判断txt文件的字符编码
- 树的层次遍历_level
- Linux基本的命令
- Spark Rpc通信源码分析