766C Mahmoud and a Message[字符串][简单dp]

codeforces 766 C Mahmoud and a Message [基础DP]【动态规划】 题目连接:http://codeforces.com/contest/766/problem/C———————————————————————–. C. Mahmoud and a Message time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandar 阅读详情

C. Mahmoud and a Message
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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".

Input

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.

Output

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.

Examples
input
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
output
3
2
2
input
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
output
401
4
3

题目大意:

给定长度为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];
}


实战指南:CentOS 7下VOS3000 8.0从零部署到授权激活全流程 本文提供了一份详尽的CentOS 7系统下部署VOS3000 8.0的实战指南。从环境准备、使用FileZilla上传安装包、执行自动化安装脚本,到获取软件码、利用注册机生成授权文件并激活,手把手带领用户完成从零部署到投入商用的全流程,确保系统稳定运行。 阅读详情

相关推荐

2024年上市公司ESG评级全解析:六大权威数据源横向对比与应用指南

本文深度解析2024年国内主流ESG评级数据源,包括华证、商道融绿、Wind、富时罗素和彭博,横向对比其覆盖范围、评分逻辑与应用侧重点。文章提供实战指南,帮助投资者与企业根据投资决策、风险管理和学术研究等不同场景,精准选择并有效应用ESG数据,实现从概念到实用分析工具的转变。

weixin_29229055的博客 311

Codeforces 766 C Mahmoud and a Message 详解(DP)

C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mahmoud wrote a message s of lengt

K键盘里的青春K 836

NFS共享速度慢?麒麟系统性能调优5个关键参数实测对比

本文针对银河麒麟服务器系统上NFS共享速度慢的问题,通过实测对比分析了五个关键调优参数。重点探讨了rsize/wsize传输块大小、TCP/UDP协议选择、sync/async模式权衡等,并结合麒麟系统特性提供了具体的优化建议与配置示例,旨在帮助用户有效提升国产化环境下的文件共享性能。

weixin_29254029的博客 332

codeforces 766C Mahmoud and a Message(基础dp)

Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mahmoud wrote a message s of length n

Chunzhen的博客 1019

【Fluent】太阳辐射模型—Solar Ray Tracing

一、引言Fluent提供了一个太阳能负载模型,可用于计算进入计算域的太阳光线的辐射效应。该模型可以分为:太阳光线追踪和太阳辐照。其中太阳辐照需要使用DO或MC模型,直接向辐射模型提供外光束方向和强度参数。太阳光线追踪不限制辐射模型,可以独立于辐射方程使用,甚至可以在辐射模型off的情况下使用。本文主要介绍太阳光线追踪模型。该模型非常适用于以下领域汽车气候控制 (ACC) 应用建筑物中的人体热舒适建模应用该模型使用着色算法进行太阳光线追踪。光线会透过半透明的面加载在照射面上。

a5567899的博客 7586

史上最全的Logisim运算器实验教程(基于华中科技大学计算机组成原理实验)

本博客是实现了Logisim的运算器设计,有具体的运算器设计的思路和运算器设计的电路,适合一步一步的按照思路做。

qq_62325622的博客 4万+

Mahmoud and a Message - c (dp)

C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mahmoud wrote a message s of length n. He wants to send ...

提拉米苏的博客 896

Qoder + ADB Supabase :5分钟GET超火AI手办生图APP

本文介绍如何利用Qoder、阿里云 ADB Supabase 和通义千问图像编辑模型,快速构建AI手办生图Flutter应用。无需传统后端,通过AI生成代码、BaaS服务与边缘函数调用,实现从原型到上线的敏捷开发,全流程轻量高效,助力Vibe Coding落地实践。

数据库技术 931

Codeforces Round #396 (Div. 2)C. Mahmoud and a Message

C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mahmoud wrote a message s of lengt

jerans的博客 576

Workbench下列车转向架随机振动疲劳分析

结果中添加功率谱响应工具(Response PSD Tool),可以查看几何体以及远程点上的功率谱密度,如下图11所示,这里查看几何体上某一硬点的加速度功率谱密度响应曲线,如下图12所示。结果中添加疲劳工具(Fatigue Tool),查看列车转向架的疲劳寿命情况,这里采用窄带的寿命假设理论,曝光时间为1s指明计算出的疲劳寿命的基本单位是秒,寿命结果如下图14所示,可以看到最大寿命为1e35s,最小寿命为2.3542e18s,注意单位制,可再次基础上将其转换为小时或天。频域上的统计计算,软件很快计算完成。

ansysxuexizhe的博客 2602

UMI——斯坦福刷盘机器人:通过手持夹爪革新数据收集方式,且使用视觉SLAM和Diffusion Policy预测动作

没想到今年年初的斯坦福mobilealoha的热度刚过,而到今年2月的下旬,斯坦福另一个团队又推出了UMI刷盘机器人,且这两个团队还互相认识、还在一块共同切磋(顺带小小感叹一下,斯坦福的氛围是真好而且真高产)斯坦福UMI刷盘机器人其与mobile aloha(以及AirExo: Low-Cost Exoskeletons for Learning Whole-Arm Manipulation in the Wild)最大的不同在于其收集数据处理的非真实的机器人,而是一个手持夹持器(从而大幅降低成本)

u013250861的博客 1357

C. Mahmoud and a Message----基础DP

C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mahmoud wrote a message s of lengt

bless295的博客 496

IGCT 测试原理概述

