766C Mahmoud and a Message[字符串][简单dp]
来源:互联网 发布:主题医院3 mac 编辑:程序博客网 时间:2024/06/01 08:37
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.
3aab2 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
322
10abcdeabcde5 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
40143
题目大意:
给定长度为n的字符串,只含有小写字母。接下来给出26个数字,代表从a到z的26个英文字母在子字符串中出现的次数限制。
输出三个数字,第一个是符合要求的分割子串的方法的数量,第二个是最大的子串的长度,第三个是最小分割子串数。
思路:
问题考虑substring,我们已知一个string的子串个数是2^n,计算的方法是dp[n] = dp[n-1] + dp[n-2] + ...+dp[1]。根据此方法,从字符串的低位从高位遍历,对于每一位,从1到i在遍历,若i-j 到i是符合要求的子串,那么dp[j]便应该累加到dp[i]
即 若 i-j到 i 满足要求:
dp[i] += dp[j];
同时MAX代表最大子串 MAX = max(MAX, j);
用f[i]表示最小分割子串的数量 则f[i] = min(f[i-j]+1, f[i]);
代码:
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int n;const int MAXN=1e3+7;const int mod=1e9+7;int limit[30];long long dp[MAXN];int f[MAXN];char s[MAXN];int check(int i,int j){ int l=j-i+1; for(int k=i;k<=j;++k) { if(limit[s[k]-'a']<l)return 0; } return 1;}int main(){ int i,j; scanf("%d",&n); scanf("%s",s+1); for(i=0;i<26;++i)scanf("%d",&limit[i]); int MAX=0; dp[0]=1; for(i=1;i<=n;++i)//长度为i { f[i]=1e9; for(j=1;j<=i;++j)//长度 { if(check(i-j+1,i))//后面的当前序列合法 { dp[i]=(dp[i]+dp[i-j])%mod; f[i]=min(f[i],f[i-j]+1); MAX=max(MAX,j); } } } cout << dp[n] << MAX << f[n];}
- 766C Mahmoud and a Message[字符串][简单dp]
- CF 766C - Mahmoud and a Message (DP+字符串)
- C. Mahmoud and a Message----基础DP
- Mahmoud and a Message - c (dp)
- Codeforces 766C Mahmoud and a Message 基础DP
- codeforces 766 c Mahmoud and a Message(dp)
- 【Codeforces 766 C Mahmoud and a Message】 + dp
- codeforces 766C Mahmoud and a Message(基础dp)
- Codeforces 766 C Mahmoud and a Message 详解(DP)
- Codeforces 766 C Mahmoud and a Message (基础dp)
- Codeforces 766 C Mahmoud and a Message(DP)
- 【DP】 Mahmoud and a Message
- 【codeforces 766C】Mahmoud and a Message
- codeforces 766C Mahmoud and a Message
- 【codeforces 766C】Mahmoud and a Message
- codeforces 766 C Mahmoud and a Message
- Codeforces 766C Mahmoud and a Message
- Codeforces 766C-Mahmoud and a Message
- DVWA系列(四)----Command Injection (命令行注入)
- C++输入输出
- 二分插入
- 常用PHP函数系列二:优化过的模拟HTTP请求函数(CURL版)
- SublimeTest3设置【中文乱码】
- 766C Mahmoud and a Message[字符串][简单dp]
- CCF201403-3 命令行选项(100分)
- Ruby on Rails学习笔记(5)--关于出错信息ActiveRecord::UnknownPrimaryKey in **(模型名称复数)Controller#show的解决办法
- 常用PHP函数系列三:输出当前URL
- JAVA设计模式--组合模式
- nginx设置开启启动
- TP框架基础介绍-----(二)
- 阿里面试回来,想和java程序员聊聊
- bzoj1024 [SCOI2009]生日快乐