Codeforces Round #396 (Div. 2) C
Mahmoud wrote a message s of length n. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That's because this magical paper doesn't allow character number i in the English alphabet to be written on it in a string of length more than ai. For example, if a1 = 2 he can't write character 'a' on this paper in a string of length 3 or more. String "aa" is allowed while string "aaa" is not.
Mahmoud decided to split the message into some non-empty substrings so that he can write every substring on an independent magical paper and fulfill the condition. The sum of their lengths should be n and they shouldn't overlap. For example, if a1 = 2 and he wants to send string "aaa", he can split it into "a" and "aa" and use 2 magical papers, or into "a", "a" and "a" and use 3 magical papers. He can't split it into "aa" and "aa" because the sum of their lengths is greater than n. He can split the message into single string if it fulfills the conditions.
A substring of string s is a string that consists of some consecutive characters from string s, strings "ab", "abc" and "b" are substrings of string "abc", while strings "acb" and "ac" are not. Any string is a substring of itself.
While Mahmoud was thinking of how to split the message, Ehab told him that there are many ways to split it. After that Mahmoud asked you three questions:
- How many ways are there to split the string into substrings such that every substring fulfills the condition of the magical paper, the sum of their lengths is n and they don't overlap? Compute the answer modulo 109 + 7.
- What is the maximum length of a substring that can appear in some valid splitting?
- What is the minimum number of substrings the message can be spit in?
Two ways are considered different, if the sets of split positions differ. For example, splitting "aa|a" and "a|aa" are considered different splittings of message "aaa".
The first line contains an integer n (1 ≤ n ≤ 103) denoting the length of the message.
The second line contains the message s of length n that consists of lowercase English letters.
The third line contains 26 integers a1, a2, ..., a26 (1 ≤ ax ≤ 103) — the maximum lengths of substring each letter can appear in.
Print three lines.
In the first line print the number of ways to split the message into substrings and fulfill the conditions mentioned in the problem modulo 109 + 7.
In the second line print the length of the longest substring over all the ways.
In the third line print the minimum number of substrings over all the ways.
3
aab
2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
3
2
2
10
abcdeabcde
5 5 5 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
401
4
3
In the first example the three ways to split the message are:
- a|a|b
- aa|b
- a|ab
The longest substrings are "aa" and "ab" of length 2.
The minimum number of substrings is 2 in "a|ab" or "aa|b".
Notice that "aab" is not a possible splitting because the letter 'a' appears in a substring of length 3, while a1 = 2.
题意:说的是字符串分割,不过是有条件的,一个字符在一个分割区域是不能出现超过ai次,问能分几次,每个区间最长多少,最短多少
解法:
1 dp[i]表示到第i位置能分割多少次,和它前i-j的位置有关,j<=i(表示能分割的长度)i-j+1是距离i有j个长度的地方(在i的左边)
2 每次讨论j都需要验证是不是满足条件,是的话就是+dp[i-j]
3 最长的距离当然是比较j喽,最短的话比较Min[i-j]+1和Min[i]
#include<bits/stdc++.h>
using namespace std;
int n;
char s[];
int mod=1e9+;
long long dp[];
long long a[];
long long Min[];
int solve(int x,int y,int len){
int Len=len;
for(int i=x;i<=y;i++){
if(Len>a[s[i]-'a']){
return ;
}
}
return ;
}
int main(){
cin>>n;
cin>>s+;
for(int i=;i<;i++){
cin>>a[i];
}
dp[]=;
int maxn=;
for(int i=;i<=n;i++){
Min[i]=1e9;
for(int j=;j<=i;j++){
if(solve(i-j+,i,j)){
dp[i]+=(dp[i-j]%mod);
dp[i]%=mod;
maxn=max(j,maxn);
Min[i]=min(Min[i-j]+,Min[i]);
}
}
}
cout<<dp[n]%mod<<endl;
cout<<maxn<<endl;
cout<<Min[n]<<endl;
return ;
}
Codeforces Round #396 (Div. 2) C的更多相关文章
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集
D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...
- Codeforces Round #396 (Div. 2) A,B,C,D,E
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集
A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary
地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...
- Codeforces Round #396 (Div. 2) D
Mahmoud wants to write a new dictionary that contains n words and relations between them. There are ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message dp
C. Mahmoud and a Message 题目连接: http://codeforces.com/contest/766/problem/C Description Mahmoud wrote ...
- Codeforces Round #396 (Div. 2) B. Mahmoud and a Triangle 贪心
B. Mahmoud and a Triangle 题目连接: http://codeforces.com/contest/766/problem/B Description Mahmoud has ...
- Codeforces Round #396 (Div. 2) A. Mahmoud and Longest Uncommon Subsequence 水题
A. Mahmoud and Longest Uncommon Subsequence 题目连接: http://codeforces.com/contest/766/problem/A Descri ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip
地址:http://codeforces.com/contest/766/problem/E 题目: E. Mahmoud and a xor trip time limit per test 2 s ...
随机推荐
- HDU3183 A Magic Lamp —— 贪心(单调队列优化)/ RMQ / 线段树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题解: 方法一:贪心. 在草稿纸上试多几次可以知道,删除数字中从左到右最后一位递增(可以等于)的 ...
- 关于redis的思考
集群版本的redis主从复制 也可以实现集群 但是不是很好 集群版redis主从复制版本集群 Spring Boot整合Redi事务 Spring Boot+Redis+Ehcache实现二级缓存 S ...
- css font-family(字体样式)
之前因为用的很少,所以没注意,最近做APP混合开发, 给字体一个样式 font-family:" 微软雅黑": 发现在有的手机上有效,但是在有的手机上是无效的, 解决方法: ...
- 【旧文章搬运】Windows中全局钩子DLL的加载过程
原文发表于百度空间,2011-03-24========================================================================== 看雪上别人 ...
- 【旧文章搬运】KeUserModeCallback用法详解
原文发表于百度空间及看雪论坛,2010-01-10 看雪论坛地址:https://bbs.pediy.com/thread-104918.htm 代码及附件可到这里下载=============== ...
- Hearthstone
题意: 有$n$个无中生有,有$m$个不同的杀,第$i$个杀掉$X_i$滴血,敌人血量$P$,求问第一回合就将敌人杀死的概率是多少. 解法: 二进制枚举$A$类,$B$类卡的顺序,这样就确定了取了几个 ...
- HTML5的LocalStorage实现记住密码
HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 localStorage ...
- java如何写接口给别人调用
参考:https://blog.csdn.net/greatkendy123/article/details/52818466 java web开发(二) 接口开发
- 2.12 Hivet中order by,sort by、distribute by和cluster by
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SortBy 一.order by 对全局数据的排序,仅仅只有一个red ...
- Flutter实战视频-移动电商-16.补充_保持页面状态
16.补充_保持页面状态 修正一个地方: 设置了item的高度为380 横向列表为380.最终build的高度也增加了50为430. 增加了上面的高度以后,下面那个横线划掉的价格可以显示出来了. 但是 ...