IGCT(Integrated Gate-Commutated Thyristor,集成门极换流晶闸管)是一种高功率半导体器件,基于门极可关断晶闸管(GTO)的原理,但通过集成门极驱动单元和优化结构(如硬驱动技术),实现了更低的导通损耗、更快的开关速度和更高的可靠性。它适用于中高压大功率应用,如高压直流输电(HVDC)、中压变频器和模块化多电平变换器(MMC)。IGCT 的结构为四层 p-n-p-n 器件,门极电流控制导通和关断,阳极电流在关断时快速换流至门极,通常在 1 μs 内完成。

专注于前沿技术的探索与实践,热衷于从代码逻辑、架构设计到行业应用等多维度深挖技术本质。期待在技术之路上与你共同成长~ 232

Codeforces 766C. Mahmoud and a Message 水题详解+错点记录

Codeforces 766C

Renatus的博客 882

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

codeforces 766 C. Mahmoud and a Message(简单dp)

题目链接:http://codeforces.com/contest/766/problem/C 题意:给你一个长度为n的字符串,这个字符串只包含小写字母,然后让你把这个字符串进行分割,形成若干个小的字符串, 每个小写字母都有一个数字ma[i],表示这个字母能够存在于长度不超过ma[i]的字符串内,在这个条件下分割问最多有几 种分割方法,最长分割串为多少,最小分割为几部分。 一...

awow80285的博客 202

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

【Codeforces 766 C Mahmoud and a Message】 + dp

C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard outputMahmoud wrote a message s of length n. He wants to send it

楚江枫 755

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Codeforces 766 C Mahmoud and a Message (基础dp)

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 dis

Tsaryu的博客 496

codeforces 766C Mahmoud and a Message

题意: 给你一个长度为n的仅包括小写字母的字符串,每个小写字母能在一个长度不超过a[i](i是小写字母序号如a为0)的子串中。 问: 1.有多少种分隔方法 2.所有分割方法中最长的子串有多长 3.所有分隔方法中最少能字字符串分隔成几段 题解: 设dp[i][0]为前i个字符包括的分隔方法,dp[i][1]为前i个字符包括的所有分隔方法中最少能分隔成几段,ans为所有分隔方法中

t51645的博客 514

Codeforces 766C Mahmoud and a Message 基础DP

点击打开链接 题意:给定string s 长度n dp[i] 前i个字符的分解方案,按照最后一段的长度来转移 最后一段长度为j && "i-j+1~j"合法 dp[i]+=dp[i-j]  f[i] 前i个字符的分解的最少个数  f[i]=min(f[i-j]+1) #include using namespace std; typedef long long ll; c

Masamiiiiii 924

Codeforces766 C.Mahmoud and a Message(dp)

题意: 答案对1e9+7取模 数据范围:n<=1e3

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz 276

codeforces 766 C. Mahmoud and a Message dp字符串的分割

题目链接:https://codeforces.com/contest/766/problem/C 题目大意:给出一串有小写字母组成的长度为n的字符串。每个小写字母都有对应值ai,分割原串,要求对应值为ai的字符不能出现在长度超过ai的字串中。问共有多少种分割方式,分割后会出现的最长子串长度是多少,采用最少分割次数的方式,最少分割次数是多少? 思路:f[i]:以第i个字符结尾的分割方案数。p[i]...

H_ang的博客 223

codeforces 766 C. Mahmoud and a Message

题目链接:codeforces 766 C. Mahmoud and a Message time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mahmo

teddywang 373

766C - Mahmoud and a Message(dp)

https://codeforces.com/problemset/problem/766/C 有一个长度为n的字符串,第二行有26个数字,位置1~26对应为a~z的字母,数值表示该字母不能出现在长度超过该值的子串中。/// 求有多少种划分该字符串的方法 求该字符串划分成子串后最大的子串的长度 求该字符串划分成满足要求的子串需要至少划分多少次 思路:dp[i]定义到i的答案,枚举最后一段的长度用以划分 #include<iostream> #include<vector&g.

代码笔记本 245

CF 766C - Mahmoud and a Message (DP+字符串)

题目链接:http://codeforces.com/problemset/problem/766/C题目描述:有一个只含小写字母的字符串,输入一个26个整数,用来限制每个字母所在字符串的最大长度,在保证符合限制的前提下。 输出分割字符串的方案数。 输出所有方案中,最少的分割次数。 输出所有方案中,最大的子串长度。 结题过程:一开始打算放弃掉的,英文题,题目很长,看起来又比较麻烦。 然后学长来讲了

ACM_Fish的博客 574

Codeforces 766 C Mahmoud and a Message(DP)

原题链接Problem DescriptionMahmoud 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

ZhaoYi1222的博客 497

codeforces 766 c Mahmoud and a Message(dp)

题意: 给一串字符串,只包含26个字母,可以把这串字符串分成若干个子串,但是限定每个字母只能出现在长度Ax的子串里,问最多有多少种分割方案,方案数对1e9+7取膜,以及分割子串最大长度,和最少分割子串数量。 解题思路: 设dp[i]为从0到i这段字符串的分割方案数,为了满足字符a[i]的限定条件,我们只能在i-Ai+1到i之间划分,设len=i-A[i]+1, 但是i-A[i]+1并

johsnows的博客 752
上一篇: 遗传算法求解TSP问题的总结
下一篇: 766D Mahmoud and a Dictionary[带权并查集][dfs]
Qer_
博客等级 码龄11年 34粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值