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

Codeforces 766C


首先毋庸置疑这是一道水题

Codeforces 766C Mahmoud and a Message

problem

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

本着熟悉代码的初衷,我开始写这道题,然而过程中还出现不少以前不知道的错误(哦,对了,本题做法是DP+贪心,自己看看就行了)

下面贴丑陋的代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1005
#define M (int)(1e9+7)
#define INF (int)2e9
using namespace std;
int dp1[maxn];
int pro[maxn];
int num[26];
char s[maxn];int len;
int DP1(int pos){
    if(pos>=len)return 1;
    if(dp1[pos])return dp1[pos];
    int& ans=dp1[pos];
    ans=0;
    for(int i=pos+1;i<=pro[pos]+1;i++){
        ans=(DP1(i)+ans)%M;
    }
    return ans;
}
void init(){
    int ch[26],minnum=INF;
    memset(ch,0,sizeof(ch));
    for(int i=0;i<len;i++){
        int k=-1;
        for(int j=i;j<len;j++){
            minnum=min(minnum,num[s[j]-'a']);
            if(j-i+1>minnum){
                k=j-1;
                break;
            }
        }
        pro[i]=(k==-1)?len-1:k;
        memset(ch,0,sizeof(ch));
        minnum=INF;
    }
    return;
}
int main(){
    scanf("%d",&len);
    scanf("%s",s);
    for(int i=0;i<26;i++)
        scanf("%d",&num[i]);
    init();
    int max1=0;
    for(int i=0;i<len;i++){
        max1=max(max1,pro[i]-i+1);
    }
    int min1=0;
    for(int i=0;i<len;i++){
        i=pro[i];
        min1++;
    }
    printf("%d\n%d\n%d",DP1(0),max1,min1);
    return 0;
}

大概的错点有如下:

1.memset函数被cstring库所包含,且参数中的
memset(*type pointer,int ascll,int size)中的第二个是ascll码,当然直接填入一个字符也行

2.使用e来表示数字时(如2e9,1e+7)时应该将这个数的全部进行强制类型转换,如:

#define M (int)(1e9+7)

否则会发生问题(要是这样写的话):

#define M (int)1e9+7

3.还有就是能不用(或简化)DP就不用(或简化),其余还要考虑到问题具有的贪心性质,这个东西可以用拟阵来进行说明,这里不予阐述

这次主要记录了几个编程方面的几个小问题,C++博大精深,要学的还有很多(当然算法也有很多QAQ)

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Goseqh/article/details/56015872
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