Codeforces Round #396 (Div. 2) C. Mahmoud and a Message
地址:http://codeforces.com/contest/766/problem/C
题目:
2 seconds
256 megabytes
standard input
standard output
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.
思路:一开始没往dp方向想,然后半天都没想出来。。。
dp[i]表示在第i个字母后面画一个分隔符时的分割方法数。
dp[i]=sum(dp[j]),字符串j+1-i合法,递推时可以从i-1开始向后递推。
至于最长分割串在dp转移时取就可以了,最小分割数量从后往前贪心取。
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int a[],dp[],mx,sum,pre[];
char ss[]; void dfs(int x)
{
if(pre[x])
sum++,dfs(pre[x]);
}
int main(void)
{
int n;
scanf("%d%s",&n,ss+);
for(int i=;i<;i++)
scanf("%d",&a['a'+i]);
dp[]=;
for(int i=;i<=n;i++)
for(int j=i-,mi=1e9;j>=;j--)
{
mi=min(a[ss[j+]],mi);
if(i-j<=mi)
dp[i]=(dp[i]+dp[j])%mod,pre[i]=j,mx=max(mx,i-j);
else break;
}
dfs(n);
printf("%d\n%d\n%d\n",dp[n],mx,sum+);
return ;
}
Codeforces Round #396 (Div. 2) C. Mahmoud and a Message的更多相关文章
- 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) 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) 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) 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) 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 ...
- Codeforces Round #396 (Div. 2) A - Mahmoud and Longest Uncommon Subsequence B - Mahmoud and a Triangle
地址:http://codeforces.com/contest/766/problem/A A题: A. Mahmoud and Longest Uncommon Subsequence time ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip 树形压位DP
题目链接:http://codeforces.com/contest/766/problem/E Examples input 3 1 2 3 1 2 2 3 out 10 题意: 给你一棵n个点 ...
随机推荐
- Django - 安装filebrowser发生Error finding Upload-Folder错误
Error finding Upload-Folder (site.storage.location + site.directory). Maybe it does not exist? 解决: F ...
- spring mvc +easy ui +Mybatis 录入数据
1.itemsEasyui.jsp 应用到的插件及知识点:日期控件My97 ,图片本地预览函数PreviewImage() (1)easy ui 的模态窗口使用时,要指定DIV的属性 data-opt ...
- 【BZOJ】3412: [Usaco2009 Dec]Music Notes乐谱(二分)
http://www.lydsy.com/JudgeOnline/problem.php?id=3412 维护前缀和,然后直接二分即可... #include <cstdio> #incl ...
- Engineer in the White Spaces
 Engineer in the White Spaces Michael Nygard A SySTEM ConSiSTS oF inTERdEpEndEnT pRogRAMS. We call ...
- java Thread方法解析: sleep join wait notify notifyAll
转载自: sleep(),yield(),wait()区别详解:http://dylanxu.iteye.com/blog/1322066 join方法详解:http://www.open-open. ...
- 【转】防止CListCtrl闪烁的几种方法
转载出处:http://blog.sina.com.cn/s/blog_5ee42ba30100g50j.html 1.使用SetRedraw禁止窗口重绘,操作完成后,再恢复窗口重绘 m_ctlLis ...
- CentOS中Apache虚拟主机(virtualHost)设置在/home目录下的若干问题
在Ubuntu中安装LAMP是非常简单的意见事情.但是在CentOS中却遇到了很多问题. 首先是CentOS中必须手动配置iptables,把80端口开放出来,不然,是访问不到的,开放80端口在/et ...
- TypeScript 变量声明(二)
ES6 中,变量声明一共有6种,分别是var.function .let.const.class和import. let 基本语法:let 变量名 :类型.其中类型不是必须的. 1.用于声明变量,其用 ...
- iOS捕获异常,常用的异常处理方法
本文转载至 http://www.cocoachina.com/ios/20141229/10787.html 前言:在开发APP时,我们通常都会需要捕获异常,防止应用程序突然的崩溃,防止给予用户不友 ...
- 向spider中传递参数
1.这里采用run.py脚本方式 # 通过CrawlerProcess同时运行几个spider import scrapy from scrapy.crawler import CrawlerProc ...